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 |
||
| 34 | class Application extends ApplicationTrait |
||
|
|
|||
| 35 | { |
||
| 36 | protected static $instance; |
||
| 37 | |||
| 38 | protected $initialized = false; |
||
| 39 | protected $initializedPlugin = false; |
||
| 40 | |||
| 41 | 727 | public static function getInstance(array $values = array()) |
|
| 42 | { |
||
| 43 | if (!is_object(self::$instance)) { |
||
| 44 | self::$instance = new Application($values); |
||
| 45 | } |
||
| 46 | |||
| 47 | 727 | return self::$instance; |
|
| 48 | 727 | } |
|
| 49 | |||
| 50 | 727 | public static function clearInstance() |
|
| 51 | { |
||
| 52 | 727 | self::$instance = null; |
|
| 53 | 727 | } |
|
| 54 | |||
| 55 | final public function __clone() |
||
| 56 | { |
||
| 57 | throw new \Exception('Clone is not allowed against '.get_class($this)); |
||
| 58 | } |
||
| 59 | |||
| 60 | 727 | public function __construct(array $values = array()) |
|
| 61 | { |
||
| 62 | parent::__construct($values); |
||
| 63 | |||
| 64 | if (is_null(self::$instance)) { |
||
| 65 | 727 | self::$instance = $this; |
|
| 66 | } |
||
| 67 | |||
| 68 | // load config |
||
| 69 | $this->initConfig(); |
||
| 70 | |||
| 71 | // init monolog |
||
| 72 | $this->initLogger(); |
||
| 73 | } |
||
| 74 | |||
| 75 | 741 | public function initConfig() |
|
| 76 | { |
||
| 77 | // load config |
||
| 78 | $this['config'] = $this->share(function() { |
||
| 79 | 730 | $ymlPath = __DIR__.'/../../app/config/eccube'; |
|
| 80 | 730 | $distPath = __DIR__.'/../../src/Eccube/Resource/config'; |
|
| 81 | |||
| 82 | 730 | $config = array(); |
|
| 83 | 730 | $config_yml = $ymlPath.'/config.yml'; |
|
| 84 | if (file_exists($config_yml)) { |
||
| 85 | $config = Yaml::parse(file_get_contents($config_yml)); |
||
| 86 | } |
||
| 87 | |||
| 88 | 730 | $config_dist = array(); |
|
| 89 | 730 | $config_yml_dist = $distPath.'/config.yml.dist'; |
|
| 90 | if (file_exists($config_yml_dist)) { |
||
| 91 | $config_dist = Yaml::parse(file_get_contents($config_yml_dist)); |
||
| 92 | } |
||
| 93 | |||
| 94 | 730 | $config_path = array(); |
|
| 95 | 730 | $path_yml = $ymlPath.'/path.yml'; |
|
| 96 | if (file_exists($path_yml)) { |
||
| 97 | $config_path = Yaml::parse(file_get_contents($path_yml)); |
||
| 98 | } |
||
| 99 | |||
| 100 | 730 | $config_constant = array(); |
|
| 101 | 730 | $constant_yml = $ymlPath.'/constant.yml'; |
|
| 102 | if (file_exists($constant_yml)) { |
||
| 103 | $config_constant = Yaml::parse(file_get_contents($constant_yml)); |
||
| 104 | $config_constant = empty($config_constant) ? array() : $config_constant; |
||
| 105 | } |
||
| 106 | |||
| 107 | 730 | $config_constant_dist = array(); |
|
| 108 | 730 | $constant_yml_dist = $distPath.'/constant.yml.dist'; |
|
| 109 | if (file_exists($constant_yml_dist)) { |
||
| 110 | $config_constant_dist = Yaml::parse(file_get_contents($constant_yml_dist)); |
||
| 111 | } |
||
| 112 | |||
| 113 | $configAll = array_replace_recursive($config_constant_dist, $config_dist, $config_constant, $config_path, $config); |
||
| 114 | |||
| 115 | 730 | $database = array(); |
|
| 116 | 730 | $yml = $ymlPath.'/database.yml'; |
|
| 117 | if (file_exists($yml)) { |
||
| 118 | $database = Yaml::parse(file_get_contents($yml)); |
||
| 119 | } |
||
| 120 | |||
| 121 | 730 | $mail = array(); |
|
| 122 | 730 | $yml = $ymlPath.'/mail.yml'; |
|
| 123 | if (file_exists($yml)) { |
||
| 124 | $mail = Yaml::parse(file_get_contents($yml)); |
||
| 125 | } |
||
| 126 | $configAll = array_replace_recursive($configAll, $database, $mail); |
||
| 127 | |||
| 128 | 730 | $config_log = array(); |
|
| 129 | 730 | $yml = $ymlPath.'/log.yml'; |
|
| 130 | if (file_exists($yml)) { |
||
| 131 | $config_log = Yaml::parse(file_get_contents($yml)); |
||
| 132 | } |
||
| 133 | 730 | $config_log_dist = array(); |
|
| 134 | 730 | $log_yml_dist = $distPath.'/log.yml.dist'; |
|
| 135 | if (file_exists($log_yml_dist)) { |
||
| 136 | $config_log_dist = Yaml::parse(file_get_contents($log_yml_dist)); |
||
| 137 | } |
||
| 138 | |||
| 139 | $configAll = array_replace_recursive($configAll, $config_log_dist, $config_log); |
||
| 140 | |||
| 141 | 730 | $config_nav = array(); |
|
| 142 | 730 | $yml = $ymlPath.'/nav.yml'; |
|
| 143 | if (file_exists($yml)) { |
||
| 144 | $config_nav = array('nav' => Yaml::parse(file_get_contents($yml))); |
||
| 145 | } |
||
| 146 | 730 | $config_nav_dist = array(); |
|
| 147 | 730 | $nav_yml_dist = $distPath.'/nav.yml.dist'; |
|
| 148 | if (file_exists($nav_yml_dist)) { |
||
| 149 | $config_nav_dist = array('nav' => Yaml::parse(file_get_contents($nav_yml_dist))); |
||
| 150 | } |
||
| 151 | |||
| 152 | $configAll = array_replace_recursive($configAll, $config_nav_dist, $config_nav); |
||
| 153 | |||
| 154 | 730 | return $configAll; |
|
| 155 | }); |
||
| 156 | 741 | } |
|
| 157 | |||
| 158 | 741 | public function initLogger() |
|
| 159 | { |
||
| 160 | 741 | $app = $this; |
|
| 161 | $this->register(new ServiceProvider\EccubeMonologServiceProvider($app)); |
||
| 162 | $this['monolog.logfile'] = __DIR__.'/../../app/log/site.log'; |
||
| 163 | $this['monolog.name'] = 'eccube'; |
||
| 164 | 741 | } |
|
| 165 | |||
| 166 | 727 | public function initialize() |
|
| 167 | { |
||
| 168 | 727 | if ($this->initialized) { |
|
| 169 | return; |
||
| 170 | } |
||
| 171 | |||
| 172 | // init locale |
||
| 173 | $this->initLocale(); |
||
| 174 | |||
| 175 | // init session |
||
| 176 | $this->initSession(); |
||
| 177 | |||
| 178 | // init twig |
||
| 179 | $this->initRendering(); |
||
| 180 | |||
| 181 | // init provider |
||
| 182 | $this->register(new \Silex\Provider\HttpFragmentServiceProvider()); |
||
| 183 | $this->register(new \Silex\Provider\UrlGeneratorServiceProvider()); |
||
| 184 | $this->register(new \Silex\Provider\FormServiceProvider()); |
||
| 185 | $this->register(new \Silex\Provider\SerializerServiceProvider()); |
||
| 186 | $this->register(new \Eccube\ServiceProvider\ValidatorServiceProvider()); |
||
| 187 | |||
| 188 | 727 | $app = $this; |
|
| 189 | $this->error(function(\Exception $e, $code) use ($app) { |
||
| 190 | if ($app['debug']) { |
||
| 191 | 6 | return; |
|
| 192 | } |
||
| 193 | |||
| 194 | switch ($code) { |
||
| 195 | case 403: |
||
| 196 | $title = 'アクセスできません。'; |
||
| 197 | $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。'; |
||
| 198 | break; |
||
| 199 | case 404: |
||
| 200 | $title = 'ページがみつかりません。'; |
||
| 201 | $message = 'URLに間違いがないかご確認ください。'; |
||
| 202 | break; |
||
| 203 | default: |
||
| 204 | $title = 'システムエラーが発生しました。'; |
||
| 205 | $message = '大変お手数ですが、サイト管理者までご連絡ください。'; |
||
| 206 | break; |
||
| 207 | } |
||
| 208 | |||
| 209 | return $app['twig']->render('error.twig', array( |
||
| 210 | 'error_title' => $title, |
||
| 211 | 'error_message' => $message, |
||
| 212 | )); |
||
| 213 | }); |
||
| 214 | |||
| 215 | // init mailer |
||
| 216 | $this->initMailer(); |
||
| 217 | |||
| 218 | // init doctrine orm |
||
| 219 | $this->initDoctrine(); |
||
| 220 | |||
| 221 | // init security |
||
| 222 | $this->initSecurity(); |
||
| 223 | |||
| 224 | // init ec-cube service provider |
||
| 225 | $this->register(new ServiceProvider\EccubeServiceProvider()); |
||
| 226 | |||
| 227 | // mount controllers |
||
| 228 | $this->register(new \Silex\Provider\ServiceControllerServiceProvider()); |
||
| 229 | $this->mount('', new ControllerProvider\FrontControllerProvider()); |
||
| 230 | $this->mount('/'.trim($this['config']['admin_route'], '/').'/', new ControllerProvider\AdminControllerProvider()); |
||
| 231 | Request::enableHttpMethodParameterOverride(); // PUTやDELETEできるようにする |
||
| 232 | |||
| 233 | 727 | $this->initialized = true; |
|
| 234 | 727 | } |
|
| 235 | |||
| 236 | 727 | public function initLocale() |
|
| 237 | { |
||
| 238 | |||
| 239 | // timezone |
||
| 240 | if (!empty($this['config']['timezone'])) { |
||
| 241 | date_default_timezone_set($this['config']['timezone']); |
||
| 242 | } |
||
| 243 | |||
| 244 | $this->register(new \Silex\Provider\TranslationServiceProvider(), array( |
||
| 245 | 727 | 'locale' => $this['config']['locale'], |
|
| 246 | )); |
||
| 247 | $this['translator'] = $this->share($this->extend('translator', function($translator, \Silex\Application $app) { |
||
| 248 | $translator->addLoader('yaml', new \Symfony\Component\Translation\Loader\YamlFileLoader()); |
||
| 249 | |||
| 250 | $r = new \ReflectionClass('Symfony\Component\Validator\Validator'); |
||
| 251 | $file = dirname($r->getFilename()).'/Resources/translations/validators.'.$app['locale'].'.xlf'; |
||
| 252 | if (file_exists($file)) { |
||
| 253 | $translator->addResource('xliff', $file, $app['locale'], 'validators'); |
||
| 254 | } |
||
| 255 | |||
| 256 | $file = __DIR__.'/Resource/locale/validator.'.$app['locale'].'.yml'; |
||
| 257 | if (file_exists($file)) { |
||
| 258 | $translator->addResource('yaml', $file, $app['locale'], 'validators'); |
||
| 259 | } |
||
| 260 | |||
| 261 | $file = __DIR__.'/Resource/locale/message.'.$app['locale'].'.yml'; |
||
| 262 | if (file_exists($file)) { |
||
| 263 | $translator->addResource('yaml', $file, $app['locale']); |
||
| 264 | } |
||
| 265 | |||
| 266 | 402 | return $translator; |
|
| 267 | })); |
||
| 268 | 727 | } |
|
| 269 | |||
| 270 | 727 | public function initSession() |
|
| 271 | { |
||
| 272 | $this->register(new \Silex\Provider\SessionServiceProvider(), array( |
||
| 273 | 'session.storage.save_path' => $this['config']['root_dir'].'/app/cache/eccube/session', |
||
| 274 | 'session.storage.options' => array( |
||
| 275 | 727 | 'name' => 'eccube', |
|
| 276 | 'cookie_path' => $this['config']['root_urlpath'] ?: '/', |
||
| 277 | 727 | 'cookie_secure' => $this['config']['force_ssl'], |
|
| 278 | 727 | 'cookie_lifetime' => $this['config']['cookie_lifetime'], |
|
| 279 | 727 | 'cookie_httponly' => true, |
|
| 280 | // cookie_domainは指定しない |
||
| 281 | // http://blog.tokumaru.org/2011/10/cookiedomain.html |
||
| 282 | 727 | ), |
|
| 283 | )); |
||
| 284 | 727 | } |
|
| 285 | |||
| 286 | 727 | public function initRendering() |
|
| 287 | { |
||
| 288 | $this->register(new \Silex\Provider\TwigServiceProvider(), array( |
||
| 289 | 'twig.form.templates' => array('Form/form_layout.twig'), |
||
| 290 | )); |
||
| 291 | $this['twig'] = $this->share($this->extend('twig', function(\Twig_Environment $twig, \Silex\Application $app) { |
||
| 292 | $twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app)); |
||
| 293 | $twig->addExtension(new \Twig_Extension_StringLoader()); |
||
| 294 | |||
| 295 | 162 | return $twig; |
|
| 296 | })); |
||
| 297 | |||
| 298 | $this->before(function(Request $request, \Silex\Application $app) { |
||
| 299 | // フロント or 管理画面ごとにtwigの探索パスを切り替える. |
||
| 300 | $app['twig'] = $app->share($app->extend('twig', function(\Twig_Environment $twig, \Silex\Application $app) { |
||
| 301 | 131 | $paths = array(); |
|
| 302 | |||
| 303 | // 互換性がないのでprofiler とproduction 時のcacheを分離する |
||
| 304 | |||
| 305 | if (isset($app['profiler'])) { |
||
| 306 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/profiler/'; |
||
| 307 | } else { |
||
| 308 | 131 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/production/'; |
|
| 309 | } |
||
| 310 | if (strpos($app['request']->getPathInfo(), '/'.trim($app['config']['admin_route'], '/')) === 0) { |
||
| 311 | if (file_exists(__DIR__.'/../../app/template/admin')) { |
||
| 312 | 63 | $paths[] = __DIR__.'/../../app/template/admin'; |
|
| 313 | } |
||
| 314 | $paths[] = $app['config']['template_admin_realdir']; |
||
| 315 | 63 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 316 | 63 | $cache = $cacheBaseDir.'admin'; |
|
| 317 | } else { |
||
| 318 | if (file_exists($app['config']['template_realdir'])) { |
||
| 319 | $paths[] = $app['config']['template_realdir']; |
||
| 320 | } |
||
| 321 | $paths[] = $app['config']['template_default_realdir']; |
||
| 322 | 68 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 323 | $cache = $cacheBaseDir.$app['config']['template_code']; |
||
| 324 | 63 | } |
|
| 325 | $twig->setCache($cache); |
||
| 326 | $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths)); |
||
| 327 | |||
| 328 | 131 | return $twig; |
|
| 329 | })); |
||
| 330 | |||
| 331 | // 管理画面のIP制限チェック. |
||
| 332 | if (strpos($app['request']->getPathInfo(), '/'.trim($app['config']['admin_route'], '/')) === 0) { |
||
| 333 | // IP制限チェック |
||
| 334 | $allowHost = $app['config']['admin_allow_host']; |
||
| 335 | if (count($allowHost) > 0) { |
||
| 336 | if (array_search($app['request']->getClientIp(), $allowHost) === false) { |
||
| 337 | throw new \Exception(); |
||
| 338 | } |
||
| 339 | } |
||
| 340 | } |
||
| 341 | }, self::EARLY_EVENT); |
||
| 342 | |||
| 343 | // twigのグローバル変数を定義. |
||
| 344 | 727 | $app = $this; |
|
| 345 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function(\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
||
| 346 | // ショップ基本情報 |
||
| 347 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 348 | $app['twig']->addGlobal('BaseInfo', $BaseInfo); |
||
| 349 | |||
| 350 | if (strpos($app['request']->getPathInfo(), '/'.trim($app['config']['admin_route'], '/')) === 0) { |
||
| 351 | // 管理画面 |
||
| 352 | // 管理画面メニュー |
||
| 353 | 63 | $menus = array('', '', ''); |
|
| 354 | $app['twig']->addGlobal('menus', $menus); |
||
| 355 | |||
| 356 | $Member = $app->user(); |
||
| 357 | if (is_object($Member)) { |
||
| 358 | // ログインしていれば管理者のロールを取得 |
||
| 359 | $AuthorityRoles = $app['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority())); |
||
| 360 | |||
| 361 | 60 | $roles = array(); |
|
| 362 | foreach ($AuthorityRoles as $AuthorityRole) { |
||
| 363 | // 管理画面でメニュー制御するため相対パス全てをセット |
||
| 364 | $roles[] = $app['request']->getBaseUrl().'/'.$app['config']['admin_route'].$AuthorityRole->getDenyUrl(); |
||
| 365 | 60 | } |
|
| 366 | |||
| 367 | $app['twig']->addGlobal('AuthorityRoles', $roles); |
||
| 368 | } |
||
| 369 | |||
| 370 | } else { |
||
| 371 | // フロント画面 |
||
| 372 | $request = $event->getRequest(); |
||
| 373 | $route = $request->attributes->get('_route'); |
||
| 374 | |||
| 375 | // ユーザ作成画面 |
||
| 376 | if ($route === trim($app['config']['user_data_route'])) { |
||
| 377 | $params = $request->attributes->get('_route_params'); |
||
| 378 | 2 | $route = $params['route']; |
|
| 379 | // プレビュー画面 |
||
| 380 | } elseif ($request->get('preview')) { |
||
| 381 | $route = 'preview'; |
||
| 382 | 2 | } |
|
| 383 | |||
| 384 | try { |
||
| 385 | $DeviceType = $app['eccube.repository.master.device_type'] |
||
| 386 | ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC); |
||
| 387 | $PageLayout = $app['eccube.repository.page_layout']->getByUrl($DeviceType, $route); |
||
| 388 | } catch (\Doctrine\ORM\NoResultException $e) { |
||
| 389 | $PageLayout = $app['eccube.repository.page_layout']->newPageLayout($DeviceType); |
||
| 390 | 34 | } |
|
| 391 | |||
| 392 | $app['twig']->addGlobal('PageLayout', $PageLayout); |
||
| 393 | $app['twig']->addGlobal('title', $PageLayout->getName()); |
||
| 394 | 63 | } |
|
| 395 | }); |
||
| 396 | 727 | } |
|
| 397 | |||
| 398 | 727 | public function initMailer() |
|
| 399 | { |
||
| 400 | |||
| 401 | // メール送信時の文字エンコード指定(デフォルトはUTF-8) |
||
| 402 | if (isset($this['config']['mail']['charset_iso_2022_jp']) && is_bool($this['config']['mail']['charset_iso_2022_jp'])) { |
||
| 403 | if ($this['config']['mail']['charset_iso_2022_jp'] === true) { |
||
| 404 | \Swift::init(function() { |
||
| 405 | \Swift_DependencyContainer::getInstance() |
||
| 406 | ->register('mime.qpheaderencoder') |
||
| 407 | ->asAliasOf('mime.base64headerencoder'); |
||
| 408 | \Swift_Preferences::getInstance()->setCharset('iso-2022-jp'); |
||
| 409 | }); |
||
| 410 | } |
||
| 411 | } |
||
| 412 | |||
| 413 | $this->register(new \Silex\Provider\SwiftmailerServiceProvider()); |
||
| 414 | $this['swiftmailer.options'] = $this['config']['mail']; |
||
| 415 | |||
| 416 | if (isset($this['config']['mail']['spool']) && is_bool($this['config']['mail']['spool'])) { |
||
| 417 | $this['swiftmailer.use_spool'] = $this['config']['mail']['spool']; |
||
| 418 | } |
||
| 419 | // デフォルトはsmtpを使用 |
||
| 420 | $transport = $this['config']['mail']['transport']; |
||
| 421 | 727 | if ($transport == 'sendmail') { |
|
| 422 | $this['swiftmailer.transport'] = \Swift_SendmailTransport::newInstance(); |
||
| 423 | 727 | } elseif ($transport == 'mail') { |
|
| 424 | $this['swiftmailer.transport'] = \Swift_MailTransport::newInstance(); |
||
| 425 | } |
||
| 426 | 727 | } |
|
| 427 | |||
| 428 | 727 | public function initDoctrine() |
|
| 477 | |||
| 478 | 727 | public function initSecurity() |
|
| 479 | { |
||
| 480 | $this->register(new \Silex\Provider\SecurityServiceProvider()); |
||
| 481 | $this->register(new \Silex\Provider\RememberMeServiceProvider()); |
||
| 482 | |||
| 483 | 727 | $this['security.firewalls'] = array( |
|
| 484 | 'admin' => array( |
||
| 485 | 'pattern' => "^/{$this['config']['admin_route']}", |
||
| 486 | 'form' => array( |
||
| 487 | 'login_path' => "/{$this['config']['admin_route']}/login", |
||
| 488 | 'check_path' => "/{$this['config']['admin_route']}/login_check", |
||
| 489 | 727 | 'username_parameter' => 'login_id', |
|
| 490 | 727 | 'password_parameter' => 'password', |
|
| 491 | 727 | 'with_csrf' => true, |
|
| 492 | 727 | 'use_forward' => true, |
|
| 493 | 727 | ), |
|
| 494 | 'logout' => array( |
||
| 495 | 'logout_path' => "/{$this['config']['admin_route']}/logout", |
||
| 496 | 'target_url' => "/{$this['config']['admin_route']}/", |
||
| 497 | 727 | ), |
|
| 498 | 'users' => $this['orm.em']->getRepository('Eccube\Entity\Member'), |
||
| 499 | 727 | 'anonymous' => true, |
|
| 500 | ), |
||
| 501 | 'customer' => array( |
||
| 502 | 727 | 'pattern' => '^/', |
|
| 503 | 'form' => array( |
||
| 504 | 'login_path' => '/mypage/login', |
||
| 505 | 'check_path' => '/login_check', |
||
| 506 | 'username_parameter' => 'login_email', |
||
| 507 | 'password_parameter' => 'login_pass', |
||
| 508 | 'with_csrf' => true, |
||
| 509 | 'use_forward' => true, |
||
| 510 | 727 | ), |
|
| 511 | 'logout' => array( |
||
| 512 | 'logout_path' => '/logout', |
||
| 513 | 'target_url' => '/', |
||
| 514 | 727 | ), |
|
| 515 | 'remember_me' => array( |
||
| 516 | 'key' => sha1($this['config']['auth_magic']), |
||
| 517 | 727 | 'name' => 'eccube_rememberme', |
|
| 518 | // lifetimeはデフォルトの1年間にする |
||
| 519 | // 'lifetime' => $this['config']['cookie_lifetime'], |
||
| 520 | 'path' => $this['config']['root_urlpath'] ?: '/', |
||
| 521 | 727 | 'secure' => $this['config']['force_ssl'], |
|
| 522 | 727 | 'httponly' => true, |
|
| 523 | 727 | 'always_remember_me' => false, |
|
| 524 | 727 | 'remember_me_parameter' => 'login_memory', |
|
| 525 | ), |
||
| 526 | 'users' => $this['orm.em']->getRepository('Eccube\Entity\Customer'), |
||
| 527 | 727 | 'anonymous' => true, |
|
| 528 | ), |
||
| 529 | ); |
||
| 530 | |||
| 531 | 727 | $this['security.access_rules'] = array( |
|
| 532 | array("^/{$this['config']['admin_route']}/login", 'IS_AUTHENTICATED_ANONYMOUSLY'), |
||
| 533 | array("^/{$this['config']['admin_route']}", 'ROLE_ADMIN'), |
||
| 534 | 727 | array('^/mypage/login', 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
| 535 | 727 | array('^/mypage/withdraw_complete', 'IS_AUTHENTICATED_ANONYMOUSLY'), |
|
| 536 | 727 | array('^/mypage/change', 'IS_AUTHENTICATED_FULLY'), |
|
| 537 | 727 | array('^/mypage', 'ROLE_USER'), |
|
| 538 | ); |
||
| 539 | |||
| 540 | $this['eccube.password_encoder'] = $this->share(function($app) { |
||
| 541 | return new \Eccube\Security\Core\Encoder\PasswordEncoder($app['config']); |
||
| 542 | }); |
||
| 543 | $this['security.encoder_factory'] = $this->share(function($app) { |
||
| 544 | return new \Symfony\Component\Security\Core\Encoder\EncoderFactory(array( |
||
| 545 | 727 | 'Eccube\Entity\Customer' => $app['eccube.password_encoder'], |
|
| 546 | 727 | 'Eccube\Entity\Member' => $app['eccube.password_encoder'], |
|
| 547 | )); |
||
| 548 | }); |
||
| 549 | $this['eccube.event_listner.security'] = $this->share(function($app) { |
||
| 550 | return new \Eccube\EventListener\SecurityEventListener($app['orm.em']); |
||
| 551 | }); |
||
| 552 | $this['user'] = $this->share(function($app) { |
||
| 553 | $token = $app['security']->getToken(); |
||
| 554 | |||
| 555 | return ($token !== null) ? $token->getUser() : null; |
||
| 556 | }); |
||
| 557 | |||
| 558 | // ログイン時のイベントを設定. |
||
| 559 | $this['dispatcher']->addListener(\Symfony\Component\Security\Http\SecurityEvents::INTERACTIVE_LOGIN, array($this['eccube.event_listner.security'], 'onInteractiveLogin')); |
||
| 560 | |||
| 561 | // Voterの設定 |
||
| 562 | 727 | $app = $this; |
|
| 563 | $this['authority_voter'] = $this->share(function($app) { |
||
| 564 | return new \Eccube\Security\Voter\AuthorityVoter($app); |
||
| 565 | }); |
||
| 566 | |||
| 567 | $app['security.voters'] = $app->extend('security.voters', function($voters) use ($app) { |
||
| 568 | $voters[] = $app['authority_voter']; |
||
| 569 | |||
| 570 | 727 | return $voters; |
|
| 571 | }); |
||
| 572 | |||
| 573 | $this['security.access_manager'] = $this->share(function($app) { |
||
| 574 | return new \Symfony\Component\Security\Core\Authorization\AccessDecisionManager($app['security.voters'], 'unanimous'); |
||
| 575 | }); |
||
| 576 | |||
| 577 | 727 | } |
|
| 578 | |||
| 579 | public function initializePlugin() |
||
| 593 | |||
| 594 | 727 | public function initPluginEventDispatcher() |
|
| 595 | { |
||
| 596 | // EventDispatcher |
||
| 597 | $this['eccube.event.dispatcher'] = $this->share(function() { |
||
| 598 | return new EventDispatcher(); |
||
| 599 | }); |
||
| 600 | |||
| 601 | // hook point |
||
| 602 | $this->before(function(Request $request, \Silex\Application $app) { |
||
| 603 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.before'); |
||
| 604 | }, self::EARLY_EVENT); |
||
| 605 | |||
| 606 | $this->before(function(Request $request, \Silex\Application $app) { |
||
| 607 | $event = 'eccube.event.controller.'.$request->attributes->get('_route').'.before'; |
||
| 608 | $app['eccube.event.dispatcher']->dispatch($event); |
||
| 609 | }); |
||
| 610 | |||
| 611 | View Code Duplication | $this->after(function(Request $request, Response $response, \Silex\Application $app) { |
|
| 612 | $event = 'eccube.event.controller.'.$request->attributes->get('_route').'.after'; |
||
| 613 | $app['eccube.event.dispatcher']->dispatch($event); |
||
| 614 | }); |
||
| 615 | |||
| 616 | $this->after(function(Request $request, Response $response, \Silex\Application $app) { |
||
| 617 | $app['eccube.event.dispatcher']->dispatch('eccube.event.app.after'); |
||
| 618 | }, self::LATE_EVENT); |
||
| 619 | |||
| 620 | View Code Duplication | $this->finish(function(Request $request, Response $response, \Silex\Application $app) { |
|
| 621 | $event = 'eccube.event.controller.'.$request->attributes->get('_route').'.finish'; |
||
| 622 | $app['eccube.event.dispatcher']->dispatch($event); |
||
| 623 | }); |
||
| 624 | |||
| 625 | 727 | $app = $this; |
|
| 626 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function(\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) { |
||
| 627 | $route = $event->getRequest()->attributes->get('_route'); |
||
| 628 | $app['eccube.event.dispatcher']->dispatch('eccube.event.render.'.$route.'.before', $event); |
||
| 629 | }); |
||
| 630 | 727 | } |
|
| 631 | |||
| 632 | public function loadPlugin() |
||
| 717 | } |
||
| 718 |