Failed Conditions
Push — master ( ee6ce9...9ed6ac )
by Yangsin
485:10 queued 475:23
created

src/Eccube/Application.php (2 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
/*
3
 * This file is part of EC-CUBE
4
 *
5
 * Copyright(c) 2000-2015 LOCKON CO.,LTD. All Rights Reserved.
6
 *
7
 * http://www.lockon.co.jp/
8
 *
9
 * This program is free software; you can redistribute it and/or
10
 * modify it under the terms of the GNU General Public License
11
 * as published by the Free Software Foundation; either version 2
12
 * of the License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU General Public License
20
 * along with this program; if not, write to the Free Software
21
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
22
 */
23
24
namespace Eccube;
25
26
use Eccube\Application\ApplicationTrait;
27
use Eccube\Common\Constant;
28
use Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver;
29
use Eccube\EventListener\TransactionListener;
30
use Symfony\Component\EventDispatcher\EventDispatcher;
31
use Symfony\Component\Finder\Finder;
32
use Symfony\Component\HttpFoundation\Request;
33
use Symfony\Component\HttpFoundation\Response;
34
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
35
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
36
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
37
use Symfony\Component\HttpKernel\KernelEvents;
38
use Symfony\Component\Yaml\Yaml;
39
40
class Application extends ApplicationTrait
41
{
42
    protected static $instance;
43
44
    protected $initialized = false;
45
    protected $initializedPlugin = false;
46
    protected $testMode = false;
47
48 1055
    public static function getInstance(array $values = array())
49
    {
50 1055
        if (!is_object(self::$instance)) {
51 1054
            self::$instance = new Application($values);
52 1054
        }
53
54 1055
        return self::$instance;
55
    }
56
57 1055
    public static function clearInstance()
58
    {
59 1055
        self::$instance = null;
60 1055
    }
61
62
    final public function __clone()
63
    {
64
        throw new \Exception('Clone is not allowed against '.get_class($this));
65
    }
66
67 1069
    public function __construct(array $values = array())
68 1069
    {
69 1069
        parent::__construct($values);
70
71 1069
        if (is_null(self::$instance)) {
72 1055
            self::$instance = $this;
73 1055
        }
74
75
        // load config
76 1069
        $this->initConfig();
77
78
        // init monolog
79 1069
        $this->initLogger();
80 1069
    }
81
82 1070
    public function initConfig()
83 1069
    {
84
        // load config
85 1069
        $app = $this;
86
        $this['config'] = $this->share(function() use ($app) {
87 1070
            $configAll = array();
88 1062
            $app->parseConfig('constant', $configAll)
89 1062
                ->parseConfig('path', $configAll)
90 1062
                ->parseConfig('config', $configAll)
91 1070
                ->parseConfig('database', $configAll)
92 1070
                ->parseConfig('mail', $configAll)
93 1062
                ->parseConfig('log', $configAll)
94 1062
                ->parseConfig('nav', $configAll, true)
95 1062
                ->parseConfig('doctrine_cache', $configAll)
96 1062
                ->parseConfig('http_cache', $configAll)
97 1062
                ->parseConfig('session_handler', $configAll);
98
99 1062
            return $configAll;
100 1069
        });
101 1069
    }
102
103 1069
    public function initLogger()
104
    {
105 1069
        $app = $this;
106 1069
        $this->register(new ServiceProvider\EccubeMonologServiceProvider($app));
107 1069
        $this['monolog.logfile'] = __DIR__.'/../../app/log/site.log';
108 1069
        $this['monolog.name'] = 'eccube';
109 1069
    }
110
111 1070
    public function initialize()
112
    {
113 1059
        if ($this->initialized) {
114
            return;
115
        }
116
117
        // init locale
118 1070
        $this->initLocale();
119
120
        // init session
121 1059
        if (!$this->isSessionStarted()) {
122 1070
            $this->initSession();
123 1059
        }
124
125
        // init twig
126 1070
        $this->initRendering();
127
128
        // init provider
129 1059
        $this->register(new \Silex\Provider\HttpCacheServiceProvider(), array(
130 1070
            'http_cache.cache_dir' => __DIR__.'/../../app/cache/http/',
131 1059
        ));
132 1059
        $this->register(new \Silex\Provider\HttpFragmentServiceProvider());
133 1059
        $this->register(new \Silex\Provider\UrlGeneratorServiceProvider());
134 1059
        $this->register(new \Silex\Provider\FormServiceProvider());
135 1059
        $this->register(new \Silex\Provider\SerializerServiceProvider());
136 1070
        $this->register(new \Silex\Provider\ValidatorServiceProvider());
137
138 1059
        $app = $this;
139
        $this->error(function (\Exception $e, $code) use ($app) {
140 17
            if ($app['debug']) {
141 17
                return;
142
            }
143
144
            switch ($code) {
145 1069
                case 403:
146
                    $title = 'アクセスできません。';
147
                    $message = 'お探しのページはアクセスができない状況にあるか、移動もしくは削除された可能性があります。';
148
                    break;
149 1069
                case 404:
150
                    $title = 'ページがみつかりません。';
151
                    $message = 'URLに間違いがないかご確認ください。';
152
                    break;
153 1069
                default:
154
                    $title = 'システムエラーが発生しました。';
155 1069
                    $message = '大変お手数ですが、サイト管理者までご連絡ください。';
156 1069
                    break;
157 1069
            }
158
159 1069
            return $app->render('error.twig', array(
160 102
                'error_title' => $title,
161
                'error_message' => $message,
162
            ));
163 1059
        });
164
165
        // init mailer
166 1059
        $this->initMailer();
167
168
        // init doctrine orm
169 1059
        $this->initDoctrine();
170
171
        // Set up the DBAL connection now to check for a proper connection to the database.
172 1059
        $this->checkDatabaseConnection();
173
174
        // init security
175 1059
        $this->initSecurity();
176
177
        // init ec-cube service provider
178 1059
        $this->register(new ServiceProvider\EccubeServiceProvider());
179
180
        // mount controllers
181 1059
        $this->register(new \Silex\Provider\ServiceControllerServiceProvider());
182 1059
        $this->mount('', new ControllerProvider\FrontControllerProvider());
183 1059
        $this->mount('/'.trim($this['config']['admin_route'], '/').'/', new ControllerProvider\AdminControllerProvider());
184 1059
        Request::enableHttpMethodParameterOverride(); // PUTやDELETEできるようにする
185
186
        // add transaction listener
187 1059
        $this['dispatcher']->addSubscriber(new TransactionListener($this));
188
189
        // init http cache
190 1059
        $this->initCacheRequest();
191
192 1059
        $this->initialized = true;
193 1059
    }
194
195 1059
    public function initLocale()
196
    {
197
198
        // timezone
199 1059
        if (!empty($this['config']['timezone'])) {
200 1059
            date_default_timezone_set($this['config']['timezone']);
201 1059
        }
202
203 1059
        $this->register(new \Silex\Provider\TranslationServiceProvider(), array(
204 1059
            'locale' => $this['config']['locale'],
205 1059
            'translator.cache_dir' => $this['debug'] ? null : $this['config']['root_dir'].'/app/cache/translator',
206 1059
        ));
207
        $this['translator'] = $this->share($this->extend('translator', function ($translator, \Silex\Application $app) {
208 715
            $translator->addLoader('yaml', new \Symfony\Component\Translation\Loader\YamlFileLoader());
209
210 715
            $file = __DIR__.'/Resource/locale/validator.'.$app['locale'].'.yml';
211 715
            if (file_exists($file)) {
212 715
                $translator->addResource('yaml', $file, $app['locale'], 'validators');
213 715
            }
214
215 715
            $file = __DIR__.'/Resource/locale/message.'.$app['locale'].'.yml';
216 715
            if (file_exists($file)) {
217 715
                $translator->addResource('yaml', $file, $app['locale']);
218 715
            }
219
220 715
            return $translator;
221 1059
        }));
222 1059
    }
223
224 1059
    public function initSession()
225
    {
226 1059
        $this->register(new \Silex\Provider\SessionServiceProvider(), array(
227 1059
            'session.storage.save_path' => $this['config']['root_dir'].'/app/cache/eccube/session',
228
            'session.storage.options' => array(
229 1059
                'name' => 'eccube',
230 1059
                'cookie_path' => $this['config']['root_urlpath'] ?: '/',
231 1059
                'cookie_secure' => $this['config']['force_ssl'],
232 1059
                'cookie_lifetime' => $this['config']['cookie_lifetime'],
233 1059
                'cookie_httponly' => true,
234
                // cookie_domainは指定しない
235
                // http://blog.tokumaru.org/2011/10/cookiedomain.html
236 1059
            ),
237 1059
        ));
238
239 1059
        $options = $this['config']['session_handler'];
240
241 1059
        if ($options['enabled']) {
242
            // @see http://silex.sensiolabs.org/doc/providers/session.html#custom-session-configurations
243
            $this['session.storage.handler'] = null;
244
            ini_set('session.save_handler', $options['save_handler']);
245
            ini_set('session.save_path', $options['save_path']);
246
        }
247 1059
    }
248
249 1059
    public function initRendering()
250
    {
251 1059
        $this->register(new \Silex\Provider\TwigServiceProvider(), array(
252 1059
            'twig.form.templates' => array('Form/form_layout.twig'),
253 1059
        ));
254
        $this['twig'] = $this->share($this->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) {
255 472
            $twig->addExtension(new \Eccube\Twig\Extension\EccubeExtension($app));
256 472
            $twig->addExtension(new \Twig_Extension_StringLoader());
257
258 472
            return $twig;
259 1059
        }));
260
261
        $this->before(function (Request $request, \Silex\Application $app) {
262
            // フロント or 管理画面ごとにtwigの探索パスを切り替える.
263
            $app['twig'] = $app->share($app->extend('twig', function (\Twig_Environment $twig, \Silex\Application $app) {
264 458
                $paths = array();
265
266
                // 互換性がないのでprofiler とproduction 時のcacheを分離する
267
268 458
                $app['admin'] = false;
269 458
                $app['front'] = false;
270
271 458
                if (isset($app['profiler'])) {
272
                    $cacheBaseDir = __DIR__.'/../../app/cache/twig/profiler/';
273
                } else {
274 458
                    $cacheBaseDir = __DIR__.'/../../app/cache/twig/production/';
275
                }
276 458
                $pathinfo = rawurldecode($app['request']->getPathInfo());
277 458
                if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) {
278 291
                    if (file_exists(__DIR__.'/../../app/template/admin')) {
279 291
                        $paths[] = __DIR__.'/../../app/template/admin';
280 291
                    }
281 291
                    $paths[] = $app['config']['template_admin_realdir'];
282 291
                    $paths[] = __DIR__.'/../../app/Plugin';
283 291
                    $cache = $cacheBaseDir.'admin';
284 291
                    $app['admin'] = true;
285 291
                } else {
286 169
                    if (file_exists($app['config']['template_realdir'])) {
287 169
                        $paths[] = $app['config']['template_realdir'];
288 169
                    }
289 169
                    $paths[] = $app['config']['template_default_realdir'];
290 169
                    $paths[] = __DIR__.'/../../app/Plugin';
291 169
                    $cache = $cacheBaseDir.$app['config']['template_code'];
292 169
                    $app['front'] = true;
293
                }
294 458
                $twig->setCache($cache);
295 458
                $app['twig.loader']->addLoader(new \Twig_Loader_Filesystem($paths));
296
297 458
                return $twig;
298 460
            }));
299
300
            // 管理画面のIP制限チェック.
301 460
            $pathinfo = rawurldecode($app['request']->getPathInfo());
302 460
            if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) {
303
                // IP制限チェック
304 291
                $allowHost = $app['config']['admin_allow_host'];
305 291
                if (count($allowHost) > 0) {
306
                    if (array_search($app['request']->getClientIp(), $allowHost) === false) {
307
                        throw new \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException();
308 1
                    }
309
                }
310 291
            }
311 1059
        }, self::EARLY_EVENT);
