1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the PHP Translation package. |
5
|
|
|
* |
6
|
|
|
* (c) PHP Translation team <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Translation\Bundle\Service; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\Finder\Finder; |
16
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\WarmableInterface; |
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* A service able to read and clear the Symfony Translation cache. |
21
|
|
|
* |
22
|
|
|
* @author Damien A. <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
final class CacheClearer |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
private $kernelCacheDir; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var TranslatorInterface |
33
|
|
|
*/ |
34
|
|
|
private $translator; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var Filesystem |
38
|
|
|
*/ |
39
|
|
|
private $filesystem; |
40
|
|
|
|
41
|
|
|
public function __construct($kernelCacheDir, TranslatorInterface $translator, Filesystem $filesystem) |
42
|
|
|
{ |
43
|
|
|
$this->kernelCacheDir = $kernelCacheDir; |
44
|
|
|
$this->translator = $translator; |
45
|
|
|
$this->filesystem = $filesystem; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Remove the Symfony translation cache and warm it up again. |
50
|
|
|
* |
51
|
|
|
* @param string|null $locale Optional filter to clear only one locale. |
52
|
|
|
*/ |
53
|
|
|
public function clearAndWarmUp($locale = null) |
54
|
|
|
{ |
55
|
|
|
$translationDir = sprintf('%s/translations', $this->kernelCacheDir); |
56
|
|
|
|
57
|
|
|
$finder = new Finder(); |
58
|
|
|
|
59
|
|
|
// Make sure the directory exists |
60
|
|
|
$this->filesystem->mkdir($translationDir); |
61
|
|
|
|
62
|
|
|
// Remove the translations for this locale |
63
|
|
|
$files = $finder->files()->name($locale ? '*.'.$locale.'.*' : '*')->in($translationDir); |
64
|
|
|
foreach ($files as $file) { |
65
|
|
|
$this->filesystem->remove($file); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// Build them again |
69
|
|
|
if ($this->translator instanceof WarmableInterface) { |
70
|
|
|
$this->translator->warmUp($translationDir); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|