Skip to content

Commit e7684c4

Browse files
authored
Merge pull request #1110 from kenjis/fix-setup-setAutoloadHelpers
fix: setup command cannot update `Config\Autoload::$helpers` with multiple lines
2 parents db10ec1 + 670ff73 commit e7684c4

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

src/Commands/Setup.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,8 @@ private function setAutoloadHelpers(): void
256256
$helpers = $config->helpers;
257257
$newHelpers = array_unique(array_merge($helpers, ['auth', 'setting']));
258258

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

264262
// check if the content is updated
265263
if ($output === $content) {
@@ -277,6 +275,18 @@ private function setAutoloadHelpers(): void
277275
}
278276
}
279277

278+
/**
279+
* @param string $content The content of Config\Autoload.
280+
* @param list<string> $newHelpers The list of helpers.
281+
*/
282+
private function updateAutoloadHelpers(string $content, array $newHelpers): string
283+
{
284+
$pattern = '/^ public \$helpers = \[.*?\];/msu';
285+
$replace = ' public $helpers = [\'' . implode("', '", $newHelpers) . '\'];';
286+
287+
return preg_replace($pattern, $replace, $content);
288+
}
289+
280290
private function removeHelperLoadingInBaseController(): void
281291
{
282292
$file = 'Controllers/BaseController.php';

tests/Commands/SetupTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,41 @@ public function testRunEmailConfigIsFine(): void
131131
);
132132
}
133133

134+
public function testUpdateAutoloadHelpers(): void
135+
{
136+
$command = new Setup(Services::logger(), Services::commands());
137+
138+
$updateAutoloadHelpers = $this->getPrivateMethodInvoker($command, 'updateAutoloadHelpers');
139+
140+
$content = <<<'EOL'
141+
class Autoload extends AutoloadConfig
142+
{
143+
/**
144+
* -------------------------------------------------------------------
145+
* Helpers
146+
* -------------------------------------------------------------------
147+
* Prototype:
148+
* $helpers = [
149+
* 'form',
150+
* ];
151+
*
152+
* @var list<string>
153+
*/
154+
public $helpers = [
155+
'text',
156+
'form',
157+
];
158+
}
159+
EOL;
160+
$helpers = ['text', 'form', 'auth', 'setting'];
161+
$output = $updateAutoloadHelpers($content, $helpers);
162+
163+
$this->assertStringContainsString(
164+
"public \$helpers = ['text', 'form', 'auth', 'setting'];",
165+
$output
166+
);
167+
}
168+
134169
/**
135170
* @return string app folder path
136171
*/

0 commit comments

Comments
 (0)