312
313
        // twigのグローバル変数を定義.
314 1059
        $app = $this;
315
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) {
316
            // 未ログイン時にマイページや管理画面以下にアクセスするとSubRequestで実行されるため,
317
            // $event->isMasterRequest()ではなく、グローバル変数が初期化済かどうかの判定を行う
318 458
            if (isset($app['twig_global_initialized']) && $app['twig_global_initialized'] === true) {
319 113
                return;
320
            }
321
            // ショップ基本情報
322 458
            $BaseInfo = $app['eccube.repository.base_info']->get();
323 458
            $app['twig']->addGlobal('BaseInfo', $BaseInfo);
324
325 458
            $pathinfo = rawurldecode($app['request']->getPathInfo());
326 458
            if (strpos($pathinfo, '/'.trim($app['config']['admin_route'], '/').'/') === 0) {
327
                // 管理画面
328
                // 管理画面メニュー
329 291
                $menus = array('', '', '');
330 291
                $app['twig']->addGlobal('menus', $menus);
331
332 291
                $Member = $app->user();
333 291
                if (is_object($Member)) {
334
                    // ログインしていれば管理者のロールを取得
335 285
                    $AuthorityRoles = $app['eccube.repository.authority_role']->findBy(array('Authority' => $Member->getAuthority()));
336
337 285
                    $roles = array();
338 285
                    foreach ($AuthorityRoles as $AuthorityRole) {
339
                        // 管理画面でメニュー制御するため相対パス全てをセット
340 3
                        $roles[] = $app['request']->getBaseUrl().'/'.$app['config']['admin_route'].$AuthorityRole->getDenyUrl();
341 285
                    }
342
343 285
                    $app['twig']->addGlobal('AuthorityRoles', $roles);
344 285
                }
345
346 291
            } else {
347
                // フロント画面
348 167
                $request = $event->getRequest();
349 167
                $route = $request->attributes->get('_route');
350
351
                // ユーザ作成画面
352 167
                if ($route === 'user_data') {
353 2
                    $params = $request->attributes->get('_route_params');
354 2
                    $route = $params['route'];
355
                    // プレビュー画面
356 167
                } elseif ($request->get('preview')) {
357
                    $route = 'preview';
358
                }
359
360
                try {
361 167
                    $DeviceType = $app['eccube.repository.master.device_type']
362 167
                        ->find(\Eccube\Entity\Master\DeviceType::DEVICE_TYPE_PC);
363 167
                    $PageLayout = $app['eccube.repository.page_layout']->getByUrl($DeviceType, $route);
364 167
                } catch (\Doctrine\ORM\NoResultException $e) {
365 65
                    $PageLayout = $app['eccube.repository.page_layout']->newPageLayout($DeviceType);
366
                }
367
368 167
                $app['twig']->addGlobal('PageLayout', $PageLayout);
369 167
                $app['twig']->addGlobal('title', $PageLayout->getName());
370
            }
371
372 458
            $app['twig_global_initialized'] = true;
373 1059
        });
