Skip to content

Commit

Permalink
bootstrap component
Browse files Browse the repository at this point in the history
  • Loading branch information
loveorigami committed Feb 3, 2017
1 parent d797a7b commit 2b622df
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 84 deletions.
20 changes: 18 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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)
Expand Down
81 changes: 0 additions & 81 deletions src/Bootstrap.php

This file was deleted.

65 changes: 64 additions & 1 deletion src/components/PluginsManager.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
<?php
namespace lo\plugins\components;

use lo\plugins\core\ShortcodeHandler;
use lo\plugins\repositories\EventDbRepository;
use lo\plugins\services\ShortcodeService;
use lo\plugins\shortcodes\ShortcodeParser;
use Yii;
use yii\base\BootstrapInterface;
use yii\base\Component;
use yii\base\Event;
use yii\base\InvalidConfigException;

/**
* Class PluginManager
* @package lo\plugins\components
* @author Lukyanov Andrey <loveorigami@mail.ru>
*/
class PluginsManager extends Component
class PluginsManager extends Component implements BootstrapInterface
{
/**
* Application id for category plugins.
Expand Down Expand Up @@ -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);
}
}
}
}
}

0 comments on commit 2b622df

Please sign in to comment.