Skip to content

Commit

Permalink
Admin modules getMenu() method must return an
Browse files Browse the repository at this point in the history
`luya\admin\components\AdminMenuBuilder` object instead of an array. A
deprecated message is triggered when using the old menu builder
functions. #1045
  • Loading branch information
nadar committed Oct 24, 2016
1 parent b99429a commit 7476db7
Show file tree
Hide file tree
Showing 8 changed files with 275 additions and 82 deletions.
9 changes: 5 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ The changelog contains informations about bug fixes, new features or bc breaking
1.0.0-RC2 (in progress)
-----------------------

### Changed

- [#1045](https://github.com/luyadev/luya/issues/1045) Admin modules `getMenu()` method must return an `luya\admin\components\AdminMenuBuilder` object instead of an array. A deprecated message is triggered when using the old menu builder functions.
- [#1043](https://github.com/luyadev/luya/issues/1043) Upgrade to 2.0.10 version of the Yii Framework.

### Added

- [#1003](https://github.com/luyadev/luya/issues/1003) The crawler tag CRAWL_TITLE has been added to ensure a customization of the titles.
Expand All @@ -14,10 +19,6 @@ The changelog contains informations about bug fixes, new features or bc breaking
- [#1038](https://github.com/luyadev/luya/issues/1038) Method `createCallbackUrl($callback)` added for ActiveWindows in order to get the absolut url for a callback, this is usefull when creating callbacks which can return a pdf for example.
- [#1007](https://github.com/luyadev/luya/issues/1007) French translations available for all core modules.

### Changed

- [#1043](https://github.com/luyadev/luya/issues/1043) Upgrade to 2.0.10 version of the Yii Framework.

### Fixed

- [#1002](https://github.com/luyadev/luya/issues/1002) Override the core commands method in the console application in order the provide the ability to use controllerMap variable for configurations in the applcation.
Expand Down
21 changes: 10 additions & 11 deletions core/web/Bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function beforeRun($app)
foreach ($module->urlRules as $rule) {
$this->_urlRules[(isset($rule['position'])) ? $rule['position'] : UrlRule::POSITION_AFTER_LUYA][] = $rule;
}

foreach ($module->apis as $alias => $class) {
$this->_apis[$alias] = $class;
}
Expand All @@ -49,18 +49,17 @@ public function beforeRun($app)

public function run($app)
{
// start the module now
foreach ($this->getModules() as $id => $module) {
if ($module instanceof AdminModuleInterface) {
$this->_adminAssets = ArrayHelper::merge($module->assets, $this->_adminAssets);
$this->_adminMenus = ArrayHelper::merge($module->getMenu(), $this->_adminMenus);
$this->_jsTranslations[$id] = $module->registerJsTranslation;
}
}

if (!$app->request->getIsConsoleRequest()) {
if ($this->hasModule('admin') && $app->request->isAdmin()) {
//$app->getModule('admin')->controllerMap = $this->_apis;

foreach ($this->getModules() as $id => $module) {
if ($module instanceof AdminModuleInterface) {
$this->_adminAssets = ArrayHelper::merge($module->assets, $this->_adminAssets);
$this->_adminMenus[$module->id] = $module->getMenu();
$this->_jsTranslations[$id] = $module->registerJsTranslation;
}
}

$app->getModule('admin')->assets = ArrayHelper::merge($this->_adminAssets, $app->getModule('admin')->assets);
$app->getModule('admin')->controllerMap = $this->_apis;
$app->getModule('admin')->moduleMenus = $this->_adminMenus;
Expand Down
28 changes: 14 additions & 14 deletions modules/admin/src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use luya\admin\components\AdminMenu;
use luya\admin\components\StorageContainer;
use luya\admin\components\Auth;
use luya\admin\components\AdminMenuBuilder;

class Module extends \luya\admin\base\Module implements CoreModuleInterface
{
Expand Down Expand Up @@ -94,20 +95,19 @@ public function setJsTranslations(array $translations)

public function getMenu()
{
return $this
->nodeRoute('menu_node_filemanager', 'folder_open', 'admin-storage-index', 'admin/storage/index')
->node('menu_node_system', 'layers')
->group('menu_group_access')
->itemApi('menu_access_item_user', 'admin-user-index', 'person', 'api-admin-user')
->itemApi('menu_access_item_group', 'admin-group-index', 'group', 'api-admin-group')
->group('menu_group_system')
->itemApi('menu_system_item_language', 'admin-lang-index', 'language', 'api-admin-lang')
->itemApi('menu_system_item_tags', 'admin-tag-index', 'label', 'api-admin-tag')
->itemApi('menu_system_logger', 'admin-logger-index', 'label', 'api-admin-logger')
->group('menu_group_images')
->itemApi('menu_images_item_effects', 'admin-effect-index', 'blur_circular', 'api-admin-effect')
->itemApi('menu_images_item_filters', 'admin-filter-index', 'adjust', 'api-admin-filter')
->menu();
return (new AdminMenuBuilder($this))
->nodeRoute('menu_node_filemanager', 'folder_open', 'admin-storage-index', 'admin/storage/index')
->node('menu_node_system', 'layers')
->group('menu_group_access')
->itemApi('menu_access_item_user', 'admin-user-index', 'person', 'api-admin-user')
->itemApi('menu_access_item_group', 'admin-group-index', 'group', 'api-admin-group')
->group('menu_group_system')
->itemApi('menu_system_item_language', 'admin-lang-index', 'language', 'api-admin-lang')
->itemApi('menu_system_item_tags', 'admin-tag-index', 'label', 'api-admin-tag')
->itemApi('menu_system_logger', 'admin-logger-index', 'label', 'api-admin-logger')
->group('menu_group_images')
->itemApi('menu_images_item_effects', 'admin-effect-index', 'blur_circular', 'api-admin-effect')
->itemApi('menu_images_item_filters', 'admin-filter-index', 'adjust', 'api-admin-filter');
}

public function registerComponents()
Expand Down
119 changes: 67 additions & 52 deletions modules/admin/src/base/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Module extends \luya\base\Module implements AdminModuleInterface

/**
* @var array The config linker property can specific the configuration class for ngRest model where the key
* is the `api` and the value is the class to the config. An array could look like this:
* is the `api` and the value is the class to the config. An array could look like this:
*
* ```php
* [
Expand All @@ -31,7 +31,7 @@ class Module extends \luya\base\Module implements AdminModuleInterface
* a project via the module configuration inside your prep/prod config. Example for override a default ngrest
* config inside a project config:
*
* ```
* ```php
* return [
* // ...
* 'modules' => [
Expand All @@ -44,18 +44,11 @@ class Module extends \luya\base\Module implements AdminModuleInterface
* ]
* ];
* ```
*
* The above example will override the api-admin-user ngrest config with your project specific config.
*/
public $ngrestConfigLinker = [];

private $_menu = [];

private $_pointers = [];

private $_permissionApis = [];

private $_permissionRoutes = [];

/**
* @var array Each module can have assets, all module controllers will register those assets in the view.. Valid class name to the asset e.g.
*
Expand All @@ -66,31 +59,67 @@ class Module extends \luya\base\Module implements AdminModuleInterface
public $assets = [];

/**
*
* @var array Register translations from admin modules, to make them available in javascript files trough
* the `i18n['yourTranslation']` object.
* @var array Register translations from admin modules, to make them available in javascript files trough the `i18n['yourTranslation']` object.
*/
public $registerJsTranslation = [];

/**
* Checks if a config exist in the linked property based on the provided `$apiEndpoint`.
*
* @param string $apiEndpoint The identifier of an apiEndpoint. ApiEndpoints are listed in the module class.
*
* @return bool|string If apiEndpoint exists in the linker property returns className, otherwhise false.
*/
public function getLinkedNgRestConfig($apiEndpoint)
{
return array_key_exists($apiEndpoint, $this->ngrestConfigLinker) ? $this->ngrestConfigLinker[$apiEndpoint] : false;
}

/**
* @return array|\luya\admin\components\AdminMenuBuilder Get an array or an instance of the AdminMenuBuilderClass.
*/
public function getMenu()
{
return [];
}

public function extendPermissionApis()
{
return [];
}

public function extendPermissionRoutes()
{
return [];
}

public function getAuthApis()
{
$this->getMenu();

return ArrayHelper::merge($this->extendPermissionApis(), $this->_permissionApis);
}

public function getAuthRoutes()
{
$this->getMenu();

return ArrayHelper::merge($this->extendPermissionRoutes(), $this->_permissionRoutes);
}

// THE CODE BELOW WILL BE REMOVED IN 1.0.0 AND IS MAKRED AS DEPRECATED

private $_menu = [];

private $_pointers = [];

private $_permissionApis = [];

private $_permissionRoutes = [];

protected function node($name, $icon, $template = false)
{
trigger_error('Deprecated method '.__METHOD__.' in '.get_called_class().', use \luya\admin\components\AdminMenuBuilder() class instead in the `getMenu()` method of your Module.', E_USER_DEPRECATED);

$this->_pointers['node'] = $name;
$this->_menu[$name] = [
'moduleId' => $this->id,
Expand All @@ -102,12 +131,14 @@ protected function node($name, $icon, $template = false)
'permissionIsRoute' => false,
'searchModelClass' => false,
];

return $this;
}

protected function nodeRoute($name, $icon, $template, $route, $searchModelClass = null)
{
trigger_error('Deprecated method '.__METHOD__.' in '.get_called_class().', use \luya\admin\components\AdminMenuBuilder() class instead in the `getMenu()` method of your Module.', E_USER_DEPRECATED);

$this->_pointers['node'] = $name;
$this->_menu[$name] = [
'moduleId' => $this->id,
Expand All @@ -119,22 +150,26 @@ protected function nodeRoute($name, $icon, $template, $route, $searchModelClass
'permissionIsRoute' => true,
'searchModelClass' => $searchModelClass,
];

$this->_permissionRoutes[] = ['route' => $route, 'alias' => $name];

return $this;
}

protected function group($name)
{
trigger_error('Deprecated method '.__METHOD__.' in '.get_called_class().', use \luya\admin\components\AdminMenuBuilder() class instead in the `getMenu()` method of your Module.', E_USER_DEPRECATED);

$this->_pointers['group'] = $name;
$this->_menu[$this->_pointers['node']]['groups'][$name] = ['name' => $name, 'items' => []];

return $this;
}

protected function itemApi($name, $route, $icon, $apiEndpoint)
{
trigger_error('Deprecated method '.__METHOD__.' in '.get_called_class().', use \luya\admin\components\AdminMenuBuilder() class instead in the `getMenu()` method of your Module.', E_USER_DEPRECATED);

$this->_menu[$this->_pointers['node']]['groups'][$this->_pointers['group']]['items'][] = [
'alias' => $name,
'route' => $route,
Expand All @@ -144,14 +179,16 @@ protected function itemApi($name, $route, $icon, $apiEndpoint)
'permissionIsApi' => true,
'searchModelClass' => false,
];

$this->_permissionApis[] = ['api' => $apiEndpoint, 'alias' => $name];

return $this;
}

protected function itemRoute($name, $route, $icon, $searchModelClass = null)
{
trigger_error('Deprecated method '.__METHOD__.' in '.get_called_class().', use \luya\admin\components\AdminMenuBuilder() class instead in the `getMenu()` method of your Module.', E_USER_DEPRECATED);

$this->_menu[$this->_pointers['node']]['groups'][$this->_pointers['group']]['items'][] = [
'alias' => $name,
'route' => $route,
Expand All @@ -161,38 +198,16 @@ protected function itemRoute($name, $route, $icon, $searchModelClass = null)
'permissionIsApi' => false,
'searchModelClass' => $searchModelClass,
];

$this->_permissionRoutes[] = ['route' => $route, 'alias' => $name];

return $this;
}

protected function menu()
public function menu()
{
trigger_error('Deprecated method '.__METHOD__.' in '.get_called_class().', use \luya\admin\components\AdminMenuBuilder() class instead in the `getMenu()` method of your Module.', E_USER_DEPRECATED);

return $this->_menu;
}

public function extendPermissionApis()
{
return [];
}

public function extendPermissionRoutes()
{
return [];
}

public function getAuthApis()
{
$this->getMenu();

return ArrayHelper::merge($this->extendPermissionApis(), $this->_permissionApis);
}

public function getAuthRoutes()
{
$this->getMenu();

return ArrayHelper::merge($this->extendPermissionRoutes(), $this->_permissionRoutes);
}
}
13 changes: 12 additions & 1 deletion modules/admin/src/components/AdminMenu.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace luya\admin\components;

use Yii;
use luya\helpers\ArrayHelper;

/**
* Admin Menu Data
Expand All @@ -27,7 +28,17 @@ public function getUserId()
public function getMenu()
{
if ($this->_menu === null) {
$this->_menu = Yii::$app->getModule('admin')->moduleMenus;
$menu = [];
$menus = Yii::$app->getModule('admin')->moduleMenus;
foreach ($menus as $k => $v) {
if (is_object($v) && $v instanceof AdminMenuBuilderInterface) {
$data = $v->menu();
} else {
$data = $v;
}
$menu = ArrayHelper::merge($data, $menu);
}
$this->_menu = $menu;
}

return $this->_menu;
Expand Down
Loading

0 comments on commit 7476db7

Please sign in to comment.