public function loadPlugin()
{
// プラグインディレクトリを探索.
$basePath = $this['config']['plugin_realdir'];
$pluginConfigs = $this->getPluginConfigAll();
// ハンドラ優先順位をdbから持ってきてハッシュテーブルを作成
$priorities = array();
$handlers = $this['orm.em']->getRepository('Eccube\\Entity\\PluginEventHandler')->getHandlers();
foreach ($handlers as $handler) {
if ($handler->getPlugin()->getEnable() && !$handler->getPlugin()->getDelFlg()) {
$priority = $handler->getPriority();
} else {
// Pluginがdisable、削除済みの場合、EventHandlerのPriorityを全て0とみなす
$priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED;
}
$priorities[$handler->getPlugin()->getClassName()][$handler->getEvent()][$handler->getHandler()] = $priority;
}
// プラグインをロードする.
// config.yml/event.ymlの定義に沿ってインスタンスの生成を行い, イベント設定を行う.
foreach ($pluginConfigs as $code => $pluginConfig) {
// 正しい形式の pluginConfig のみ読み込む
$path = $basePath . '/' . $code;
try {
$this['eccube.service.plugin']->checkPluginArchiveContent($path, $pluginConfig['config']);
} catch (\Eccube\Exception\PluginException $e) {
$this['monolog']->warning("skip {$code} config loading. config.yml not foud or invalid.", array('path' => $path, 'original-message' => $e->getMessage()));
continue;
}
$config = $pluginConfig['config'];
$plugin = $this['orm.em']->getRepository('Eccube\\Entity\\Plugin')->findOneBy(array('code' => $config['code']));
// const
if (isset($config['const'])) {
$this['config'] = $this->share($this->extend('config', function ($eccubeConfig) use($config) {
$eccubeConfig[$config['code']] = array('const' => $config['const']);
return $eccubeConfig;
}));
}
if ($plugin && $plugin->getEnable() == Constant::DISABLED) {
// プラグインが無効化されていれば読み込まない
continue;
}
// Type: Event
if (isset($config['event'])) {
$class = '\\Plugin\\' . $config['code'] . '\\' . $config['event'];
$eventExists = true;
if (!class_exists($class)) {
$this['monolog']->warning("skip {$code} loading. event class not foud.", array('class' => $class));
$eventExists = false;
}
if ($eventExists && isset($config['event'])) {
$subscriber = new $class($this);
foreach ($pluginConfig['event'] as $event => $handlers) {
foreach ($handlers as $handler) {
if (!isset($priorities[$config['event']][$event][$handler[0]])) {
// ハンドラテーブルに登録されていない(ソースにしか記述されていない)ハンドラは一番後ろにする
$priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_LATEST;
} else {
$priority = $priorities[$config['event']][$event][$handler[0]];
}
// 優先度が0のプラグインは登録しない
if (\Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED != $priority) {
$this['eccube.event.dispatcher']->addListener($event, array($subscriber, $handler[0]), $priority);
}
}
}
}
}
// Type: ServiceProvider
if (isset($config['service'])) {
foreach ($config['service'] as $service) {
$class = '\\Plugin\\' . $config['code'] . '\\ServiceProvider\\' . $service;
if (!class_exists($class)) {
$this['monolog']->warning("skip {$code} loading. service provider class not foud.", array('class' => $class));
continue;
}
$this->register(new $class($this));
}
}
}
}