374 1059
    }
375
376 1059
    public function initMailer()
377
    {
378
379
        // メール送信時の文字エンコード指定(デフォルトはUTF-8)
380 1059
        if (isset($this['config']['mail']['charset_iso_2022_jp']) && is_bool($this['config']['mail']['charset_iso_2022_jp'])) {
381 1059
            if ($this['config']['mail']['charset_iso_2022_jp'] === true) {
382
                \Swift::init(function () {
383
                    \Swift_DependencyContainer::getInstance()
384
                        ->register('mime.qpheaderencoder')
385
                        ->asAliasOf('mime.base64headerencoder');
386
                    \Swift_Preferences::getInstance()->setCharset('iso-2022-jp');
387
                });
388
            }
389 1059
        }
390
391 1059
        $this->register(new \Silex\Provider\SwiftmailerServiceProvider());
392 1059
        $this['swiftmailer.options'] = $this['config']['mail'];
393
394 1059
        if (isset($this['config']['mail']['spool']) && is_bool($this['config']['mail']['spool'])) {
395
            $this['swiftmailer.use_spool'] = $this['config']['mail']['spool'];
396
        }
397
        // デフォルトはsmtpを使用
398 1059
        $transport = $this['config']['mail']['transport'];
399 1059
        if ($transport == 'sendmail') {
400
            $this['swiftmailer.transport'] = \Swift_SendmailTransport::newInstance();
401 1059
        } elseif ($transport == 'mail') {
402
            $this['swiftmailer.transport'] = \Swift_MailTransport::newInstance();
403
        }
404 1059
    }
405
406 1059
    public function initDoctrine()
407
    {
408 1059
        $this->register(new \Silex\Provider\DoctrineServiceProvider(), array(
409
            'dbs.options' => array(
410 1059
                'default' => $this['config']['database']
411 1059
            )));
412 1059
        $this->register(new \Saxulum\DoctrineOrmManagerRegistry\Silex\Provider\DoctrineOrmManagerRegistryProvider());
413
414
        // プラグインのmetadata定義を合わせて行う.
415 1059
        $pluginConfigs = $this->getPluginConfigAll();
416 1059
        $ormMappings = array();
417 1059
        $ormMappings[] = array(
418 1059
            'type' => 'yml',
419 1059
            'namespace' => 'Eccube\Entity',
420
            'path' => array(
421 1059
                __DIR__.'/Resource/doctrine',
422 1059
                __DIR__.'/Resource/doctrine/master',
423 1059
            ),
424 1059
        );
425
426 1059
        foreach ($pluginConfigs as $code) {
427 1059
            $config = $code['config'];
428 1059
            // Doctrine Extend
429
            if (isset($config['orm.path']) && is_array($config['orm.path'])) {
430
                $paths = array();
431 1059
                foreach ($config['orm.path'] as $path) {
432
                    $paths[] = $this['config']['plugin_realdir'].'/'.$config['code'].$path;
433 139
                }
434
                $ormMappings[] = array(
435 139
                    'type' => 'yml',
436 139
                    'namespace' => 'Plugin\\'.$config['code'].'\\Entity',
437 139
                    'path' => $paths,
438
                );
439
            }
440
        }
441
442
        $options = array(
443
            'mappings' => $ormMappings
444 139
        );
445
446
        if (!$this['debug']) {
447
            $cacheDrivers = array();
448
            if (array_key_exists('doctrine_cache', $this['config'])) {
449
                $cacheDrivers = $this['config']['doctrine_cache'];
450
            }
451
452
            if (array_key_exists('metadata_cache', $cacheDrivers)) {
453
                $options['metadata_cache'] = $cacheDrivers['metadata_cache'];
454
            }
455 1059
            if (array_key_exists('query_cache', $cacheDrivers)) {
456
                $options['query_cache'] = $cacheDrivers['query_cache'];
457
            }
458
            if (array_key_exists('result_cache', $cacheDrivers)) {
459 1059
                $options['result_cache'] = $cacheDrivers['result_cache'];
460
            }
461 1059
            if (array_key_exists('hydration_cache', $cacheDrivers)) {
462 4
                $options['hydration_cache'] = $cacheDrivers['hydration_cache'];
463 4
            }
464 4
        }
465 4
466
        $this->register(new \Dflydev\Silex\Provider\DoctrineOrm\DoctrineOrmServiceProvider(), array(
467 4
            'orm.proxies_dir' => __DIR__.'/../../app/cache/doctrine/proxies',
468 4
            'orm.em.options' => $options,
469 4
            'orm.custom.functions.numeric' => array(
470 4
                'EXTRACT' => 'Eccube\Doctrine\ORM\Query\Extract',
471 4
            ),
472 4
        ));
473 4
474 4
        /**
475 4
         * YamlDriverのPHP7対応. Doctrine2.4で修正されれば不要.
476 4
         * @see https://github.com/EC-CUBE/ec-cube/issues/1338
477 4
         */
478 4
        $config = $this['orm.em']->getConfiguration();
479 4
        /** @var $driver \Doctrine\Common\Persistence\Mapping\Driver\MappingDriverChain */
480
        $chain = $config->getMetadataDriverImpl();
481 1059
        // $ormMappingsの1要素ごとにDriverが生成されている.
482 1059
        $drivers = $chain->getDrivers();
483 1059
        foreach ($drivers as $namespace => $oldDriver) {
484
            /** @var $newDriver \Eccube\Doctrine\ORM\Mapping\Driver\YamlDriver */
485 1059
            $newDriver = new YamlDriver($oldDriver->getLocator());
486 1059
            // 修正したDriverに差し替える. メソッド名はaddだけど実際はsetしてる.
487 1059
            $chain->addDriver($newDriver, $namespace);
488
        }
489
    }
