Skip to content

Commit

Permalink
Merge pull request #1110 from kenjis/fix-setup-setAutoloadHelpers
Browse files Browse the repository at this point in the history
fix: setup command cannot update `Config\Autoload::$helpers` with multiple lines
  • Loading branch information
kenjis authored May 2, 2024
2 parents db10ec1 + 670ff73 commit e7684c4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
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

0 comments on commit e7684c4

Please sign in to comment.