Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like EccubePluginServiceProvider often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use EccubePluginServiceProvider, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 21 | class EccubePluginServiceProvider implements ServiceProviderInterface, BootableProviderInterface |
||
|
|
|||
| 22 | { |
||
| 23 | /** |
||
| 24 | * {@inheritDoc} |
||
| 25 | */ |
||
| 26 | 1 | public function register(Container $app) |
|
| 27 | { |
||
| 28 | // EventDispatcher |
||
| 29 | $app['eccube.event.dispatcher'] = function () { |
||
| 30 | return new EventDispatcher(); |
||
| 31 | }; |
||
| 32 | |||
| 33 | // プラグインディレクトリを探索. |
||
| 34 | 1 | $pluginConfigs = PluginConfigManager::getPluginConfigAll($app['debug']); |
|
| 35 | |||
| 36 | 1 | $enabledPlugins = $app['orm.em']->getRepository('Eccube\Entity\Plugin')->findAllEnabled(); |
|
| 37 | |||
| 38 | $app['eccube.routers.plugin'] = function ($app) use ($enabledPlugins) { |
||
| 39 | $pluginDirs = array_map(function($plugin) use ($app) { |
||
| 40 | return $app['config']['root_dir'].'/app/Plugin/'.$plugin->getCode(); |
||
| 41 | }, $enabledPlugins); |
||
| 42 | |||
| 43 | $routers = []; |
||
| 44 | |||
| 45 | if ((empty($pluginDirs)) === false) { |
||
| 46 | $dirs = Finder::create() |
||
| 47 | ->in($pluginDirs) |
||
| 48 | ->name('Controller') |
||
| 49 | ->directories(); |
||
| 50 | |||
| 51 | foreach ($dirs as $dir) { |
||
| 52 | $realPath = $dir->getRealPath(); |
||
| 53 | $pluginCode = basename(dirname($realPath)); |
||
| 54 | $routers[] = $app['eccube.router']($realPath, 'Plugin'.$pluginCode); |
||
| 55 | } |
||
| 56 | } |
||
| 57 | |||
| 58 | return $routers; |
||
| 59 | }; |
||
| 60 | |||
| 61 | 1 | foreach ($pluginConfigs as $code => $pluginConfig) { |
|
| 62 | 1 | $config = $pluginConfig['config']; |
|
| 63 | |||
| 64 | 1 | if (isset($config['const'])) { |
|
| 65 | $app->extend('config', function ($eccubeConfig) use ($config) { |
||
| 66 | $eccubeConfig[$config['code']] = array( |
||
| 67 | 'const' => $config['const'], |
||
| 68 | ); |
||
| 69 | return $eccubeConfig; |
||
| 70 | 1 | }); |
|
| 71 | } |
||
| 72 | } |
||
| 73 | } |
||
| 74 | |||
| 75 | /** |
||
| 76 | * {@inheritDoc} |
||
| 77 | */ |
||
| 78 | public function boot(Application $app) |
||
| 79 | { |
||
| 80 | $this->initPluginEventDispatcher($app); |
||
| 81 | $this->loadPlugin($app); |
||
| 82 | } |
||
| 83 | |||
| 84 | public function initPluginEventDispatcher(Application $app) |
||
| 85 | { |
||
| 86 | // hook point |
||
| 87 | $app->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) { |
||
| 88 | if (!$event->isMasterRequest()) { |
||
| 89 | return; |
||
| 90 | } |
||
| 91 | $hookpoint = 'eccube.event.app.before'; |
||
| 92 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
||
| 93 | }, Application::EARLY_EVENT); |
||
| 94 | |||
| 95 | View Code Duplication | $app->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) { |
|
| 96 | if (!$event->isMasterRequest()) { |
||
| 97 | return; |
||
| 98 | } |
||
| 99 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 100 | $hookpoint = "eccube.event.controller.$route.before"; |
||
| 101 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
||
| 102 | }); |
||
| 103 | |||
| 104 | View Code Duplication | $app->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { |
|
| 105 | if (!$event->isMasterRequest()) { |
||
| 106 | return; |
||
| 107 | } |
||
| 108 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 109 | $hookpoint = "eccube.event.controller.$route.after"; |
||
| 110 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
||
| 111 | }); |
||
| 112 | |||
| 113 | $app->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { |
||
| 114 | if (!$event->isMasterRequest()) { |
||
| 115 | return; |
||
| 116 | } |
||
| 117 | $hookpoint = 'eccube.event.app.after'; |
||
| 118 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
||
| 119 | }, Application::LATE_EVENT); |
||
| 120 | |||
| 121 | $app->on(KernelEvents::TERMINATE, function (PostResponseEvent $event) use ($app) { |
||
| 122 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 123 | $hookpoint = "eccube.event.controller.$route.finish"; |
||
| 124 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
||
| 125 | }); |
||
| 126 | |||
| 127 | $app->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
||
| 128 | if (!$event->isMasterRequest()) { |
||
| 129 | return; |
||
| 130 | } |
||
| 131 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 132 | $app['eccube.event.dispatcher']->dispatch('eccube.event.render.'.$route.'.before', $event); |
||
| 133 | }); |
||
| 134 | |||
| 135 | // Request Event |
||
| 136 | View Code Duplication | $app->on(\Symfony\Component\HttpKernel\KernelEvents::REQUEST, function (\Symfony\Component\HttpKernel\Event\GetResponseEvent $event) use ($app) { |
|
| 137 | |||
| 138 | if (!$event->isMasterRequest()) { |
||
| 139 | return; |
||
| 140 | } |
||
| 141 | |||
| 142 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 143 | |||
| 144 | if (is_null($route)) { |
||
| 145 | return; |
||
| 146 | } |
||
| 147 | |||
| 148 | $app['monolog']->debug('KernelEvents::REQUEST '.$route); |
||
| 149 | |||
| 150 | // 全体 |
||
| 151 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.request', $event); |
||
| 152 | |||
| 153 | if (strpos($route, 'admin') === 0) { |
||
| 154 | // 管理画面 |
||
| 155 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.request', $event); |
||
| 156 | } else { |
||
| 157 | // フロント画面 |
||
| 158 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.request', $event); |
||
| 159 | } |
||
| 160 | |||
| 161 | // ルーティング単位 |
||
| 162 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.request", $event); |
||
| 163 | |||
| 164 | }, 30); // Routing(32)が解決し, 認証判定(8)が実行される前のタイミング. |
||
| 165 | |||
| 166 | // Controller Event |
||
| 167 | View Code Duplication | $app->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
|
| 168 | |||
| 169 | if (!$event->isMasterRequest()) { |
||
| 170 | return; |
||
| 171 | } |
||
| 172 | |||
| 173 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 174 | |||
| 175 | if (is_null($route)) { |
||
| 176 | return; |
||
| 177 | } |
||
| 178 | |||
| 179 | $app['monolog']->debug('KernelEvents::CONTROLLER '.$route); |
||
| 180 | |||
| 181 | // 全体 |
||
| 182 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.controller', $event); |
||
| 183 | |||
| 184 | if (strpos($route, 'admin') === 0) { |
||
| 185 | // 管理画面 |
||
| 186 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.controller', $event); |
||
| 187 | } else { |
||
| 188 | // フロント画面 |
||
| 189 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.controller', $event); |
||
| 190 | } |
||
| 191 | // ルーティング単位 |
||
| 192 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.controller", $event); |
||
| 193 | }); |
||
| 194 | |||
| 195 | // Response Event |
||
| 196 | View Code Duplication | $app->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
|
| 197 | if (!$event->isMasterRequest()) { |
||
| 198 | return; |
||
| 199 | } |
||
| 200 | |||
| 201 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 202 | |||
| 203 | if (is_null($route)) { |
||
| 204 | return; |
||
| 205 | } |
||
| 206 | |||
| 207 | $app['monolog']->debug('KernelEvents::RESPONSE '.$route); |
||
| 208 | |||
| 209 | // ルーティング単位 |
||
| 210 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.response", $event); |
||
| 211 | |||
| 212 | if (strpos($route, 'admin') === 0) { |
||
| 213 | // 管理画面 |
||
| 214 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.response', $event); |
||
| 215 | } else { |
||
| 216 | // フロント画面 |
||
| 217 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.response', $event); |
||
| 218 | } |
||
| 219 | |||
| 220 | // 全体 |
||
| 221 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.response', $event); |
||
| 222 | }); |
||
| 223 | |||
| 224 | // Exception Event |
||
| 225 | View Code Duplication | $app->on(\Symfony\Component\HttpKernel\KernelEvents::EXCEPTION, function (\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event) use ($app) { |
|
| 226 | |||
| 227 | if (!$event->isMasterRequest()) { |
||
| 228 | return; |
||
| 229 | } |
||
| 230 | |||
| 231 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 232 | |||
| 233 | if (is_null($route)) { |
||
| 234 | return; |
||
| 235 | } |
||
| 236 | |||
| 237 | $app['monolog']->debug('KernelEvents::EXCEPTION '.$route); |
||
| 238 | |||
| 239 | // ルーティング単位 |
||
| 240 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.exception", $event); |
||
| 241 | |||
| 242 | if (strpos($route, 'admin') === 0) { |
||
| 243 | // 管理画面 |
||
| 244 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.exception', $event); |
||
| 245 | } else { |
||
| 246 | // フロント画面 |
||
| 247 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.exception', $event); |
||
| 248 | } |
||
| 249 | |||
| 250 | // 全体 |
||
| 251 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.exception', $event); |
||
| 252 | }); |
||
| 253 | |||
| 254 | // Terminate Event |
||
| 255 | View Code Duplication | $app->on(\Symfony\Component\HttpKernel\KernelEvents::TERMINATE, function (\Symfony\Component\HttpKernel\Event\PostResponseEvent $event) use ($app) { |
|
| 256 | |||
| 257 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 258 | |||
| 259 | if (is_null($route)) { |
||
| 260 | return; |
||
| 261 | } |
||
| 262 | |||
| 263 | $app['monolog']->debug('KernelEvents::TERMINATE '.$route); |
||
| 264 | |||
| 265 | // ルーティング単位 |
||
| 266 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.terminate", $event); |
||
| 267 | |||
| 268 | if (strpos($route, 'admin') === 0) { |
||
| 269 | // 管理画面 |
||
| 270 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.terminate', $event); |
||
| 271 | } else { |
||
| 272 | // フロント画面 |
||
| 273 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.terminate', $event); |
||
| 274 | } |
||
| 275 | |||
| 276 | // 全体 |
||
| 277 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.terminate', $event); |
||
| 278 | }); |
||
| 279 | } |
||
| 280 | |||
| 281 | public function loadPlugin(Application $app) |
||
| 282 | { |
||
| 283 | // ハンドラ優先順位をdbから持ってきてハッシュテーブルを作成 |
||
| 284 | $priorities = array(); |
||
| 285 | $pluginConfigs = PluginConfigManager::getPluginConfigAll($app['debug']); |
||
| 286 | |||
| 287 | $handlers = $app['orm.em'] |
||
| 288 | ->getRepository('Eccube\Entity\PluginEventHandler') |
||
| 289 | ->getHandlers(); |
||
| 290 | |||
| 291 | foreach ($handlers as $handler) { |
||
| 292 | if ($handler->getPlugin()->getEnable()) { |
||
| 293 | |||
| 294 | $priority = $handler->getPriority(); |
||
| 295 | } else { |
||
| 296 | // Pluginがdisable、削除済みの場合、EventHandlerのPriorityを全て0とみなす |
||
| 297 | $priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED; |
||
| 298 | } |
||
| 299 | $priorities[$handler->getPlugin()->getClassName()][$handler->getEvent()][$handler->getHandler()] = $priority; |
||
| 300 | } |
||
| 301 | |||
| 302 | // プラグインをロードする. |
||
| 303 | // config.yml/event.ymlの定義に沿ってインスタンスの生成を行い, イベント設定を行う. |
||
| 304 | foreach ($pluginConfigs as $code => $pluginConfig) { |
||
| 305 | // 正しい形式の pluginConfig のみ読み込む |
||
| 306 | $path = PluginConfigManager::getPluginRealDir().'/'.$code; |
||
| 307 | try { |
||
| 308 | $app[PluginService::class]->checkPluginArchiveContent($path, $pluginConfig['config']); |
||
| 309 | } catch (\Eccube\Exception\PluginException $e) { |
||
| 310 | $app['monolog']->warning("Configuration file config.yml for plugin {$code} not found or is invalid. Skipping loading.", array( |
||
| 311 | 'path' => $path, |
||
| 312 | 'original-message' => $e->getMessage() |
||
| 313 | )); |
||
| 314 | continue; |
||
| 315 | } |
||
| 316 | $config = $pluginConfig['config']; |
||
| 317 | |||
| 318 | $plugin = $app['orm.em'] |
||
| 319 | ->getRepository('Eccube\Entity\Plugin') |
||
| 320 | ->findOneBy(array('code' => $config['code'])); |
||
| 321 | |||
| 322 | // const |
||
| 323 | if (is_null($plugin) || $plugin->getEnable() == Constant::DISABLED) { |
||
| 324 | // プラグインが無効化されていれば読み込まない |
||
| 325 | continue; |
||
| 326 | } |
||
| 327 | |||
| 328 | // Type: Event |
||
| 329 | if (isset($config['event'])) { |
||
| 330 | $class = '\\Plugin\\'.$config['code'].'\\'.$config['event']; |
||
| 331 | $eventExists = true; |
||
| 332 | |||
| 333 | View Code Duplication | if (!class_exists($class)) { |
|
| 334 | $app['monolog']->warning("Event class for plugin {$code} not exists.", array( |
||
| 335 | 'class' => $class, |
||
| 336 | )); |
||
| 337 | $eventExists = false; |
||
| 338 | } |
||
| 339 | |||
| 340 | if ($eventExists && isset($config['event'])) { |
||
| 341 | |||
| 342 | $subscriber = new $class($app); |
||
| 343 | |||
| 344 | foreach ($pluginConfig['event'] as $event => $handlers) { |
||
| 345 | foreach ($handlers as $handler) { |
||
| 346 | if (!isset($priorities[$config['event']][$event][$handler[0]])) { // ハンドラテーブルに登録されていない(ソースにしか記述されていない)ハンドラは一番後ろにする |
||
| 347 | $priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_LATEST; |
||
| 348 | } else { |
||
| 349 | $priority = $priorities[$config['event']][$event][$handler[0]]; |
||
| 350 | } |
||
| 351 | // 優先度が0のプラグインは登録しない |
||
| 352 | if (\Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED != $priority) { |
||
| 353 | $app['eccube.event.dispatcher']->addListener($event, array($subscriber, $handler[0]), $priority); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | } |
||
| 358 | } |
||
| 359 | |||
| 360 | $this->registerServiceProviders($app, $config); |
||
| 361 | } |
||
| 362 | } |
||
| 363 | |||
| 364 | private function registerServiceProviders($app, $config) |
||
| 395 | } |
||
| 396 |