490
491
    public function initSecurity()
492
    {
493 1059
        $this->register(new \Silex\Provider\SecurityServiceProvider());
494
        $this->register(new \Silex\Provider\RememberMeServiceProvider());
495 1059
496
        $this['security.firewalls'] = array(
497 1059
            'admin' => array(
498 1059
                'pattern' => "^/{$this['config']['admin_route']}/",
499
                'form' => array(
500 1059
                    'login_path' => "/{$this['config']['admin_route']}/login",
501
                    'check_path' => "/{$this['config']['admin_route']}/login_check",
502 1059
                    'username_parameter' => 'login_id',
503 1059
                    'password_parameter' => 'password',
504 1059
                    'with_csrf' => true,
505
                    'use_forward' => true,
506 1059
                ),
507
                'logout' => array(
508 1059
                    'logout_path' => "/{$this['config']['admin_route']}/logout",
509 1059
                    'target_url' => "/{$this['config']['admin_route']}/",
510
                ),
511 1059
                'users' => $this['orm.em']->getRepository('Eccube\Entity\Member'),
512
                'anonymous' => true,
513 1059
            ),
514
            'customer' => array(
515 1059
                'pattern' => '^/',
516 1059
                'form' => array(
517 1059
                    'login_path' => '/mypage/login',
518 1059
                    'check_path' => '/login_check',
519 1059
                    'username_parameter' => 'login_email',
520 1059
                    'password_parameter' => 'login_pass',
521 1059
                    'with_csrf' => true,
522
                    'use_forward' => true,
523 1059
                ),
524 1059
                'logout' => array(
525 1059
                    'logout_path' => '/logout',
526 1059
                    'target_url' => '/',
527 1059
                ),
528 1059
                'remember_me' => array(
529
                    'key' => sha1($this['config']['auth_magic']),
530 1059
                    'name' => 'eccube_rememberme',
531
                    // lifetimeはデフォルトの1年間にする
532 1059
                    // 'lifetime' => $this['config']['cookie_lifetime'],
533 1059
                    'path' => $this['config']['root_urlpath'] ?: '/',
534 1059
                    'secure' => $this['config']['force_ssl'],
535 1059
                    'httponly' => true,
536 1059
                    'always_remember_me' => false,
537 1059
                    'remember_me_parameter' => 'login_memory',
538 1059
                ),
539
                'users' => $this['orm.em']->getRepository('Eccube\Entity\Customer'),
540 1059
                'anonymous' => true,
541 1059
            ),
542 1059
        );
543
544 1059
        $this['security.access_rules'] = array(
545 1059
            array("^/{$this['config']['admin_route']}/login", 'IS_AUTHENTICATED_ANONYMOUSLY'),
546
            array("^/{$this['config']['admin_route']}/", 'ROLE_ADMIN'),
547
            array('^/mypage/login', 'IS_AUTHENTICATED_ANONYMOUSLY'),
548 1059
            array('^/mypage/withdraw_complete', 'IS_AUTHENTICATED_ANONYMOUSLY'),
549 1059
            array('^/mypage/change', 'IS_AUTHENTICATED_FULLY'),
550 1059
            array('^/mypage', 'ROLE_USER'),
551 1059
        );
552 1059
553 1059
        $this['eccube.password_encoder'] = $this->share(function ($app) {
554 1059
            return new \Eccube\Security\Core\Encoder\PasswordEncoder($app['config']);
555 1059
        });
556 1059
        $this['security.encoder_factory'] = $this->share(function ($app) {
557
            return new \Symfony\Component\Security\Core\Encoder\EncoderFactory(array(
558
                'Eccube\Entity\Customer' => $app['eccube.password_encoder'],
559 1059
                'Eccube\Entity\Member' => $app['eccube.password_encoder'],
560 1059
            ));
561 1059
        });
562 1059
        $this['eccube.event_listner.security'] = $this->share(function ($app) {
563 1059
            return new \Eccube\EventListener\SecurityEventListener($app['orm.em']);
564 1059
        });
565 1059
        $this['user'] = function ($app) {
566
            $token = $app['security']->getToken();
567
568
            return ($token !== null) ? $token->getUser() : null;
569 1059
        };
570 1059
571
        // ログイン時のイベントを設定.
572 1059
        $this['dispatcher']->addListener(\Symfony\Component\Security\Http\SecurityEvents::INTERACTIVE_LOGIN, array($this['eccube.event_listner.security'], 'onInteractiveLogin'));
573 1059
574 1059
        // Voterの設定
575 1059
        $app = $this;
576 1059
        $this['authority_voter'] = $this->share(function ($app) {
577
            return new \Eccube\Security\Voter\AuthorityVoter($app);
578 1059
        });
579 1059
580
        $app['security.voters'] = $app->extend('security.voters', function ($voters) use ($app) {
581 362
            $voters[] = $app['authority_voter'];
582
583 362
            return $voters;
584
        });
585
586
        $this['security.access_manager'] = $this->share(function ($app) {
587 1059
            return new \Symfony\Component\Security\Core\Authorization\AccessDecisionManager($app['security.voters'], 'unanimous');
588
        });
589
590 1059
    }
