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 | 1044 | public static function getInstance(array $values = array()) |
|
49 | { |
||
50 | 1044 | if (!is_object(self::$instance)) { |
|
51 | 1043 | self::$instance = new Application($values); |
|
52 | 1043 | } |
|
53 | |||
54 | 1044 | return self::$instance; |
|
55 | } |
||
56 | |||
57 | 1044 | public static function clearInstance() |
|
58 | { |
||
59 | 1044 | self::$instance = null; |
|
60 | 1044 | } |
|
61 | |||
62 | final public function __clone() |
||
66 | |||
67 | 1058 | public function __construct(array $values = array()) |
|
68 | 1058 | { |
|
81 | |||
82 | 1059 | public function initConfig() |
|
83 | 1058 | { |
|
84 | // load config |
||
85 | 1058 | $app = $this; |
|
86 | $this['config'] = $this->share(function() use ($app) { |
||
87 | 1059 | $configAll = array(); |
|
88 | 1051 | $app->parseConfig('constant', $configAll) |
|
89 | 1051 | ->parseConfig('path', $configAll) |
|
90 | 1051 | ->parseConfig('config', $configAll) |
|
91 | 1059 | ->parseConfig('database', $configAll) |
|
92 | 1059 | ->parseConfig('mail', $configAll) |
|
93 | 1051 | ->parseConfig('log', $configAll) |
|
94 | 1051 | ->parseConfig('nav', $configAll, true) |
|
95 | 1051 | ->parseConfig('doctrine_cache', $configAll) |
|
96 | 1051 | ->parseConfig('http_cache', $configAll) |
|
97 | 1051 | ->parseConfig('session_handler', $configAll); |
|
98 | |||
99 | 1051 | return $configAll; |
|
100 | 1058 | }); |
|
101 | 1058 | } |
|
102 | |||
103 | 1058 | public function initLogger() |
|
104 | { |
||
105 | 1058 | $app = $this; |
|
106 | 1058 | $this->register(new ServiceProvider\EccubeMonologServiceProvider($app)); |
|
107 | 1058 | $this['monolog.logfile'] = __DIR__.'/../../app/log/site.log'; |
|
108 | 1058 | $this['monolog.name'] = 'eccube'; |
|
109 | 1058 | } |
|
110 | |||
111 | 1059 | public function initialize() |
|
112 | { |
||
113 | 1048 | if ($this->initialized) { |
|
114 | return; |
||
115 | } |
||
116 | |||
117 | // init locale |
||
118 | 1059 | $this->initLocale(); |
|
119 | |||
120 | // init session |
||
121 | 1048 | if (!$this->isSessionStarted()) { |
|
122 | 1059 | $this->initSession(); |
|
123 | 1048 | } |
|
124 | |||
125 | // init twig |
||
126 | 1059 | $this->initRendering(); |
|
127 | |||
128 | // init provider |
||
129 | 1048 | $this->register(new \Silex\Provider\HttpCacheServiceProvider(), array( |
|
130 | 1059 | 'http_cache.cache_dir' => __DIR__.'/../../app/cache/http/', |
|
131 | 1048 | )); |
|
132 | 1048 | $this->register(new \Silex\Provider\HttpFragmentServiceProvider()); |
|
133 | 1048 | $this->register(new \Silex\Provider\UrlGeneratorServiceProvider()); |
|
134 | 1048 | $this->register(new \Silex\Provider\FormServiceProvider()); |
|
135 | 1048 | $this->register(new \Silex\Provider\SerializerServiceProvider()); |
|
136 | 1059 | $this->register(new \Eccube\ServiceProvider\ValidatorServiceProvider()); |
|
137 | |||
138 | 1048 | $app = $this; |
|
139 | $this->error(function (\Exception $e, $code) use ($app) { |
||
140 | 17 | if ($app['debug']) { |
|
141 | 17 | return; |
|
142 | } |
||
143 | |||
144 | switch ($code) { |
||
145 | 1058 | case 403: |
|
146 | $title = 'アクセスできません。'; |
||
147 | $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。'; |
||
148 | break; |
||
149 | 1058 | case 404: |
|
150 | $title = 'ページがみつかりません。'; |
||
151 | $message = 'URLに間違いがないかご確認ください。'; |
||
152 | break; |
||
153 | 1058 | default: |
|
154 | $title = 'システムエラーが発生しました。'; |
||
155 | 1058 | $message = '大変お手数ですが、サイト管理者までご連絡ください。'; |
|
156 | 1058 | break; |
|
157 | 1058 | } |
|
158 | |||
159 | 1058 | return $app->render('error.twig', array( |
|
160 | 'error_title' => $title, |
||
161 | 'error_message' => $message, |
||
162 | )); |
||
163 | 1048 | }); |
|
164 | |||
165 | // init mailer |
||
166 | 1048 | $this->initMailer(); |
|
167 | |||
168 | // init doctrine orm |
||
169 | 1048 | $this->initDoctrine(); |
|
170 | |||
171 | // Set up the DBAL connection now to check for a proper connection to the database. |
||
172 | 1048 | $this->checkDatabaseConnection(); |
|
173 | |||
174 | // init security |
||
175 | 1048 | $this->initSecurity(); |
|
176 | |||
177 | // init ec-cube service provider |
||
178 | 1048 | $this->register(new ServiceProvider\EccubeServiceProvider()); |
|
179 | |||
180 | // mount controllers |
||
181 | 1048 | $this->register(new \Silex\Provider\ServiceControllerServiceProvider()); |
|
182 | 1048 | $this->mount('', new ControllerProvider\FrontControllerProvider()); |
|
183 | 1048 | $this->mount('/'.trim($this['config']['admin_route'], '/').'/', new ControllerProvider\AdminControllerProvider()); |
|
184 | 1048 | Request::enableHttpMethodParameterOverride(); // PUTやDELETEできるようにする |
|
185 | |||
186 | // add transaction listener |
||
187 | 1048 | $this['dispatcher']->addSubscriber(new TransactionListener($this)); |
|
188 | |||
189 | // init http cache |
||
190 | 1048 | $this->initCacheRequest(); |
|
191 | |||
192 | 1048 | $this->initialized = true; |
|
193 | 1048 | } |
|
194 | |||
195 | 1048 | public function initLocale() |
|
228 | |||
229 | 1048 | public function initSession() |
|
230 | { |
||
231 | 1048 | $this->register(new \Silex\Provider\SessionServiceProvider(), array( |
|
232 | 1048 | 'session.storage.save_path' => $this['config']['root_dir'].'/app/cache/eccube/session', |
|
233 | 'session.storage.options' => array( |
||
234 | 1048 | 'name' => 'eccube', |
|
235 | 1048 | 'cookie_path' => $this['config']['root_urlpath'] ?: '/', |
|
236 | 1048 | 'cookie_secure' => $this['config']['force_ssl'], |
|
237 | 1048 | 'cookie_lifetime' => $this['config']['cookie_lifetime'], |
|
238 | 1048 | 'cookie_httponly' => true, |
|
239 | // cookie_domainは指定しない |
||
240 | // http://blog.tokumaru.org/2011/10/cookiedomain.html |
||
241 | 1048 | ), |
|
242 | 1048 | )); |
|
243 | |||
244 | 1048 | $options = $this['config']['session_handler']; |
|
245 | |||
246 | 1048 | if ($options['enabled']) { |
|
247 | // @see http://silex.sensiolabs.org/doc/providers/session.html#custom-session-configurations |
||
248 | $this['session.storage.handler'] = null; |
||
249 | ini_set('session.save_handler', $options['save_handler']); |
||
250 | ini_set('session.save_path', $options['save_path']); |
||
251 | } |
||
252 | 1048 | } |
|
253 | |||
254 | 1048 | public function initRendering() |
|
255 | { |
||
256 | 1048 | $this->register(new \Silex\Provider\TwigServiceProvider(), array( |
|
257 | 1048 | 'twig.form.templates' => array('Form/form_layout.twig'), |
|
258 | 1048 | )); |
|
259 | $this['twig'] = $this->share($this->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) { |
||
260 | 468 | $twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app)); |
|
261 | 468 | $twig->addExtension(new \Twig_Extension_StringLoader()); |
|
262 | |||
263 | 468 | return $twig; |
|
264 | 1048 | })); |
|
265 | |||
266 | $this->before(function (Request $request, \Silex\Application $app) { |
||
267 | // フロント or 管理画面ごとにtwigの探索パスを切り替える. |
||
268 | $app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) { |
||
269 | 454 | $paths = array(); |
|
270 | |||
271 | // 互換性がないのでprofiler とproduction 時のcacheを分離する |
||
272 | |||
273 | 454 | $app['admin'] = false; |
|
274 | 454 | $app['front'] = false; |
|
275 | |||
276 | 454 | if (isset($app['profiler'])) { |
|
277 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/profiler/'; |
||
278 | } else { |
||
279 | 454 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/production/'; |
|
280 | } |
||
281 | 454 | $pathinfo = rawurldecode($app['request']->getPathInfo()); |
|
282 | 454 | if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) { |
|
283 | 288 | if (file_exists(__DIR__.'/../../app/template/admin')) { |
|
284 | 288 | $paths[] = __DIR__.'/../../app/template/admin'; |
|
285 | 288 | } |
|
286 | 288 | $paths[] = $app['config']['template_admin_realdir']; |
|
287 | 288 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
288 | 288 | $cache = $cacheBaseDir.'admin'; |
|
289 | 288 | $app['admin'] = true; |
|
290 | 288 | } else { |
|
291 | 168 | if (file_exists($app['config']['template_realdir'])) { |
|
292 | 168 | $paths[] = $app['config']['template_realdir']; |
|
293 | 168 | } |
|
294 | 168 | $paths[] = $app['config']['template_default_realdir']; |
|
295 | 168 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
296 | 168 | $cache = $cacheBaseDir.$app['config']['template_code']; |
|
297 | 168 | $app['front'] = true; |
|
298 | } |
||
299 | 454 | $twig->setCache($cache); |
|
300 | 454 | $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths)); |
|
301 | |||
302 | 454 | return $twig; |
|
303 | 456 | })); |
|
304 | |||
305 | // 管理画面のIP制限チェック. |
||
306 | 456 | $pathinfo = rawurldecode($app['request']->getPathInfo()); |
|
307 | 456 | if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) { |
|
308 | // IP制限チェック |
||
309 | 288 | $allowHost = $app['config']['admin_allow_host']; |
|
310 | 288 | if (count($allowHost) > 0) { |
|
311 | if (array_search($app['request']->getClientIp(), $allowHost) === false) { |
||
312 | throw new \Exception(); |
||
313 | 1048 | } |
|
314 | } |
||
315 | 288 | } |
|
316 | 1048 | }, self::EARLY_EVENT); |
|
317 | |||
318 | // twigのグローバル変数を定義. |
||
319 | 1048 | $app = $this; |
|
320 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
||
321 | // ショップ基本情報 |
||
322 | 454 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
|
323 | 454 | $app['twig']->addGlobal('BaseInfo', $BaseInfo); |
|
324 | |||
325 | 454 | $pathinfo = rawurldecode($app['request']->getPathInfo()); |
|
326 | 454 | if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) { |
|
327 | // 管理画面 |
||
328 | // 管理画面メニュー |
||
329 | 288 | $menus = array('', '', ''); |
|
330 | 288 | $app['twig']->addGlobal('menus', $menus); |
|
331 | |||
332 | 288 | $Member = $app->user(); |
|
333 | 288 | if (is_object($Member)) { |
|
334 | // ログインしていれば管理者のロールを取得 |
||
335 | 282 | $AuthorityRoles = $app['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority())); |
|
336 | |||
337 | 282 | $roles = array(); |
|
338 | 282 | foreach ($AuthorityRoles as $AuthorityRole) { |
|
339 | // 管理画面でメニュー制御するため相対パス全てをセット |
||
340 | 3 | $roles[] = $app['request']->getBaseUrl().'/'.$app['config']['admin_route'].$AuthorityRole->getDenyUrl(); |
|
341 | 282 | } |
|
342 | |||
343 | 282 | $app['twig']->addGlobal('AuthorityRoles', $roles); |
|
344 | 282 | } |
|
345 | |||
346 | 288 | } else { |
|
347 | // フロント画面 |
||
348 | 168 | $request = $event->getRequest(); |
|
349 | 168 | $route = $request->attributes->get('_route'); |
|
350 | |||
351 | // ユーザ作成画面 |
||
352 | 168 | if ($route === 'user_data') { |
|
353 | 2 | $params = $request->attributes->get('_route_params'); |
|
354 | 2 | $route = $params['route']; |
|
355 | // プレビュー画面 |
||
356 | 168 | } elseif ($request->get('preview')) { |
|
357 | $route = 'preview'; |
||
358 | } |
||
359 | |||
360 | try { |
||
361 | 168 | $DeviceType = $app['eccube.repository.master.device_type'] |
|
362 | 168 | ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC); |
|
363 | 168 | $PageLayout = $app['eccube.repository.page_layout']->getByUrl($DeviceType, $route); |
|
364 | 168 | } catch (\Doctrine\ORM\NoResultException $e) { |
|
365 | 138 | $PageLayout = $app['eccube.repository.page_layout']->newPageLayout($DeviceType); |
|
366 | } |
||
367 | |||
368 | 168 | $app['twig']->addGlobal('PageLayout', $PageLayout); |
|
369 | 168 | $app['twig']->addGlobal('title', $PageLayout->getName()); |
|
370 | } |
||
371 | 1048 | }); |
|
372 | 1048 | } |
|
373 | |||
374 | 1048 | public function initMailer() |
|
403 | |||
404 | 1048 | public function initDoctrine() |
|
405 | { |
||
406 | 1048 | $this->register(new \Silex\Provider\DoctrineServiceProvider(), array( |
|
407 | 'dbs.options' => array( |
||
408 | 1048 | 'default' => $this['config']['database'] |
|
409 | 1048 | ))); |
|
410 | 1048 | $this->register(new \Saxulum\DoctrineOrmManagerRegistry\Silex\Provider\DoctrineOrmManagerRegistryProvider()); |
|
411 | |||
412 | // プラグインのmetadata定義を合わせて行う. |
||
413 | 1048 | $pluginBasePath = __DIR__.'/../../app/Plugin'; |
|
414 | 1048 | $finder = Finder::create() |
|
415 | 1048 | ->in($pluginBasePath) |
|
416 | 1048 | ->directories() |
|
417 | 1048 | ->depth(0); |
|
418 | |||
419 | 1048 | $ormMappings = array(); |
|
420 | 1048 | $ormMappings[] = array( |
|
421 | 1048 | 'type' => 'yml', |
|
422 | 1048 | 'namespace' => 'Eccube\Entity', |
|
423 | 'path' => array( |
||
424 | 1048 | __DIR__.'/Resource/doctrine', |
|
425 | 1048 | __DIR__.'/Resource/doctrine/master', |
|
426 | 1048 | ), |
|
427 | ); |
||
428 | |||
429 | 1048 | foreach ($finder as $dir) { |
|
430 | |||
431 | 139 | $file = $dir->getRealPath().'/config.yml'; |
|
432 | |||
433 | 139 | if (file_exists($file)) { |
|
434 | 139 | $config = Yaml::parse(file_get_contents($file)); |
|
435 | 139 | } else { |
|
436 | $code = $dir->getBaseName(); |
||
437 | $this['monolog']->warning("skip {$code} orm.path loading. config.yml not found.", array('path' => $file)); |
||
438 | continue; |
||
439 | } |
||
440 | |||
441 | // Doctrine Extend |
||
442 | 139 | if (isset($config['orm.path']) && is_array($config['orm.path'])) { |
|
443 | $paths = array(); |
||
444 | foreach ($config['orm.path'] as $path) { |
||
445 | $paths[] = $pluginBasePath.'/'.$config['code'].$path; |
||
446 | } |
||
447 | $ormMappings[] = array( |
||
448 | 'type' => 'yml', |
||
449 | 'namespace' => 'Plugin\\'.$config['code'].'\\Entity', |
||
450 | 'path' => $paths, |
||
451 | ); |
||
452 | } |
||
453 | 1048 | } |
|
454 | |||
455 | $options = array( |
||
456 | 'mappings' => $ormMappings |
||
457 | 1048 | ); |
|
458 | |||
459 | 1048 | if (!$this['debug']) { |
|
460 | 4 | $cacheDrivers = array(); |
|
461 | 4 | if (array_key_exists('doctrine_cache', $this['config'])) { |
|
462 | 4 | $cacheDrivers = $this['config']['doctrine_cache']; |
|
463 | 4 | } |
|
464 | |||
465 | 4 | if (array_key_exists('metadata_cache', $cacheDrivers)) { |
|
466 | 4 | $options['metadata_cache'] = $cacheDrivers['metadata_cache']; |
|
467 | 4 | } |
|
468 | 4 | if (array_key_exists('query_cache', $cacheDrivers)) { |
|
469 | 4 | $options['query_cache'] = $cacheDrivers['query_cache']; |
|
470 | 4 | } |
|
471 | 4 | if (array_key_exists('result_cache', $cacheDrivers)) { |
|
472 | 4 | $options['result_cache'] = $cacheDrivers['result_cache']; |
|
473 | 4 | } |
|
474 | 4 | if (array_key_exists('hydration_cache', $cacheDrivers)) { |
|
475 | 4 | $options['hydration_cache'] = $cacheDrivers['hydration_cache']; |
|
476 | 4 | } |
|
477 | 4 | } |
|
478 | |||
479 | 1048 | $this->register(new \Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array( |
|
480 | 1048 | 'orm.proxies_dir' => __DIR__.'/../../app/cache/doctrine/proxies', |
|
481 | 'orm.em.options' => $options |
||
482 | 1048 | )); |
|
483 | |||
484 | /** |
||
485 | * YamlDriverのPHP7対応. Doctrine2.4で修正されれば不要. |
||
486 | * @see https://github.com/EC-CUBE/ec-cube/issues/1338 |
||
487 | */ |
||
488 | 1048 | $config = $this['orm.em']->getConfiguration(); |
|
489 | /** @var $driver \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain */ |
||
490 | 1048 | $chain = $config->getMetadataDriverImpl(); |
|
491 | // $ormMappingsの1要素ごとにDriverが生成されている. |
||
492 | 1048 | $drivers = $chain->getDrivers(); |
|
493 | 1048 | foreach ($drivers as $namespace => $oldDriver) { |
|
494 | /** @var $newDriver \Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver */ |
||
495 | 1048 | $newDriver = new YamlDriver($oldDriver->getLocator()); |
|
496 | // 修正したDriverに差し替える. メソッド名はaddだけど実際はsetしてる. |
||
497 | 1048 | $chain->addDriver($newDriver, $namespace); |
|
498 | 1048 | } |
|
499 | 1048 | } |
|
500 | |||
501 | 1048 | public function initSecurity() |
|
502 | { |
||
503 | 1048 | $this->register(new \Silex\Provider\SecurityServiceProvider()); |
|
504 | 1048 | $this->register(new \Silex\Provider\RememberMeServiceProvider()); |
|
505 | |||
506 | 1048 | $this['security.firewalls'] = array( |
|
507 | 'admin' => array( |
||
508 | 1048 | 'pattern' => "^/{$this['config']['admin_route']}/", |
|
509 | 'form' => array( |
||
510 | 1048 | 'login_path' => "/{$this['config']['admin_route']}/login", |
|
511 | 1048 | 'check_path' => "/{$this['config']['admin_route']}/login_check", |
|
512 | 1048 | 'username_parameter' => 'login_id', |
|
513 | 1048 | 'password_parameter' => 'password', |
|
514 | 1048 | 'with_csrf' => true, |
|
515 | 1048 | 'use_forward' => true, |
|
516 | 1048 | ), |
|
517 | 'logout' => array( |
||
518 | 1048 | 'logout_path' => "/{$this['config']['admin_route']}/logout", |
|
519 | 1048 | 'target_url' => "/{$this['config']['admin_route']}/", |
|
520 | 1048 | ), |
|
521 | 1048 | 'users' => $this['orm.em']->getRepository('Eccube\Entity\Member'), |
|
522 | 1048 | 'anonymous' => true, |
|
523 | 1048 | ), |
|
524 | 'customer' => array( |
||
525 | 1048 | 'pattern' => '^/', |
|
526 | 'form' => array( |
||
527 | 1048 | 'login_path' => '/mypage/login', |
|
528 | 1048 | 'check_path' => '/login_check', |
|
529 | 1048 | 'username_parameter' => 'login_email', |
|
530 | 1048 | 'password_parameter' => 'login_pass', |
|
531 | 1048 | 'with_csrf' => true, |
|
532 | 1048 | 'use_forward' => true, |
|
533 | 1048 | ), |
|
534 | 'logout' => array( |
||
535 | 1048 | 'logout_path' => '/logout', |
|
536 | 1048 | 'target_url' => '/', |
|
537 | 1048 | ), |
|
538 | 'remember_me' => array( |
||
539 | 1048 | 'key' => sha1($this['config']['auth_magic']), |
|
540 | 1048 | 'name' => 'eccube_rememberme', |
|
541 | // lifetimeはデフォルトの1年間にする |
||
542 | // 'lifetime' => $this['config']['cookie_lifetime'], |
||
543 | 1048 | 'path' => $this['config']['root_urlpath'] ?: '/', |
|
544 | 1048 | 'secure' => $this['config']['force_ssl'], |
|
545 | 1048 | 'httponly' => true, |
|
546 | 1048 | 'always_remember_me' => false, |
|
547 | 1048 | 'remember_me_parameter' => 'login_memory', |
|
548 | 1048 | ), |
|
549 | 1048 | 'users' => $this['orm.em']->getRepository('Eccube\Entity\Customer'), |
|
550 | 1048 | 'anonymous' => true, |
|
551 | 1048 | ), |
|
552 | ); |
||
553 | |||
554 | 1048 | $this['security.access_rules'] = array( |
|
555 | 1048 | array("^/{$this['config']['admin_route']}/login", 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
556 | 1048 | array("^/{$this['config']['admin_route']}/", 'ROLE_ADMIN'), |
|
557 | 1048 | array('^/mypage/login', 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
558 | 1048 | array('^/mypage/withdraw_complete', 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
559 | 1048 | array('^/mypage/change', 'IS_AUTHENTICATED_FULLY'), |
|
560 | 1048 | array('^/mypage', 'ROLE_USER'), |
|
561 | ); |
||
562 | |||
563 | $this['eccube.password_encoder'] = $this->share(function ($app) { |
||
564 | 1048 | return new \Eccube\Security\Core\Encoder\PasswordEncoder($app['config']); |
|
565 | 1048 | }); |
|
566 | $this['security.encoder_factory'] = $this->share(function ($app) { |
||
567 | 1048 | return new \Symfony\Component\Security\Core\Encoder\EncoderFactory(array( |
|
568 | 1048 | 'Eccube\Entity\Customer' => $app['eccube.password_encoder'], |
|
569 | 1048 | 'Eccube\Entity\Member' => $app['eccube.password_encoder'], |
|
570 | 1048 | )); |
|
571 | 1048 | }); |
|
572 | $this['eccube.event_listner.security'] = $this->share(function ($app) { |
||
573 | 1048 | return new \Eccube\EventListener\SecurityEventListener($app['orm.em']); |
|
574 | 1048 | }); |
|
575 | $this['user'] = function ($app) { |
||
576 | 358 | $token = $app['security']->getToken(); |
|
577 | |||
578 | 358 | return ($token !== null) ? $token->getUser() : null; |
|
579 | }; |
||
580 | |||
581 | // ログイン時のイベントを設定. |
||
582 | 1048 | $this['dispatcher']->addListener(\Symfony\Component\Security\Http\SecurityEvents::INTERACTIVE_LOGIN, array($this['eccube.event_listner.security'], 'onInteractiveLogin')); |
|
583 | |||
584 | // Voterの設定 |
||
585 | 1048 | $app = $this; |
|
586 | $this['authority_voter'] = $this->share(function ($app) { |
||
587 | 1048 | return new \Eccube\Security\Voter\AuthorityVoter($app); |
|
588 | 1048 | }); |
|
589 | |||
590 | $app['security.voters'] = $app->extend('security.voters', function ($voters) use ($app) { |
||
591 | 1048 | $voters[] = $app['authority_voter']; |
|
592 | |||
593 | 1048 | return $voters; |
|
594 | 1048 | }); |
|
595 | |||
596 | $this['security.access_manager'] = $this->share(function ($app) { |
||
597 | 1048 | return new \Symfony\Component\Security\Core\Authorization\AccessDecisionManager($app['security.voters'], 'unanimous'); |
|
598 | 1048 | }); |
|
599 | |||
600 | 1048 | } |
|
601 | |||
602 | 1048 | public function initializePlugin() |
|
616 | |||
617 | 1048 | public function initPluginEventDispatcher() |
|
618 | { |
||
619 | // EventDispatcher |
||
620 | $this['eccube.event.dispatcher'] = $this->share(function () { |
||
621 | 470 | return new EventDispatcher(); |
|
622 | 1048 | }); |
|
623 | |||
624 | 1048 | $app = $this; |
|
625 | |||
626 | // hook point |
||
627 | $this->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) { |
||
628 | 456 | if (!$event->isMasterRequest()) { |
|
629 | 106 | return; |
|
630 | } |
||
631 | 456 | $hookpoint = 'eccube.event.app.before'; |
|
632 | 456 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
633 | 1048 | }, self::EARLY_EVENT); |
|
634 | |||
635 | View Code Duplication | $this->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) { |
|
636 | 455 | if (!$event->isMasterRequest()) { |
|
637 | 106 | return; |
|
638 | } |
||
639 | 453 | $route = $event->getRequest()->attributes->get('_route'); |
|
640 | 453 | $hookpoint = "eccube.event.controller.$route.before"; |
|
641 | 453 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
642 | 1048 | }); |
|
643 | |||
644 | View Code Duplication | $this->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { |
|
645 | 443 | if (!$event->isMasterRequest()) { |
|
646 | 106 | return; |
|
647 | } |
||
648 | 443 | $route = $event->getRequest()->attributes->get('_route'); |
|
649 | 443 | $hookpoint = "eccube.event.controller.$route.after"; |
|
650 | 443 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
651 | 1048 | }); |
|
652 | |||
653 | $this->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) { |
||
654 | 443 | if (!$event->isMasterRequest()) { |
|
655 | 106 | return; |
|
656 | } |
||
657 | 443 | $hookpoint = 'eccube.event.app.after'; |
|
658 | 443 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
659 | 1048 | }, self::LATE_EVENT); |
|
660 | |||
661 | $this->on(KernelEvents::TERMINATE, function (PostResponseEvent $event) use ($app) { |
||
662 | 443 | $route = $event->getRequest()->attributes->get('_route'); |
|
663 | 443 | $hookpoint = "eccube.event.controller.$route.finish"; |
|
664 | 443 | $app['eccube.event.dispatcher']->dispatch($hookpoint, $event); |
|
665 | 1048 | }); |
|
666 | |||
667 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
||
668 | 443 | $route = $event->getRequest()->attributes->get('_route'); |
|
669 | 443 | $app['eccube.event.dispatcher']->dispatch('eccube.event.render.'.$route.'.before', $event); |
|
670 | 1048 | }); |
|
671 | |||
672 | // Request Event |
||
673 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::REQUEST, function (\Symfony\Component\HttpKernel\Event\GetResponseEvent $event) use ($app) { |
|
674 | |||
675 | 455 | if (\Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { |
|
676 | 106 | return; |
|
677 | } |
||
678 | |||
679 | 455 | $route = $event->getRequest()->attributes->get('_route'); |
|
680 | |||
681 | 455 | if (is_null($route)) { |
|
682 | return; |
||
683 | } |
||
684 | |||
685 | 455 | $app['monolog']->debug('KernelEvents::REQUEST '.$route); |
|
686 | |||
687 | // 全体 |
||
688 | 455 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.request', $event); |
|
689 | |||
690 | 455 | if (strpos($route, 'admin') === 0) { |
|
691 | // 管理画面 |
||
692 | 288 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.request', $event); |
|
693 | 288 | } else { |
|
694 | // フロント画面 |
||
695 | 169 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.request', $event); |
|
696 | } |
||
697 | |||
698 | // ルーティング単位 |
||
699 | 455 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.request", $event); |
|
700 | |||
701 | 1048 | }, 30); // Routing(32)が解決しし, 認証判定(8)が実行される前のタイミング. |
|
702 | |||
703 | // Controller Event |
||
704 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
|
705 | |||
706 | 454 | if (\Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { |
|
707 | 106 | return; |
|
708 | } |
||
709 | |||
710 | |||
711 | 452 | $route = $event->getRequest()->attributes->get('_route'); |
|
712 | |||
713 | 452 | if (is_null($route)) { |
|
714 | return; |
||
715 | } |
||
716 | |||
717 | 452 | $app['monolog']->debug('KernelEvents::CONTROLLER '.$route); |
|
718 | |||
719 | // 全体 |
||
720 | 452 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.controller', $event); |
|
721 | |||
722 | 452 | if (strpos($route, 'admin') === 0) { |
|
723 | // 管理画面 |
||
724 | 286 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.controller', $event); |
|
725 | 286 | } else { |
|
726 | // フロント画面 |
||
727 | 168 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.controller', $event); |
|
728 | } |
||
729 | |||
730 | // ルーティング単位 |
||
731 | 452 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.controller", $event); |
|
732 | 1048 | }); |
|
733 | |||
734 | // Response Event |
||
735 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
|
736 | |||
737 | 443 | if (\Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { |
|
738 | 106 | return; |
|
739 | } |
||
740 | |||
741 | 443 | $route = $event->getRequest()->attributes->get('_route'); |
|
742 | |||
743 | 443 | if (is_null($route)) { |
|
744 | 1 | return; |
|
745 | } |
||
746 | |||
747 | 442 | $app['monolog']->debug('KernelEvents::RESPONSE '.$route); |
|
748 | |||
749 | // ルーティング単位 |
||
750 | 442 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.response", $event); |
|
751 | |||
752 | 442 | if (strpos($route, 'admin') === 0) { |
|
753 | // 管理画面 |
||
754 | 281 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.response', $event); |
|
755 | 281 | } else { |
|
756 | // フロント画面 |
||
757 | 163 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.response', $event); |
|
758 | } |
||
759 | |||
760 | // 全体 |
||
761 | 442 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.response', $event); |
|
762 | 1048 | }); |
|
763 | |||
764 | // Exception Event |
||
765 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::EXCEPTION, function (\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event) use ($app) { |
|
766 | |||
767 | 19 | if (\Symfony\Component\HttpKernel\HttpKernelInterface::MASTER_REQUEST !== $event->getRequestType()) { |
|
768 | return; |
||
769 | } |
||
770 | |||
771 | 19 | $route = $event->getRequest()->attributes->get('_route'); |
|
772 | |||
773 | 19 | if (is_null($route)) { |
|
774 | return; |
||
775 | } |
||
776 | |||
777 | 19 | $app['monolog']->debug('KernelEvents::EXCEPTION '.$route); |
|
778 | |||
779 | // ルーティング単位 |
||
780 | 19 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.exception", $event); |
|
781 | |||
782 | 19 | if (strpos($route, 'admin') === 0) { |
|
783 | // 管理画面 |
||
784 | 9 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.exception', $event); |
|
785 | 9 | } else { |
|
786 | // フロント画面 |
||
787 | 10 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.exception', $event); |
|
788 | } |
||
789 | |||
790 | // 全体 |
||
791 | 19 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.exception', $event); |
|
792 | 1048 | }); |
|
793 | |||
794 | // Terminate Event |
||
795 | View Code Duplication | $this->on(\Symfony\Component\HttpKernel\KernelEvents::TERMINATE, function (\Symfony\Component\HttpKernel\Event\PostResponseEvent $event) use ($app) { |
|
796 | |||
797 | 443 | $route = $event->getRequest()->attributes->get('_route'); |
|
798 | |||
799 | 443 | if (is_null($route)) { |
|
800 | 1 | return; |
|
801 | } |
||
802 | |||
803 | 442 | $app['monolog']->debug('KernelEvents::TERMINATE '.$route); |
|
804 | |||
805 | // ルーティング単位 |
||
806 | 442 | $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.terminate", $event); |
|
807 | |||
808 | 442 | if (strpos($route, 'admin') === 0) { |
|
809 | // 管理画面 |
||
810 | 281 | $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.terminate', $event); |
|
811 | 281 | } else { |
|
812 | // フロント画面 |
||
813 | 163 | $app['eccube.event.dispatcher']->dispatch('eccube.event.front.terminate', $event); |
|
814 | } |
||
815 | |||
816 | // 全体 |
||
817 | 442 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.terminate', $event); |
|
818 | 1048 | }); |
|
819 | 1048 | } |
|
820 | |||
821 | 1048 | public function loadPlugin() |
|
822 | { |
||
823 | // プラグインディレクトリを探索. |
||
824 | 1048 | $basePath = __DIR__.'/../../app/Plugin'; |
|
825 | 1048 | $finder = Finder::create() |
|
826 | 1048 | ->in($basePath) |
|
827 | 1048 | ->directories() |
|
828 | 1048 | ->depth(0); |
|
829 | |||
830 | 1048 | $finder->sortByName(); |
|
831 | |||
832 | // ハンドラ優先順位をdbから持ってきてハッシュテーブルを作成 |
||
833 | 1048 | $priorities = array(); |
|
834 | 1048 | $handlers = $this['orm.em'] |
|
835 | 1048 | ->getRepository('Eccube\Entity\PluginEventHandler') |
|
836 | 1048 | ->getHandlers(); |
|
837 | 1048 | foreach ($handlers as $handler) { |
|
838 | if ($handler->getPlugin()->getEnable() && !$handler->getPlugin()->getDelFlg()) { |
||
839 | |||
840 | $priority = $handler->getPriority(); |
||
841 | } else { |
||
842 | // Pluginがdisable、削除済みの場合、EventHandlerのPriorityを全て0とみなす |
||
843 | $priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED; |
||
844 | } |
||
845 | $priorities[$handler->getPlugin()->getClassName()][$handler->getEvent()][$handler->getHandler()] = $priority; |
||
846 | 1048 | } |
|
847 | |||
848 | // プラグインをロードする. |
||
849 | // config.yml/event.ymlの定義に沿ってインスタンスの生成を行い, イベント設定を行う. |
||
850 | 1048 | foreach ($finder as $dir) { |
|
851 | //config.ymlのないディレクトリは無視する |
||
852 | 140 | $path = $dir->getRealPath(); |
|
853 | 140 | $code = $dir->getBaseName(); |
|
854 | try { |
||
855 | 140 | $this['eccube.service.plugin']->checkPluginArchiveContent($path); |
|
856 | 140 | } catch (\Eccube\Exception\PluginException $e) { |
|
857 | $this['monolog']->warning("skip {$code} config loading. config.yml not foud or invalid.", array( |
||
858 | 'path' => $path, |
||
859 | 'original-message' => $e->getMessage() |
||
860 | )); |
||
861 | continue; |
||
862 | } |
||
863 | 140 | $config = $this['eccube.service.plugin']->readYml($dir->getRealPath().'/config.yml'); |
|
864 | |||
865 | 140 | $plugin = $this['orm.em'] |
|
866 | 140 | ->getRepository('Eccube\Entity\Plugin') |
|
867 | 140 | ->findOneBy(array('code' => $config['code'])); |
|
868 | |||
869 | // const |
||
870 | 140 | if (isset($config['const'])) { |
|
871 | $this['config'] = $this->share($this->extend('config', function ($eccubeConfig) use ($config) { |
||
872 | 1 | $eccubeConfig[$config['code']] = array( |
|
873 | 1 | 'const' => $config['const'], |
|
874 | ); |
||
875 | |||
876 | 1 | return $eccubeConfig; |
|
877 | 1 | })); |
|
878 | 1 | } |
|
879 | |||
880 | 140 | if ($plugin && $plugin->getEnable() == Constant::DISABLED) { |
|
881 | // プラグインが無効化されていれば読み込まない |
||
882 | 1 | continue; |
|
883 | } |
||
884 | |||
885 | // Type: Event |
||
886 | 139 | if (isset($config['event'])) { |
|
887 | 139 | $class = '\\Plugin\\'.$config['code'].'\\'.$config['event']; |
|
888 | 139 | $eventExists = true; |
|
889 | |||
890 | 139 | View Code Duplication | if (!class_exists($class)) { |
891 | $this['monolog']->warning("skip {$code} loading. event class not foud.", array( |
||
892 | 'class' => $class, |
||
893 | )); |
||
894 | $eventExists = false; |
||
895 | } |
||
896 | |||
897 | 139 | if ($eventExists && file_exists($dir->getRealPath().'/event.yml')) { |
|
898 | |||
899 | 139 | $subscriber = new $class($this); |
|
900 | |||
901 | 139 | foreach (Yaml::parse(file_get_contents($dir->getRealPath().'/event.yml')) as $event => $handlers) { |
|
902 | 139 | foreach ($handlers as $handler) { |
|
903 | 139 | if (!isset($priorities[$config['event']][$event][$handler[0]])) { // ハンドラテーブルに登録されていない(ソースにしか記述されていない)ハンドラは一番後ろにする |
|
904 | 139 | $priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_LATEST; |
|
905 | 139 | } else { |
|
906 | $priority = $priorities[$config['event']][$event][$handler[0]]; |
||
907 | } |
||
908 | // 優先度が0のプラグインは登録しない |
||
909 | 139 | if (\Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED != $priority) { |
|
910 | 139 | $this['eccube.event.dispatcher']->addListener($event, array($subscriber, $handler[0]), $priority); |
|
911 | 139 | } |
|
912 | 139 | } |
|
913 | 139 | } |
|
914 | 139 | } |
|
915 | 139 | } |
|
916 | // Type: ServiceProvider |
||
917 | 139 | if (isset($config['service'])) { |
|
918 | foreach ($config['service'] as $service) { |
||
919 | $class = '\\Plugin\\'.$config['code'].'\\ServiceProvider\\'.$service; |
||
920 | View Code Duplication | if (!class_exists($class)) { |
|
921 | $this['monolog']->warning("skip {$code} loading. service provider class not foud.", array( |
||
922 | 'class' => $class, |
||
923 | )); |
||
924 | continue; |
||
925 | } |
||
926 | $this->register(new $class($this)); |
||
927 | } |
||
928 | } |
||
929 | 1048 | } |
|
930 | 1048 | } |
|
931 | |||
932 | /** |
||
933 | * PHPUnit を実行中かどうかを設定する. |
||
934 | * |
||
935 | * @param boolean $testMode PHPUnit を実行中の場合 true |
||
936 | */ |
||
937 | 1034 | public function setTestMode($testMode) |
|
938 | { |
||
939 | 1034 | $this->testMode = $testMode; |
|
940 | 1034 | } |
|
941 | |||
942 | /** |
||
943 | * PHPUnit を実行中かどうか. |
||
944 | * |
||
945 | * @return boolean PHPUnit を実行中の場合 true |
||
946 | */ |
||
947 | 456 | public function isTestMode() |
|
948 | { |
||
949 | 456 | return $this->testMode; |
|
950 | } |
||
951 | |||
952 | /** |
||
953 | * |
||
954 | * データベースの接続を確認 |
||
955 | * 成功 : trueを返却 |
||
956 | * 失敗 : \Doctrine\DBAL\DBALExceptionエラーが発生( 接続に失敗した場合 )、エラー画面を表示しdie() |
||
957 | * 備考 : app['debug']がtrueの際は処理を行わない |
||
958 | * |
||
959 | * @return boolean true |
||
960 | * |
||
961 | */ |
||
962 | 1048 | protected function checkDatabaseConnection() |
|
986 | |||
987 | /** |
||
988 | * Config ファイルをパースし、連想配列を返します. |
||
989 | * |
||
990 | * $config_name.yml ファイルをパースし、連想配列を返します. |
||
991 | * $config_name.php が存在する場合は、 PHP ファイルに記述された連想配列を使用します。 |
||
992 | * |
||
993 | * @param string $config_name Config 名称 |
||
994 | * @param array $configAll Config の連想配列 |
||
995 | * @param boolean $wrap_key Config の連想配列に config_name のキーを生成する場合 true, デフォルト false |
||
996 | * @param string $ymlPath config yaml を格納したディレクトリ |
||
997 | * @param string $distPath config yaml dist を格納したディレクトリ |
||
998 | * @return Application |
||
999 | */ |
||
1000 | 1051 | public function parseConfig($config_name, array &$configAll, $wrap_key = false, $ymlPath = null, $distPath = null) |
|
1041 | |||
1042 | /** |
||
1043 | * セッションが開始されているかどうか. |
||
1044 | * |
||
1045 | * @return boolean セッションが開始済みの場合 true |
||
1046 | * @link http://php.net/manual/ja/function.session-status.php#113468 |
||
1047 | */ |
||
1048 | 1048 | protected function isSessionStarted() |
|
1060 | |||
1061 | /** |
||
1062 | * Http Cache対応 |
||
1063 | */ |
||
1064 | 1048 | protected function initCacheRequest() |
|
1123 | } |
||
1124 |