|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
|
|
4
|
|
|
class DataIntegrityTestYML extends BuildTask |
|
5
|
|
|
{ |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* list of files you want to check |
|
9
|
|
|
* @var array |
|
10
|
|
|
*/ |
|
11
|
|
|
private static $config_files = array("mysite/_config/config.yml"); |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* list of classes that do not need to be checked |
|
15
|
|
|
* NB: they are all lowercase, as we test for them only lowercase! |
|
16
|
|
|
* @var array |
|
17
|
|
|
*/ |
|
18
|
|
|
private static $classes_to_skip = array("name", "before", "only", "after"); |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* list of variables that do not need checking... |
|
22
|
|
|
* @var array |
|
23
|
|
|
*/ |
|
24
|
|
|
private static $variables_to_skip = array(); |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* standard SS variable |
|
28
|
|
|
* @var String |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $title = "Check your config files for rogue entries."; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* standard SS variable |
|
34
|
|
|
* @var String |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $description = "Checks a selection of yml files to see if there are any entries that may be incorrect."; |
|
37
|
|
|
|
|
38
|
|
|
public function run($request) |
|
39
|
|
|
{ |
|
40
|
|
|
ini_set('max_execution_time', 3000); |
|
41
|
|
|
require_once 'thirdparty/spyc/spyc.php'; |
|
42
|
|
|
$filesArray = Config::inst()->get("DataIntegrityTestYML", "config_files"); |
|
43
|
|
|
$classesToSkip = Config::inst()->get("DataIntegrityTestYML", "classes_to_skip"); |
|
44
|
|
|
$variablesToSkip = Config::inst()->get("DataIntegrityTestYML", "variables_to_skip"); |
|
45
|
|
|
foreach ($filesArray as $folderAndFileLocation) { |
|
|
|
|
|
|
46
|
|
|
db::alteration_message("<h2>Checking $folderAndFileLocation</h2>"); |
|
47
|
|
|
$fixtureFolderAndFile = Director::baseFolder().'/'. $folderAndFileLocation; |
|
48
|
|
|
if (!file_exists($fixtureFolderAndFile)) { |
|
49
|
|
|
user_error('No custom configuration has been setup here : "' . $fixtureFolderAndFile . '" set the files here: DataIntegrityTestYML::config_files', E_USER_NOTICE); |
|
50
|
|
|
} |
|
51
|
|
|
$parser = new Spyc(); |
|
52
|
|
|
$arrayOfSettings = $parser->loadFile($fixtureFolderAndFile); |
|
53
|
|
|
foreach ($arrayOfSettings as $className => $variables) { |
|
54
|
|
|
if (in_array(strtolower($className), $classesToSkip)) { |
|
55
|
|
|
db::alteration_message("$className : skipped"); |
|
56
|
|
|
} else { |
|
57
|
|
|
echo "<br /><br />"; |
|
58
|
|
|
if (!class_exists($className)) { |
|
59
|
|
|
db::alteration_message("$className does not exist", "deleted"); |
|
60
|
|
|
} else { |
|
61
|
|
|
db::alteration_message("$className", "created"); |
|
62
|
|
|
foreach ($variables as $variable => $setting) { |
|
63
|
|
|
if ($variable == "icon") { |
|
64
|
|
|
$fileLocationForOthers = Director::baseFolder().'/'.$setting; |
|
65
|
|
|
$fileLocationForSiteTree = Director::baseFolder().'/'.$setting.'-file.gif'; |
|
66
|
|
|
if ($className::create() instanceof SiteTree) { |
|
67
|
|
View Code Duplication |
if (!file_exists($fileLocationForSiteTree)) { |
|
|
|
|
|
|
68
|
|
|
db::alteration_message(" <u>$className.$variable</u> icon $fileLocationForSiteTree can not be found", "deleted"); |
|
69
|
|
|
} else { |
|
70
|
|
|
db::alteration_message(" <u>$className.$variable</u> icon $fileLocationForSiteTree exists", "created"); |
|
71
|
|
|
} |
|
72
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
73
|
|
|
if (!file_exists($fileLocationForOthers)) { |
|
74
|
|
|
db::alteration_message(" <u>$className.$variable</u> icon $fileLocationForOthers can not be found", "deleted"); |
|
75
|
|
|
} else { |
|
76
|
|
|
db::alteration_message(" <u>$className.$variable</u> icon $fileLocationForOthers exists", "created"); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
} elseif ($variable == "extensions") { |
|
80
|
|
|
if (!is_array($setting)) { |
|
81
|
|
|
db::alteration_message(" <u>$className.$variable</u> extensions should be set as an array.", "deleted"); |
|
82
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
83
|
|
|
foreach ($setting as $extensionClassName) { |
|
84
|
|
|
if (!class_exists($extensionClassName)) { |
|
85
|
|
|
db::alteration_message(" <u>$className.$variable</u> extension class <u>$extensionClassName</u> does not exist", "deleted"); |
|
86
|
|
|
} else { |
|
87
|
|
|
db::alteration_message(" <u>$className.$variable</u> extension class <u>$extensionClassName</u> found", "created"); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} elseif (in_array($variable, $variablesToSkip)) { |
|
92
|
|
|
db::alteration_message(" <u>$className.$variable</u> skipped"); |
|
93
|
|
View Code Duplication |
} else { |
|
|
|
|
|
|
94
|
|
|
if (!property_exists($className, $variable)) { |
|
95
|
|
|
db::alteration_message(" <u>$className.$variable</u> does not exist", "deleted"); |
|
96
|
|
|
} else { |
|
97
|
|
|
db::alteration_message(" <u>$className.$variable</u> found", "created"); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.