1 | <?php |
||
2 | |||
3 | namespace LeKoala\Multilingual; |
||
4 | |||
5 | use SilverStripe\ORM\ArrayLib; |
||
6 | use SilverStripe\Core\ClassInfo; |
||
7 | use SilverStripe\ORM\DataObject; |
||
8 | use SilverStripe\Control\Director; |
||
9 | use SilverStripe\Core\Environment; |
||
10 | use SilverStripe\Control\HTTPRequest; |
||
11 | use SilverStripe\Core\Manifest\ClassLoader; |
||
12 | use SilverStripe\Core\Manifest\ModuleLoader; |
||
13 | |||
14 | /** |
||
15 | * Makes your life easier with build tasks |
||
16 | * TODO: make this into a separate module |
||
17 | */ |
||
18 | trait BuildTaskTools |
||
19 | { |
||
20 | /** |
||
21 | * @var \SilverStripe\Control\HTTPRequest |
||
22 | */ |
||
23 | protected $request; |
||
24 | |||
25 | /** |
||
26 | * @var array<string,array<mixed>> |
||
27 | */ |
||
28 | protected $options = []; |
||
29 | |||
30 | /** |
||
31 | * Increase time limit |
||
32 | * |
||
33 | * @return void |
||
34 | */ |
||
35 | protected function increaseTimeLimitTo($timeLimit = null) |
||
36 | { |
||
37 | Environment::increaseTimeLimitTo($timeLimit); |
||
38 | if (!$timeLimit) { |
||
39 | $this->message("Time limit is disabled", "info"); |
||
40 | } else { |
||
41 | $this->message("Time limit has been set to $timeLimit seconds", "info"); |
||
42 | } |
||
43 | } |
||
44 | |||
45 | /** |
||
46 | * Rebuild the class manifest |
||
47 | * |
||
48 | * @return void |
||
49 | */ |
||
50 | protected function regenerateClassManifest() |
||
51 | { |
||
52 | ClassLoader::inst()->getManifest()->regenerate(false); |
||
53 | $this->message("The class manifest has been rebuilt", "created"); |
||
54 | } |
||
55 | |||
56 | /** |
||
57 | * All dataobjects |
||
58 | * |
||
59 | * @return array |
||
60 | */ |
||
61 | protected function getValidDataObjects() |
||
62 | { |
||
63 | $list = ClassInfo::getValidSubClasses(DataObject::class); |
||
64 | array_shift($list); |
||
65 | return $list; |
||
66 | } |
||
67 | |||
68 | /** |
||
69 | * All modules |
||
70 | * |
||
71 | * @return array |
||
72 | */ |
||
73 | protected function getModules() |
||
74 | { |
||
75 | return ArrayLib::valuekey(array_keys(ModuleLoader::inst()->getManifest()->getModules())); |
||
0 ignored issues
–
show
|
|||
76 | } |
||
77 | |||
78 | /** |
||
79 | * All modules + themes |
||
80 | * |
||
81 | * @return array |
||
82 | */ |
||
83 | protected function getModulesAndThemes() |
||
84 | { |
||
85 | $themes = Director::baseFolder() . '/themes'; |
||
86 | $folders = glob($themes . '/*'); |
||
87 | $modules = $this->getModules(); |
||
88 | foreach ($folders as $f) { |
||
89 | $modules['themes:' . basename($f)] = 'themes/' . basename($f); |
||
90 | } |
||
91 | return $modules; |
||
92 | } |
||
93 | |||
94 | /** |
||
95 | * Get the request (and keep your imports clean :-) ) |
||
96 | * |
||
97 | * @return HTTPRequest |
||
98 | */ |
||
99 | protected function getRequest() |
||
100 | { |
||
101 | if (!$this->request) { |
||
102 | die('Make sure to call $this->request = $request in your own class'); |
||
0 ignored issues
–
show
|
|||
103 | } |
||
104 | return $this->request; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * Add options (to be called later with askOptions) |
||
109 | * |
||
110 | * @param string $key |
||
111 | * @param string $title |
||
112 | * @param mixed $default Default value. Input type will be based on this (bool => checkbox, etc) |
||
113 | * @param array|Map $list An array of value for a dropdown |
||
0 ignored issues
–
show
The type
LeKoala\Multilingual\Map was not found. Maybe you did not declare it correctly or list all dependencies?
The issue could also be caused by a filter entry in the build configuration.
If the path has been excluded in your configuration, e.g. filter:
dependency_paths: ["lib/*"]
For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths ![]() |
|||
114 | * @return void |
||
115 | */ |
||
116 | protected function addOption($key, $title, $default = '', $list = null) |
||
117 | { |
||
118 | // Handle maps |
||
119 | if (is_object($list) && method_exists($list, 'toArray')) { |
||
120 | $list = $list->toArray(); |
||
121 | } |
||
122 | $opt = [ |
||
123 | 'key' => $key, |
||
124 | 'title' => $title, |
||
125 | 'default' => $default, |
||
126 | 'list' => $list, |
||
127 | ]; |
||
128 | $this->options[] = $opt; |
||
129 | |||
130 | return $opt; |
||
0 ignored issues
–
show
|
|||
131 | } |
||
132 | |||
133 | /** |
||
134 | * Display a form with options |
||
135 | * |
||
136 | * Options are added through addOption method |
||
137 | * |
||
138 | * @return array Array with key => value corresponding to options asked |
||
139 | */ |
||
140 | protected function askOptions() |
||
141 | { |
||
142 | $values = []; |
||
143 | $request = $this->getRequest(); |
||
144 | echo '<form action="" method="post"><fieldset>'; |
||
145 | foreach ($this->options as $opt) { |
||
146 | $val = $request->requestVar($opt['key']); |
||
147 | if ($val === null) { |
||
148 | $val = $opt['default']; |
||
149 | } |
||
150 | |||
151 | $values[$opt['key']] = $val; |
||
152 | |||
153 | if ($opt['list']) { |
||
154 | $input = '<select name="' . $opt['key'] . '">'; |
||
155 | $input .= '<option></option>'; |
||
156 | foreach ($opt['list'] as $k => $v) { |
||
157 | $selected = ''; |
||
158 | if ($k == $val) { |
||
159 | $selected = ' selected="selected"'; |
||
160 | } |
||
161 | $input .= '<option value="' . $k . '"' . $selected . '>' . $v . '</option>'; |
||
162 | } |
||
163 | $input .= '</select>'; |
||
164 | } else { |
||
165 | $type = 'text'; |
||
166 | $input = null; |
||
167 | if (isset($opt['default'])) { |
||
168 | if (is_bool($opt['default'])) { |
||
169 | $type = 'checkbox'; |
||
170 | $checked = $val ? ' checked="checked"' : ''; |
||
171 | $input = '<input type="hidden" name="' . $opt['key'] . '" value="0" />'; |
||
172 | $input .= '<input type="' . $type . '" name="' . $opt['key'] . '" value="1"' . $checked . ' />'; |
||
173 | } else { |
||
174 | if (is_int($opt['default'])) { |
||
175 | $type = 'numeric'; |
||
176 | } |
||
177 | } |
||
178 | } |
||
179 | if (!$input) { |
||
180 | $input = '<input type="' . $type . '" name="' . $opt['key'] . '" value="' . $val . '" />'; |
||
181 | } |
||
182 | } |
||
183 | echo '<div class="field">'; |
||
184 | echo '<label> ' . $opt['title'] . ' ' . $input . '</label>'; |
||
185 | echo '</div>'; |
||
186 | echo '<br/>'; |
||
187 | } |
||
188 | echo '</fieldset><br/><input type="submit" />'; |
||
189 | echo '</form>'; |
||
190 | echo '<hr/ >'; |
||
191 | return $values; |
||
192 | } |
||
193 | |||
194 | protected function message($message, $type = 'default') |
||
195 | { |
||
196 | if (Director::is_cli()) { |
||
197 | $cli_map = [ |
||
198 | 'repaired' => '>', |
||
199 | 'success' => '✓', |
||
200 | 'created' => '+', |
||
201 | 'changed' => '+', |
||
202 | 'bad' => '-', |
||
203 | 'obsolete' => '-', |
||
204 | 'deleted' => '-', |
||
205 | 'notice' => '!', |
||
206 | 'error' => '-', |
||
207 | ]; |
||
208 | |||
209 | $message = strip_tags($message); |
||
210 | if (isset($cli_map[$type])) { |
||
211 | $message = $cli_map[$type] . ' ' . $message; |
||
212 | } |
||
213 | if (!is_string($message)) { |
||
0 ignored issues
–
show
|
|||
214 | $message = json_encode($message); |
||
215 | } |
||
216 | echo " $message\n"; |
||
217 | } else { |
||
218 | $web_map = [ |
||
219 | 'info' => 'blue', |
||
220 | 'repaired' => 'blue', |
||
221 | 'success' => 'green', |
||
222 | 'created' => 'green', |
||
223 | 'changed' => 'green', |
||
224 | 'obsolete' => 'red', |
||
225 | 'notice' => 'orange', |
||
226 | 'deleted' => 'red', |
||
227 | 'bad' => 'red', |
||
228 | 'error' => 'red', |
||
229 | ]; |
||
230 | $color = '#000000'; |
||
231 | if (isset($web_map[$type])) { |
||
232 | $color = $web_map[$type]; |
||
233 | } |
||
234 | if (!is_string($message)) { |
||
235 | $message = print_r($message, true); |
||
236 | echo "<pre style=\"color:$color\">$message</pre>"; |
||
237 | } else { |
||
238 | echo "<div style=\"color:$color\">$message</div>"; |
||
239 | } |
||
240 | } |
||
241 | } |
||
242 | |||
243 | protected function isDev() |
||
244 | { |
||
245 | return Director::isDev(); |
||
246 | } |
||
247 | |||
248 | protected function isLive() |
||
249 | { |
||
250 | return Director::isLive(); |
||
251 | } |
||
252 | } |
||
253 |
This function has been deprecated. The supplier of the function has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.