1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Yawik project. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Core\Service; |
11
|
|
|
|
12
|
|
|
use Core\Application; |
13
|
|
|
use Interop\Container\ContainerInterface; |
14
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
15
|
|
|
use Symfony\Component\Finder\Finder; |
16
|
|
|
use Zend\ModuleManager\Listener\ListenerOptions; |
17
|
|
|
use Zend\Stdlib\Glob; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class CacheWarmupService |
21
|
|
|
* |
22
|
|
|
* @package Core\Service |
23
|
|
|
* @author Anthonius Munthi <[email protected]> |
24
|
|
|
* @since 0.32 |
25
|
|
|
*/ |
26
|
|
|
class ClearCacheService |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var ListenerOptions |
30
|
|
|
*/ |
31
|
|
|
private $options; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Filesystem |
35
|
|
|
*/ |
36
|
|
|
private $filesystem; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Clear cache constructor. |
40
|
|
|
* @param ListenerOptions $options |
41
|
|
|
* @param Filesystem|null $filesystem |
42
|
|
|
*/ |
43
|
|
|
public function __construct(ListenerOptions $options, Filesystem $filesystem = null) |
44
|
|
|
{ |
45
|
|
|
if (is_null($filesystem)) { |
46
|
|
|
$filesystem = new Filesystem(); |
47
|
|
|
} |
48
|
|
|
$this->options = $options; |
49
|
|
|
$this->filesystem = $filesystem; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Creates new ClearCacheService object |
54
|
|
|
* @param ContainerInterface $container |
55
|
|
|
* @return ClearCacheService |
56
|
|
|
*/ |
57
|
|
|
public static function factory(ContainerInterface $container) |
58
|
|
|
{ |
59
|
|
|
/* @var \Zend\ModuleManager\ModuleManager $manager */ |
60
|
|
|
$config = $container->get('ApplicationConfig'); |
61
|
|
|
$options = new ListenerOptions($config['module_listener_options']); |
62
|
|
|
return new static($options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Clear all cache files |
67
|
|
|
*/ |
68
|
|
|
public function clearCache() |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
// do not clear cache when cache directory not exists |
71
|
|
|
if (is_null($cacheDir = $this->options->getCacheDir()) || !is_dir($cacheDir)) { |
72
|
|
|
return false; |
73
|
|
|
} |
74
|
|
|
$finder = Finder::create() |
75
|
|
|
->in($cacheDir) |
76
|
|
|
->ignoreDotFiles(false) |
77
|
|
|
->name('*.php') |
78
|
|
|
->name('.checksum') |
79
|
|
|
->files() |
80
|
|
|
; |
81
|
|
|
$fs = $this->filesystem; |
82
|
|
|
$fs->remove($finder); |
83
|
|
|
return true; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* This function will check cache by creating md5 sum |
88
|
|
|
* from all file modification time in config/autoload/*.php. |
89
|
|
|
* If checksum is invalid it will automatically call clear cache. |
90
|
|
|
*/ |
91
|
|
|
public function checkCache() |
92
|
|
|
{ |
93
|
|
|
$options = $this->options; |
94
|
|
|
|
95
|
|
|
$configDir = Application::getConfigDir(); |
96
|
|
|
|
97
|
|
|
$cacheDir = $options->getCacheDir(); |
98
|
|
|
|
99
|
|
|
if (!is_dir($cacheDir)) { |
100
|
|
|
mkdir($cacheDir, 0777, true); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$mtimes = []; |
104
|
|
|
$mtimes[] = filemtime($configDir.'/config.php'); |
105
|
|
|
|
106
|
|
|
foreach ($options->getConfigGlobPaths() as $path) { |
107
|
|
|
foreach (Glob::glob($path, Glob::GLOB_BRACE) as $file) { |
108
|
|
|
$mtimes[] = filemtime($file); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
$checksum = md5(serialize($mtimes)); |
113
|
|
|
|
114
|
|
|
$checksumFile = $options->getCacheDir().'/.checksum'; |
115
|
|
|
if (!file_exists($checksumFile)) { |
116
|
|
|
touch($checksumFile); |
117
|
|
|
} |
118
|
|
|
$cacheSum = file_get_contents($checksumFile); |
119
|
|
|
if ($cacheSum != $checksum) { |
120
|
|
|
$this->clearCache(); |
121
|
|
|
file_put_contents($checksumFile, $checksum, LOCK_EX); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.