Completed
Pull Request — experimental/sf (#3412)
by Kentaro
14:51 queued 07:29
created

CacheUtil::getSubscribedEvents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of EC-CUBE
5
 *
6
 * Copyright(c) LOCKON CO.,LTD. All Rights Reserved.
7
 *
8
 * http://www.lockon.co.jp/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Eccube\Util;
15
16
use Symfony\Bundle\FrameworkBundle\Console\Application;
17
use Symfony\Component\Console\Input\ArrayInput;
18
use Symfony\Component\Console\Output\BufferedOutput;
19
use Symfony\Component\Console\Output\OutputInterface;
20
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
21
use Symfony\Component\Filesystem\Filesystem;
22
use Symfony\Component\Finder\Finder;
23
use Symfony\Component\HttpKernel\Event\PostResponseEvent;
24
use Symfony\Component\HttpKernel\KernelEvents;
25
use Symfony\Component\HttpKernel\KernelInterface;
26
27
/**
28
 * キャッシュ関連のユーティリティクラス.
29
 */
30
class CacheUtil implements EventSubscriberInterface
31
{
32
    private $clearCacheAfterResponse = false;
33
34
    /**
35
     * @var KernelInterface
36
     */
37
    protected $kernel;
38
39
    /**
40
     * CacheUtil constructor.
41
     *
42
     * @param KernelInterface $kernel
43
     */
44 434
    public function __construct(KernelInterface $kernel)
45
    {
46 434
        $this->kernel = $kernel;
47
    }
48
49
    public function clearCache($env = null)
50
    {
51
        $this->clearCacheAfterResponse = $env;
52
    }
53
54 433
    public function forceClearCache(PostResponseEvent $event)
0 ignored issues
show
Unused Code introduced by
The parameter $event is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
55
    {
56 433
        if ($this->clearCacheAfterResponse === false) {
57 433
            return;
58
        }
59
60
        $console = new Application($this->kernel);
61
        $console->setAutoExit(false);
62
63
        $command = [
64
            'command' => 'cache:clear',
65
            '--no-warmup' => true,
66
            '--no-ansi' => true,
67
        ];
68
69
        if ($this->clearCacheAfterResponse !== null) {
70
            $command['--env'] = $this->clearCacheAfterResponse;
71
        }
72
73
        $input = new ArrayInput($command);
74
75
        $output = new BufferedOutput(
76
            OutputInterface::VERBOSITY_DEBUG,
77
            true
78
        );
79
80
        $console->run($input, $output);
81
82
        return $output->fetch();
83
    }
84
85
    /**
86
     * キャッシュを削除する.
87
     *
88
     * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
89
     * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
90
     *
91
     * @param Application $app
92
     * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
introduced by
Expected 2 spaces after parameter name; 1 found
Loading history...
93
     * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
0 ignored issues
show
introduced by
Expected 5 spaces after parameter type; 1 found
Loading history...
94
     *
95
     * @return boolean 削除に成功した場合 true
96
     *
97
     * @deprecated CacheUtil::clearCacheを利用すること
98
     */
99 2
    public static function clear($app, $isAll, $isTwig = false)
100
    {
101 2
        $cacheDir = $app['config']['root_dir'].'/app/cache';
102
103 2
        $filesystem = new Filesystem();
104 2
        $finder = Finder::create()->notName('.gitkeep')->files();
105 2
        if ($isAll) {
106 1
            $finder = $finder->in($cacheDir);
107 1
            $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
108 1 View Code Duplication
        } elseif ($isTwig) {
109
            if (is_dir($cacheDir.'/twig')) {
110
                $finder = $finder->in($cacheDir.'/twig');
111
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
112
            }
113
        } else {
114 1 View Code Duplication
            if (is_dir($cacheDir.'/doctrine')) {
115 1
                $finder = $finder->in($cacheDir.'/doctrine');
116 1
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
117
            }
118 1 View Code Duplication
            if (is_dir($cacheDir.'/profiler')) {
119 1
                $finder = $finder->in($cacheDir.'/profiler');
120 1
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
121
            }
122 1 View Code Duplication
            if (is_dir($cacheDir.'/twig')) {
123 1
                $finder = $finder->in($cacheDir.'/twig');
124 1
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
125
            }
126 1 View Code Duplication
            if (is_dir($cacheDir.'/translator')) {
127
                $finder = $finder->in($cacheDir.'/translator');
128
                $filesystem->remove($finder);
0 ignored issues
show
Documentation introduced by
$finder is of type object<Symfony\Component\Finder\Finder>, but the function expects a string|object<Symfony\Co...nt\Filesystem\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
129
            }
130
        }
131
132 2
        if (function_exists('opcache_reset')) {
133
            opcache_reset();
134
        }
135
136 2
        if (function_exists('apc_clear_cache')) {
137
            apc_clear_cache('user');
138
            apc_clear_cache();
139
        }
140
141 2
        if (function_exists('wincache_ucache_clear')) {
142
            wincache_ucache_clear();
143
        }
144
145 2
        return true;
146
    }
147
148
    /**
149
     * {@inheritdoc}
150
     */
151 1
    public static function getSubscribedEvents()
152
    {
153 1
        return [KernelEvents::TERMINATE => 'forceClearCache'];
154
    }
155
}
156