-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d797a7b
commit 2b622df
Showing
3 changed files
with
82 additions
and
84 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 <[email protected]> | ||
*/ | ||
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); | ||
} | ||
} | ||
} | ||
} | ||
} |