|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SilverStripe\Core\Startup; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Kernel; |
|
6
|
|
|
use SilverStripe\Core\Environment; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Checks whether a filesystem resource has been changed since |
|
10
|
|
|
* the manifest generation |
|
11
|
|
|
* |
|
12
|
|
|
* For this discoverer to get activated you should define SS_FLUSH_ON_DEPLOY |
|
13
|
|
|
* variable |
|
14
|
|
|
* - if the environment variable SS_FLUSH_ON_DEPLOY undefined or `false`, then does nothing |
|
15
|
|
|
* - if SS_FLUSH_ON_DEPLOY is true, then checks __FILE__ modification time |
|
16
|
|
|
* - otherwise takes {BASE_PATH/SS_FLUSH_ON_DEPLOY} as the resource to check |
|
17
|
|
|
* |
|
18
|
|
|
* Examples: |
|
19
|
|
|
* |
|
20
|
|
|
* - `SS_FLUSH_ON_DEPLOY=""` would check the BASE_PATH folder for modifications (not the files within) |
|
21
|
|
|
* - `SS_FLUSH_ON_DEPLOY=true` would check BASE_PATH/vendor/silverstripe/framework/src/Core/Startup/DeployFlushDiscoverer.php |
|
22
|
|
|
* file modification |
|
23
|
|
|
* - `SS_FLUSH_ON_DEPLOY=false` disable filesystem checks |
|
24
|
|
|
* - `SS_FLUSH_ON_DEPLOY="public/index.php"` checks BASE_PATH/public/index.php file modification time |
|
25
|
|
|
*/ |
|
26
|
|
|
class DeployFlushDiscoverer implements FlushDiscoverer |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* Active kernel |
|
30
|
|
|
* |
|
31
|
|
|
* @var Kernel |
|
32
|
|
|
*/ |
|
33
|
|
|
protected $kernel; |
|
34
|
|
|
|
|
35
|
|
|
public function __construct(Kernel $kernel) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->kernel = $kernel; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Returns the timestamp of the manifest generation or null |
|
42
|
|
|
* if no cache has been found (or couldn't read the cache) |
|
43
|
|
|
* |
|
44
|
|
|
* @return int|null unix timestamp |
|
45
|
|
|
*/ |
|
46
|
|
|
protected function getCacheTimestamp() |
|
47
|
|
|
{ |
|
48
|
|
|
$classLoader = $this->kernel->getClassLoader(); |
|
49
|
|
|
$classManifest = $classLoader->getManifest(); |
|
50
|
|
|
$cacheTimestamp = $classManifest->getManifestTimestamp(); |
|
51
|
|
|
|
|
52
|
|
|
return $cacheTimestamp; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Returns the resource to be checked for deployment |
|
57
|
|
|
* |
|
58
|
|
|
* - if the environment variable SS_FLUSH_ON_DEPLOY undefined or false, then returns null |
|
59
|
|
|
* - if SS_FLUSH_ON_DEPLOY is true, then takes __FILE__ as the resource to check |
|
60
|
|
|
* - otherwise takes {BASE_PATH/SS_FLUSH_ON_DEPLOY} as the resource to check |
|
61
|
|
|
* |
|
62
|
|
|
* @return string|null returns the resource path or null if not set |
|
63
|
|
|
*/ |
|
64
|
|
|
protected function getDeployResource() |
|
65
|
|
|
{ |
|
66
|
|
|
$resource = Environment::getEnv('SS_FLUSH_ON_DEPLOY'); |
|
67
|
|
|
|
|
68
|
|
|
if ($resource === false) { |
|
69
|
|
|
return null; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
if ($resource === true) { |
|
73
|
|
|
$path = __FILE__; |
|
74
|
|
|
} else { |
|
75
|
|
|
$path = sprintf("%s/%s", BASE_PATH, $resource); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $path; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Returns the resource modification timestamp |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $resource Path to the filesystem |
|
86
|
|
|
* |
|
87
|
|
|
* @return int |
|
88
|
|
|
*/ |
|
89
|
|
|
protected function getDeployTimestamp($resource) |
|
90
|
|
|
{ |
|
91
|
|
|
if (!file_exists($resource)) { |
|
92
|
|
|
return 0; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
return max(filemtime($resource), filectime($resource)); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Returns true if the deploy timestamp greater than the cache generation timestamp |
|
100
|
|
|
* |
|
101
|
|
|
* {@inheritdoc} |
|
102
|
|
|
*/ |
|
103
|
|
|
public function shouldFlush() |
|
104
|
|
|
{ |
|
105
|
|
|
$resource = $this->getDeployResource(); |
|
106
|
|
|
|
|
107
|
|
|
if (is_null($resource)) { |
|
108
|
|
|
return null; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$deploy = $this->getDeployTimestamp($resource); |
|
112
|
|
|
$cache = $this->getCacheTimestamp(); |
|
113
|
|
|
|
|
114
|
|
|
if ($deploy && $cache && $deploy > $cache) { |
|
115
|
|
|
return true; |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
return null; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|