| 1 | <?php |
||
| 2 | |||
| 3 | namespace LeKoala\DevToolkit; |
||
| 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 | */ |
||
| 17 | trait BuildTaskTools |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var \SilverStripe\Control\HTTPRequest |
||
| 21 | */ |
||
| 22 | protected $request; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var array |
||
| 26 | */ |
||
| 27 | protected $options = []; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * Increase time limit |
||
| 31 | * |
||
| 32 | * @return void |
||
| 33 | */ |
||
| 34 | protected function increaseTimeLimitTo($timeLimit = null) |
||
| 35 | { |
||
| 36 | Environment::increaseTimeLimitTo($timeLimit); |
||
| 37 | if (!$timeLimit) { |
||
| 38 | $this->message("Time limit is disabled", "info"); |
||
| 39 | } else { |
||
| 40 | $this->message("Time limit has been set to $timeLimit seconds", "info"); |
||
| 41 | } |
||
| 42 | } |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Rebuild the class manifest |
||
| 46 | * |
||
| 47 | * @return void |
||
| 48 | */ |
||
| 49 | protected function regenerateClassManifest() |
||
| 50 | { |
||
| 51 | ClassLoader::inst()->getManifest()->regenerate(false); |
||
| 52 | $this->message("The class manifest has been rebuilt", "created"); |
||
| 53 | } |
||
| 54 | |||
| 55 | /** |
||
| 56 | * All dataobjects |
||
| 57 | * |
||
| 58 | * @return array |
||
| 59 | */ |
||
| 60 | protected function getValidDataObjects() |
||
| 61 | { |
||
| 62 | $list = ClassInfo::getValidSubClasses(DataObject::class); |
||
| 63 | array_shift($list); |
||
| 64 | return $list; |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * All modules |
||
| 69 | * |
||
| 70 | * @return array |
||
| 71 | */ |
||
| 72 | protected function getModules() |
||
| 73 | { |
||
| 74 | return ArrayLib::valuekey(array_keys(ModuleLoader::inst()->getManifest()->getModules())); |
||
| 75 | } |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Get the request (and keep your imports clean :-) ) |
||
| 79 | * |
||
| 80 | * @return HTTPRequest |
||
| 81 | */ |
||
| 82 | protected function getRequest() |
||
| 83 | { |
||
| 84 | if (!$this->request) { |
||
| 85 | die('Make sure to call $this->request = $request in your own class'); |
||
|
0 ignored issues
–
show
|
|||
| 86 | } |
||
| 87 | return $this->request; |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Add options (to be called later with askOptions) |
||
| 92 | * |
||
| 93 | * @param string $key |
||
| 94 | * @param string $title |
||
| 95 | * @param mixed $default Default value. Input type will be based on this (bool => checkbox, etc) |
||
| 96 | * @param array|Map $list An array of value for a dropdown |
||
|
0 ignored issues
–
show
The type
LeKoala\DevToolkit\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 Loading history...
|
|||
| 97 | * @return void |
||
| 98 | */ |
||
| 99 | protected function addOption($key, $title, $default = '', $list = null) |
||
| 100 | { |
||
| 101 | // Handle maps |
||
| 102 | if (is_object($list) && method_exists($list, 'toArray')) { |
||
| 103 | $list = $list->toArray(); |
||
| 104 | } |
||
| 105 | $opt = [ |
||
| 106 | 'key' => $key, |
||
| 107 | 'title' => $title, |
||
| 108 | 'default' => $default, |
||
| 109 | 'list' => $list, |
||
| 110 | ]; |
||
| 111 | $this->options[] = $opt; |
||
| 112 | |||
| 113 | return $opt; |
||
|
0 ignored issues
–
show
|
|||
| 114 | } |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Display a form with options |
||
| 118 | * |
||
| 119 | * Options are added through addOption method |
||
| 120 | * |
||
| 121 | * @return array Array with key => value corresponding to options asked |
||
| 122 | */ |
||
| 123 | protected function askOptions() |
||
| 124 | { |
||
| 125 | $values = []; |
||
| 126 | $request = $this->getRequest(); |
||
| 127 | echo '<form action="" method="post"><fieldset>'; |
||
| 128 | foreach ($this->options as $opt) { |
||
| 129 | $val = $request->requestVar($opt['key']); |
||
| 130 | if ($val === null) { |
||
| 131 | $val = $opt['default']; |
||
| 132 | } |
||
| 133 | |||
| 134 | $values[$opt['key']] = $val; |
||
| 135 | |||
| 136 | if ($opt['list']) { |
||
| 137 | $input = '<select name="' . $opt['key'] . '">'; |
||
| 138 | $input .= '<option></option>'; |
||
| 139 | foreach ($opt['list'] as $k => $v) { |
||
| 140 | $selected = ''; |
||
| 141 | if ($k == $val) { |
||
| 142 | $selected = ' selected="selected"'; |
||
| 143 | } |
||
| 144 | $input .= '<option value="' . $k . '"' . $selected . '>' . $v . '</option>'; |
||
| 145 | } |
||
| 146 | $input .= '</select>'; |
||
| 147 | } else { |
||
| 148 | $type = 'text'; |
||
| 149 | $input = null; |
||
| 150 | if (isset($opt['default'])) { |
||
| 151 | if (is_bool($opt['default'])) { |
||
| 152 | $type = 'checkbox'; |
||
| 153 | $checked = $val ? ' checked="checked"' : ''; |
||
| 154 | $input = '<input type="hidden" name="' . $opt['key'] . '" value="0" />'; |
||
| 155 | $input .= '<input type="' . $type . '" name="' . $opt['key'] . '" value="1"' . $checked . ' />'; |
||
| 156 | } else { |
||
| 157 | if (is_int($opt['default'])) { |
||
| 158 | $type = 'numeric'; |
||
| 159 | } |
||
| 160 | } |
||
| 161 | } |
||
| 162 | if (!$input) { |
||
| 163 | $input = '<input type="' . $type . '" name="' . $opt['key'] . '" value="' . $val . '" />'; |
||
| 164 | } |
||
| 165 | } |
||
| 166 | echo '<div class="field">'; |
||
| 167 | echo '<label> ' . $opt['title'] . ' ' . $input . '</label>'; |
||
| 168 | echo '</div>'; |
||
| 169 | echo '<br/>'; |
||
| 170 | } |
||
| 171 | echo '</fieldset><br/><input type="submit" />'; |
||
| 172 | echo '</form>'; |
||
| 173 | echo '<hr/ >'; |
||
| 174 | return $values; |
||
| 175 | } |
||
| 176 | |||
| 177 | protected function message($message, $type = 'default') |
||
| 178 | { |
||
| 179 | if (Director::is_cli()) { |
||
| 180 | $cli_map = [ |
||
| 181 | 'repaired' => '>', |
||
| 182 | 'success' => '✓', |
||
| 183 | 'created' => '+', |
||
| 184 | 'changed' => '+', |
||
| 185 | 'bad' => '-', |
||
| 186 | 'obsolete' => '-', |
||
| 187 | 'deleted' => '-', |
||
| 188 | 'notice' => '!', |
||
| 189 | 'error' => '-', |
||
| 190 | ]; |
||
| 191 | |||
| 192 | $message = strip_tags($message); |
||
| 193 | if (isset($cli_map[$type])) { |
||
| 194 | $message = $cli_map[$type] . ' ' . $message; |
||
| 195 | } |
||
| 196 | if (!is_string($message)) { |
||
|
0 ignored issues
–
show
|
|||
| 197 | $message = json_encode($message); |
||
| 198 | } |
||
| 199 | echo " $message\n"; |
||
| 200 | } else { |
||
| 201 | $web_map = [ |
||
| 202 | 'info' => 'blue', |
||
| 203 | 'repaired' => 'blue', |
||
| 204 | 'success' => 'green', |
||
| 205 | 'created' => 'green', |
||
| 206 | 'changed' => 'green', |
||
| 207 | 'obsolete' => 'red', |
||
| 208 | 'notice' => 'orange', |
||
| 209 | 'deleted' => 'red', |
||
| 210 | 'bad' => 'red', |
||
| 211 | 'error' => 'red', |
||
| 212 | ]; |
||
| 213 | $color = '#000000'; |
||
| 214 | if (isset($web_map[$type])) { |
||
| 215 | $color = $web_map[$type]; |
||
| 216 | } |
||
| 217 | if (!is_string($message)) { |
||
| 218 | $message = print_r($message, true); |
||
| 219 | echo "<pre style=\"color:$color\">$message</pre>"; |
||
| 220 | } else { |
||
| 221 | echo "<div style=\"color:$color\">$message</div>"; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | } |
||
| 225 | |||
| 226 | protected function isDev() |
||
| 227 | { |
||
| 228 | return Director::isDev(); |
||
| 229 | } |
||
| 230 | |||
| 231 | protected function isLive() |
||
| 232 | { |
||
| 233 | return Director::isLive(); |
||
| 234 | } |
||
| 235 | } |
||
| 236 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.