1
|
|
|
<?php |
2
|
|
|
/******************************************************************************* |
3
|
|
|
* This file is part of the GraphQL Bundle package. |
4
|
|
|
* |
5
|
|
|
* (c) YnloUltratech <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
******************************************************************************/ |
10
|
|
|
|
11
|
|
|
namespace Ynlo\GraphQLBundle\Cache; |
12
|
|
|
|
13
|
|
|
use Symfony\Component\Finder\Finder; |
14
|
|
|
use Symfony\Component\HttpKernel\CacheWarmer\CacheWarmer; |
15
|
|
|
use Symfony\Component\HttpKernel\Kernel; |
16
|
|
|
use Ynlo\GraphQLBundle\Definition\Registry\DefinitionRegistry; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* DefinitionCacheWarmer |
20
|
|
|
*/ |
21
|
|
|
class DefinitionCacheWarmer extends CacheWarmer |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var DefinitionRegistry |
25
|
|
|
*/ |
26
|
|
|
protected $registry; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Kernel |
30
|
|
|
*/ |
31
|
|
|
protected $kernel; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* DefinitionCacheWarmer constructor. |
35
|
|
|
* |
36
|
|
|
* @param Kernel $kernel |
37
|
|
|
* @param DefinitionRegistry $registry |
38
|
|
|
*/ |
39
|
|
|
public function __construct(Kernel $kernel, DefinitionRegistry $registry) |
40
|
|
|
{ |
41
|
|
|
$this->kernel = $kernel; |
42
|
|
|
$this->registry = $registry; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
48
|
|
|
public function isOptional() |
49
|
|
|
{ |
50
|
|
|
return true; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritDoc} |
55
|
|
|
*/ |
56
|
|
|
public function warmUp($cacheDir) |
57
|
|
|
{ |
58
|
|
|
$this->registry->clearCache(true); |
59
|
|
|
$this->updateControlFile(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return bool |
64
|
|
|
*/ |
65
|
|
|
protected function isFreshCache() |
66
|
|
|
{ |
67
|
|
|
if (!file_exists($this->getControlFileName())) { |
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$controlTime = filemtime($this->getControlFileName()); |
72
|
|
|
|
73
|
|
|
$projectDir = $this->kernel->getProjectDir(); |
74
|
|
|
|
75
|
|
|
if (Kernel::VERSION_ID >= 40000) { |
76
|
|
|
$dirs[] = $projectDir.'/config'; |
|
|
|
|
77
|
|
|
$dirs[] = $projectDir.'/src'; |
78
|
|
|
} else { |
79
|
|
|
$dirs[] = $this->kernel->getRootDir(); |
|
|
|
|
80
|
|
|
$dirs[] = $this->kernel->getRootDir().'/../src'; |
|
|
|
|
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** @var iterable $files */ |
84
|
|
|
$files = Finder::create() |
85
|
|
|
->in($dirs) |
86
|
|
|
->date(sprintf('>= %s', date('Y-m-d H:i:s', $controlTime))) |
87
|
|
|
->files(); |
88
|
|
|
|
89
|
|
|
//exist at least one modified file |
90
|
|
|
foreach ($files as $file) { |
91
|
|
|
return false; |
92
|
|
|
break; |
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
protected function getControlFileName() |
99
|
|
|
{ |
100
|
|
|
return $this->kernel->getCacheDir().'/graphal.schema.timestamp'; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function updateControlFile() |
104
|
|
|
{ |
105
|
|
|
file_put_contents($this->getControlFileName(), time()); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|