591
592 1059
    public function initializePlugin()
593 1059
    {
594
        if ($this->initializedPlugin) {
595
            return;
596 1059
        }
597
598 1059
        // setup event dispatcher
599 1059
        $this->initPluginEventDispatcher();
600
601
        // load plugin
602 1059
        $this->loadPlugin();
603 1059
604
        $this->initializedPlugin = true;
605 1059
    }
606
607 1059
    public function initPluginEventDispatcher()
608
    {
609 1059
        // EventDispatcher
610
        $this['eccube.event.dispatcher'] = $this->share(function () {
611
            return new EventDispatcher();
612
        });
613
614 1059
        $app = $this;
615
616
        // hook point
617 1059
        $this->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) {
618
            if (!$event->isMasterRequest()) {
619 1059
                return;
620 1059
            }
621
            $hookpoint = 'eccube.event.app.before';
622 1059
            $app['eccube.event.dispatcher']->dispatch($hookpoint, $event);
623
        }, self::EARLY_EVENT);
624
625 View Code Duplication
        $this->on(KernelEvents::REQUEST, function (GetResponseEvent $event) use ($app) {
626 474
            if (!$event->isMasterRequest()) {
627 1059
                return;
628
            }
629 1059
            $route = $event->getRequest()->attributes->get('_route');
630
            $hookpoint = "eccube.event.controller.$route.before";
631
            $app['eccube.event.dispatcher']->dispatch($hookpoint, $event);
632
        });
633 460
634 72 View Code Duplication
        $this->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) {
635
            if (!$event->isMasterRequest()) {
636 460
                return;
637 460
            }
638 1059
            $route = $event->getRequest()->attributes->get('_route');
639
            $hookpoint = "eccube.event.controller.$route.after";
640
            $app['eccube.event.dispatcher']->dispatch($hookpoint, $event);
641 459
        });
642 72
643
        $this->on(KernelEvents::RESPONSE, function (FilterResponseEvent $event) use ($app) {
644 457
            if (!$event->isMasterRequest()) {
645 457
                return;
646 457
            }
647 1059
            $hookpoint = 'eccube.event.app.after';
648
            $app['eccube.event.dispatcher']->dispatch($hookpoint, $event);
649
        }, self::LATE_EVENT);
650 447
651 72
        $this->on(KernelEvents::TERMINATE, function (PostResponseEvent $event) use ($app) {
652
            $route = $event->getRequest()->attributes->get('_route');
653 447
            $hookpoint = "eccube.event.controller.$route.finish";
654 447
            $app['eccube.event.dispatcher']->dispatch($hookpoint, $event);
655 447
        });
656 1059
657
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) {
658
            if (!$event->isMasterRequest()) {
659 447
                return;
660 72
            }
661
            $route = $event->getRequest()->attributes->get('_route');
662 447
            $app['eccube.event.dispatcher']->dispatch('eccube.event.render.'.$route.'.before', $event);
663 447
        });
664 1059
665
        // Request Event
666 View Code Duplication
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::REQUEST, function (\Symfony\Component\HttpKernel\Event\GetResponseEvent $event) use ($app) {
667 447
668 447
            if (!$event->isMasterRequest()) {
669 447
                return;
670 1059
            }
671
672
            $route = $event->getRequest()->attributes->get('_route');
673 447
674 72
            if (is_null($route)) {
675
                return;
676 447
            }
677 447
678 1059
            $app['monolog']->debug('KernelEvents::REQUEST '.$route);
679
680
            // 全体
681
            $app['eccube.event.dispatcher']->dispatch('eccube.event.app.request', $event);
682
683 459
            if (strpos($route, 'admin') === 0) {
684 72
                // 管理画面
685
                $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.request', $event);
686
            } else {
687 459
                // フロント画面
688
                $app['eccube.event.dispatcher']->dispatch('eccube.event.front.request', $event);
689 459
            }
690
691
            // ルーティング単位
692
            $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.request", $event);
693 459
694
        }, 30); // Routing(32)が解決しし, 認証判定(8)が実行される前のタイミング.
695
696 459
        // Controller Event
697 View Code Duplication
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::CONTROLLER, function (\Symfony\Component\HttpKernel\Event\FilterControllerEvent $event) use ($app) {
698 459
699
            if (!$event->isMasterRequest()) {
700 291
                return;
701 291
            }
702
703 170
            $route = $event->getRequest()->attributes->get('_route');
704
705
            if (is_null($route)) {
706
                return;
707 459
            }
708
709 1059
            $app['monolog']->debug('KernelEvents::CONTROLLER '.$route);
710
711
            // 全体
712
            $app['eccube.event.dispatcher']->dispatch('eccube.event.app.controller', $event);
713
714 458
            if (strpos($route, 'admin') === 0) {
715 72
                // 管理画面
716
                $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.controller', $event);
717
            } else {
718 456
                // フロント画面
719
                $app['eccube.event.dispatcher']->dispatch('eccube.event.front.controller', $event);
720 456
            }
721
722
            // ルーティング単位
723
            $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.controller", $event);
724 456
        });
725
726
        // Response Event
727 456 View Code Duplication
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) {
728
            if (!$event->isMasterRequest()) {
729 456
                return;
730
            }
731 289
732 289
            $route = $event->getRequest()->attributes->get('_route');
733
734 169
            if (is_null($route)) {
735
                return;
736
            }
737
738 456
            $app['monolog']->debug('KernelEvents::RESPONSE '.$route);
739 1059
740
            // ルーティング単位
741
            $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.response", $event);
742
743 447
            if (strpos($route, 'admin') === 0) {
744 72
                // 管理画面
745
                $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.response', $event);
746
            } else {
747 447
                // フロント画面
748
                $app['eccube.event.dispatcher']->dispatch('eccube.event.front.response', $event);
749 447
            }
750 1
751
            // 全体
752
            $app['eccube.event.dispatcher']->dispatch('eccube.event.app.response', $event);
753 446
        });
