Issues (83)

Service/CacheClearer.php (1 issue)

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 as LegacyTranslatorInterface;
0 ignored issues
show
The type Symfony\Component\Translation\TranslatorInterface was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
18
use Symfony\Contracts\Translation\TranslatorInterface;
19
20
/**
21
 * A service able to read and clear the Symfony Translation cache.
22
 *
23
 * @author Damien A. <[email protected]>
24
 */
25
final class CacheClearer
26
{
27
    /**
28
     * @var string
29
     */
30
    private $kernelCacheDir;
31
32
    /**
33
     * @var TranslatorInterface
34
     */
35
    private $translator;
36
37
    /**
38
     * @var Filesystem
39
     */
40
    private $filesystem;
41
42
    public function __construct(string $kernelCacheDir, $translator, Filesystem $filesystem)
43
    {
44
        // The TranslatorInterface has been deprecated in favor of Symfony\Contracts\Translation\TranslatorInterface in sf4.2.
45
        // Use this class to type hint event & remove the following condition once sf ^4.2 become the minimum supported version.
46
        // @see https://github.com/symfony/symfony/blob/master/UPGRADE-4.2.md#translation
47
        if (!$translator instanceof LegacyTranslatorInterface && !$translator instanceof TranslatorInterface) {
48
            throw new \InvalidArgumentException('Unable to deal with the given translator.');
49
        }
50
51
        $this->kernelCacheDir = $kernelCacheDir;
52
        $this->translator = $translator;
53
        $this->filesystem = $filesystem;
54
    }
55
56
    /**
57
     * Remove the Symfony translation cache and warm it up again.
58
     *
59
     * @param string|null $locale optional filter to clear only one locale
60
     */
61
    public function clearAndWarmUp(?string $locale = null): void
62
    {
63
        $translationDir = \sprintf('%s/translations', $this->kernelCacheDir);
64
65
        $finder = new Finder();
66
67
        // Make sure the directory exists
68
        $this->filesystem->mkdir($translationDir);
69
70
        // Remove the translations for this locale
71
        $files = $finder->files()->name($locale ? '*.'.$locale.'.*' : '*')->in($translationDir);
72
        foreach ($files as $file) {
73
            $this->filesystem->remove($file);
74
        }
75
76
        // Build them again
77
        if ($this->translator instanceof WarmableInterface) {
78
            $this->translator->warmUp($translationDir);
79
        }
80
    }
81
}
82