1 | <?php |
||
2 | |||
3 | namespace Sunnysideup\MigrateData\Tasks; |
||
4 | |||
5 | use SilverStripe\Control\Director; |
||
6 | |||
7 | /** |
||
8 | * Reorganizing the SiteTree led to records being in the _Live table but not in the |
||
9 | * draft table. This tasks should be run once to get rid of them. |
||
10 | */ |
||
11 | class UpgradeOnlyCheckYMLClassNames extends MigrateDataTaskBase |
||
12 | { |
||
13 | /** |
||
14 | * @var string |
||
15 | */ |
||
16 | protected $title = 'Load your list of class names'; |
||
17 | |||
18 | protected $description = 'Upgrade ONLY!'; |
||
19 | |||
20 | protected $enabled = true; |
||
21 | |||
22 | private static $segment = 'check-yml-class-names'; |
||
23 | |||
24 | private static $folders_to_ignore = [ |
||
25 | 'public', |
||
26 | 'resources', |
||
27 | 'themes', |
||
28 | 'vendor', |
||
29 | ]; |
||
30 | |||
31 | private static $files_to_ignore = [ |
||
32 | 'database.legacy.yml', |
||
33 | ]; |
||
34 | |||
35 | /** |
||
36 | * Implement this method in the task subclass to |
||
37 | * execute via the TaskRunner. |
||
38 | */ |
||
39 | protected function performMigration() |
||
40 | { |
||
41 | $count = 0; |
||
0 ignored issues
–
show
Unused Code
introduced
by
![]() |
|||
42 | $subDirs = $this->getSubDirectories(Director::baseFolder() . '/'); |
||
43 | $ymlFiles = []; |
||
44 | foreach ($subDirs as $subDir) { |
||
45 | $subDir = rtrim($subDir, '/') . '/'; |
||
46 | $fullSubDir = $subDir . '_config/'; |
||
47 | if (file_exists($fullSubDir)) { |
||
48 | $toAdds = $this->getYMLFiles($fullSubDir); |
||
49 | foreach ($toAdds as $toAdd) { |
||
50 | if (! in_array(basename($toAdd), $this->Config()->get('files_to_ignore'), true)) { |
||
51 | $ymlFiles[] = $toAdd; |
||
52 | } |
||
53 | } |
||
54 | } |
||
55 | } |
||
56 | foreach ($ymlFiles as $fileName) { |
||
57 | $this->flushNowLine(); |
||
58 | $this->flushNow('STARTING TESTING: ' . $fileName . ''); |
||
59 | $this->flushNowLine(); |
||
60 | $count = 0; |
||
61 | $alreadySet = []; |
||
62 | if (! file_exists($fileName)) { |
||
63 | die(' Could not find ' . $fileName); |
||
0 ignored issues
–
show
|
|||
64 | } |
||
65 | $fp = fopen($fileName, 'r'); |
||
66 | $hasLine = true; |
||
67 | while ($hasLine) { |
||
68 | $line = stream_get_line($fp, 1024 * 1024, "\n"); |
||
69 | if (! $line) { |
||
70 | break; |
||
71 | } |
||
72 | ++$count; |
||
73 | $isProperty = false; |
||
74 | // $this->flushNow( '...'; |
||
75 | //skip lines that are indented |
||
76 | if (' ' === substr((string) $line, 0, 1)) { |
||
77 | $isProperty = true; |
||
78 | } |
||
79 | |||
80 | if ($isProperty) { |
||
81 | $className = end($alreadySet); |
||
82 | if ($className && class_exists($className)) { |
||
83 | if (strpos($line, ':')) { |
||
84 | $myItems = explode(':', $line); |
||
85 | if (2 === count($myItems) && $myItems[0] && $myItems[1]) { |
||
86 | $property = trim($myItems[0]); |
||
0 ignored issues
–
show
|
|||
87 | $property = trim($myItems[0], "'"); |
||
88 | $property = trim($myItems[0], '"'); |
||
89 | if (strpos($property, '\\')) { |
||
90 | if (! class_exists($property)) { |
||
91 | $this->flushNow(' |
||
92 | ERROR ' . $className . '.' . $property . ' may not exist as class name but looks like one.<br>'); |
||
93 | } |
||
94 | } elseif (strpos($property, '*')) { |
||
95 | $this->flushNow(' |
||
96 | ERROR ' . $className . '.' . $property . ' contains *.<br>'); |
||
97 | } elseif (! property_exists($className, $property)) { |
||
98 | $this->flushNow(' |
||
99 | ERROR ' . $className . '.' . $property . ' property could not be found<br>'); |
||
100 | } else { |
||
101 | $this->flushNow(' |
||
102 | SUCCESS ' . $className . '.' . $property . ''); |
||
103 | } |
||
104 | } else { |
||
105 | $this->flushNow(' |
||
106 | ' . $line . ' ... not two items<br>'); |
||
107 | } |
||
108 | } else { |
||
109 | $this->flushNow(' |
||
110 | ' . $line . ' ... no colon<br>'); |
||
111 | } |
||
112 | } elseif ($className) { |
||
113 | $this->flushNow(' |
||
114 | COULD NOT FIND ' . $className . '<br>'); |
||
115 | } |
||
116 | } else { |
||
117 | if (! strpos($line, '\\')) { |
||
118 | continue; |
||
119 | } |
||
120 | if (strpos($line, '*')) { |
||
121 | continue; |
||
122 | } |
||
123 | $line = str_replace(':', '', (string) $line); |
||
124 | $line = trim($line); |
||
125 | if (isset($alreadySet[$line])) { |
||
126 | $this->flushNow(' |
||
127 | |||
128 | ERROR: Two mentions of ' . $line . '<br>'); |
||
129 | } else { |
||
130 | $alreadySet[$line] = $line; |
||
131 | if (! class_exists($line)) { |
||
132 | $this->flushNow(' |
||
133 | |||
134 | ERROR: Could not find class ' . $line . '<br>'); |
||
135 | } |
||
136 | } |
||
137 | } |
||
138 | //not a class name |
||
139 | } |
||
140 | fclose($fp); |
||
141 | $this->flushNowLine(); |
||
142 | $this->flushNow('' . $count . ' lines'); |
||
143 | $this->flushNowLine(); |
||
144 | $count = 0; |
||
145 | } |
||
146 | } |
||
147 | |||
148 | private function getSubDirectories($dir) |
||
149 | { |
||
150 | $subDirs = []; |
||
151 | $ignore = $this->Config()->folders_to_ignore; |
||
152 | $dir = rtrim($dir, '/') . '/'; |
||
153 | // Get and add directories of $dir |
||
154 | $directories = array_filter(glob($dir . '*'), 'is_dir'); |
||
155 | // $subDir = array_merge($subDir, $directories); |
||
156 | // Foreach directory, recursively get and add sub directories |
||
157 | foreach ($directories as $directory) { |
||
158 | if (! in_array(basename($directory), $ignore, true)) { |
||
159 | $subDirs[] = $directory; |
||
160 | } |
||
161 | } |
||
162 | |||
163 | return $subDirs; |
||
164 | // Return list of sub directories |
||
165 | } |
||
166 | |||
167 | private function getYMLFiles($dir) |
||
168 | { |
||
169 | $dir = rtrim($dir, '/') . '/'; |
||
170 | $files = glob($dir . '*.yaml'); |
||
171 | foreach (glob($dir . '*.yml') as $file) { |
||
172 | $files[] = $file; |
||
173 | } |
||
174 | |||
175 | return $files; |
||
176 | } |
||
177 | } |
||
178 |