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 |
||
| 35 | class Application extends ApplicationTrait |
||
|
|
|||
| 36 | { |
||
| 37 | protected static $instance; |
||
| 38 | |||
| 39 | protected $initialized = false; |
||
| 40 | protected $initializedPlugin = false; |
||
| 41 | |||
| 42 | 420 | public static function getInstance(array $values = array()) |
|
| 43 | { |
||
| 44 | if (!is_object(self::$instance)) { |
||
| 45 | self::$instance = new Application($values); |
||
| 46 | } |
||
| 47 | |||
| 48 | 420 | return self::$instance; |
|
| 49 | 420 | } |
|
| 50 | |||
| 51 | 760 | public static function clearInstance() |
|
| 52 | { |
||
| 53 | 760 | self::$instance = null; |
|
| 54 | 760 | } |
|
| 55 | |||
| 56 | final public function __clone() |
||
| 57 | { |
||
| 58 | throw new \Exception('Clone is not allowed against '.get_class($this)); |
||
| 59 | } |
||
| 60 | |||
| 61 | 760 | public function __construct(array $values = array()) |
|
| 62 | { |
||
| 63 | parent::__construct($values); |
||
| 64 | |||
| 65 | if (is_null(self::$instance)) { |
||
| 66 | 760 | self::$instance = $this; |
|
| 67 | } |
||
| 68 | |||
| 69 | // load config |
||
| 70 | $this->initConfig(); |
||
| 71 | |||
| 72 | // init monolog |
||
| 73 | $this->initLogger(); |
||
| 74 | } |
||
| 75 | |||
| 76 | 774 | public function initConfig() |
|
| 77 | { |
||
| 78 | // load config |
||
| 79 | $this['config'] = $this->share(function() { |
||
| 80 | 767 | $ymlPath = __DIR__.'/../../app/config/eccube'; |
|
| 81 | 767 | $distPath = __DIR__.'/../../src/Eccube/Resource/config'; |
|
| 82 | |||
| 83 | 767 | $config = array(); |
|
| 84 | 767 | $config_yml = $ymlPath.'/config.yml'; |
|
| 85 | if (file_exists($config_yml)) { |
||
| 86 | $config = Yaml::parse(file_get_contents($config_yml)); |
||
| 87 | } |
||
| 88 | |||
| 89 | 767 | $config_dist = array(); |
|
| 90 | 767 | $config_yml_dist = $distPath.'/config.yml.dist'; |
|
| 91 | if (file_exists($config_yml_dist)) { |
||
| 92 | $config_dist = Yaml::parse(file_get_contents($config_yml_dist)); |
||
| 93 | } |
||
| 94 | |||
| 95 | 767 | $config_path = array(); |
|
| 96 | 767 | $path_yml = $ymlPath.'/path.yml'; |
|
| 97 | if (file_exists($path_yml)) { |
||
| 98 | $config_path = Yaml::parse(file_get_contents($path_yml)); |
||
| 99 | } |
||
| 100 | |||
| 101 | 767 | $config_constant = array(); |
|
| 102 | 767 | $constant_yml = $ymlPath.'/constant.yml'; |
|
| 103 | if (file_exists($constant_yml)) { |
||
| 104 | $config_constant = Yaml::parse(file_get_contents($constant_yml)); |
||
| 105 | $config_constant = empty($config_constant) ? array() : $config_constant; |
||
| 106 | } |
||
| 107 | |||
| 108 | 767 | $config_constant_dist = array(); |
|
| 109 | 767 | $constant_yml_dist = $distPath.'/constant.yml.dist'; |
|
| 110 | if (file_exists($constant_yml_dist)) { |
||
| 111 | $config_constant_dist = Yaml::parse(file_get_contents($constant_yml_dist)); |
||
| 112 | } |
||
| 113 | |||
| 114 | $configAll = array_replace_recursive($config_constant_dist, $config_dist, $config_constant, $config_path, $config); |
||
| 115 | |||
| 116 | 767 | $database = array(); |
|
| 117 | 767 | $yml = $ymlPath.'/database.yml'; |
|
| 118 | if (file_exists($yml)) { |
||
| 119 | $database = Yaml::parse(file_get_contents($yml)); |
||
| 120 | } |
||
| 121 | |||
| 122 | 767 | $mail = array(); |
|
| 123 | 767 | $yml = $ymlPath.'/mail.yml'; |
|
| 124 | if (file_exists($yml)) { |
||
| 125 | $mail = Yaml::parse(file_get_contents($yml)); |
||
| 126 | } |
||
| 127 | $configAll = array_replace_recursive($configAll, $database, $mail); |
||
| 128 | |||
| 129 | 767 | $config_log = array(); |
|
| 130 | 767 | $yml = $ymlPath.'/log.yml'; |
|
| 131 | if (file_exists($yml)) { |
||
| 132 | $config_log = Yaml::parse(file_get_contents($yml)); |
||
| 133 | } |
||
| 134 | 767 | $config_log_dist = array(); |
|
| 135 | 767 | $log_yml_dist = $distPath.'/log.yml.dist'; |
|
| 136 | if (file_exists($log_yml_dist)) { |
||
| 137 | $config_log_dist = Yaml::parse(file_get_contents($log_yml_dist)); |
||
| 138 | } |
||
| 139 | |||
| 140 | $configAll = array_replace_recursive($configAll, $config_log_dist, $config_log); |
||
| 141 | |||
| 142 | 767 | $config_nav = array(); |
|
| 143 | 767 | $yml = $ymlPath.'/nav.yml'; |
|
| 144 | if (file_exists($yml)) { |
||
| 145 | $config_nav = array('nav' => Yaml::parse(file_get_contents($yml))); |
||
| 146 | } |
||
| 147 | 767 | $config_nav_dist = array(); |
|
| 148 | 767 | $nav_yml_dist = $distPath.'/nav.yml.dist'; |
|
| 149 | if (file_exists($nav_yml_dist)) { |
||
| 150 | $config_nav_dist = array('nav' => Yaml::parse(file_get_contents($nav_yml_dist))); |
||
| 151 | } |
||
| 152 | |||
| 153 | $configAll = array_replace_recursive($configAll, $config_nav_dist, $config_nav); |
||
| 154 | |||
| 155 | 767 | return $configAll; |
|
| 156 | }); |
||
| 157 | 774 | } |
|
| 158 | |||
| 159 | 774 | public function initLogger() |
|
| 160 | { |
||
| 161 | 774 | $app = $this; |
|
| 162 | $this->register(new ServiceProvider\EccubeMonologServiceProvider($app)); |
||
| 163 | $this['monolog.logfile'] = __DIR__.'/../../app/log/site.log'; |
||
| 164 | $this['monolog.name'] = 'eccube'; |
||
| 165 | 774 | } |
|
| 166 | |||
| 167 | 764 | public function initialize() |
|
| 168 | { |
||
| 169 | 764 | if ($this->initialized) { |
|
| 170 | return; |
||
| 171 | } |
||
| 172 | |||
| 173 | // init locale |
||
| 174 | $this->initLocale(); |
||
| 175 | |||
| 176 | // init session |
||
| 177 | $this->initSession(); |
||
| 178 | |||
| 179 | // init twig |
||
| 180 | $this->initRendering(); |
||
| 181 | |||
| 182 | // init provider |
||
| 183 | $this->register(new \Silex\Provider\HttpFragmentServiceProvider()); |
||
| 184 | $this->register(new \Silex\Provider\UrlGeneratorServiceProvider()); |
||
| 185 | $this->register(new \Silex\Provider\FormServiceProvider()); |
||
| 186 | $this->register(new \Silex\Provider\SerializerServiceProvider()); |
||
| 187 | $this->register(new \Eccube\ServiceProvider\ValidatorServiceProvider()); |
||
| 188 | |||
| 189 | 764 | $app = $this; |
|
| 190 | $this->error(function(\Exception $e, $code) use ($app) { |
||
| 191 | if ($app['debug']) { |
||
| 192 | 6 | return; |
|
| 193 | } |
||
| 194 | |||
| 195 | switch ($code) { |
||
| 196 | case 403: |
||
| 197 | $title = 'アクセスできません。'; |
||
| 198 | $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。'; |
||
| 199 | break; |
||
| 200 | case 404: |
||
| 201 | $title = 'ページがみつかりません。'; |
||
| 202 | $message = 'URLに間違いがないかご確認ください。'; |
||
| 203 | break; |
||
| 204 | default: |
||
| 205 | $title = 'システムエラーが発生しました。'; |
||
| 206 | $message = '大変お手数ですが、サイト管理者までご連絡ください。'; |
||
| 207 | break; |
||
| 208 | } |
||
| 209 | |||
| 210 | return $app->render('error.twig', array( |
||
| 211 | 'error_title' => $title, |
||
| 212 | 'error_message' => $message, |
||
| 213 | )); |
||
| 214 | }); |
||
| 215 | |||
| 216 | // init mailer |
||
| 217 | $this->initMailer(); |
||
| 218 | |||
| 219 | // init doctrine orm |
||
| 220 | $this->initDoctrine(); |
||
| 221 | |||
| 222 | // Set up the DBAL connection now to check for a proper connection to the database. |
||
| 223 | $this->checkDatabaseConnection(); |
||
| 224 | |||
| 225 | // init security |
||
| 226 | $this->initSecurity(); |
||
| 227 | |||
| 228 | // init ec-cube service provider |
||
| 229 | $this->register(new ServiceProvider\EccubeServiceProvider()); |
||
| 230 | |||
| 231 | // mount controllers |
||
| 232 | $this->register(new \Silex\Provider\ServiceControllerServiceProvider()); |
||
| 233 | $this->mount('', new ControllerProvider\FrontControllerProvider()); |
||
| 234 | $this->mount('/'.trim($this['config']['admin_route'], '/').'/', new ControllerProvider\AdminControllerProvider()); |
||
| 235 | Request::enableHttpMethodParameterOverride(); // PUTやDELETEできるようにする |
||
| 236 | |||
| 237 | 764 | $this->initialized = true; |
|
| 238 | 764 | } |
|
| 239 | |||
| 240 | 764 | public function initLocale() |
|
| 273 | |||
| 274 | 764 | public function initSession() |
|
| 275 | { |
||
| 276 | $this->register(new \Silex\Provider\SessionServiceProvider(), array( |
||
| 277 | 'session.storage.save_path' => $this['config']['root_dir'].'/app/cache/eccube/session', |
||
| 278 | 'session.storage.options' => array( |
||
| 279 | 764 | 'name' => 'eccube', |
|
| 280 | 'cookie_path' => $this['config']['root_urlpath'] ?: '/', |
||
| 281 | 764 | 'cookie_secure' => $this['config']['force_ssl'], |
|
| 282 | 764 | 'cookie_lifetime' => $this['config']['cookie_lifetime'], |
|
| 283 | 764 | 'cookie_httponly' => true, |
|
| 284 | // cookie_domainは指定しない |
||
| 285 | // http://blog.tokumaru.org/2011/10/cookiedomain.html |
||
| 286 | 764 | ), |
|
| 287 | )); |
||
| 288 | 764 | } |
|
| 289 | |||
| 290 | 764 | public function initRendering() |
|
| 291 | { |
||
| 292 | $this->register(new \Silex\Provider\TwigServiceProvider(), array( |
||
| 293 | 'twig.form.templates' => array('Form/form_layout.twig'), |
||
| 294 | )); |
||
| 295 | $this['twig'] = $this->share($this->extend('twig', function(\Twig_Environment $twig, \Silex\Application $app) { |
||
| 296 | $twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app)); |
||
| 297 | $twig->addExtension(new \Twig_Extension_StringLoader()); |
||
| 298 | |||
| 299 | 198 | return $twig; |
|
| 300 | })); |
||
| 301 | |||
| 302 | $this->before(function(Request $request, \Silex\Application $app) { |
||
| 303 | // フロント or 管理画面ごとにtwigの探索パスを切り替える. |
||
| 304 | $app['twig'] = $app->share($app->extend('twig', function(\Twig_Environment $twig, \Silex\Application $app) { |
||
| 305 | 160 | $paths = array(); |
|
| 306 | |||
| 307 | // 互換性がないのでprofiler とproduction 時のcacheを分離する |
||
| 308 | |||
| 309 | $app['admin'] = false; |
||
| 310 | $app['front'] = false; |
||
| 311 | |||
| 312 | if (isset($app['profiler'])) { |
||
| 313 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/profiler/'; |
||
| 314 | } else { |
||
| 315 | 160 | $cacheBaseDir = __DIR__.'/../../app/cache/twig/production/'; |
|
| 316 | } |
||
| 317 | if (strpos($app['request']->getPathInfo(), '/'.trim($app['config']['admin_route'], '/')) === 0) { |
||
| 318 | if (file_exists(__DIR__.'/../../app/template/admin')) { |
||
| 319 | 92 | $paths[] = __DIR__.'/../../app/template/admin'; |
|
| 320 | } |
||
| 321 | $paths[] = $app['config']['template_admin_realdir']; |
||
| 322 | 92 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 323 | 92 | $cache = $cacheBaseDir.'admin'; |
|
| 324 | $app['admin'] = true; |
||
| 325 | } else { |
||
| 326 | if (file_exists($app['config']['template_realdir'])) { |
||
| 327 | $paths[] = $app['config']['template_realdir']; |
||
| 328 | } |
||
| 329 | $paths[] = $app['config']['template_default_realdir']; |
||
| 330 | 68 | $paths[] = __DIR__.'/../../app/Plugin'; |
|
| 331 | $cache = $cacheBaseDir.$app['config']['template_code']; |
||
| 332 | $app['front'] = true; |
||
| 333 | 92 | } |
|
| 334 | $twig->setCache($cache); |
||
| 335 | $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths)); |
||
| 336 | |||
| 337 | 160 | return $twig; |
|
| 338 | })); |
||
| 339 | |||
| 340 | // 管理画面のIP制限チェック. |
||
| 341 | if (strpos($app['request']->getPathInfo(), '/'.trim($app['config']['admin_route'], '/')) === 0) { |
||
| 342 | // IP制限チェック |
||
| 343 | $allowHost = $app['config']['admin_allow_host']; |
||
| 344 | if (count($allowHost) > 0) { |
||
| 345 | if (array_search($app['request']->getClientIp(), $allowHost) === false) { |
||
| 346 | throw new \Exception(); |
||
| 347 | } |
||
| 348 | } |
||
| 349 | } |
||
| 350 | }, self::EARLY_EVENT); |
||
| 351 | |||
| 352 | // twigのグローバル変数を定義. |
||
| 353 | 764 | $app = $this; |
|
| 354 | $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function(\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) { |
||
| 355 | // ショップ基本情報 |
||
| 356 | $BaseInfo = $app['eccube.repository.base_info']->get(); |
||
| 357 | $app['twig']->addGlobal('BaseInfo', $BaseInfo); |
||
| 358 | |||
| 359 | if (strpos($app['request']->getPathInfo(), '/'.trim($app['config']['admin_route'], '/')) === 0) { |
||
| 360 | // 管理画面 |
||
| 361 | // 管理画面メニュー |
||
| 362 | 92 | $menus = array('', '', ''); |
|
| 363 | $app['twig']->addGlobal('menus', $menus); |
||
| 364 | |||
| 365 | $Member = $app->user(); |
||
| 366 | if (is_object($Member)) { |
||
| 367 | // ログインしていれば管理者のロールを取得 |
||
| 368 | $AuthorityRoles = $app['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority())); |
||
| 369 | |||
| 370 | 89 | $roles = array(); |
|
| 371 | foreach ($AuthorityRoles as $AuthorityRole) { |
||
| 372 | // 管理画面でメニュー制御するため相対パス全てをセット |
||
| 373 | $roles[] = $app['request']->getBaseUrl().'/'.$app['config']['admin_route'].$AuthorityRole->getDenyUrl(); |
||
| 374 | 89 | } |
|
| 375 | |||
| 376 | $app['twig']->addGlobal('AuthorityRoles', $roles); |
||
| 377 | } |
||
| 378 | |||
| 379 | } else { |
||
| 380 | // フロント画面 |
||
| 381 | $request = $event->getRequest(); |
||
| 382 | $route = $request->attributes->get('_route'); |
||
| 383 | |||
| 384 | // ユーザ作成画面 |
||
| 385 | if ($route === trim($app['config']['user_data_route'])) { |
||
| 386 | $params = $request->attributes->get('_route_params'); |
||
| 387 | 2 | $route = $params['route']; |
|
| 388 | // プレビュー画面 |
||
| 389 | } elseif ($request->get('preview')) { |
||
| 390 | $route = 'preview'; |
||
| 391 | 2 | } |
|
| 392 | |||
| 393 | try { |
||
| 394 | $DeviceType = $app['eccube.repository.master.device_type'] |
||
| 395 | ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC); |
||
| 396 | $PageLayout = $app['eccube.repository.page_layout']->getByUrl($DeviceType, $route); |
||
| 397 | } catch (\Doctrine\ORM\NoResultException $e) { |
||
| 398 | $PageLayout = $app['eccube.repository.page_layout']->newPageLayout($DeviceType); |
||
| 399 | 34 | } |
|
| 400 | |||
| 401 | $app['twig']->addGlobal('PageLayout', $PageLayout); |
||
| 402 | $app['twig']->addGlobal('title', $PageLayout->getName()); |
||
| 403 | 92 | } |
|
| 404 | }); |
||
| 405 | 764 | } |
|
| 406 | |||
| 407 | 764 | public function initMailer() |
|
| 436 | |||
| 437 | 764 | public function initDoctrine() |
|
| 438 | { |
||
| 439 | $this->register(new \Silex\Provider\DoctrineServiceProvider(), array( |
||
| 440 | 'dbs.options' => array( |
||
| 441 | 764 | 'default' => $this['config']['database'] |
|
| 442 | ))); |
||
|
1 ignored issue
–
show
|
|||
| 443 | $this->register(new \Saxulum\DoctrineOrmManagerRegistry\Silex\Provider\DoctrineOrmManagerRegistryProvider()); |
||
| 444 | |||
| 495 | |||
| 496 | public function initSecurity() |
||
| 596 | 4 | ||
| 597 | public function initializePlugin() |
||
| 611 | |||
| 612 | public function initPluginEventDispatcher() |
||
| 797 | |||
| 798 | 4 | public function loadPlugin() |
|
| 908 | |||
| 909 | /** |
||
| 910 | * |
||
| 911 | * データベースの接続を確認 |
||
| 912 | * 成功 : trueを返却 |
||
| 913 | * 失敗 : \Doctrine\DBAL\DBALExceptionエラーが発生( 接続に失敗した場合 )、エラー画面を表示しdie() |
||
| 914 | * 備考 : app['debug']がtrueの際は処理を行わない |
||
| 915 | * @return boolean true |
||
| 916 | * |
||
| 917 | */ |
||
| 918 | 4 | protected function checkDatabaseConnection() |
|
| 941 | } |
||
| 942 |