Completed
Push — 4.0 ( b48f64...137622 )
by chihiro
20:21 queued 10s
created

src/Eccube/Util/CacheUtil.php (4 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
/*
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 437
    public function __construct(KernelInterface $kernel)
45
    {
46 437
        $this->kernel = $kernel;
47
    }
48
49
    /**
50
     * @param string $env
51
     */
52
    public function clearCache($env = null)
53
    {
54
        $this->clearCacheAfterResponse = $env;
55
    }
56
57 436
    public function forceClearCache(PostResponseEvent $event)
58
    {
59 436
        if ($this->clearCacheAfterResponse === false) {
60 436
            return;
61
        }
62
63
        $console = new Application($this->kernel);
64
        $console->setAutoExit(false);
65
66
        $command = [
67
            'command' => 'cache:clear',
68
            '--no-warmup' => true,
69
            '--no-ansi' => true,
70
        ];
71
72
        if ($this->clearCacheAfterResponse !== null) {
73
            $command['--env'] = $this->clearCacheAfterResponse;
74
        }
75
76
        $input = new ArrayInput($command);
77
78
        $output = new BufferedOutput(
79
            OutputInterface::VERBOSITY_DEBUG,
80
            true
81
        );
82
83
        $console->run($input, $output);
84
85
        if (function_exists('opcache_reset')) {
86
            opcache_reset();
87
        }
88
89
        if (function_exists('apc_clear_cache')) {
90
            apc_clear_cache('user');
91
            apc_clear_cache();
92
        }
93
94
        if (function_exists('wincache_ucache_clear')) {
95
            wincache_ucache_clear();
96
        }
97
98
        return $output->fetch();
99
    }
100
101
    /**
102 2
     * Doctrineのキャッシュを削除します.
103
     * APP_ENV=prodの場合のみ実行されます.
104 2
     *
105
     * @param null $env
106 2
     *
107 2
     * @return string
108 2
     *
109 1
     * @throws \Exception
110 1
     */
111 1
    public function clearDoctrineCache()
112
    {
113
        if ($this->kernel->getEnvironment() !== 'prod') {
114
            return;
115
        }
116
        $console = new Application($this->kernel);
117 1
        $console->setAutoExit(false);
118 1
119 1
        $command = [
120
            'command' => 'cache:pool:clear',
121 1
            'pools' => ['doctrine.app_cache_pool'],
122 1
            '--no-ansi' => true,
123 1
        ];
124
125 1
        $input = new ArrayInput($command);
126 1
127 1
        $output = new BufferedOutput(
128
            OutputInterface::VERBOSITY_DEBUG,
129 1
            true
130
        );
131
132
        $console->run($input, $output);
133
134
        return $output->fetch();
135 2
    }
136
137
    /**
138
     * Twigキャッシュを削除します.
139 2
     */
140
    public function clearTwigCache()
141
    {
142
        $cacheDir = $this->kernel->getCacheDir().'/twig';
143
        $fs = new Filesystem();
144 2
        $fs->remove($cacheDir);
145
    }
146
147
    /**
148 2
     * キャッシュを削除する.
149
     *
150
     * doctrine, profiler, twig によって生成されたキャッシュディレクトリを削除する.
151
     * キャッシュは $app['config']['root_dir'].'/app/cache' に生成されます.
152
     *
153
     * @param Application $app
154 1
     * @param boolean $isAll .gitkeep を残してすべてのファイル・ディレクトリを削除する場合 true, 各ディレクトリのみを削除する場合 false
155
     * @param boolean $isTwig Twigキャッシュファイルのみ削除する場合 true
156 1
     *
157
     * @return boolean 削除に成功した場合 true
158
     *
159
     * @deprecated CacheUtil::clearCacheを利用すること
160
     */
161
    public static function clear($app, $isAll, $isTwig = false)
162
    {
163
        $cacheDir = $app['config']['root_dir'].'/app/cache';
164
165
        $filesystem = new Filesystem();
166
        $finder = Finder::create()->notName('.gitkeep')->files();
167
        if ($isAll) {
168
            $finder = $finder->in($cacheDir);
169
            $filesystem->remove($finder);
0 ignored issues
show
$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...
170 View Code Duplication
        } elseif ($isTwig) {
171
            if (is_dir($cacheDir.'/twig')) {
172
                $finder = $finder->in($cacheDir.'/twig');
173
                $filesystem->remove($finder);
174
            }
175
        } else {
176 View Code Duplication
            if (is_dir($cacheDir.'/doctrine')) {
177
                $finder = $finder->in($cacheDir.'/doctrine');
178
                $filesystem->remove($finder);
0 ignored issues
show
$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...
179
            }
180 View Code Duplication
            if (is_dir($cacheDir.'/profiler')) {
181
                $finder = $finder->in($cacheDir.'/profiler');
182
                $filesystem->remove($finder);
0 ignored issues
show
$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...
183
            }
184 View Code Duplication
            if (is_dir($cacheDir.'/twig')) {
185
                $finder = $finder->in($cacheDir.'/twig');
186
                $filesystem->remove($finder);
0 ignored issues
show
$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...
187
            }
188 View Code Duplication
            if (is_dir($cacheDir.'/translator')) {
189
                $finder = $finder->in($cacheDir.'/translator');
190
                $filesystem->remove($finder);
191
            }
192
        }
193
194
        if (function_exists('opcache_reset')) {
195
            opcache_reset();
196
        }
197
198
        if (function_exists('apc_clear_cache')) {
199
            apc_clear_cache('user');
200
            apc_clear_cache();
201
        }
202
203
        if (function_exists('wincache_ucache_clear')) {
204
            wincache_ucache_clear();
205
        }
206
207
        return true;
208
    }
209
210
    /**
211
     * {@inheritdoc}
212
     */
213
    public static function getSubscribedEvents()
214
    {
215
        return [KernelEvents::TERMINATE => 'forceClearCache'];
216
    }
217
}
218