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 Application 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 Application, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 40 | class Application extends ApplicationTrait |
||
|
|
|||
| 41 | { |
||
| 42 | protected static $instance; |
||
| 43 | |||
| 44 | protected $initialized = false; |
||
| 45 | protected $initializedPlugin = false; |
||
| 46 | protected $testMode = false; |
||
| 47 | |||
| 48 | 1058 | public static function getInstance(array $values = array()) |
|
| 49 | { |
||
| 50 | 1058 | if (!is_object(self::$instance)) { |
|
| 51 | 1054 | self::$instance = new Application($values); |
|
| 52 | 1054 | } |
|
| 53 | |||
| 54 | 1058 | return self::$instance; |
|
| 55 | } |
||
| 56 | |||
| 57 | 1055 | public static function clearInstance() |
|
| 58 | { |
||
| 59 | 1055 | self::$instance = null; |
|
| 60 | 1055 | } |
|
| 61 | |||
| 62 | final public function __clone() |
||
| 66 | |||
| 67 | 1069 | public function __construct(array $values = array()) |
|
| 68 | 1069 | { |
|
| 81 | |||
| 82 | /** |
||
| 83 | * Application::runが実行されているか親クラスのプロパティから判定 |
||
| 84 | * |
||
| 85 | * @return bool |
||
| 86 | */ |
||
| 87 | 1062 | public function isBooted() |
|
| 91 | |||
| 92 | 1069 | public function initConfig() |
|
| 112 | |||
| 113 | 1069 | public function initLogger() |
|
| 118 | |||
| 119 | 1070 | public function initialize() |
|
| 202 | |||
| 203 | 1059 | public function initLocale() |
|
| 231 | |||
| 232 | 1059 | public function initSession() |
|
| 256 | |||
| 257 | 1059 | public function initRendering() |
|
| 258 | { |
||
| 259 | 1059 | $this->register(new \Silex\Provider\TwigServiceProvider(), array( |
|
| 260 | 1059 | 'twig.form.templates' => array('Form/form_layout.twig'), |
|
| 261 | 1059 | )); |
|
| 262 | $this['twig'] = $this->share($this->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) { |
||
| 263 | 472 | $twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app)); |
|
| 264 | 472 | $twig->addExtension(new \Twig_Extension_StringLoader()); |
|
| 265 | |||
| 266 | 472 | return $twig; |
|
| 267 | 1059 | })); |
|
| 268 | |||
| 269 | $this->before(function (Request $request, \Silex\Application $app) { |
||
| 270 | 460 | $app['admin'] = false; |
|
| 271 | 460 | $app['front'] = false; |
|
| 272 | 460 | $pathinfo = rawurldecode($request->getPathInfo()); |
|
| 273 | 460 | if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) { |
|
| 274 | 291 | $app['admin'] = true; |
|
| 275 | 291 | } else { |
|
| 276 | 171 | $app['front'] = true; |
|
| 277 | } |
||
| 278 | |||
| 279 | // フロント or 管理画面ごとにtwigの探索パスを切り替える. |
||
| 280 | $app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) { |
||
| 281 | 458 | $paths = array(); |
|
| 282 | |||
| 283 | // 互換性がないのでprofiler とproduction 時のcacheを分離する |
||
| 284 | 458 | if (isset($app['profiler'])) { |
|
| 285 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/profiler/'; |
||
| 286 | } else { |
||
| 287 | 458 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/production/'; |
|
| 288 | } |
||
| 289 | |||
| 290 | 458 | if ($app->isAdminRequest()) { |
|
| 291 | 291 | if (file_exists(__DIR__.'/../../app/template/admin')) { |
|
| 292 | 291 | $paths[] = __DIR__.'/../../app/template/admin'; |
|
| 293 | 291 | } |
|
| 294 | 291 | $paths[] = $app['config']['template_admin_realdir']; |
|
| 295 | 291 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 296 | 291 | $cache = $cacheBaseDir.'admin'; |
|
| 297 | |||
| 298 | 291 | } else { |
|
| 299 | 169 | if (file_exists($app['config']['template_realdir'])) { |
|
| 300 | 169 | $paths[] = $app['config']['template_realdir']; |
|
| 301 | 169 | } |
|
| 302 | 169 | $paths[] = $app['config']['template_default_realdir']; |
|
| 303 | 169 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 304 | 169 | $cache = $cacheBaseDir.$app['config']['template_code']; |
|
| 305 | 169 | $app['front'] = true; |
|
| 306 | } |
||
| 307 | 458 | $twig->setCache($cache); |
|
| 308 | 459 | $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths)); |
|
| 309 | |||
| 310 | 458 | return $twig; |
|
| 311 | 460 | })); |
|
| 312 | |||
| 313 | // 管理画面のIP制限チェック. |
||
| 314 | 460 | if ($app->isAdminRequest()) { |
|
| 315 | // IP制限チェック |
||
| 316 | 291 | $allowHost = $app['config']['admin_allow_host']; |
|
| 317 | 291 | if (count($allowHost) > 0) { |
|
| 318 | if (array_search($app['request']->getClientIp(), $allowHost) === false) { |
||
| 319 | throw new \Exception(); |
||
| 320 | } |
||
| 321 | } |
||
| 322 | 291 | } |
|
| 323 | 1059 | }, self::EARLY_EVENT); |
|
| 324 | |||
| 325 | // twigのグローバル変数を定義. |
||
| 326 | 1059 | $app = $this; |
|
| 327 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
||
| 328 | // 未ログイン時にマイページや管理画面以下にアクセスするとSubRequestで実行されるため, |
||
| 329 | // $event->isMasterRequest()ではなく、グローバル変数が初期化済かどうかの判定を行う |
||
| 330 | 458 | if (isset($app['twig_global_initialized']) && $app['twig_global_initialized'] === true) { |
|
| 331 | 113 | return; |
|
| 332 | } |
||
| 333 | // ショップ基本情報 |
||
| 334 | 458 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
| 335 | 458 | $app['twig']->addGlobal('BaseInfo', $BaseInfo); |
|
| 336 | |||
| 337 | 458 | if ($app->isAdminRequest()) { |
|
| 338 | // 管理画面 |
||
| 339 | // 管理画面メニュー |
||
| 340 | 291 | $menus = array('', '', ''); |
|
| 341 | 291 | $app['twig']->addGlobal('menus', $menus); |
|
| 342 | |||
| 343 | 291 | $Member = $app->user(); |
|
| 344 | 291 | if (is_object($Member)) { |
|
| 345 | // ログインしていれば管理者のロールを取得 |
||
| 346 | 285 | $AuthorityRoles = $app['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority())); |
|
| 347 | |||
| 348 | 285 | $roles = array(); |
|
| 349 | 285 | foreach ($AuthorityRoles as $AuthorityRole) { |
|
| 350 | // 管理画面でメニュー制御するため相対パス全てをセット |
||
| 351 | 3 | $roles[] = $app['request']->getBaseUrl().'/'.$app['config']['admin_route'].$AuthorityRole->getDenyUrl(); |
|
| 352 | 285 | } |
|
| 353 | |||
| 354 | 285 | $app['twig']->addGlobal('AuthorityRoles', $roles); |
|
| 355 | 285 | } |
|
| 356 | |||
| 357 | 291 | } else { |
|
| 358 | // フロント画面 |
||
| 359 | 167 | $request = $event->getRequest(); |
|
| 360 | 167 | $route = $request->attributes->get('_route'); |
|
| 361 | |||
| 362 | // ユーザ作成画面 |
||
| 363 | 167 | if ($route === 'user_data') { |
|
| 364 | 2 | $params = $request->attributes->get('_route_params'); |
|
| 365 | 2 | $route = $params['route']; |
|
| 366 | // プレビュー画面 |
||
| 367 | 167 | } elseif ($request->get('preview')) { |
|
| 368 | $route = 'preview'; |
||
| 369 | } |
||
| 370 | |||
| 371 | 1 | try { |
|
| 372 | 167 | $DeviceType = $app['eccube.repository.master.device_type'] |
|
| 373 | 167 | ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC); |
|
| 374 | 167 | $PageLayout = $app['eccube.repository.page_layout']->getByUrl($DeviceType, $route); |
|
| 375 | 167 | } catch (\Doctrine\ORM\NoResultException $e) { |
|
| 376 | 65 | $PageLayout = $app['eccube.repository.page_layout']->newPageLayout($DeviceType); |
|
| 377 | } |
||
| 378 | |||
| 379 | 167 | $app['twig']->addGlobal('PageLayout', $PageLayout); |
|
| 380 | 167 | $app['twig']->addGlobal('title', $PageLayout->getName()); |
|
| 381 | } |
||
| 382 | |||
| 383 | 458 | $app['twig_global_initialized'] = true; |
|
| 384 | 1059 | }); |
|
| 385 | 1059 | } |
|
| 386 | |||
| 387 | 1059 | public function initMailer() |
|
| 388 | { |
||
| 389 | |||
| 390 | // メール送信時の文字エンコード指定(デフォルトはUTF-8) |
||
| 391 | 1059 | if (isset($this['config']['mail']['charset_iso_2022_jp']) && is_bool($this['config']['mail']['charset_iso_2022_jp'])) { |
|
| 392 | 1059 | if ($this['config']['mail']['charset_iso_2022_jp'] === true) { |
|
| 393 | \Swift::init(function () { |
||
| 394 | \Swift_DependencyContainer::getInstance() |
||
| 395 | ->register('mime.qpheaderencoder') |
||
| 396 | ->asAliasOf('mime.base64headerencoder'); |
||
| 397 | \Swift_Preferences::getInstance()->setCharset('iso-2022-jp'); |
||
| 398 | }); |
||
| 399 | } |
||
| 400 | 1059 | } |
|
| 401 | |||
| 402 | 1059 | $this->register(new \Silex\Provider\SwiftmailerServiceProvider()); |
|
| 403 | 1059 | $this['swiftmailer.options'] = $this['config']['mail']; |
|
| 404 | |||
| 405 | 1059 | if (isset($this['config']['mail']['spool']) && is_bool($this['config']['mail']['spool'])) { |
|
| 406 | $this['swiftmailer.use_spool'] = $this['config']['mail']['spool']; |
||
| 407 | } |
||
| 408 | // デフォルトはsmtpを使用 |
||
| 409 | 1059 | $transport = $this['config']['mail']['transport']; |
|
| 410 | 1059 | if ($transport == 'sendmail') { |
|
| 411 | $this['swiftmailer.transport'] = \Swift_SendmailTransport::newInstance(); |
||
| 412 | 1059 | } elseif ($transport == 'mail') { |
|
| 413 | $this['swiftmailer.transport'] = \Swift_MailTransport::newInstance(); |
||
| 414 | } |
||
| 415 | 1059 | } |
|
| 416 | |||
| 417 | 1059 | public function initDoctrine() |
|
| 418 | { |
||
| 419 | 1059 | $this->register(new \Silex\Provider\DoctrineServiceProvider(), array( |
|
| 420 | 'dbs.options' => array( |
||
| 421 | 1059 | 'default' => $this['config']['database'] |
|
| 422 | 1059 | ))); |
|
| 423 | 1059 | $this->register(new \Saxulum\DoctrineOrmManagerRegistry\Silex\Provider\DoctrineOrmManagerRegistryProvider()); |
|
| 424 | |||
| 425 | // プラグインのmetadata定義を合わせて行う. |
||
| 426 | 1059 | $pluginBasePath = __DIR__.'/../../app/Plugin'; |
|
| 427 | 1059 | $finder = Finder::create() |
|
| 428 | 1059 | ->in($pluginBasePath) |
|
| 429 | 1059 | ->directories() |
|
| 430 | 1059 | ->depth(0); |
|
| 431 | |||
| 432 | 1059 | $ormMappings = array(); |
|
| 433 | 1059 | $ormMappings[] = array( |
|
| 434 | 1059 | 'type' => 'yml', |
|
| 435 | 1059 | 'namespace' => 'Eccube\Entity', |
|
| 436 | 'path' => array( |
||
| 437 | 1059 | __DIR__.'/Resource/doctrine', |
|
| 438 | 1059 | __DIR__.'/Resource/doctrine/master', |
|
| 439 | 1059 | ), |
|
| 440 | ); |
||
| 441 | |||
| 442 | 1059 | foreach ($finder as $dir) { |
|
| 443 | |||
| 444 | 139 | $file = $dir->getRealPath().'/config.yml'; |
|
| 445 | |||
| 446 | 139 | if (file_exists($file)) { |
|
| 447 | 139 | $config = Yaml::parse(file_get_contents($file)); |
|
| 448 | 139 | } else { |
|
| 449 | $code = $dir->getBaseName(); |
||
| 450 | $this['monolog']->warning("skip {$code} orm.path loading. config.yml not found.", array('path' => $file)); |
||
| 451 | continue; |
||
| 452 | } |
||
| 453 | |||
| 454 | // Doctrine Extend |
||
| 455 | 139 | if (isset($config['orm.path']) && is_array($config['orm.path'])) { |
|
| 456 | $paths = array(); |
||
| 457 | foreach ($config['orm.path'] as $path) { |
||
| 458 | $paths[] = $pluginBasePath.'/'.$config['code'].$path; |
||
| 459 | } |
||
| 460 | $ormMappings[] = array( |
||
| 461 | 'type' => 'yml', |
||
| 462 | 'namespace' => 'Plugin\\'.$config['code'].'\\Entity', |
||
| 463 | 'path' => $paths, |
||
| 464 | ); |
||
| 465 | } |
||
| 466 | 1059 | } |
|
| 467 | |||
| 468 | $options = array( |
||
| 469 | 'mappings' => $ormMappings |
||
| 470 | 1059 | ); |
|
| 471 | |||
| 472 | 1059 | if (!$this['debug']) { |
|
| 473 | 4 | $cacheDrivers = array(); |
|
| 474 | 4 | if (array_key_exists('doctrine_cache', $this['config'])) { |
|
| 475 | 4 | $cacheDrivers = $this['config']['doctrine_cache']; |
|
| 476 | 4 | } |
|
| 477 | |||
| 478 | 4 | if (array_key_exists('metadata_cache', $cacheDrivers)) { |
|
| 479 | 4 | $options['metadata_cache'] = $cacheDrivers['metadata_cache']; |
|
| 480 | 4 | } |
|
| 481 | 4 | if (array_key_exists('query_cache', $cacheDrivers)) { |
|
| 482 | 4 | $options['query_cache'] = $cacheDrivers['query_cache']; |
|
| 483 | 4 | } |
|
| 484 | 4 | if (array_key_exists('result_cache', $cacheDrivers)) { |
|
| 485 | 4 | $options['result_cache'] = $cacheDrivers['result_cache']; |
|
| 486 | 4 | } |
|
| 487 | 4 | if (array_key_exists('hydration_cache', $cacheDrivers)) { |
|
| 488 | 4 | $options['hydration_cache'] = $cacheDrivers['hydration_cache']; |
|
| 489 | 4 | } |
|
| 490 | 4 | } |
|
| 491 | |||
| 492 | 1059 | $this->register(new \Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array( |
|
| 493 | 1059 | 'orm.proxies_dir' => __DIR__.'/../../app/cache/doctrine/proxies', |
|
| 494 | 1059 | 'orm.em.options' => $options, |
|
| 495 | 'orm.custom.functions.numeric' => array( |
||
| 496 | 1059 | 'EXTRACT' => 'Eccube\Doctrine\ORM\Query\Extract', |
|
| 497 | 1059 | ), |
|
| 498 | 1059 | )); |
|
| 499 | |||
| 500 | /** |
||
| 501 | * YamlDriverのPHP7対応. Doctrine2.4で修正されれば不要. |
||
| 502 | * @see https://github.com/EC-CUBE/ec-cube/issues/1338 |
||
| 503 | */ |
||
| 504 | 1059 | $config = $this['orm.em']->getConfiguration(); |
|
| 505 | /** @var $driver \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain */ |
||
| 506 | 1059 | $chain = $config->getMetadataDriverImpl(); |
|
| 507 | // $ormMappingsの1要素ごとにDriverが生成されている. |
||
| 508 | 1059 | $drivers = $chain->getDrivers(); |
|
| 509 | 1059 | foreach ($drivers as $namespace => $oldDriver) { |
|
| 510 | /** @var $newDriver \Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver */ |
||
| 511 | 1059 | $newDriver = new YamlDriver($oldDriver->getLocator()); |
|
| 512 | // 修正したDriverに差し替える. メソッド名はaddだけど実際はsetしてる. |
||
| 513 | 1059 | $chain->addDriver($newDriver, $namespace); |
|
| 514 | 1059 | } |
|
| 515 | 1059 | } |
|
| 516 | |||
| 517 | 1059 | public function initSecurity() |
|
| 518 | { |
||
| 519 | 1059 | $this->register(new \Silex\Provider\SecurityServiceProvider()); |
|
| 520 | 1059 | $this->register(new \Silex\Provider\RememberMeServiceProvider()); |
|
| 521 | |||
| 522 | 1059 | $this['security.firewalls'] = array( |
|
| 523 | 'admin' => array( |
||
| 524 | 1059 | 'pattern' => "^/{$this['config']['admin_route']}/", |
|
| 525 | 'form' => array( |
||
| 526 | 1059 | 'login_path' => "/{$this['config']['admin_route']}/login", |
|
| 527 | 1059 | 'check_path' => "/{$this['config']['admin_route']}/login_check", |
|
| 528 | 1059 | 'username_parameter' => 'login_id', |
|
| 529 | 1059 | 'password_parameter' => 'password', |
|
| 530 | 1059 | 'with_csrf' => true, |
|
| 531 | 1059 | 'use_forward' => true, |
|
| 532 | 1059 | ), |
|
| 533 | 'logout' => array( |
||
| 534 | 1059 | 'logout_path' => "/{$this['config']['admin_route']}/logout", |
|
| 535 | 1059 | 'target_url' => "/{$this['config']['admin_route']}/", |
|
| 536 | 1059 | ), |
|
| 537 | 1059 | 'users' => $this['orm.em']->getRepository('Eccube\Entity\Member'), |
|
| 538 | 1059 | 'anonymous' => true, |
|
| 539 | 1059 | ), |
|
| 540 | 'customer' => array( |
||
| 541 | 1059 | 'pattern' => '^/', |
|
| 542 | 'form' => array( |
||
| 543 | 1059 | 'login_path' => '/mypage/login', |
|
| 544 | 1059 | 'check_path' => '/login_check', |
|
| 545 | 1059 | 'username_parameter' => 'login_email', |
|
| 546 | 1059 | 'password_parameter' => 'login_pass', |
|
| 547 | 1059 | 'with_csrf' => true, |
|
| 548 | 1059 | 'use_forward' => true, |
|
| 549 | 1059 | ), |
|
| 550 | 'logout' => array( |
||
| 551 | 1059 | 'logout_path' => '/logout', |
|
| 552 | 1059 | 'target_url' => '/', |
|
| 553 | 1059 | ), |
|
| 554 | 'remember_me' => array( |
||
| 555 | 1059 | 'key' => sha1($this['config']['auth_magic']), |
|
| 556 | 1059 | 'name' => 'eccube_rememberme', |
|
| 557 | // lifetimeはデフォルトの1年間にする |
||
| 558 | // 'lifetime' => $this['config']['cookie_lifetime'], |
||
| 559 | 1059 | 'path' => $this['config']['root_urlpath'] ?: '/', |
|
| 560 | 1059 | 'secure' => $this['config']['force_ssl'], |
|
| 561 | 1059 | 'httponly' => true, |
|
| 562 | 1059 | 'always_remember_me' => false, |
|
| 563 | 1059 | 'remember_me_parameter' => 'login_memory', |
|
| 564 | 1059 | ), |
|
| 565 | 1059 | 'users' => $this['orm.em']->getRepository('Eccube\Entity\Customer'), |
|
| 566 | 1059 | 'anonymous' => true, |
|
| 567 | 1059 | ), |
|
| 568 | ); |
||
| 569 | |||
| 570 | 1059 | $this['security.access_rules'] = array( |
|
| 571 | 1059 | array("^/{$this['config']['admin_route']}/login", 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
| 572 | 1059 | array("^/{$this['config']['admin_route']}/", 'ROLE_ADMIN'), |
|
| 573 | 1059 | array('^/mypage/login', 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
| 574 | 1059 | array('^/mypage/withdraw_complete', 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
| 575 | 1059 | array('^/mypage/change', 'IS_AUTHENTICATED_FULLY'), |
|
| 576 | 1059 | array('^/mypage', 'ROLE_USER'), |
|
| 577 | ); |
||
| 578 | |||
| 579 | $this['eccube.password_encoder'] = $this->share(function ($app) { |
||
| 580 | 1059 | return new \Eccube\Security\Core\Encoder\PasswordEncoder($app['config']); |
|
| 581 | 1059 | }); |
|
| 582 | $this['security.encoder_factory'] = $this->share(function ($app) { |
||
| 583 | 1059 | return new \Symfony\Component\Security\Core\Encoder\EncoderFactory(array( |
|
| 584 | 1059 | 'Eccube\Entity\Customer' => $app['eccube.password_encoder'], |
|
| 585 | 1059 | 'Eccube\Entity\Member' => $app['eccube.password_encoder'], |
|
| 586 | 1059 | )); |
|
| 587 | 1059 | }); |
|
| 588 | $this['eccube.event_listner.security'] = $this->share(function ($app) { |
||
| 589 | 1059 | return new \Eccube\EventListener\SecurityEventListener($app['orm.em']); |
|
| 590 | 1059 | }); |
|
| 591 | $this['user'] = function ($app) { |
||
| 592 | 1058 | $token = $app['security']->getToken(); |
|
| 593 | |||
| 594 | 1058 | return ($token !== null) ? $token->getUser() : null; |
|
| 595 | }; |
||
| 596 | |||
| 597 | // ログイン時のイベントを設定. |
||
| 598 | 1059 | $this['dispatcher']->addListener(\Symfony\Component\Security\Http\SecurityEvents::INTERACTIVE_LOGIN, array($this['eccube.event_listner.security'], 'onInteractiveLogin')); |
|
| 599 | |||
| 600 | // Voterの設定 |
||
| 601 | 1059 | $app = $this; |
|
| 602 | $this['authority_voter'] = $this->share(function ($app) { |
||
| 603 | 1059 | return new \Eccube\Security\Voter\AuthorityVoter($app); |
|
| 604 | 1059 | }); |
|
| 605 | |||
| 606 | $app['security.voters'] = $app->extend('security.voters', function ($voters) use ($app) { |
||
| 607 | 1059 | $voters[] = $app['authority_voter']; |
|
| 608 | |||
| 609 | 1059 | return $voters; |
|
| 610 | 1059 | }); |
|
| 611 | |||
| 612 | $this['security.access_manager'] = $this->share(function ($app) { |
||
| 613 | 1059 | return new \Symfony\Component\Security\Core\Authorization\AccessDecisionManager($app['security.voters'], 'unanimous'); |
|
| 614 | 1059 | }); |
|
| 615 | |||
| 616 | 1059 | } |
|
| 617 | |||
| 618 | 1059 | public function initializePlugin() |
|
| 619 | { |
||
| 620 | 1059 | if ($this->initializedPlugin) { |
|
| 621 | return; |
||
| 622 | } |
||
| 623 | |||
| 624 | // setup event dispatcher |
||
| 625 | 1059 | $this->initPluginEventDispatcher(); |
|
| 626 | |||
| 627 | // load plugin |
||
| 628 | 1059 | $this->loadPlugin(); |
|
| 629 | |||
| 630 | 1059 | $this->initializedPlugin = true; |
|
| 631 | 1059 | } |
|
| 632 | |||
| 633 | 1059 | public function initPluginEventDispatcher() |
|
| 634 | { |
||
| 635 | // EventDispatcher |
||
| 636 | $this['eccube.event.dispatcher'] = $this->share(function () { |
||
| 637 | 474 | return new EventDispatcher(); |
|
| 638 | 1059 | }); |
|
| 639 | |||
| 640 | 1059 | $app = $this; |
|
| 641 | |||
| 642 | // hook point |
||
| 643 | $this->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) { |
||
| 644 | 460 | if (!$event->isMasterRequest()) { |
|
| 645 | 72 | return; |
|
| 646 | } |
||
| 647 | 460 | $hookpoint = 'eccube.event.app.before'; |
|
| 648 | 460 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
| 649 | 1059 | }, self::EARLY_EVENT); |
|
| 650 | |||
| 651 | View Code Duplication | $this->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) { |
|
| 652 | 459 | if (!$event->isMasterRequest()) { |
|
| 653 | 72 | return; |
|
| 654 | } |
||
| 655 | 457 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 656 | 457 | $hookpoint = "eccube.event.controller.$route.before"; |
|
| 657 | 457 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
| 658 | 1059 | }); |
|
| 659 | |||
| 660 | View Code Duplication | $this->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { |
|
| 661 | 447 | if (!$event->isMasterRequest()) { |
|
| 662 | 72 | return; |
|
| 663 | } |
||
| 664 | 447 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 665 | 447 | $hookpoint = "eccube.event.controller.$route.after"; |
|
| 666 | 447 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
| 667 | 1059 | }); |
|
| 668 | |||
| 669 | $this->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { |
||
| 670 | 447 | if (!$event->isMasterRequest()) { |
|
| 671 | 72 | return; |
|
| 672 | } |
||
| 673 | 447 | $hookpoint = 'eccube.event.app.after'; |
|
| 674 | 447 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
| 675 | 1059 | }, self::LATE_EVENT); |
|
| 676 | |||
| 677 | $this->on(KernelEvents::TERMINATE, function (PostResponseEvent $event) use ($app) { |
||
| 678 | 447 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 679 | 447 | $hookpoint = "eccube.event.controller.$route.finish"; |
|
| 680 | 447 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
| 681 | 1059 | }); |
|
| 682 | |||
| 683 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
||
| 684 | 447 | if (!$event->isMasterRequest()) { |
|
| 685 | 72 | return; |
|
| 686 | } |
||
| 687 | 447 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 688 | 447 | $app['eccube.event.dispatcher']->dispatch('eccube.event.render.'.$route.'.before', $event); |
|
| 689 | 1059 | }); |
|
| 690 | |||
| 691 | // Request Event |
||
| 692 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::REQUEST, function (\Symfony\Component\HttpKernel\Event\GetResponseEvent $event) use ($app) { |
|
| 693 | |||
| 694 | 459 | if (!$event->isMasterRequest()) { |
|
| 695 | 72 | return; |
|
| 696 | } |
||
| 697 | |||
| 698 | 459 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 699 | |||
| 700 | 459 | if (is_null($route)) { |
|
| 701 | return; |
||
| 702 | } |
||
| 703 | |||
| 704 | 459 | $app['monolog']->debug('KernelEvents::REQUEST '.$route); |
|
| 705 | |||
| 706 | // 全体 |
||
| 707 | 459 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.request', $event); |
|
| 708 | |||
| 709 | 459 | if (strpos($route, 'admin') === 0) { |
|
| 710 | // 管理画面 |
||
| 711 | 291 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.request', $event); |
|
| 712 | 291 | } else { |
|
| 713 | // フロント画面 |
||
| 714 | 170 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.request', $event); |
|
| 715 | } |
||
| 716 | |||
| 717 | // ルーティング単位 |
||
| 718 | 459 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.request", $event); |
|
| 719 | |||
| 720 | 1059 | }, 30); // Routing(32)が解決しし, 認証判定(8)が実行される前のタイミング. |
|
| 721 | |||
| 722 | // Controller Event |
||
| 723 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
|
| 724 | |||
| 725 | 458 | if (!$event->isMasterRequest()) { |
|
| 726 | 72 | return; |
|
| 727 | } |
||
| 728 | |||
| 729 | 456 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 730 | |||
| 731 | 456 | if (is_null($route)) { |
|
| 732 | return; |
||
| 733 | } |
||
| 734 | |||
| 735 | 456 | $app['monolog']->debug('KernelEvents::CONTROLLER '.$route); |
|
| 736 | |||
| 737 | // 全体 |
||
| 738 | 456 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.controller', $event); |
|
| 739 | |||
| 740 | 456 | if (strpos($route, 'admin') === 0) { |
|
| 741 | // 管理画面 |
||
| 742 | 289 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.controller', $event); |
|
| 743 | 289 | } else { |
|
| 744 | // フロント画面 |
||
| 745 | 169 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.controller', $event); |
|
| 746 | } |
||
| 747 | |||
| 748 | // ルーティング単位 |
||
| 749 | 456 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.controller", $event); |
|
| 750 | 1059 | }); |
|
| 751 | |||
| 752 | // Response Event |
||
| 753 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
|
| 754 | 447 | if (!$event->isMasterRequest()) { |
|
| 755 | 72 | return; |
|
| 756 | } |
||
| 757 | |||
| 758 | 447 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 759 | |||
| 760 | 447 | if (is_null($route)) { |
|
| 761 | 1 | return; |
|
| 762 | } |
||
| 763 | |||
| 764 | 446 | $app['monolog']->debug('KernelEvents::RESPONSE '.$route); |
|
| 765 | |||
| 766 | // ルーティング単位 |
||
| 767 | 446 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.response", $event); |
|
| 768 | |||
| 769 | 446 | if (strpos($route, 'admin') === 0) { |
|
| 770 | // 管理画面 |
||
| 771 | 284 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.response', $event); |
|
| 772 | 284 | } else { |
|
| 773 | // フロント画面 |
||
| 774 | 164 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.response', $event); |
|
| 775 | } |
||
| 776 | |||
| 777 | // 全体 |
||
| 778 | 446 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.response', $event); |
|
| 779 | 1059 | }); |
|
| 780 | |||
| 781 | // Exception Event |
||
| 782 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::EXCEPTION, function (\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event) use ($app) { |
|
| 783 | |||
| 784 | 19 | if (!$event->isMasterRequest()) { |
|
| 785 | return; |
||
| 786 | } |
||
| 787 | |||
| 788 | 19 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 789 | |||
| 790 | 19 | if (is_null($route)) { |
|
| 791 | return; |
||
| 792 | } |
||
| 793 | |||
| 794 | 19 | $app['monolog']->debug('KernelEvents::EXCEPTION '.$route); |
|
| 795 | |||
| 796 | // ルーティング単位 |
||
| 797 | 19 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.exception", $event); |
|
| 798 | |||
| 799 | 19 | if (strpos($route, 'admin') === 0) { |
|
| 800 | // 管理画面 |
||
| 801 | 9 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.exception', $event); |
|
| 802 | 9 | } else { |
|
| 803 | // フロント画面 |
||
| 804 | 10 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.exception', $event); |
|
| 805 | } |
||
| 806 | |||
| 807 | // 全体 |
||
| 808 | 19 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.exception', $event); |
|
| 809 | 1059 | }); |
|
| 810 | |||
| 811 | // Terminate Event |
||
| 812 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::TERMINATE, function (\Symfony\Component\HttpKernel\Event\PostResponseEvent $event) use ($app) { |
|
| 813 | |||
| 814 | 447 | $route = $event->getRequest()->attributes->get('_route'); |
|
| 815 | |||
| 816 | 447 | if (is_null($route)) { |
|
| 817 | 1 | return; |
|
| 818 | } |
||
| 819 | |||
| 820 | 446 | $app['monolog']->debug('KernelEvents::TERMINATE '.$route); |
|
| 821 | |||
| 822 | // ルーティング単位 |
||
| 823 | 446 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.terminate", $event); |
|
| 824 | |||
| 825 | 446 | if (strpos($route, 'admin') === 0) { |
|
| 826 | // 管理画面 |
||
| 827 | 284 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.terminate', $event); |
|
| 828 | 284 | } else { |
|
| 829 | // フロント画面 |
||
| 830 | 164 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.terminate', $event); |
|
| 831 | } |
||
| 832 | |||
| 833 | // 全体 |
||
| 834 | 446 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.terminate', $event); |
|
| 835 | 1059 | }); |
|
| 836 | 1059 | } |
|
| 837 | |||
| 838 | 1059 | public function loadPlugin() |
|
| 948 | |||
| 949 | /** |
||
| 950 | * PHPUnit を実行中かどうかを設定する. |
||
| 951 | * |
||
| 952 | * @param boolean $testMode PHPUnit を実行中の場合 true |
||
| 953 | */ |
||
| 954 | 1045 | public function setTestMode($testMode) |
|
| 958 | |||
| 959 | /** |
||
| 960 | * PHPUnit を実行中かどうか. |
||
| 961 | * |
||
| 962 | * @return boolean PHPUnit を実行中の場合 true |
||
| 963 | */ |
||
| 964 | 460 | public function isTestMode() |
|
| 968 | |||
| 969 | /** |
||
| 970 | * |
||
| 971 | * データベースの接続を確認 |
||
| 972 | * 成功 : trueを返却 |
||
| 973 | * 失敗 : \Doctrine\DBAL\DBALExceptionエラーが発生( 接続に失敗した場合 )、エラー画面を表示しdie() |
||
| 974 | * 備考 : app['debug']がtrueの際は処理を行わない |
||
| 975 | * |
||
| 976 | * @return boolean true |
||
| 977 | * |
||
| 978 | */ |
||
| 979 | 1059 | protected function checkDatabaseConnection() |
|
| 1003 | |||
| 1004 | /** |
||
| 1005 | * Config ファイルをパースし、連想配列を返します. |
||
| 1006 | * |
||
| 1007 | * $config_name.yml ファイルをパースし、連想配列を返します. |
||
| 1008 | * $config_name.php が存在する場合は、 PHP ファイルに記述された連想配列を使用します。 |
||
| 1009 | * |
||
| 1010 | * @param string $config_name Config 名称 |
||
| 1011 | * @param array $configAll Config の連想配列 |
||
| 1012 | * @param boolean $wrap_key Config の連想配列に config_name のキーを生成する場合 true, デフォルト false |
||
| 1013 | * @param string $ymlPath config yaml を格納したディレクトリ |
||
| 1014 | * @param string $distPath config yaml dist を格納したディレクトリ |
||
| 1015 | * @return Application |
||
| 1016 | */ |
||
| 1017 | 1069 | public function parseConfig($config_name, array &$configAll, $wrap_key = false, $ymlPath = null, $distPath = null) |
|
| 1058 | |||
| 1059 | /** |
||
| 1060 | * セッションが開始されているかどうか. |
||
| 1061 | * |
||
| 1062 | * @return boolean セッションが開始済みの場合 true |
||
| 1063 | * @link http://php.net/manual/ja/function.session-status.php#113468 |
||
| 1064 | */ |
||
| 1065 | 1059 | protected function isSessionStarted() |
|
| 1077 | |||
| 1078 | /** |
||
| 1079 | * Http Cache対応 |
||
| 1080 | */ |
||
| 1081 | 1059 | protected function initCacheRequest() |
|
| 1144 | } |
||
| 1145 |