1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Zikula package. |
7
|
|
|
* |
8
|
|
|
* Copyright Zikula Foundation - https://ziku.la/ |
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 Zikula\SecurityCenterModule\Helper; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Filesystem\Exception\IOExceptionInterface; |
17
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
18
|
|
|
use Symfony\Component\HttpFoundation\Session\SessionInterface; |
19
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
20
|
|
|
|
21
|
|
|
class CacheDirHelper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var Filesystem |
25
|
|
|
*/ |
26
|
|
|
private $fileSystem; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var SessionInterface |
30
|
|
|
*/ |
31
|
|
|
private $session; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var TranslatorInterface |
35
|
|
|
*/ |
36
|
|
|
private $translator; |
37
|
|
|
|
38
|
|
|
public function __construct( |
39
|
|
|
Filesystem $fileSystem, |
40
|
|
|
SessionInterface $session, |
41
|
|
|
TranslatorInterface $translator |
42
|
|
|
) { |
43
|
|
|
$this->fileSystem = $fileSystem; |
44
|
|
|
$this->session = $session; |
45
|
|
|
$this->translator = $translator; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function ensureCacheDirectoryExists( |
49
|
|
|
string $cacheDirectory, |
50
|
|
|
bool $forHtmlPurifier = false |
51
|
|
|
): void { |
52
|
|
|
$fs = $this->fileSystem; |
53
|
|
|
|
54
|
|
|
try { |
55
|
|
|
if (!$fs->exists($cacheDirectory)) { |
56
|
|
|
if (true === $forHtmlPurifier) { |
57
|
|
|
// this uses always a fixed environment (e.g. "prod") that is serialized |
58
|
|
|
// in purifier configuration |
59
|
|
|
// so ensure the main directory exists even if another environment is currently used |
60
|
|
|
$parentDirectory = mb_substr($cacheDirectory, 0, -9); |
61
|
|
|
if (!$fs->exists($parentDirectory)) { |
62
|
|
|
$fs->mkdir($parentDirectory); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
$fs->mkdir($cacheDirectory); |
66
|
|
|
} |
67
|
|
|
} catch (IOExceptionInterface $exception) { |
68
|
|
|
$this->session->getFlashBag()->add( |
69
|
|
|
'error', |
70
|
|
|
$this->translator->trans( |
71
|
|
|
'An error occurred while creating cache directory at %path%', |
72
|
|
|
['%path%' => $exception->getPath()] |
73
|
|
|
) |
74
|
|
|
); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|