From 2b622df36d5ae9c7ccf7581d2866f7cd52873095 Mon Sep 17 00:00:00 2001 From: Loveorigami Date: Fri, 3 Feb 2017 11:08:51 +0300 Subject: [PATCH] bootstrap component --- README.md | 20 +++++++- src/Bootstrap.php | 81 ------------------------------- src/components/PluginsManager.php | 65 ++++++++++++++++++++++++- 3 files changed, 82 insertions(+), 84 deletions(-) delete mode 100644 src/Bootstrap.php diff --git a/README.md b/README.md index 7b1ba14..e430d42 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ Next, open `@frontend/config/main.php` and add following: ```php ... +'bootstrap' => ['log', 'plugins'], +... 'components' => [ 'plugins' => [ 'class' => lo\plugins\components\PluginsManager::class, @@ -68,14 +70,21 @@ Next, open `@frontend/config/main.php` and add following: ] ``` -Also do the same thing with `@backend/config/main.php`: +Also do the same thing with +* `@backend/config/main.php` +* `@console/config/main.php` +* `@api/config/main.php` +* our modules +* etc... ```php ... +'bootstrap' => ['log', 'plugins'], +... 'components' => [ 'plugins' => [ 'class' => lo\plugins\components\PluginsManager::class, - 'appId' => 2 // lo\plugins\BasePlugin::APP_BACKEND + 'appId' => 2 // lo\plugins\BasePlugin::APP_BACKEND or our appId ], 'view' => [ 'class' => lo\plugins\components\View::class, @@ -84,6 +93,13 @@ Also do the same thing with `@backend/config/main.php`: ] ``` +## Base AppId ```lo\plugins\BasePlugin::``` +* const APP_FRONTEND = 1; +* const APP_BACKEND = 2; +* const APP_COMMON = 3; +* const APP_API = 4; +* const APP_CONSOLE = 5; + ## Shortcodes * [Yii2-shortcodes-pack](https://github.com/loveorigami/yii2-shortcodes-pack) diff --git a/src/Bootstrap.php b/src/Bootstrap.php deleted file mode 100644 index 55bbfd8..0000000 --- a/src/Bootstrap.php +++ /dev/null @@ -1,81 +0,0 @@ - - */ -class Bootstrap implements BootstrapInterface -{ - /** - * @param \yii\base\Application $app - * @throws InvalidConfigException - */ - public function bootstrap($app) - { - if (!isset(Yii::$app->i18n->translations['plugin'])) { - Yii::$app->i18n->translations['plugin'] = [ - 'class' => 'yii\i18n\PhpMessageSource', - 'sourceLanguage' => 'en', - 'basePath' => '@lo/plugins/messages' - ]; - } - - /** @var PluginsManager $pluginsManager */ - if (isset($app->plugins)) { - $pluginsManager = $app->plugins; - } else { - throw new InvalidConfigException('Component "plugins" must be set'); - } - - $appId = $pluginsManager->appId; - - if ($pluginsManager->enablePlugins && $appId) { - $this->registerEvents($appId); - } - - if ($pluginsManager->shortcodesParse) { - Yii::$container->setSingleton(ShortcodeParser::class); - Yii::$container->set(ShortcodeService::class); - Event::on(View::class, View::EVENT_DO_BODY, [ - ShortcodeHandler::class, ShortcodeHandler::PARSE_SHORTCODES - ], $pluginsManager); - } - } - - /** - * @param $appId - */ - protected function registerEvents($appId) - { - $repository = new EventDbRepository(); - /** @var \lo\plugins\models\Event [] $events */ - $events = $repository->findEventsByApp($appId); - if ($events) { - foreach ($events as $event) { - $triggerClass = $event->getTriggerClass(); - $triggerEvent = $event->getTriggerEvent(); - $handler = $event->getHandler(); - if (is_array($handler) && is_callable($handler[0])) { - $data = isset($handler[1]) ? array_pop($handler) : null; - $append = isset($handler[2]) ? array_pop($handler) : null; - Event::on($triggerClass, $triggerEvent, $handler[0], $data, $append); - } else if (is_callable($handler)) { - Event::on($triggerClass, $triggerEvent, $handler); - } - } - } - } -} diff --git a/src/components/PluginsManager.php b/src/components/PluginsManager.php index d5a9385..49acbc7 100644 --- a/src/components/PluginsManager.php +++ b/src/components/PluginsManager.php @@ -1,14 +1,22 @@ */ -class PluginsManager extends Component +class PluginsManager extends Component implements BootstrapInterface { /** * Application id for category plugins. @@ -43,4 +51,59 @@ class PluginsManager extends Component */ public $shortcodesIgnoreBlocks = null; + + /** + * @param \yii\base\Application $app + * @throws InvalidConfigException + */ + public function bootstrap($app) + { + if (!isset(Yii::$app->i18n->translations['plugin'])) { + Yii::$app->i18n->translations['plugin'] = [ + 'class' => 'yii\i18n\PhpMessageSource', + 'sourceLanguage' => 'en', + 'basePath' => '@lo/plugins/messages' + ]; + } + + if (!isset($app->plugins)) { + throw new InvalidConfigException('Component "plugins" must be set'); + } + + if ($this->enablePlugins && $this->appId) { + $this->registerEvents($this->appId); + } + + if ($this->shortcodesParse) { + Yii::$container->setSingleton(ShortcodeParser::class); + Yii::$container->set(ShortcodeService::class); + Event::on(View::class, View::EVENT_DO_BODY, [ + ShortcodeHandler::class, ShortcodeHandler::PARSE_SHORTCODES + ], $this); + } + } + + /** + * @param $appId + */ + protected function registerEvents($appId) + { + $repository = new EventDbRepository(); + /** @var \lo\plugins\models\Event [] $events */ + $events = $repository->findEventsByApp($appId); + if ($events) { + foreach ($events as $event) { + $triggerClass = $event->getTriggerClass(); + $triggerEvent = $event->getTriggerEvent(); + $handler = $event->getHandler(); + if (is_array($handler) && is_callable($handler[0])) { + $data = isset($handler[1]) ? array_pop($handler) : null; + $append = isset($handler[2]) ? array_pop($handler) : null; + Event::on($triggerClass, $triggerEvent, $handler[0], $data, $append); + } else if (is_callable($handler)) { + Event::on($triggerClass, $triggerEvent, $handler); + } + } + } + } } \ No newline at end of file