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 |
||
62 | class Application extends \Silex\Application |
||
|
|||
63 | { |
||
64 | use \Silex\Application\FormTrait; |
||
65 | use \Silex\Application\UrlGeneratorTrait; |
||
66 | use \Silex\Application\MonologTrait; |
||
67 | use \Silex\Application\SwiftmailerTrait; |
||
68 | use \Silex\Application\SecurityTrait; |
||
69 | use \Silex\Application\TranslationTrait; |
||
70 | use \Eccube\Application\ApplicationTrait; |
||
71 | use \Eccube\Application\SecurityTrait; |
||
72 | use \Eccube\Application\TwigTrait; |
||
73 | 1069 | ||
74 | protected static $instance; |
||
75 | 1069 | ||
76 | 1069 | protected $initialized = false; |
|
77 | protected $initializedPlugin = false; |
||
78 | protected $testMode = false; |
||
79 | 1069 | ||
80 | public static function getInstance(array $values = array()) |
||
88 | |||
89 | public static function clearInstance() |
||
93 | |||
94 | 1070 | final public function __clone() |
|
98 | |||
99 | public function __construct(array $values = array()) |
||
100 | { |
||
101 | 1070 | parent::__construct($values); |
|
102 | |||
103 | if (is_null(self::$instance)) { |
||
104 | 1070 | self::$instance = $this; |
|
105 | } |
||
106 | |||
107 | // load config |
||
108 | $this->initConfig(); |
||
109 | |||
110 | // init monolog |
||
111 | $this->initLogger(); |
||
112 | 1069 | ||
113 | // init class loader. |
||
114 | 1069 | $this->initClassLoader(); |
|
115 | |||
116 | // init request. |
||
117 | 1070 | $this->initRequest(); |
|
118 | } |
||
119 | |||
120 | 1070 | /** |
|
121 | 1070 | * Application::runが実行されているか親クラスのプロパティから判定 |
|
122 | 1070 | * |
|
123 | * @return bool |
||
124 | */ |
||
125 | public function isBooted() |
||
126 | { |
||
127 | 1070 | return $this->booted; |
|
128 | 1070 | } |
|
129 | 1070 | ||
130 | 1070 | public function initConfig() |
|
131 | 1070 | { |
|
132 | 1070 | // load .env |
|
133 | 1070 | $envFile = __DIR__.'/../../.env'; |
|
134 | 1070 | if (file_exists($envFile)) { |
|
135 | 1070 | (new Dotenv())->load($envFile); |
|
136 | 1070 | } |
|
137 | 1070 | ||
138 | // load config |
||
139 | 1070 | $this['config'] = function() { |
|
140 | $configAll = array(); |
||
141 | $this->parseConfig('constant', $configAll) |
||
142 | ->parseConfig('path', $configAll) |
||
143 | 1070 | ->parseConfig('config', $configAll) |
|
144 | ->parseConfig('database', $configAll) |
||
145 | 1070 | ->parseConfig('mail', $configAll) |
|
146 | 1070 | ->parseConfig('log', $configAll) |
|
147 | ->parseConfig('nav', $configAll, true) |
||
148 | ->parseConfig('doctrine_cache', $configAll) |
||
149 | 1069 | ->parseConfig('http_cache', $configAll) |
|
150 | ->parseConfig('session_handler', $configAll); |
||
151 | 1069 | ||
152 | 2 | return $configAll; |
|
153 | }; |
||
154 | } |
||
155 | |||
156 | 1069 | public function initLogger() |
|
157 | { |
||
158 | $app = $this; |
||
159 | 1069 | $this->register(new ServiceProvider\LogServiceProvider($app)); |
|
160 | 1069 | } |
|
161 | |||
162 | public function initClassLoader() |
||
163 | { |
||
164 | 1069 | if (!isset($this['config']['vendor_name'])) { |
|
165 | $this['logger']->log('config.vendor_name is not set.'); |
||
166 | |||
167 | 1069 | return; |
|
168 | 1069 | } |
|
169 | |||
170 | 1069 | $name = $this['config']['vendor_name']; |
|
171 | 1069 | $dir = $this['config']['root_dir'].'/app/'.$name; |
|
172 | 1069 | ||
173 | 1069 | if (false === file_exists($dir)) { |
|
174 | 1069 | $this['logger']->log(sprintf('%s is not exists.', $dir)); |
|
175 | 1069 | ||
176 | 1069 | return; |
|
177 | } |
||
178 | |||
179 | 25 | $path = realpath($dir); |
|
180 | 25 | ||
181 | $loader = $this['eccube.autoloader']; |
||
182 | $loader->addPsr4($name.'\\', $path); |
||
183 | |||
184 | $config = $this['config']; |
||
185 | $config['vendor_dir'] = $path; |
||
186 | $this->overwrite('config', $config); |
||
187 | } |
||
188 | |||
189 | public function initialize() |
||
190 | { |
||
191 | if ($this->initialized) { |
||
192 | return; |
||
193 | } |
||
194 | |||
195 | // init locale |
||
196 | $this->initLocale(); |
||
197 | |||
198 | // init session |
||
199 | if (!$this->isSessionStarted()) { |
||
200 | $this->initSession(); |
||
201 | } |
||
202 | 1069 | ||
203 | // init twig |
||
204 | $this->initRendering(); |
||
205 | 1069 | ||
206 | // init provider |
||
207 | 1069 | $this->register(new \Silex\Provider\HttpCacheServiceProvider(), array( |
|
208 | 1069 | 'http_cache.cache_dir' => __DIR__.'/../../app/cache/http/', |
|
209 | 1069 | )); |
|
210 | $this->register(new \Silex\Provider\HttpFragmentServiceProvider()); |
||
211 | 1069 | $this->register(new \Silex\Provider\FormServiceProvider()); |
|
212 | 1069 | $this->register(new \Silex\Provider\SerializerServiceProvider()); |
|
213 | $this->register(new \Silex\Provider\ValidatorServiceProvider()); |
||
214 | $this->register(new \Silex\Provider\AssetServiceProvider(), [ |
||
215 | // default package settings |
||
216 | 'assets.version' => Constant::VERSION, |
||
217 | 1069 | 'assets.version_format' => '%s?v=%s', |
|
218 | 'assets.base_path' => '/html/template/'.$this['config']['template_code'], |
||
219 | // additional packages settings |
||
220 | 1069 | 'assets.named_packages' => [ |
|
221 | 'admin' => [ |
||
222 | 'version' => Constant::VERSION, |
||
223 | 1069 | 'version_format' => '%s?v=%s', |
|
224 | 'base_path' => '/html/template/admin', |
||
225 | 1069 | ], |
|
226 | 1069 | 'save_image' => ['base_path' => '/html/upload/save_image'], |
|
227 | 'temp_image' => ['base_path' => '/html/upload/temp_image'], |
||
228 | 1069 | 'user_data' => ['base_path' => '/html/'.$this['config']['user_data_route']], |
|
229 | 1069 | 'plugin' => ['base_path' => '/html/plugin'], |
|
230 | 1069 | ], |
|
231 | ]); |
||
232 | |||
233 | $this->register(new \Saxulum\Validator\Provider\SaxulumValidatorProvider()); |
||
234 | $this->register(new MobileDetectServiceProvider()); |
||
235 | 1069 | $this->register(new TwigLintServiceProvider()); |
|
236 | |||
237 | 1069 | $this->error(function (\Exception $e, Request $request, $code) { |
|
238 | 1069 | if ($this['debug']) { |
|
239 | return; |
||
240 | } |
||
241 | 1069 | ||
242 | switch ($code) { |
||
243 | case 403: |
||
244 | $title = 'アクセスできません。'; |
||
245 | $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。'; |
||
246 | 1069 | break; |
|
247 | 1069 | case 404: |
|
248 | $title = 'ページがみつかりません。'; |
||
249 | $message = 'URLに間違いがないかご確認ください。'; |
||
250 | 1069 | break; |
|
251 | default: |
||
252 | 1069 | $title = 'システムエラーが発生しました。'; |
|
253 | 1069 | $message = '大変お手数ですが、サイト管理者までご連絡ください。'; |
|
254 | 1069 | break; |
|
255 | 1069 | } |
|
256 | 1069 | ||
257 | 1069 | return $this->render('error.twig', array( |
|
258 | 1069 | 'error_title' => $title, |
|
259 | 1069 | 'error_message' => $message, |
|
260 | 1069 | )); |
|
261 | 1069 | }); |
|
262 | 1069 | ||
263 | 1069 | // init mailer |
|
264 | 1069 | $this->initMailer(); |
|
265 | 1069 | ||
266 | 1069 | $this->register(new \Sergiors\Silex\Provider\DoctrineCacheServiceProvider()); |
|
267 | 1069 | $this->register(new \Sergiors\Silex\Provider\AnnotationsServiceProvider(), [ |
|
268 | 1069 | 'annotations.debug' => $this['debug'], |
|
269 | 1069 | 'annotations.options' => [ |
|
270 | 1069 | 'cache_driver' => $this['debug'] ? 'array' : 'filesystem', |
|
271 | 1069 | 'cache_dir' => $this['debug'] ? null : __DIR__.'/../../app/cache/annotation' |
|
272 | 1069 | ] |
|
273 | 1069 | ]); |
|
274 | |||
275 | 1069 | // init doctrine orm |
|
276 | $this->initDoctrine(); |
||
277 | |||
278 | 1069 | // Set up the DBAL connection now to check for a proper connection to the database. |
|
279 | 1069 | $this->checkDatabaseConnection(); |
|
280 | 1069 | ||
281 | // init security |
||
282 | 1069 | $this->initSecurity(); |
|
283 | 1069 | ||
284 | $this->register(new \Sergiors\Silex\Provider\RoutingServiceProvider(), [ |
||
285 | 'routing.cache_dir' => $this['debug'] ? null : __DIR__.'/../../app/cache/routing' |
||
286 | ]); |
||
287 | 1069 | $this->register(new \Sergiors\Silex\Provider\TemplatingServiceProvider()); |
|
288 | $this->register(new \Sergiors\Silex\Provider\SensioFrameworkExtraServiceProvider(), [ |
||
289 | 'request' => [ |
||
290 | 1069 | 'auto_convert' => true |
|
291 | 1069 | ] |
|
292 | 1069 | ]); |
|
293 | 1069 | ||
294 | 1069 | $enabledPlugins = $this['orm.em']->getRepository('Eccube\Entity\Plugin')->findAllEnabled(); |
|
295 | 1069 | $configRootDir = $this['config']['root_dir']; |
|
296 | $enabledPluginDirs = array_map(function($plugin) use ($configRootDir) { |
||
297 | 1069 | return $configRootDir.'/app/Plugin/'.$plugin->getCode(); |
|
298 | 1069 | }, $enabledPlugins); |
|
299 | 1069 | ||
300 | 1069 | $pluginSubDirs = (function($dirName) use ($enabledPluginDirs) { |
|
301 | 1069 | return array_map(function($pluginDir) use ($dirName) { |
|
302 | 1069 | return $pluginDir . '/' . $dirName; |
|
303 | }, $enabledPluginDirs); |
||
304 | }); |
||
305 | 1069 | ||
306 | 1069 | // init ec-cube service provider |
|
307 | 1069 | $this->register(new DIServiceProvider(), [ |
|
308 | 'eccube.di.wirings' => [ |
||
309 | 1069 | new RouteAutoWiring(array_merge([ |
|
310 | 1069 | $this['config']['vendor_dir'].'/Controller', |
|
311 | $this['config']['root_dir'].'/src/Eccube/Controller', |
||
312 | ], $pluginSubDirs('Controller'))), |
||
313 | 1069 | new FormTypeAutoWiring(array_merge([ |
|
314 | 1069 | $this['config']['vendor_dir'].'/Form/Type', |
|
315 | $this['config']['root_dir'].'/src/Eccube/Form/Type' |
||
316 | 1069 | ], $pluginSubDirs('Form/Type'))), |
|
317 | new FormExtensionAutoWiring(array_merge([ |
||
318 | $this['config']['vendor_dir'].'/Form/Extension', |
||
319 | 1069 | $this['config']['root_dir'].'/src/Eccube/Form/Extension' |
|
320 | ], $pluginSubDirs('Form/Extension'))), |
||
321 | new ServiceAutoWiring(array_merge([ |
||
322 | $this['config']['vendor_dir'].'/Service', |
||
323 | 1069 | $this['config']['root_dir'].'/src/Eccube/Service' |
|
324 | 1069 | ], $pluginSubDirs('Service'))), |
|
325 | new RepositoryAutoWiring(array_merge([ |
||
326 | 1069 | $this['config']['vendor_dir'].'/Repository', |
|
327 | $this['config']['root_dir'].'/src/Eccube/Repository' |
||
328 | 1069 | ], $pluginSubDirs('Repository'))), |
|
329 | new QueryExtensionAutoWiring(array_merge([ |
||
330 | $this['config']['vendor_dir'].'/Repository' |
||
331 | ], $pluginSubDirs('Repository'))), |
||
332 | 1069 | new EntityEventAutowiring(array_merge([ |
|
333 | 1069 | $this['config']['vendor_dir'].'/Entity' |
|
334 | 1069 | ], $pluginSubDirs('Entity'))) |
|
335 | ], |
||
336 | 'eccube.di.generator.dir' => $this['config']['root_dir'].'/app/cache/provider' |
||
337 | 1069 | ]); |
|
338 | 1069 | ||
339 | $this->register(new CompatRepositoryProvider()); |
||
340 | 1069 | $this->register(new CompatServiceProvider()); |
|
341 | 1069 | $this->register(new ServiceProvider\EccubeServiceProvider()); |
|
342 | $this->register(new PagenatorServiceProvider()); |
||
343 | $this->register(new PaymentServiceProvider()); |
||
344 | 1069 | $this->register(new PurchaseFlowServiceProvider()); |
|
345 | 1069 | $this->register(new ForwardOnlyServiceProvider()); |
|
346 | 1069 | $this->register(new TransactionServiceProvider()); |
|
347 | $this->register(new QueriesServiceProvider()); |
||
348 | |||
349 | 1069 | $this->register(new \Silex\Provider\ServiceControllerServiceProvider()); |
|
350 | 1069 | ||
351 | // ルーティングの設定 |
||
352 | 1069 | // TODO EccubeRoutingServiceProviderに移植する. |
|
353 | 1069 | $app = $this; |
|
354 | $this['eccube.router'] = $this->protect(function($resoure, $cachePrefix) use ($app) { |
||
355 | $options = [ |
||
356 | 'debug' => $app['debug'], |
||
357 | 1069 | 'cache_dir' => $app['routing.cache_dir'], |
|
358 | 1069 | 'matcher_base_class' => $app['request_matcher_class'], |
|
359 | 'matcher_class' => $app['request_matcher_class'], |
||
360 | 'matcher_cache_class' => $cachePrefix.'UrlMatcher', |
||
361 | 1069 | 'generator_cache_class' => $cachePrefix.'UrlGenerator' |
|
362 | ]; |
||
363 | 1069 | $router = new EccubeRouter( |
|
364 | 1069 | $app['routing.loader'], |
|
365 | $resoure, |
||
366 | $options, |
||
367 | 1069 | $app['request_context'], |
|
368 | $app['logger'] |
||
369 | 1069 | ); |
|
370 | |||
371 | $router->setAdminPrefix($app['config']['admin_route']); |
||
372 | 1069 | $router->setUserDataPrefix($app['config']['user_data_route']); |
|
373 | $router->setRequireHttps($app['config']['force_ssl']); |
||
374 | |||
375 | 1069 | return $router; |
|
376 | 1069 | }); |
|
377 | |||
378 | $this['eccube.router.origin'] = function ($app) { |
||
379 | $resource = __DIR__.'/Controller'; |
||
380 | 1069 | $cachePrefix = 'Origin'; |
|
381 | 1069 | ||
382 | return $app['eccube.router']($resource, $cachePrefix); |
||
383 | }; |
||
384 | 1069 | ||
385 | 1069 | $this['eccube.routers.plugin'] = []; |
|
386 | 1069 | ||
387 | if (isset($this['config']['vendor_dir']) && file_exists($this['config']['vendor_dir'].'/Controller')) { |
||
388 | $this['eccube.router.extend'] = function ($app) { |
||
389 | $resource = $app['config']['vendor_dir'].'/Controller'; |
||
390 | 1069 | $cachePrefix = 'Extend'; |
|
391 | |||
392 | 1069 | $router = $app['eccube.router']($resource, $cachePrefix); |
|
393 | 1069 | ||
394 | 1069 | return $router; |
|
395 | 1069 | }; |
|
396 | } |
||
397 | View Code Duplication | $this->extend('request_matcher', function ($matcher, $app) { |
|
398 | 1069 | $matchers = []; |
|
399 | 1069 | if (isset($app['eccube.router.extend'])) { |
|
400 | $matchers[] = $app['eccube.router.extend']; |
||
401 | } |
||
402 | 1069 | foreach ($app['eccube.routers.plugin'] as $router) { |
|
403 | $matchers[] = $router; |
||
404 | 1069 | }; |
|
405 | 1069 | $matchers[] = $app['eccube.router.origin']; |
|
406 | $matchers[] = $matcher; |
||
407 | 1069 | ||
408 | 1069 | return new ChainUrlMatcher($matchers, $app['request_context']); |
|
409 | 1069 | }); |
|
410 | 1069 | ||
411 | View Code Duplication | $this->extend('url_generator', function ($generator, $app) { |
|
412 | $generators = []; |
||
413 | if (isset($app['eccube.router.extend'])) { |
||
414 | $generators[] = $app['eccube.router.extend']; |
||
415 | } |
||
416 | foreach ($app['eccube.routers.plugin'] as $router) { |
||
417 | 1069 | $generators[] = $router; |
|
418 | }; |
||
419 | 1069 | $generators[] = $app['eccube.router.origin']; |
|
420 | $generators[] = $generator; |
||
421 | |||
422 | return new ChainUrlGenerator($generators, $app['request_context']); |
||
423 | }); |
||
424 | |||
425 | // Route CollectionにEC-CUBEで定義したルーティングを追加(debug tool barに出力するため) |
||
426 | $this->extend('routes', function ($routes, $app) { |
||
427 | 1069 | if (isset($app['eccube.router.extend'])) { |
|
428 | $routes->addCollection($app['eccube.router.extend']->getRouteCollection()); |
||
429 | 1069 | } |
|
430 | 1069 | foreach ($app['eccube.routers.plugin'] as $router) { |
|
431 | $routes->addCollection($router->getRouteCollection()); |
||
432 | }; |
||
433 | 1069 | $routes->addCollection($app['eccube.router.origin']->getRouteCollection()); |
|
434 | 1069 | ||
435 | return $routes; |
||
436 | 1069 | }); |
|
437 | 1069 | ||
438 | // init http cache |
||
439 | $this->initCacheRequest(); |
||
440 | 319 | ||
441 | 319 | $this->initialized = true; |
|
442 | 319 | } |
|
443 | 221 | ||
444 | public function initLocale() |
||
473 | 319 | ||
474 | public function initSession() |
||
475 | { |
||
476 | 319 | $this->register(new \Silex\Provider\SessionServiceProvider(), array( |
|
477 | 'session.storage.save_path' => $this['config']['root_dir'].'/app/cache/eccube/session', |
||
478 | 221 | 'session.storage.options' => array( |
|
479 | 221 | 'name' => $this['config']['cookie_name'], |
|
480 | 'cookie_path' => $this['eccube.request']->getBasePath().'/', |
||
481 | 'cookie_secure' => $this['config']['force_ssl'], |
||
482 | 'cookie_lifetime' => $this['config']['cookie_lifetime'], |
||
483 | 'cookie_httponly' => true, |
||
484 | // cookie_domainは指定しない |
||
485 | 1069 | // http://blog.tokumaru.org/2011/10/cookiedomain.html |
|
486 | ), |
||
487 | )); |
||
488 | |||
489 | $options = $this['config']['session_handler']; |
||
490 | |||
491 | 317 | if ($options['enabled']) { |
|
492 | 71 | // @see http://silex.sensiolabs.org/doc/providers/session.html#custom-session-configurations |
|
493 | $this['session.storage.handler'] = null; |
||
494 | ini_set('session.save_handler', $options['save_handler']); |
||
495 | 317 | ini_set('session.save_path', $options['save_path']); |
|
496 | } |
||
497 | 317 | } |
|
498 | |||
499 | public function initRendering() |
||
638 | |||
639 | public function initMailer() |
||
668 | |||
669 | public function initDoctrine() |
||
863 | 1067 | ||
864 | 1067 | public function initSecurity() |
|
963 | |||
964 | protected function initRequest() |
||
965 | { |
||
966 | // PUTやDELETEメソッドを有効にする |
||
967 | Request::enableHttpMethodParameterOverride(); |
||
968 | |||
969 | // ロードバランサやプロキシの設定をする |
||
970 | $config = $this['config']; |
||
982 | |||
983 | public function initializePlugin() |
||
998 | 1070 | ||
999 | /** |
||
1000 | * |
||
1001 | * データベースの接続を確認 |
||
1002 | 1070 | * 成功 : trueを返却 |
|
1003 | * 失敗 : \Doctrine\DBAL\DBALExceptionエラーが発生( 接続に失敗した場合 )、エラー画面を表示しdie() |
||
1004 | 1070 | * 備考 : app['debug']がtrueの際は処理を行わない |
|
1005 | 1070 | * |
|
1006 | * @return boolean true |
||
1007 | 1070 | * |
|
1008 | 1070 | */ |
|
1009 | protected function checkDatabaseConnection() |
||
1033 | 1069 | ||
1034 | 1069 | /** |
|
1035 | * Config ファイルをパースし、連想配列を返します. |
||
1036 | * |
||
1037 | * $config_name.yml ファイルをパースし、連想配列を返します. |
||
1038 | * $config_name.php が存在する場合は、 PHP ファイルに記述された連想配列を使用します。 |
||
1039 | * |
||
1040 | * @param string $config_name Config 名称 |
||
1041 | * @param array $configAll Config の連想配列 |
||
1042 | * @param boolean $wrap_key Config の連想配列に config_name のキーを生成する場合 true, デフォルト false |
||
1043 | * @param string $ymlPath config yaml を格納したディレクトリ |
||
1044 | * @param string $distPath config yaml dist を格納したディレクトリ |
||
1045 | * @return Application |
||
1046 | 1069 | */ |
|
1047 | public function parseConfig($config_name, array &$configAll, $wrap_key = false, $ymlPath = null, $distPath = null) |
||
1080 | |||
1081 | /** |
||
1082 | * セッションが開始されているかどうか. |
||
1083 | * |
||
1084 | * @return boolean セッションが開始済みの場合 true |
||
1085 | * @link http://php.net/manual/ja/function.session-status.php#113468 |
||
1086 | */ |
||
1087 | protected function isSessionStarted() |
||
1099 | |||
1100 | /** |
||
1101 | * Http Cache対応 |
||
1102 | */ |
||
1103 | protected function initCacheRequest() |
||
1166 | } |
||
1167 |