754
755
        // Exception Event
756 446 View Code Duplication
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::EXCEPTION, function (\Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent $event) use ($app) {
757
758 446
            if (!$event->isMasterRequest()) {
759
                return;
760 284
            }
761 284
762
            $route = $event->getRequest()->attributes->get('_route');
763 164
764
            if (is_null($route)) {
765
                return;
766
            }
767 446
768 1059
            $app['monolog']->debug('KernelEvents::EXCEPTION '.$route);
769
770
            // ルーティング単位
771
            $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.exception", $event);
772
773 19
            if (strpos($route, 'admin') === 0) {
774
                // 管理画面
775
                $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.exception', $event);
776
            } else {
777 19
                // フロント画面
778
                $app['eccube.event.dispatcher']->dispatch('eccube.event.front.exception', $event);
779 19
            }
780
781
            // 全体
782
            $app['eccube.event.dispatcher']->dispatch('eccube.event.app.exception', $event);
783 19
        });
784
785
        // Terminate Event
786 19 View Code Duplication
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::TERMINATE, function (\Symfony\Component\HttpKernel\Event\PostResponseEvent $event) use ($app) {
787
788 19
            $route = $event->getRequest()->attributes->get('_route');
789
790 9
            if (is_null($route)) {
791 9
                return;
792
            }
793 10
794
            $app['monolog']->debug('KernelEvents::TERMINATE '.$route);
795
796
            // ルーティング単位
797 19
            $app['eccube.event.dispatcher']->dispatch("eccube.event.route.{$route}.terminate", $event);
798 1059
799
            if (strpos($route, 'admin') === 0) {
800
                // 管理画面
801
                $app['eccube.event.dispatcher']->dispatch('eccube.event.admin.terminate', $event);
802
            } else {
803 447
                // フロント画面
804
                $app['eccube.event.dispatcher']->dispatch('eccube.event.front.terminate', $event);
805 447
            }
806 1
807
            // 全体
808
            $app['eccube.event.dispatcher']->dispatch('eccube.event.app.terminate', $event);
809 446
        });
810
    }
811
812 446
    public function loadPlugin()
813
    {
814 446
        // プラグインディレクトリを探索.
815
        $basePath = $this['config']['plugin_realdir'];
816 284
        $pluginConfigs = $this->getPluginConfigAll();
817 284
818
        // ハンドラ優先順位をdbから持ってきてハッシュテーブルを作成
819 164
        $priorities = array();
820
        $handlers = $this['orm.em']
821
            ->getRepository('Eccube\Entity\PluginEventHandler')
822
            ->getHandlers();
823 446
824 1059
        foreach ($handlers as $handler) {
825 1059
            if ($handler->getPlugin()->getEnable() && !$handler->getPlugin()->getDelFlg()) {
826
827 1059
                $priority = $handler->getPriority();
828
            } else {
829
                // Pluginがdisable、削除済みの場合、EventHandlerのPriorityを全て0とみなす
830 1059
                $priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED;
831 1059
            }
832 1059
            $priorities[$handler->getPlugin()->getClassName()][$handler->getEvent()][$handler->getHandler()] = $priority;
833 1059
        }
834 1059
835
        // プラグインをロードする.
836 1059
        // config.yml/event.ymlの定義に沿ってインスタンスの生成を行い, イベント設定を行う.
837
        foreach ($pluginConfigs as $code => $pluginConfig) {
838
            // 正しい形式の pluginConfig のみ読み込む
839 1059
            $path = $basePath.'/'.$code;
840 1059
            try {
841 1059
                $this['eccube.service.plugin']->checkPluginArchiveContent($path, $pluginConfig['config']);
842 1059
            } catch (\Eccube\Exception\PluginException $e) {
843 1059
                $this['monolog']->warning("skip {$code} config loading. config.yml not foud or invalid.", array(
844
                    'path' => $path,
845
                    'original-message' => $e->getMessage()
846
                ));
847
                continue;
848
            }
849
            $config = $pluginConfig['config'];
850
851
            $plugin = $this['orm.em']
852 1059
                ->getRepository('Eccube\Entity\Plugin')
853
                ->findOneBy(array('code' => $config['code']));
854
855
            // const
856 1059
            if (isset($config['const'])) {
857
                $this['config'] = $this->share($this->extend('config', function ($eccubeConfig) use ($config) {
858 140
                    $eccubeConfig[$config['code']] = array(
859 140
                        'const' => $config['const'],
860
                    );
861 140
862 140
                    return $eccubeConfig;
863
                }));
864
            }
865
866
            if ($plugin && $plugin->getEnable() == Constant::DISABLED) {
867
                // プラグインが無効化されていれば読み込まない
868
                continue;
869 140
            }
870
871 140
            // Type: Event
872 140
            if (isset($config['event'])) {
873 140
                $class = '\\Plugin\\'.$config['code'].'\\'.$config['event'];
874
                $eventExists = true;
875
876 140 View Code Duplication
                if (!class_exists($class)) {
877
                    $this['monolog']->warning("skip {$code} loading. event class not foud.", array(
878 1
                        'class' => $class,
879 1
                    ));
880
                    $eventExists = false;
881
                }
882 1
883 1
                if ($eventExists && isset($config['event'])) {
884 1
885
                    $subscriber = new $class($this);
886 140
887
                    foreach ($pluginConfig['event'] as $event => $handlers) {
888 1
                        foreach ($handlers as $handler) {
889
                            if (!isset($priorities[$config['event']][$event][$handler[0]])) { // ハンドラテーブルに登録されていない(ソースにしか記述されていない)ハンドラは一番後ろにする
890
                                $priority = \Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_LATEST;
891
                            } else {
892 139
                                $priority = $priorities[$config['event']][$event][$handler[0]];
893 139
                            }
894 139
                            // 優先度が0のプラグインは登録しない
895
                            if (\Eccube\Entity\PluginEventHandler::EVENT_PRIORITY_DISABLED != $priority) {
896 139
                                $this['eccube.event.dispatcher']->addListener($event, array($subscriber, $handler[0]), $priority);
897
                            }
898
                        }
899
                    }
900
                }
901
            }
902
            // Type: ServiceProvider
903 139
            if (isset($config['service'])) {
904
                foreach ($config['service'] as $service) {
905 139
                    $class = '\\Plugin\\'.$config['code'].'\\ServiceProvider\\'.$service;
906 View Code Duplication
                    if (!class_exists($class)) {
907 139
                        $this['monolog']->warning("skip {$code} loading. service provider class not foud.", array(
908 139
                            'class' => $class,
909 139
                        ));
910 139
                        continue;
911 139
                    }
912
                    $this->register(new $class($this));
913
                }
914
            }
915 139
        }
916 139
    }
