Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions src/Commands/Setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,8 @@ private function setAutoloadHelpers(): void
$helpers = $config->helpers;
$newHelpers = array_unique(array_merge($helpers, ['auth', 'setting']));

$pattern = '/^ public \$helpers = \[.*\];/mu';
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';
$content = file_get_contents($path);
$output = preg_replace($pattern, $replace, $content);
$output = $this->updateAutoloadHelpers($content, $newHelpers);

// check if the content is updated
if ($output === $content) {
Expand All @@ -277,6 +275,18 @@ private function setAutoloadHelpers(): void
}
}

/**
* @param string $content The content of Config\Autoload.
* @param list<string> $newHelpers The list of helpers.
*/
private function updateAutoloadHelpers(string $content, array $newHelpers): string
{
$pattern = '/^ public \$helpers = \[.*?\];/msu';
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';

return preg_replace($pattern, $replace, $content);
}

private function removeHelperLoadingInBaseController(): void
{
$file = 'Controllers/BaseController.php';
Expand Down
35 changes: 35 additions & 0 deletions tests/Commands/SetupTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,41 @@ public function testRunEmailConfigIsFine(): void
);
}

public function testUpdateAutoloadHelpers(): void
{
$command = new Setup(Services::logger(), Services::commands());

$updateAutoloadHelpers = $this->getPrivateMethodInvoker($command, 'updateAutoloadHelpers');

$content = <<<'EOL'
class Autoload extends AutoloadConfig
{
/**
* -------------------------------------------------------------------
* Helpers
* -------------------------------------------------------------------
* Prototype:
* $helpers = [
* 'form',
* ];
*
* @var list<string>
*/
public $helpers = [
'text',
'form',
];
}
EOL;
$helpers = ['text', 'form', 'auth', 'setting'];
$output = $updateAutoloadHelpers($content, $helpers);

$this->assertStringContainsString(
"public \$helpers = ['text', 'form', 'auth', 'setting'];",
$output
);
}

/**
* @return string app folder path
*/
Expand Down