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