917 139
918 139
    /**
919 139
     * PHPUnit を実行中かどうかを設定する.
920 139
     *
921 139
     * @param boolean $testMode PHPUnit を実行中の場合 true
922
     */
923 139
    public function setTestMode($testMode)
924
    {
925
        $this->testMode = $testMode;
926
    }
927
928
    /**
929
     * PHPUnit を実行中かどうか.
930
     *
931
     * @return boolean PHPUnit を実行中の場合 true
932
     */
933
    public function isTestMode()
934
    {
935 1059
        return $this->testMode;
936 1059
    }
937
938
    /**
939
     *
940
     * データベースの接続を確認
941
     * 成功 : trueを返却
942
     * 失敗 : \Doctrine\DBAL\DBALExceptionエラーが発生( 接続に失敗した場合 )、エラー画面を表示しdie()
943 1045
     * 備考 : app['debug']がtrueの際は処理を行わない
944
     *
945 1045
     * @return boolean true
946 1045
     *
947
     */
948
    protected function checkDatabaseConnection()
949
    {
950
        if ($this['debug']) {
951
            return;
952
        }
953 460
        try {
954
            $this['db']->connect();
955 460
        } catch (\Doctrine\DBAL\DBALException $e) {
956
            $this['monolog']->error($e->getMessage());
957
            $this['twig.path'] = array(__DIR__.'/Resource/template/exception');
958
            $html = $this['twig']->render('error.twig', array(
959
                'error_title' => 'データーベース接続エラー',
960
                'error_message' => 'データーベースを確認してください',
961
            ));
962
            $response = new Response();
963
            $response->setContent($html);
964
            $response->setStatusCode('500');
965
            $response->headers->set('Content-Type', 'text/html');
966
            $response->send();
967
            die();
968 1059
        }
969
970 1059
        return true;
971 1055
    }
972
973
    /**
974 4
     * Config ファイルをパースし、連想配列を返します.
975 4
     *
976
     * $config_name.yml ファイルをパースし、連想配列を返します.
977
     * $config_name.php が存在する場合は、 PHP ファイルに記述された連想配列を使用します。
978
     *
979
     * @param string $config_name Config 名称
980
     * @param array $configAll Config の連想配列
981
     * @param boolean $wrap_key Config の連想配列に config_name のキーを生成する場合 true, デフォルト false
982
     * @param string $ymlPath config yaml を格納したディレクトリ
983
     * @param string $distPath config yaml dist を格納したディレクトリ
984
     * @return Application
985
     */
986
    public function parseConfig($config_name, array &$configAll, $wrap_key = false, $ymlPath = null, $distPath = null)
987
    {
988
        $ymlPath = $ymlPath ? $ymlPath : __DIR__.'/../../app/config/eccube';
989
        $distPath = $distPath ? $distPath : __DIR__.'/../../src/Eccube/Resource/config';
990 4
        $config = array();
991
        $config_php = $ymlPath.'/'.$config_name.'.php';
992
        if (!file_exists($config_php)) {
993
            $config_yml = $ymlPath.'/'.$config_name.'.yml';
994
            if (file_exists($config_yml)) {
995
                $config = Yaml::parse(file_get_contents($config_yml));
996
                $config = empty($config) ? array() : $config;
997 View Code Duplication
                if (isset($this['output_config_php']) && $this['output_config_php']) {
998
                    file_put_contents($config_php, sprintf('<?php return %s', var_export($config, true)).';');
999
                }
1000
            }
1001
        } else {
1002
            $config = require $config_php;
1003
        }
1004
1005
        $config_dist = array();
1006 1062
        $config_php_dist = $distPath.'/'.$config_name.'.dist.php';
1007
        if (!file_exists($config_php_dist)) {
1008 1062
            $config_yml_dist = $distPath.'/'.$config_name.'.yml.dist';
1009 1062
            if (file_exists($config_yml_dist)) {
1010 1062
                $config_dist = Yaml::parse(file_get_contents($config_yml_dist));
1011 1062 View Code Duplication
                if (isset($this['output_config_php']) && $this['output_config_php']) {
1012 1062
                    file_put_contents($config_php_dist, sprintf('<?php return %s', var_export($config_dist, true)).';');
1013 1062
                }
1014 1062
            }
1015 1062
        } else {
1016 1062
            $config_dist = require $config_php_dist;
1017 1062
        }
1018
1019
        if ($wrap_key) {
1020 1062
            $configAll = array_replace_recursive($configAll, array($config_name => $config_dist), array($config_name => $config));
1021 1062
        } else {
1022
            $configAll = array_replace_recursive($configAll, $config_dist, $config);
1023
        }
1024
1025 1062
        return $this;
1026 1062
    }
1027 1062
1028 1062
    /**
1029 1062
     * セッションが開始されているかどうか.
1030 1062
     *
1031 1062
     * @return boolean セッションが開始済みの場合 true
1032
     * @link http://php.net/manual/ja/function.session-status.php#113468
1033
     */
1034 1062
    protected function isSessionStarted()
1035 1062
    {
1036
        if (php_sapi_name() !== 'cli') {
1037
            if (version_compare(phpversion(), '5.4.0', '>=')) {
1038
                return session_status() === PHP_SESSION_ACTIVE ? true : false;
1039 1062
            } else {
1040 1062
                return session_id() === '' ? false : true;
1041 1062
            }
1042 1062
        }
1043
1044
        return false;
1045 1062
    }
1046
1047
    /**
1048
     * Http Cache対応
1049
     */
1050
    protected function initCacheRequest()
