Skip to content

Commit 81d2d75

Browse files
committed
feat: setup command configure helper autoloading
1 parent 7c6a017 commit 81d2d75

File tree

2 files changed

+55
-14
lines changed

2 files changed

+55
-14
lines changed

src/Commands/Setup.php

Lines changed: 50 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use CodeIgniter\Commands\Database\Migrate;
99
use CodeIgniter\Shield\Commands\Setup\ContentReplacer;
1010
use CodeIgniter\Test\Filters\CITestStreamFilter;
11+
use Config\Autoload as AutoloadConfig;
1112
use Config\Email as EmailConfig;
1213
use Config\Services;
1314

@@ -78,7 +79,7 @@ private function publishConfig(): void
7879
$this->publishConfigAuthGroups();
7980
$this->publishConfigAuthToken();
8081

81-
$this->setupHelper();
82+
$this->setAutoloadHelpers();
8283
$this->setupRoutes();
8384

8485
$this->setSecurityCSRF();
@@ -236,24 +237,61 @@ private function replace(string $file, array $replaces): bool
236237
return false;
237238
}
238239

239-
private function setupHelper(): void
240+
private function setAutoloadHelpers(): void
240241
{
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+
if (! is_file($path)) {
248+
$this->error(" Not found file '{$cleanPath}'.");
249+
250+
return;
251+
}
252+
253+
$config = new AutoloadConfig();
254+
$helpers = $config->helpers;
255+
$newHelpers = array_unique(array_merge($helpers, ['auth', 'setting']));
256+
257+
$pattern = '/^ public \$helpers = \[.*\];/mu';
258+
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';
259+
$content = file_get_contents($path);
260+
$output = preg_replace($pattern, $replace, $content);
261+
262+
// check if the content is updated
263+
if ($output === $content) {
264+
$this->write(CLI::color(' Autoload Setup: ', 'green') . 'Everything is fine.');
265+
266+
return;
267+
}
268+
269+
if (write_file($path, $output)) {
270+
$this->write(CLI::color(' Updated: ', 'green') . $cleanPath);
271+
272+
$this->removeHelperLoadingInBaseController();
273+
} else {
274+
$this->error(" Error updating file '{$cleanPath}'.");
275+
}
276+
}
277+
278+
private function removeHelperLoadingInBaseController(): void
279+
{
280+
$file = 'Controllers/BaseController.php';
281+
282+
$check = ' $this->helpers = array_merge($this->helpers, [\'setting\']);';
243283

244284
// Replace old helper setup
245285
$replaces = [
246286
'$this->helpers = array_merge($this->helpers, [\'auth\', \'setting\']);' => $check,
247287
];
248-
if ($this->replace($file, $replaces)) {
249-
return;
250-
}
288+
$this->replace($file, $replaces);
251289

252-
// Add helper setup
253-
$pattern = '/(' . preg_quote('// Do Not Edit This Line', '/') . ')/u';
254-
$replace = $check . "\n\n " . '$1';
255-
256-
$this->add($file, $check, $pattern, $replace);
290+
// Remove helper setup
291+
$replaces = [
292+
"\n" . $check . "\n" => '',
293+
];
294+
$this->replace($file, $replaces);
257295
}
258296

259297
private function setupRoutes(): void

tests/Commands/SetupTest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ public function testRun(): void
6565
$this->assertStringContainsString('namespace Config;', $authToken);
6666
$this->assertStringContainsString('use CodeIgniter\Shield\Config\AuthToken as ShieldAuthToken;', $authToken);
6767

68+
$autoload = file_get_contents($appFolder . 'Config/Autoload.php');
69+
$this->assertStringContainsString('$helpers = [\'auth\', \'setting\'];', $autoload);
70+
6871
$routes = file_get_contents($appFolder . 'Config/Routes.php');
6972
$this->assertStringContainsString('service(\'auth\')->routes($routes);', $routes);
7073

@@ -77,7 +80,7 @@ public function testRun(): void
7780
' Created: vfs://root/Config/Auth.php
7881
Created: vfs://root/Config/AuthGroups.php
7982
Created: vfs://root/Config/AuthToken.php
80-
Updated: vfs://root/Controllers/BaseController.php
83+
Updated: vfs://root/Config/Autoload.php
8184
Updated: vfs://root/Config/Routes.php
8285
Updated: We have updated file \'vfs://root/Config/Security.php\' for security reasons.
8386
Updated: vfs://root/Config/Email.php',
@@ -112,7 +115,7 @@ public function testRunEmailConfigIsFine(): void
112115
' Created: vfs://root/Config/Auth.php
113116
Created: vfs://root/Config/AuthGroups.php
114117
Created: vfs://root/Config/AuthToken.php
115-
Updated: vfs://root/Controllers/BaseController.php
118+
Updated: vfs://root/Config/Autoload.php
116119
Updated: vfs://root/Config/Routes.php
117120
Updated: We have updated file \'vfs://root/Config/Security.php\' for security reasons.',
118121
$result

0 commit comments

Comments
 (0)