/
RequestFlushDiscoverer.php
87 lines (76 loc) · 2.17 KB
/
RequestFlushDiscoverer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
namespace SilverStripe\Core\Startup;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Core\Kernel;
use SilverStripe\Core\Environment;
/**
* The default flush discovery implementation
*
* - if request has `flush` or URL is `dev/build`
* - AND in CLI or DEV mode
* - then flush
*/
class RequestFlushDiscoverer implements FlushDiscoverer
{
/**
* Environment type (dev, test or live)
*
* @var string
*/
protected $env;
/**
* Active request instance (session is not initialized yet!)
*
* @var HTTPRequest
*/
protected $request;
/**
* Initialize it with active Request and Kernel
*
* @param HTTPRequest $request instance of the request (session is not initialized yet!)
* @param string $env Environment type (dev, test or live)
*/
public function __construct(HTTPRequest $request, $env)
{
$this->env = $env;
$this->request = $request;
}
/**
* Checks whether the request contains any flush indicators
*
*
* @return null|bool flush or don't care
*/
protected function lookupRequest()
{
$request = $this->request;
$getVar = array_key_exists('flush', $request->getVars() ?? []);
$devBuild = $request->getURL() === 'dev/build';
// WARNING!
// We specifically return `null` and not `false` here so that
// it does not override other FlushDiscoverers
return ($getVar || $devBuild) ? true : null;
}
/**
* Checks for permission to flush
*
* Startup flush through a request is only allowed
* to CLI or DEV modes for security reasons
*
* @return bool|null true for allow, false for denying, or null if don't care
*/
protected function isAllowed()
{
// WARNING!
// We specifically return `null` and not `false` here so that
// it does not override other FlushDiscoverers
return (Environment::isCli() || $this->env === Kernel::DEV) ? true : null;
}
public function shouldFlush()
{
if (!$allowed = $this->isAllowed()) {
return $allowed;
}
return $this->lookupRequest();
}
}