1051
    {
1052
        // httpキャッシュが無効の場合はイベント設定を行わない.
1053
        if (!$this['config']['http_cache']['enabled']) {
1054 1059
            return;
1055
        }
1056 1059
1057
        $app = $this;
1058
1059
        // Response Event(http cache対応、event実行は一番遅く設定)
1060
        $this->on(\Symfony\Component\HttpKernel\KernelEvents::RESPONSE, function (\Symfony\Component\HttpKernel\Event\FilterResponseEvent $event) use ($app) {
1061
1062
            if (!$event->isMasterRequest()) {
1063
                return;
1064 1059
            }
1065
1066
            $request = $event->getRequest();
1067
            $response = $event->getResponse();
1068
1069
            $route = $request->attributes->get('_route');
1070 1059
1071
            $etag = md5($response->getContent());
1072
1073 1059
            if (strpos($route, 'admin') === 0) {
1074 1059
                // 管理画面
1075
1076
                // 管理画面ではコンテンツの中身が変更された時点でキャッシュを更新し、キャッシュの適用範囲はprivateに設定
1077
                $response->setCache(array(
1078
                    'etag' => $etag,
1079
                    'private' => true,
1080
                ));
1081
1082
                if ($response->isNotModified($request)) {
1083
                    return $response;
1084
                }
1085
1086
            } else {
1087
                // フロント画面
1088
                $cacheRoute = $app['config']['http_cache']['route'];
1089
1090
                if (in_array($route, $cacheRoute) === true) {
1091
                    // キャッシュ対象となる画面lが含まれていた場合、キャッシュ化
1092
                    // max-ageを設定しているためExpiresは不要
1093
                    // Last-Modifiedだと比較する項目がないためETagで対応
1094
                    // max-ageを設定していた場合、contentの中身が変更されても変更されない
1095
1096
                    $age = $app['config']['http_cache']['age'];
1097
1098
                    $response->setCache(array(
1099
                        'etag' => $etag,
1100
                        'max_age' => $age,
1101
                        's_maxage' => $age,
1102
                        'public' => true,
1103
                    ));
1104
1105
                    if ($response->isNotModified($request)) {
1106
                        return $response;
1107
                    }
1108
                }
1109
            }
1110
1111
        }, -1024);
1112
    }
1113
1114
    /**
1115
     * すべてのプラグインの設定情報を返す.
1116
     *
1117
     * すべてのプラグインの config.yml 及び event.yml を読み込み、連想配列で返す.
1118
     * キャッシュファイルが存在する場合は、キャッシュを利用する.
1119
     * キャッシュファイルが存在しない場合は、キャッシュを生成する.
1120
     * $app['debug'] = true の場合は、キャッシュを利用しない.
1121
     *
1122
     * @return array
1123
     */
1124
    public function getPluginConfigAll()
1125
    {
1126
        if ($this['debug']) {
1127
            return $this->parsePluginConfigs();
1128
        }
1129
        $pluginConfigCache = $this->getPluginConfigCacheFile();
1130
        if (file_exists($pluginConfigCache)) {
1131
            return require $pluginConfigCache;
1132
        }
1133
        if ($this->writePluginConfigCache($pluginConfigCache) === false) {
1134
            return $this->parsePluginConfigs();
1135
        } else {
1136
            return require $pluginConfigCache;
1137
        }
1138
    }
1139
1140
    /**
1141
     * プラグイン設定情報のキャッシュを書き込む.
1142
     *
1143
     * @param string $cacheFile
1144
     * @return int|boolean file_put_contents() の結果
1145
     */
1146
    public function writePluginConfigCache($cacheFile = null)
1147
    {
1148
        if (is_null($cacheFile)) {
1149
            $cacheFile = $this->getPluginConfigCacheFile();
1150
        }
1151
        $pluginConfigs = $this->parsePluginConfigs();
1152
        if (!file_exists($this['config']['plugin_temp_realdir'])) {
1153
            @mkdir($this['config']['plugin_temp_realdir']);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
1154
        }
1155
        $this['monolog']->debug("write plugin config cache", array($pluginConfigs));
1156
        return file_put_contents($cacheFile, sprintf('<?php return %s', var_export($pluginConfigs, true)).';');
1157
    }
1158
1159
    /**
1160
     * プラグイン設定情報のキャッシュファイルを削除する.
1161
     *
1162
     * @return boolean
1163
     */
1164
    public function removePluginConfigCache()
1165
    {
1166
        $cacheFile = $this->getPluginConfigCacheFile();
1167
        if (file_exists($cacheFile)) {
1168
            $this['monolog']->debug("remove plugin config cache");
1169
            return unlink($cacheFile);
1170
        }
1171
        return false;
1172
    }
1173
1174
    /**
1175
     * プラグイン設定情報のキャッシュファイルパスを返す.
1176
     *
1177
     * @return string
1178
     */
1179
    public function getPluginConfigCacheFile()
1180
    {
1181
        return $this['config']['plugin_temp_realdir'].'/config_cache.php';
1182
    }
1183
1184
    /**
1185
     * プラグイン設定情報をパースし, 連想配列で返す.
1186
     *
1187
     * すべてのプラグインを探索し、 config.yml 及び event.yml をパースする.
1188
     * パースした情報を連想配列で返す.
1189
     *
1190
     * @return array
1191
     */
1192
    public function parsePluginConfigs()
1193
    {
1194
1195
        $finder = Finder::create()
1196
            ->in($this['config']['plugin_realdir'])
1197
            ->directories()
1198
            ->depth(0);
1199
        $finder->sortByName();
1200
1201
        $pluginConfigs = array();
1202
        foreach ($finder as $dir) {
1203
            $code = $dir->getBaseName();
1204
            $file = $dir->getRealPath().'/config.yml';
1205
            $config = null;
0 ignored issues
show
$config is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1206 View Code Duplication
            if (file_exists($file)) {
1207
                $config = Yaml::parse(file_get_contents($file));
1208
            } else {
1209
                $this['monolog']->warning("skip {$code} orm.path loading. config.yml not found.", array('path' => $file));
1210
                continue;
1211
            }
1212
1213
            $file = $dir->getRealPath().'/event.yml';
1214
            $event = null;
1215 View Code Duplication
            if (file_exists($file)) {
1216
                $event = Yaml::parse(file_get_contents($file));
1217
            } else {
1218
                $this['monolog']->info("skip {$code} event.yml not found.", array('path' => $file));
1219
            }
1220
            if (!is_null($config)) {
1221
                $pluginConfigs[$code] = array(
1222
                    'config' => $config,
1223
                    'event' => $event
1224
                );
1225
                $this['monolog']->debug("parse {$code} config", array($code => $pluginConfigs[$code]));
1226
            }
1227
        }
1228
1229
        return $pluginConfigs;
1230
    }
1231
}
1232