|
8 | 8 | use CodeIgniter\Commands\Database\Migrate; |
9 | 9 | use CodeIgniter\Shield\Commands\Setup\ContentReplacer; |
10 | 10 | use CodeIgniter\Test\Filters\CITestStreamFilter; |
| 11 | +use Config\Autoload as AutoloadConfig; |
11 | 12 | use Config\Email as EmailConfig; |
12 | 13 | use Config\Services; |
13 | 14 |
|
@@ -78,7 +79,7 @@ private function publishConfig(): void |
78 | 79 | $this->publishConfigAuthGroups(); |
79 | 80 | $this->publishConfigAuthToken(); |
80 | 81 |
|
81 | | - $this->setupHelper(); |
| 82 | + $this->setAutoloadHelpers(); |
82 | 83 | $this->setupRoutes(); |
83 | 84 |
|
84 | 85 | $this->setSecurityCSRF(); |
@@ -236,24 +237,55 @@ private function replace(string $file, array $replaces): bool |
236 | 237 | return false; |
237 | 238 | } |
238 | 239 |
|
239 | | - private function setupHelper(): void |
| 240 | + private function setAutoloadHelpers(): void |
240 | 241 | { |
241 | | - $file = 'Controllers/BaseController.php'; |
242 | | - $check = '$this->helpers = array_merge($this->helpers, [\'setting\']);'; |
| 242 | + $file = 'Config/Autoload.php'; |
| 243 | + |
| 244 | + $path = $this->distPath . $file; |
| 245 | + $cleanPath = clean_path($path); |
| 246 | + |
| 247 | + $config = new AutoloadConfig(); |
| 248 | + $helpers = $config->helpers; |
| 249 | + $newHelpers = array_unique(array_merge($helpers, ['auth', 'setting'])); |
| 250 | + |
| 251 | + $pattern = '/^ public \$helpers = \[.*\];/mu'; |
| 252 | + $replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];'; |
| 253 | + $content = file_get_contents($path); |
| 254 | + $output = preg_replace($pattern, $replace, $content); |
| 255 | + |
| 256 | + // check if the content is updated |
| 257 | + if ($output === $content) { |
| 258 | + $this->write(CLI::color(' Autoload Setup: ', 'green') . 'Everything is fine.'); |
| 259 | + |
| 260 | + return; |
| 261 | + } |
| 262 | + |
| 263 | + if (write_file($path, $output)) { |
| 264 | + $this->write(CLI::color(' Updated: ', 'green') . $cleanPath); |
| 265 | + |
| 266 | + $this->removeHelperLoadingInBaseController(); |
| 267 | + } else { |
| 268 | + $this->error(" Error updating file '{$cleanPath}'."); |
| 269 | + } |
| 270 | + } |
| 271 | + |
| 272 | + private function removeHelperLoadingInBaseController(): void |
| 273 | + { |
| 274 | + $file = 'Controllers/BaseController.php'; |
| 275 | + |
| 276 | + $check = ' $this->helpers = array_merge($this->helpers, [\'setting\']);'; |
243 | 277 |
|
244 | 278 | // Replace old helper setup |
245 | 279 | $replaces = [ |
246 | 280 | '$this->helpers = array_merge($this->helpers, [\'auth\', \'setting\']);' => $check, |
247 | 281 | ]; |
248 | | - if ($this->replace($file, $replaces)) { |
249 | | - return; |
250 | | - } |
251 | | - |
252 | | - // Add helper setup |
253 | | - $pattern = '/(' . preg_quote('// Do Not Edit This Line', '/') . ')/u'; |
254 | | - $replace = $check . "\n\n " . '$1'; |
| 282 | + $this->replace($file, $replaces); |
255 | 283 |
|
256 | | - $this->add($file, $check, $pattern, $replace); |
| 284 | + // Remove helper setup |
| 285 | + $replaces = [ |
| 286 | + "\n" . $check . "\n" => '', |
| 287 | + ]; |
| 288 | + $this->replace($file, $replaces); |
257 | 289 | } |
258 | 290 |
|
259 | 291 | private function setupRoutes(): void |
|
0 commit comments