1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverLeague\IDEAnnotator\Helpers; |
4
|
|
|
|
5
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
6
|
|
|
use ReflectionException; |
7
|
|
|
use SilverLeague\IDEAnnotator\DataObjectAnnotator; |
8
|
|
|
use SilverStripe\Control\Controller; |
9
|
|
|
use SilverStripe\Control\Director; |
10
|
|
|
use SilverStripe\Core\Extension; |
11
|
|
|
use SilverStripe\Core\Injector\Injector; |
12
|
|
|
use SilverStripe\Core\Manifest\ModuleManifest; |
13
|
|
|
use SilverStripe\ORM\DataExtension; |
14
|
|
|
use SilverStripe\ORM\DataObject; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class AnnotatePermissionChecker |
18
|
|
|
* |
19
|
|
|
* Helperclass to check if the current environment, class or module is allowed to be annotated. |
20
|
|
|
* This is abstracted from @see DataObjectAnnotator to separate and clean up. |
21
|
|
|
* |
22
|
|
|
* @package IDEAnnotator/Helpers |
23
|
|
|
*/ |
24
|
|
|
class AnnotatePermissionChecker |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* In the future we will support other Classes as well. |
29
|
|
|
* We list the core classes, but in fact only it's subclasses are supported |
30
|
|
|
* @see AnnotatePermissionChecker::classNameIsSupported(); |
31
|
|
|
*/ |
32
|
|
|
protected $supportedParentClasses = [ |
33
|
|
|
DataObject::class, |
34
|
|
|
DataExtension::class, |
35
|
|
|
Controller::class, |
36
|
|
|
Extension::class |
37
|
|
|
]; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
|
|
public function environmentIsAllowed() |
43
|
|
|
{ |
44
|
|
|
if (!$this->isEnabled()) { |
45
|
|
|
return false; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
return Director::isDev(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function isEnabled() |
55
|
|
|
{ |
56
|
|
|
return (bool)DataObjectAnnotator::config()->get('enabled'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return array |
61
|
|
|
*/ |
62
|
|
|
public function getSupportedParentClasses() |
63
|
|
|
{ |
64
|
|
|
return $this->supportedParentClasses; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Check if a DataObject or DataExtension subclass is allowed by checking if the file |
69
|
|
|
* is in the $allowed_modules array |
70
|
|
|
* The permission is checked by matching the filePath and modulePath |
71
|
|
|
* |
72
|
|
|
* @param $className |
73
|
|
|
* |
74
|
|
|
* @return bool |
75
|
|
|
* @throws NotFoundExceptionInterface |
76
|
|
|
* @throws ReflectionException |
77
|
|
|
*/ |
78
|
|
|
public function classNameIsAllowed($className) |
79
|
|
|
{ |
80
|
|
|
if ($this->classNameIsSupported($className)) { |
81
|
|
|
$classInfo = new AnnotateClassInfo($className); |
82
|
|
|
$filePath = $classInfo->getClassFilePath(); |
83
|
|
|
$module = Injector::inst()->createWithArgs( |
84
|
|
|
ModuleManifest::class, |
85
|
|
|
[Director::baseFolder()] |
86
|
|
|
)->getModuleByPath($filePath); |
87
|
|
|
|
88
|
|
|
$allowedModules = (array)DataObjectAnnotator::config()->get('enabled_modules'); |
89
|
|
|
|
90
|
|
|
return in_array($module->getName(), $allowedModules, true); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return false; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Check if a (subclass of ) class is a supported |
98
|
|
|
* |
99
|
|
|
* @param $className |
100
|
|
|
* @return bool |
101
|
|
|
*/ |
102
|
|
|
public function classNameIsSupported($className) |
103
|
|
|
{ |
104
|
|
|
foreach ($this->supportedParentClasses as $supportedParent) { |
105
|
|
|
if (is_subclass_of($className, $supportedParent)) { |
106
|
|
|
return true; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
return false; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Check if a module is in the $allowed_modules array |
115
|
|
|
* Required for the buildTask. |
116
|
|
|
* |
117
|
|
|
* @param string $moduleName |
118
|
|
|
* |
119
|
|
|
* @return bool |
120
|
|
|
*/ |
121
|
|
|
public function moduleIsAllowed($moduleName) |
122
|
|
|
{ |
123
|
|
|
return in_array($moduleName, $this->enabledModules(), false); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
|
|
public function enabledModules() |
130
|
|
|
{ |
131
|
|
|
$enabled = (array)DataObjectAnnotator::config()->get('enabled_modules'); |
132
|
|
|
|
133
|
|
|
// modules might be enabled more then once. |
134
|
|
|
return array_combine($enabled, $enabled); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|