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 |
||
| 49 | class Application extends \Silex\Application |
||
|
|
|||
| 50 | { |
||
| 51 | use \Silex\Application\FormTrait; |
||
| 52 | use \Silex\Application\UrlGeneratorTrait; |
||
| 53 | use \Silex\Application\MonologTrait; |
||
| 54 | use \Silex\Application\SwiftmailerTrait; |
||
| 55 | use \Silex\Application\SecurityTrait; |
||
| 56 | use \Silex\Application\TranslationTrait; |
||
| 57 | use \Eccube\Application\ApplicationTrait; |
||
| 58 | use \Eccube\Application\SecurityTrait; |
||
| 59 | use \Eccube\Application\TwigTrait; |
||
| 60 | |||
| 61 | protected static $instance; |
||
| 62 | |||
| 63 | protected $initialized = false; |
||
| 64 | protected $initializedPlugin = false; |
||
| 65 | protected $testMode = false; |
||
| 66 | |||
| 67 | 1091 | public static function getInstance(array $values = array()) |
|
| 75 | |||
| 76 | 1091 | public static function clearInstance() |
|
| 80 | |||
| 81 | final public function __clone() |
||
| 85 | |||
| 86 | 1092 | public function __construct(array $values = array()) |
|
| 100 | |||
| 101 | /** |
||
| 102 | * Application::runが実行されているか親クラスのプロパティから判定 |
||
| 103 | * |
||
| 104 | * @return bool |
||
| 105 | */ |
||
| 106 | 1091 | public function isBooted() |
|
| 110 | |||
| 111 | 1092 | public function initConfig() |
|
| 112 | { |
||
| 113 | // load .env |
||
| 114 | 1092 | $envFile = __DIR__.'/../../.env'; |
|
| 115 | 1092 | if (file_exists($envFile)) { |
|
| 116 | (new Dotenv())->load($envFile); |
||
| 117 | } |
||
| 118 | |||
| 119 | // load config |
||
| 120 | $this['config'] = function() { |
||
| 121 | 1092 | $configAll = array(); |
|
| 122 | 1092 | $this->parseConfig('constant', $configAll) |
|
| 123 | 1092 | ->parseConfig('path', $configAll) |
|
| 124 | 1092 | ->parseConfig('config', $configAll) |
|
| 125 | 1092 | ->parseConfig('database', $configAll) |
|
| 126 | 1092 | ->parseConfig('mail', $configAll) |
|
| 127 | 1092 | ->parseConfig('log', $configAll) |
|
| 128 | 1092 | ->parseConfig('nav', $configAll, true) |
|
| 129 | 1092 | ->parseConfig('doctrine_cache', $configAll) |
|
| 130 | 1092 | ->parseConfig('http_cache', $configAll) |
|
| 131 | 1092 | ->parseConfig('session_handler', $configAll); |
|
| 132 | |||
| 133 | 1092 | return $configAll; |
|
| 134 | }; |
||
| 135 | } |
||
| 136 | |||
| 137 | 1092 | public function initLogger() |
|
| 138 | { |
||
| 139 | 1092 | $app = $this; |
|
| 140 | 1092 | $this->register(new ServiceProvider\LogServiceProvider($app)); |
|
| 141 | } |
||
| 142 | |||
| 143 | 1091 | public function initialize() |
|
| 144 | { |
||
| 145 | 1091 | if ($this->initialized) { |
|
| 146 | 2 | return; |
|
| 147 | } |
||
| 148 | |||
| 149 | // init locale |
||
| 150 | 1091 | $this->initLocale(); |
|
| 151 | |||
| 152 | // init session |
||
| 153 | 1091 | if (!$this->isSessionStarted()) { |
|
| 154 | 1091 | $this->initSession(); |
|
| 155 | } |
||
| 156 | |||
| 157 | // init twig |
||
| 158 | 1091 | $this->initRendering(); |
|
| 159 | |||
| 160 | // init provider |
||
| 161 | 1091 | $this->register(new \Silex\Provider\HttpCacheServiceProvider(), array( |
|
| 162 | 1091 | 'http_cache.cache_dir' => __DIR__.'/../../app/cache/http/', |
|
| 163 | )); |
||
| 164 | 1091 | $this->register(new \Silex\Provider\HttpFragmentServiceProvider()); |
|
| 165 | 1091 | $this->register(new \Silex\Provider\FormServiceProvider()); |
|
| 166 | 1091 | $this->register(new \Silex\Provider\SerializerServiceProvider()); |
|
| 167 | 1091 | $this->register(new \Silex\Provider\ValidatorServiceProvider()); |
|
| 168 | 1091 | $this->register(new \Saxulum\Validator\Provider\SaxulumValidatorProvider()); |
|
| 169 | 1091 | $this->register(new MobileDetectServiceProvider()); |
|
| 170 | 1091 | $this->register(new TwigLintServiceProvider()); |
|
| 171 | |||
| 172 | $this->error(function (\Exception $e, Request $request, $code) { |
||
| 173 | 23 | if ($this['debug']) { |
|
| 174 | 23 | return; |
|
| 175 | } |
||
| 176 | |||
| 177 | switch ($code) { |
||
| 178 | case 403: |
||
| 179 | $title = 'アクセスできません。'; |
||
| 180 | $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。'; |
||
| 181 | break; |
||
| 182 | case 404: |
||
| 183 | $title = 'ページがみつかりません。'; |
||
| 184 | $message = 'URLに間違いがないかご確認ください。'; |
||
| 185 | break; |
||
| 186 | default: |
||
| 187 | $title = 'システムエラーが発生しました。'; |
||
| 188 | $message = '大変お手数ですが、サイト管理者までご連絡ください。'; |
||
| 189 | break; |
||
| 190 | } |
||
| 191 | |||
| 192 | return $this->render('error.twig', array( |
||
| 193 | 'error_title' => $title, |
||
| 194 | 'error_message' => $message, |
||
| 195 | )); |
||
| 196 | 1091 | }); |
|
| 197 | |||
| 198 | // init mailer |
||
| 199 | 1091 | $this->initMailer(); |
|
| 200 | |||
| 201 | 1091 | $this->register(new \Sergiors\Silex\Provider\DoctrineCacheServiceProvider()); |
|
| 202 | 1091 | $this->register(new \Sergiors\Silex\Provider\AnnotationsServiceProvider(), [ |
|
| 203 | 1091 | 'annotations.debug' => $this['debug'], |
|
| 204 | 'annotations.options' => [ |
||
| 205 | 1091 | 'cache_driver' => $this['debug'] ? 'array' : 'filesystem', |
|
| 206 | 1091 | 'cache_dir' => $this['debug'] ? null : __DIR__.'/../../app/cache/annotation' |
|
| 207 | ] |
||
| 208 | ]); |
||
| 209 | |||
| 210 | // init doctrine orm |
||
| 211 | 1091 | $this->initDoctrine(); |
|
| 212 | |||
| 213 | // Set up the DBAL connection now to check for a proper connection to the database. |
||
| 214 | 1091 | $this->checkDatabaseConnection(); |
|
| 215 | |||
| 216 | // init security |
||
| 217 | 1091 | $this->initSecurity(); |
|
| 218 | |||
| 219 | 1091 | $this->register(new \Sergiors\Silex\Provider\RoutingServiceProvider(), [ |
|
| 220 | 1091 | 'routing.cache_dir' => $this['debug'] ? null : __DIR__.'/../../app/cache/routing' |
|
| 221 | ]); |
||
| 222 | 1091 | $this->register(new \Sergiors\Silex\Provider\TemplatingServiceProvider()); |
|
| 223 | 1091 | $this->register(new \Sergiors\Silex\Provider\SensioFrameworkExtraServiceProvider(), [ |
|
| 224 | 1091 | 'request' => [ |
|
| 225 | 'auto_convert' => true |
||
| 226 | ] |
||
| 227 | ]); |
||
| 228 | // init proxy |
||
| 229 | 1091 | $this->initProxy(); |
|
| 230 | |||
| 231 | // init ec-cube service provider |
||
| 232 | 1091 | $this->register(new DiServiceProvider(), [ |
|
| 233 | 'eccube.di.scanners' => [ |
||
| 234 | 1091 | new \Eccube\Di\Scanner\ComponentScanner([ |
|
| 235 | 1091 | $this['config']['root_dir'].'/app/Acme/Controller', |
|
| 236 | 1091 | $this['config']['root_dir'].'/src/Eccube/Controller' |
|
| 237 | ]), |
||
| 238 | 1091 | new \Eccube\Di\Scanner\FormTypeScanner([ |
|
| 239 | 1091 | $this['config']['root_dir'].'/src/Eccube/Form/Type' |
|
| 240 | ]), |
||
| 241 | 1091 | new \Eccube\Di\Scanner\FormExtensionScanner([ |
|
| 242 | 1091 | $this['config']['root_dir'].'/src/Eccube/Form/Extension' |
|
| 243 | ]), |
||
| 244 | 1091 | new \Eccube\Di\Scanner\ServiceScanner([ |
|
| 245 | 1091 | $this['config']['root_dir'].'/src/Eccube/Service' |
|
| 246 | ]), |
||
| 247 | 1091 | new \Eccube\Di\Scanner\RepositoryScanner([ |
|
| 248 | 1091 | $this['config']['root_dir'].'/src/Eccube/Repository' |
|
| 249 | ]) |
||
| 250 | ], |
||
| 251 | 1091 | 'eccube.di.generator.dir' => $this['config']['root_dir'].'/app/cache/provider' |
|
| 252 | ]); |
||
| 253 | |||
| 254 | 1091 | $this->register(new CompatRepositoryProvider()); |
|
| 255 | 1091 | $this->register(new CompatServiceProvider()); |
|
| 256 | 1091 | $this->register(new ServiceProvider\EccubeServiceProvider()); |
|
| 257 | |||
| 258 | 1091 | $this->register(new \Silex\Provider\ServiceControllerServiceProvider()); |
|
| 259 | 1091 | Request::enableHttpMethodParameterOverride(); // PUTやDELETEできるようにする |
|
| 260 | |||
| 261 | // ルーティングの設定 |
||
| 262 | // TODO EccubeRoutingServiceProviderに移植する. |
||
| 263 | 1091 | $app = $this; |
|
| 264 | $this['eccube.router'] = $this->protect(function($resoure, $cachePrefix) use ($app) { |
||
| 265 | $options = [ |
||
| 266 | 1091 | 'debug' => $app['debug'], |
|
| 267 | 1091 | 'cache_dir' => $app['routing.cache_dir'], |
|
| 268 | 1091 | 'matcher_base_class' => $app['request_matcher_class'], |
|
| 269 | 1091 | 'matcher_class' => $app['request_matcher_class'], |
|
| 270 | 1091 | 'matcher_cache_class' => $cachePrefix.'UrlMatcher', |
|
| 271 | 1091 | 'generator_cache_class' => $cachePrefix.'UrlGenerator' |
|
| 272 | ]; |
||
| 273 | 1091 | $router = new EccubeRouter( |
|
| 274 | 1091 | $app['routing.loader'], |
|
| 275 | 1091 | $resoure, |
|
| 276 | 1091 | $options, |
|
| 277 | 1091 | $app['request_context'], |
|
| 278 | 1091 | $app['logger'] |
|
| 279 | ); |
||
| 280 | |||
| 281 | 1091 | $router->setAdminPrefix($app['config']['admin_route']); |
|
| 282 | 1091 | $router->setUserDataPrefix($app['config']['user_data_route']); |
|
| 283 | 1091 | $router->setRequireHttps($app['config']['force_ssl']); |
|
| 284 | |||
| 285 | 1091 | return $router; |
|
| 286 | 1091 | }); |
|
| 287 | |||
| 288 | $this['eccube.router.origin'] = function ($app) { |
||
| 289 | 1091 | $resource = __DIR__.'/Controller'; |
|
| 290 | 1091 | $cachePrefix = 'Origin'; |
|
| 291 | |||
| 292 | 1091 | return $app['eccube.router']($resource, $cachePrefix); |
|
| 293 | }; |
||
| 294 | |||
| 295 | $this['eccube.routers.plugin'] = function ($app) { |
||
| 296 | // TODO 有効なプラグインを対象とする必要がある. |
||
| 297 | 1091 | $dirs = Finder::create() |
|
| 298 | 1091 | ->in($app['config']['root_dir'].'/app/Plugin') |
|
| 299 | 1091 | ->name('Controller') |
|
| 300 | 1091 | ->directories(); |
|
| 301 | |||
| 302 | 1091 | $routers = []; |
|
| 303 | 1091 | foreach ($dirs as $dir) { |
|
| 304 | 1091 | $realPath = $dir->getRealPath(); |
|
| 305 | 1091 | $pluginCode = basename(dirname($realPath)); |
|
| 306 | 1091 | $routers[] = $app['eccube.router']($realPath, 'Plugin'.$pluginCode); |
|
| 307 | } |
||
| 308 | |||
| 309 | 1091 | return $routers; |
|
| 310 | }; |
||
| 311 | |||
| 312 | $this['eccube.router.extend'] = function ($app) { |
||
| 313 | // TODO ディレクトリ名は暫定 |
||
| 314 | 1091 | $resource = $app['config']['root_dir'].'/app/Acme/Controller'; |
|
| 315 | 1091 | $cachePrefix = 'Extend'; |
|
| 316 | |||
| 317 | 1091 | $router = $app['eccube.router']($resource, $cachePrefix); |
|
| 318 | |||
| 319 | 1091 | return $router; |
|
| 320 | }; |
||
| 321 | |||
| 322 | View Code Duplication | $this->extend('request_matcher', function ($matcher, $app) { |
|
| 323 | 1091 | $matchers = []; |
|
| 324 | 1091 | $matchers[] = $app['eccube.router.extend']; |
|
| 325 | 1091 | foreach ($app['eccube.routers.plugin'] as $router) { |
|
| 326 | 1091 | $matchers[] = $router; |
|
| 327 | }; |
||
| 328 | 1091 | $matchers[] = $app['eccube.router.origin']; |
|
| 329 | 1091 | $matchers[] = $matcher; |
|
| 330 | |||
| 331 | 1091 | return new ChainUrlMatcher($matchers, $app['request_context']); |
|
| 332 | 1091 | }); |
|
| 333 | |||
| 334 | View Code Duplication | $this->extend('url_generator', function ($generator, $app) { |
|
| 335 | 1091 | $generators = []; |
|
| 336 | 1091 | $generators[] = $app['eccube.router.extend']; |
|
| 337 | 1091 | foreach ($app['eccube.routers.plugin'] as $router) { |
|
| 338 | 1091 | $generators[] = $router; |
|
| 339 | }; |
||
| 340 | 1091 | $generators[] = $app['eccube.router.origin']; |
|
| 341 | 1091 | $generators[] = $generator; |
|
| 342 | |||
| 343 | 1091 | return new ChainUrlGenerator($generators, $app['request_context']); |
|
| 344 | 1091 | }); |
|
| 345 | |||
| 346 | // Route CollectionにEC-CUBEで定義したルーティングを追加(debug tool barに出力するため) |
||
| 347 | $this->extend('routes', function ($routes, $app) { |
||
| 348 | 1091 | $routes->addCollection($app['eccube.router.extend']->getRouteCollection()); |
|
| 349 | 1091 | foreach ($app['eccube.routers.plugin'] as $router) { |
|
| 350 | 1091 | $routes->addCollection($router->getRouteCollection()); |
|
| 351 | }; |
||
| 352 | 1091 | $routes->addCollection($app['eccube.router.origin']->getRouteCollection()); |
|
| 353 | |||
| 354 | 1091 | return $routes; |
|
| 355 | 1091 | }); |
|
| 356 | |||
| 357 | // init http cache |
||
| 358 | 1091 | $this->initCacheRequest(); |
|
| 359 | |||
| 360 | 1091 | $this->initialized = true; |
|
| 361 | } |
||
| 362 | |||
| 363 | 1091 | public function initLocale() |
|
| 364 | { |
||
| 365 | // locale |
||
| 366 | 1091 | if (!empty($this['config']['locale'])) { |
|
| 367 | 1091 | \Locale::setDefault($this['config']['locale']); |
|
| 368 | }; |
||
| 369 | |||
| 370 | // timezone |
||
| 371 | 1091 | if (!empty($this['config']['timezone'])) { |
|
| 372 | 1091 | date_default_timezone_set($this['config']['timezone']); |
|
| 373 | } |
||
| 374 | |||
| 375 | 1091 | $this->register(new \Silex\Provider\TranslationServiceProvider(), array( |
|
| 376 | 1091 | 'locale' => $this['config']['locale'], |
|
| 377 | 1091 | 'translator.cache_dir' => $this['debug'] ? null : $this['config']['root_dir'].'/app/cache/translator', |
|
| 378 | 'locale_fallbacks' => ['ja', 'en'], |
||
| 379 | )); |
||
| 380 | $this->extend('translator', function ($translator, \Silex\Application $app) { |
||
| 381 | 1091 | $translator->addLoader('php', new \Symfony\Component\Translation\Loader\PhpFileLoader()); |
|
| 382 | |||
| 383 | 1091 | $file = __DIR__.'/Resource/locale/messages.'.$app['locale'].'.php'; |
|
| 384 | 1091 | if (file_exists($file)) { |
|
| 385 | 1091 | $translator->addResource('php', $file, $app['locale']); |
|
| 386 | 1091 | $translator->addResource('php', $file, $app['locale'], 'validators'); |
|
| 387 | } |
||
| 388 | |||
| 389 | 1091 | return $translator; |
|
| 390 | 1091 | }); |
|
| 391 | } |
||
| 392 | |||
| 393 | 1091 | public function initSession() |
|
| 394 | { |
||
| 395 | 1091 | $this->register(new \Silex\Provider\SessionServiceProvider(), array( |
|
| 396 | 1091 | 'session.storage.save_path' => $this['config']['root_dir'].'/app/cache/eccube/session', |
|
| 397 | 'session.storage.options' => array( |
||
| 398 | 1091 | 'name' => $this['config']['cookie_name'], |
|
| 399 | 1091 | 'cookie_path' => $this['config']['root_urlpath'] ?: '/', |
|
| 400 | 1091 | 'cookie_secure' => $this['config']['force_ssl'], |
|
| 401 | 1091 | 'cookie_lifetime' => $this['config']['cookie_lifetime'], |
|
| 402 | 'cookie_httponly' => true, |
||
| 403 | // cookie_domainは指定しない |
||
| 404 | // http://blog.tokumaru.org/2011/10/cookiedomain.html |
||
| 405 | ), |
||
| 406 | )); |
||
| 407 | |||
| 408 | 1091 | $options = $this['config']['session_handler']; |
|
| 409 | |||
| 410 | 1091 | if ($options['enabled']) { |
|
| 411 | // @see http://silex.sensiolabs.org/doc/providers/session.html#custom-session-configurations |
||
| 412 | $this['session.storage.handler'] = null; |
||
| 413 | ini_set('session.save_handler', $options['save_handler']); |
||
| 414 | ini_set('session.save_path', $options['save_path']); |
||
| 415 | } |
||
| 416 | } |
||
| 417 | |||
| 418 | 1091 | public function initRendering() |
|
| 419 | { |
||
| 420 | 1091 | $this->register(new \Silex\Provider\TwigServiceProvider(), array( |
|
| 421 | 1091 | 'twig.form.templates' => array('Form/form_layout.twig'), |
|
| 422 | )); |
||
| 423 | $this->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) { |
||
| 424 | 1091 | $twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app)); |
|
| 425 | 1091 | $twig->addExtension(new \Twig_Extension_StringLoader()); |
|
| 426 | |||
| 427 | 1091 | return $twig; |
|
| 428 | 1091 | }); |
|
| 429 | |||
| 430 | $this->before(function (Request $request, \Silex\Application $app) { |
||
| 431 | 315 | $app['admin'] = $app['front'] = false; |
|
| 432 | 315 | $pathinfo = rawurldecode($request->getPathInfo()); |
|
| 433 | 315 | if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) { |
|
| 434 | 218 | $app['admin'] = true; |
|
| 435 | } else { |
||
| 436 | 97 | $app['front'] = true; |
|
| 437 | } |
||
| 438 | |||
| 439 | // フロント or 管理画面ごとにtwigの探索パスを切り替える. |
||
| 440 | 315 | if ($app->isAdminRequest()) { |
|
| 441 | 218 | if (file_exists(__DIR__.'/../../app/template/admin')) { |
|
| 442 | 218 | $paths[] = __DIR__.'/../../app/template/admin'; |
|
| 443 | } |
||
| 444 | 218 | $paths[] = $app['config']['template_admin_realdir']; |
|
| 445 | 218 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 446 | 218 | $cacheDir = __DIR__.'/../../app/cache/twig/admin'; |
|
| 447 | } else { |
||
| 448 | // モバイル端末時、smartphoneディレクトリを探索パスに追加する. |
||
| 449 | 97 | if ($app['mobile_detect.device_type'] == \Eccube\Entity\Master\DeviceType::DEVICE_TYPE_SP) { |
|
| 450 | if (file_exists(__DIR__.'/../../app/template/smartphone')) { |
||
| 451 | $paths[] = __DIR__.'/../../app/template/smartphone'; |
||
| 452 | } |
||
| 453 | $paths[] = __DIR__.'/Resource/template/smartphone'; |
||
| 454 | } |
||
| 455 | |||
| 456 | 97 | if (file_exists($app['config']['template_realdir'])) { |
|
| 457 | 97 | $paths[] = $app['config']['template_realdir']; |
|
| 458 | } |
||
| 459 | 97 | $paths[] = $app['config']['template_default_realdir']; |
|
| 460 | 97 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 461 | 97 | $cacheDir = __DIR__.'/../../app/cache/twig/'.$app['config']['template_code']; |
|
| 462 | } |
||
| 463 | 315 | $app['twig']->setCache($app['debug'] ? null : $cacheDir); |
|
| 464 | 315 | $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths)); |
|
| 465 | |||
| 466 | // 管理画面のIP制限チェック. |
||
| 467 | 315 | if ($app->isAdminRequest()) { |
|
| 468 | // IP制限チェック |
||
| 469 | 218 | $allowHost = $app['config']['admin_allow_host']; |
|
| 470 | 218 | if (count($allowHost) > 0) { |
|
| 471 | if (array_search($app['request_stack']->getCurrentRequest()->getClientIp(), $allowHost) === false) { |
||
| 472 | throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException(); |
||
| 473 | } |
||
| 474 | } |
||
| 475 | } |
||
| 476 | 1091 | }, self::EARLY_EVENT); |
|
| 477 | |||
| 478 | // twigのグローバル変数を定義. |
||
| 479 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) { |
||
| 480 | // 未ログイン時にマイページや管理画面以下にアクセスするとSubRequestで実行されるため, |
||
| 481 | // $event->isMasterRequest()ではなく、グローバル変数が初期化済かどうかの判定を行う |
||
| 482 | 313 | if (isset($this['twig_global_initialized']) && $this['twig_global_initialized'] === true) { |
|
| 483 | 71 | return; |
|
| 484 | } |
||
| 485 | // ショップ基本情報 |
||
| 486 | 313 | $this['twig']->addGlobal('BaseInfo', $this[BaseInfo::class]); |
|
| 487 | |||
| 488 | 313 | if ($this->isAdminRequest()) { |
|
| 489 | // 管理画面 |
||
| 490 | // 管理画面メニュー |
||
| 491 | 218 | $menus = array('', '', ''); |
|
| 492 | 218 | $this['twig']->addGlobal('menus', $menus); |
|
| 493 | |||
| 494 | 218 | $Member = $this->user(); |
|
| 495 | 218 | if (is_object($Member)) { |
|
| 496 | // ログインしていれば管理者のロールを取得 |
||
| 497 | 215 | $AuthorityRoles = $this['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority())); |
|
| 498 | |||
| 499 | 215 | $roles = array(); |
|
| 500 | 215 | $request = $event->getRequest(); |
|
| 501 | 215 | foreach ($AuthorityRoles as $AuthorityRole) { |
|
| 502 | // 管理画面でメニュー制御するため相対パス全てをセット |
||
| 503 | 3 | $roles[] = $request->getBaseUrl().'/'.$this['config']['admin_route'].$AuthorityRole->getDenyUrl(); |
|
| 504 | } |
||
| 505 | |||
| 506 | 218 | $this['twig']->addGlobal('AuthorityRoles', $roles); |
|
| 507 | } |
||
| 508 | |||
| 509 | } else { |
||
| 510 | // フロント画面 |
||
| 511 | 95 | $request = $event->getRequest(); |
|
| 512 | 95 | $route = $request->attributes->get('_route'); |
|
| 513 | |||
| 514 | // ユーザ作成画面 |
||
| 515 | 95 | if ($route === 'user_data') { |
|
| 516 | 2 | $params = $request->attributes->get('_route_params'); |
|
| 517 | 2 | $route = $params['route']; |
|
| 518 | // プレビュー画面 |
||
| 519 | 93 | } elseif ($request->get('preview')) { |
|
| 520 | $route = 'preview'; |
||
| 521 | } |
||
| 522 | |||
| 523 | try { |
||
| 524 | 95 | $device_type_id = $this['mobile_detect.device_type']; |
|
| 525 | |||
| 526 | // TODO デバッグ用 |
||
| 527 | 95 | if ($request->query->has('device_type_id')) { |
|
| 528 | $device_type_id = $request->get('device_type_id', \Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC); |
||
| 529 | } |
||
| 530 | |||
| 531 | 95 | $DeviceType = $this['eccube.repository.master.device_type'] |
|
| 532 | 95 | ->find($device_type_id); |
|
| 533 | 95 | $qb = $this['eccube.repository.page_layout']->createQueryBuilder('p'); |
|
| 534 | 95 | $PageLayout = $qb->select('p, pll,l, bp, b') |
|
| 535 | 95 | ->leftJoin('p.PageLayoutLayouts', 'pll') |
|
| 536 | 95 | ->leftJoin('pll.Layout', 'l') |
|
| 537 | 95 | ->leftJoin('l.BlockPositions', 'bp') |
|
| 538 | 95 | ->leftJoin('bp.Block', 'b') |
|
| 539 | 95 | ->where('p.url = :route') |
|
| 540 | 95 | ->andWhere('l.DeviceType = :DeviceType') |
|
| 541 | 95 | ->orderBy('bp.block_row', 'ASC') |
|
| 542 | 95 | ->setParameter('route', $route) |
|
| 543 | 95 | ->setParameter('DeviceType', $DeviceType) |
|
| 544 | 95 | ->getQuery() |
|
| 545 | 95 | ->getSingleResult(); |
|
| 546 | 33 | } catch (\Doctrine\ORM\NoResultException $e) { |
|
| 547 | 33 | $PageLayout = $this['eccube.repository.page_layout']->newPageLayout($DeviceType); |
|
| 548 | } |
||
| 549 | |||
| 550 | 95 | $this['twig']->addGlobal('PageLayout', $PageLayout); |
|
| 551 | 95 | $this['twig']->addGlobal('title', $PageLayout->getName()); |
|
| 552 | } |
||
| 553 | |||
| 554 | 313 | $this['twig_global_initialized'] = true; |
|
| 555 | 1091 | }); |
|
| 556 | } |
||
| 557 | |||
| 558 | 1091 | public function initMailer() |
|
| 559 | { |
||
| 560 | |||
| 561 | // メール送信時の文字エンコード指定(デフォルトはUTF-8) |
||
| 562 | 1091 | if (isset($this['config']['mail']['charset_iso_2022_jp']) && is_bool($this['config']['mail']['charset_iso_2022_jp'])) { |
|
| 563 | if ($this['config']['mail']['charset_iso_2022_jp'] === true) { |
||
| 564 | \Swift::init(function () { |
||
| 565 | \Swift_DependencyContainer::getInstance() |
||
| 566 | ->register('mime.qpheaderencoder') |
||
| 567 | ->asAliasOf('mime.base64headerencoder'); |
||
| 568 | \Swift_Preferences::getInstance()->setCharset('iso-2022-jp'); |
||
| 569 | }); |
||
| 570 | } |
||
| 571 | } |
||
| 572 | |||
| 573 | 1091 | $this->register(new \Silex\Provider\SwiftmailerServiceProvider()); |
|
| 574 | 1091 | $this['swiftmailer.options'] = $this['config']['mail']; |
|
| 575 | |||
| 576 | 1091 | if (isset($this['config']['mail']['spool']) && is_bool($this['config']['mail']['spool'])) { |
|
| 577 | 1091 | $this['swiftmailer.use_spool'] = $this['config']['mail']['spool']; |
|
| 578 | } |
||
| 579 | // デフォルトはsmtpを使用 |
||
| 580 | 1091 | $transport = $this['config']['mail']['transport']; |
|
| 581 | 1091 | if ($transport == 'sendmail') { |
|
| 582 | $this['swiftmailer.transport'] = \Swift_SendmailTransport::newInstance(); |
||
| 583 | 1091 | } elseif ($transport == 'mail') { |
|
| 584 | $this['swiftmailer.transport'] = \Swift_MailTransport::newInstance(); |
||
| 585 | } |
||
| 586 | } |
||
| 587 | |||
| 588 | 1091 | public function initDoctrine() |
|
| 589 | { |
||
| 590 | 1091 | $this->register(new EntityEventServiceProvider()); |
|
| 591 | 1091 | $this->register(new \Silex\Provider\DoctrineServiceProvider(), array( |
|
| 592 | 'dbs.options' => array( |
||
| 593 | 1091 | 'default' => $this['config']['database'] |
|
| 594 | ) |
||
| 595 | )); |
||
| 596 | 1091 | $this->register(new \Saxulum\DoctrineOrmManagerRegistry\Provider\DoctrineOrmManagerRegistryProvider()); |
|
| 597 | |||
| 598 | 1091 | $app = $this; |
|
| 599 | $this->extend('db.event_manager', function ($evm) use ($app) { |
||
| 600 | 1091 | $initSubscriber = new InitSubscriber($app); |
|
| 601 | 1091 | $evm->addEventSubscriber($initSubscriber); |
|
| 602 | |||
| 603 | 1091 | return $evm; |
|
| 604 | 1091 | }); |
|
| 605 | |||
| 606 | // UTCで保存 |
||
| 607 | // @see http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/cookbook/working-with-datetime.html |
||
| 608 | 1091 | UTCDateTimeType::setTimeZone($this['config']['timezone']); |
|
| 609 | 1091 | UTCDateTimeTzType::setTimeZone($this['config']['timezone']); |
|
| 610 | 1091 | Type::overrideType('datetime', UTCDateTimeType::class); |
|
| 611 | 1091 | Type::overrideType('datetimetz', UTCDateTimeTzType::class); |
|
| 612 | |||
| 613 | // プラグインのmetadata定義を合わせて行う. |
||
| 614 | 1091 | $pluginConfigs = PluginConfigManager::getPluginConfigAll($this['debug']); |
|
| 615 | 1091 | $ormMappings = array(); |
|
| 616 | 1091 | $ormMappings[] = array( |
|
| 617 | 'type' => 'annotation', |
||
| 618 | 'namespace' => 'Eccube\Entity', |
||
| 619 | 'path' => array( |
||
| 620 | __DIR__.'/Entity' |
||
| 621 | ), |
||
| 622 | 'use_simple_annotation_reader' => false, |
||
| 623 | ); |
||
| 624 | |||
| 625 | // TODO namespace は暫定 |
||
| 626 | 1091 | $ormMappings[] = array( |
|
| 627 | 'type' => 'annotation', |
||
| 628 | 'namespace' => 'Acme\Entity', |
||
| 629 | 'path' => array( |
||
| 630 | __DIR__.'/../../app/Acme/Entity', |
||
| 631 | ), |
||
| 632 | 'use_simple_annotation_reader' => false, |
||
| 633 | ); |
||
| 634 | |||
| 635 | 1091 | foreach ($pluginConfigs as $code) { |
|
| 636 | 1091 | $config = $code['config']; |
|
| 637 | // Doctrine Extend |
||
| 638 | 1091 | if (isset($config['orm.path']) && is_array($config['orm.path'])) { |
|
| 639 | $paths = array(); |
||
| 640 | View Code Duplication | foreach ($config['orm.path'] as $path) { |
|
| 641 | $paths[] = $this['config']['plugin_realdir'].'/'.$config['code'].$path; |
||
| 642 | } |
||
| 643 | $ormMappings[] = array( |
||
| 644 | 'type' => 'yml', |
||
| 645 | 'namespace' => 'Plugin\\'.$config['code'].'\\Entity', |
||
| 646 | 'path' => $paths, |
||
| 647 | ); |
||
| 648 | $ormMappings[] = array( |
||
| 649 | 1091 | 'type' => 'annotation', |
|
| 650 | 'namespace' => 'Plugin\\'.$config['code'].'\\Entity', |
||
| 651 | 'path' => $paths, |
||
| 652 | 'use_simple_annotation_reader' => false, |
||
| 653 | ); |
||
| 654 | } |
||
| 655 | } |
||
| 656 | |||
| 657 | $options = array( |
||
| 658 | 1091 | 'mappings' => $ormMappings |
|
| 659 | ); |
||
| 660 | |||
| 661 | 1091 | if (!$this['debug']) { |
|
| 662 | $cacheDrivers = array(); |
||
| 663 | if (array_key_exists('doctrine_cache', $this['config'])) { |
||
| 664 | $cacheDrivers = $this['config']['doctrine_cache']; |
||
| 665 | } |
||
| 666 | |||
| 667 | if (array_key_exists('metadata_cache', $cacheDrivers)) { |
||
| 668 | $options['metadata_cache'] = $cacheDrivers['metadata_cache']; |
||
| 669 | } |
||
| 670 | if (array_key_exists('query_cache', $cacheDrivers)) { |
||
| 671 | $options['query_cache'] = $cacheDrivers['query_cache']; |
||
| 672 | } |
||
| 673 | if (array_key_exists('result_cache', $cacheDrivers)) { |
||
| 674 | $options['result_cache'] = $cacheDrivers['result_cache']; |
||
| 675 | } |
||
| 676 | if (array_key_exists('hydration_cache', $cacheDrivers)) { |
||
| 677 | $options['hydration_cache'] = $cacheDrivers['hydration_cache']; |
||
| 678 | } |
||
| 679 | } |
||
| 680 | |||
| 681 | 1091 | $this->register(new \Dflydev\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array( |
|
| 682 | 1091 | 'orm.proxies_dir' => __DIR__.'/../../app/cache/doctrine/proxies', |
|
| 683 | 1091 | 'orm.em.options' => $options, |
|
| 684 | 'orm.custom.functions.string' => array( |
||
| 685 | 'NORMALIZE' => 'Eccube\Doctrine\ORM\Query\Normalize', |
||
| 686 | ), |
||
| 687 | 'orm.custom.functions.numeric' => array( |
||
| 688 | 'EXTRACT' => 'Eccube\Doctrine\ORM\Query\Extract', |
||
| 689 | ), |
||
| 690 | )); |
||
| 691 | |||
| 692 | 1091 | $this->extend( |
|
| 693 | 1091 | 'orm.em.config', |
|
| 694 | function (\Doctrine\ORM\Configuration $config, \Silex\Application $app) { |
||
| 695 | |||
| 696 | /** @var $chain \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain */ |
||
| 697 | 1091 | $chain = $config->getMetadataDriverImpl(); |
|
| 698 | 1091 | $drivers = $chain->getDrivers(); |
|
| 699 | 1091 | foreach ($drivers as $namespace => $oldDriver) { |
|
| 700 | 1091 | if ('Eccube\Entity' === $namespace) { |
|
| 701 | 1091 | $newDriver = new AnnotationDriver( |
|
| 702 | 1091 | $app['annotations'], |
|
| 703 | 1091 | $oldDriver->getPaths()); |
|
| 704 | 1091 | $newDriver->setFileExtension($oldDriver->getFileExtension()); |
|
| 705 | 1091 | $newDriver->addExcludePaths($oldDriver->getExcludePaths()); |
|
| 706 | 1091 | $newDriver->setTraitProxiesDirectory( |
|
| 707 | 1091 | realpath(__DIR__.'/../../app/proxy/entity')); |
|
| 708 | 1091 | $chain->addDriver($newDriver, $namespace); |
|
| 709 | } |
||
| 710 | } |
||
| 711 | |||
| 712 | 1091 | return $config; |
|
| 713 | 1091 | } |
|
| 714 | ); |
||
| 715 | |||
| 716 | $this->extend('orm.em', function (\Doctrine\ORM\EntityManager $em, \Silex\Application $app) { |
||
| 717 | // save |
||
| 718 | 1091 | $saveEventSubscriber = new \Eccube\Doctrine\EventSubscriber\SaveEventSubscriber($app); |
|
| 719 | 1091 | $em->getEventManager()->addEventSubscriber($saveEventSubscriber); |
|
| 720 | |||
| 721 | // clear cache |
||
| 722 | 1091 | $clearCacheEventSubscriber = new \Eccube\Doctrine\EventSubscriber\ClearCacheEventSubscriber($app); |
|
| 723 | 1091 | $em->getEventManager()->addEventSubscriber($clearCacheEventSubscriber); |
|
| 724 | |||
| 725 | // filters |
||
| 726 | 1091 | $config = $em->getConfiguration(); |
|
| 727 | 1091 | $config->addFilter("nostock_hidden", '\Eccube\Doctrine\Filter\NoStockHiddenFilter'); |
|
| 728 | 1091 | $config->addFilter("incomplete_order_status_hidden", '\Eccube\Doctrine\Filter\OrderStatusFilter'); |
|
| 729 | |||
| 730 | 1091 | return $em; |
|
| 731 | 1091 | }); |
|
| 732 | |||
| 733 | 1091 | if (!$this['debug']) { |
|
| 734 | // second level cacheの設定. |
||
| 735 | $this->extend( |
||
| 736 | 'orm.em.config', |
||
| 737 | function (\Doctrine\ORM\Configuration $config, \Silex\Application $app) { |
||
| 738 | $config->setSecondLevelCacheEnabled(); |
||
| 739 | $cacheConfig = $config->getSecondLevelCacheConfiguration(); |
||
| 740 | $regionConfig = $cacheConfig->getRegionsConfiguration(); |
||
| 741 | // TODO キャッシュ先は設定で切り替えられるように |
||
| 742 | $cache = $this['orm.cache.factory']( |
||
| 743 | 'filesystem', |
||
| 744 | [ |
||
| 745 | 'path' => __DIR__.'/../../app/cache/doctrine/second' |
||
| 746 | ] |
||
| 747 | ); |
||
| 748 | $factory = new \Doctrine\ORM\Cache\DefaultCacheFactory($regionConfig, $cache); |
||
| 749 | $cacheConfig->setCacheFactory($factory); |
||
| 750 | |||
| 751 | return $config; |
||
| 752 | } |
||
| 753 | ); |
||
| 754 | } |
||
| 755 | } |
||
| 756 | |||
| 757 | 1091 | public function initSecurity() |
|
| 856 | |||
| 857 | /** |
||
| 858 | * ロードバランサー、プロキシサーバの設定を行う |
||
| 859 | */ |
||
| 860 | 1091 | public function initProxy() |
|
| 872 | |||
| 873 | 1089 | public function initializePlugin() |
|
| 888 | |||
| 889 | /** |
||
| 890 | * PHPUnit を実行中かどうかを設定する. |
||
| 891 | * |
||
| 892 | * @param boolean $testMode PHPUnit を実行中の場合 true |
||
| 893 | */ |
||
| 894 | 1081 | public function setTestMode($testMode) |
|
| 898 | |||
| 899 | /** |
||
| 900 | * PHPUnit を実行中かどうか. |
||
| 901 | * |
||
| 902 | * @return boolean PHPUnit を実行中の場合 true |
||
| 903 | */ |
||
| 904 | 315 | public function isTestMode() |
|
| 908 | |||
| 909 | /** |
||
| 910 | * |
||
| 911 | * データベースの接続を確認 |
||
| 912 | * 成功 : trueを返却 |
||
| 913 | * 失敗 : \Doctrine\DBAL\DBALExceptionエラーが発生( 接続に失敗した場合 )、エラー画面を表示しdie() |
||
| 914 | * 備考 : app['debug']がtrueの際は処理を行わない |
||
| 915 | * |
||
| 916 | * @return boolean true |
||
| 917 | * |
||
| 918 | */ |
||
| 919 | 1091 | protected function checkDatabaseConnection() |
|
| 943 | |||
| 944 | /** |
||
| 945 | * Config ファイルをパースし、連想配列を返します. |
||
| 946 | * |
||
| 947 | * $config_name.yml ファイルをパースし、連想配列を返します. |
||
| 948 | * $config_name.php が存在する場合は、 PHP ファイルに記述された連想配列を使用します。 |
||
| 949 | * |
||
| 950 | * @param string $config_name Config 名称 |
||
| 951 | * @param array $configAll Config の連想配列 |
||
| 952 | * @param boolean $wrap_key Config の連想配列に config_name のキーを生成する場合 true, デフォルト false |
||
| 953 | * @param string $ymlPath config yaml を格納したディレクトリ |
||
| 954 | * @param string $distPath config yaml dist を格納したディレクトリ |
||
| 955 | * @return Application |
||
| 956 | */ |
||
| 957 | 1092 | public function parseConfig($config_name, array &$configAll, $wrap_key = false, $ymlPath = null, $distPath = null) |
|
| 985 | |||
| 986 | /** |
||
| 987 | * セッションが開始されているかどうか. |
||
| 988 | * |
||
| 989 | * @return boolean セッションが開始済みの場合 true |
||
| 990 | * @link http://php.net/manual/ja/function.session-status.php#113468 |
||
| 991 | */ |
||
| 992 | 1091 | protected function isSessionStarted() |
|
| 1004 | |||
| 1005 | /** |
||
| 1006 | * Http Cache対応 |
||
| 1007 | */ |
||
| 1008 | 1091 | protected function initCacheRequest() |
|
| 1071 | } |
||
| 1072 |