Skip to content

Commit 3c172a1

Browse files
Added step to create folders
1 parent e364ac3 commit 3c172a1

3 files changed

Lines changed: 119 additions & 7 deletions

File tree

README.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ so you can change the final structure on the fly (e.g. have a folder structure t
3636
```php
3737
use de\codenamephp\installer\StepExecutor;
3838
use de\codenamephp\installer\steps\CopyTemplateFolder;
39-
use de\codenamephp\installer\steps\DeleteFilesAndFolders;
39+
use de\codenamephp\installer\steps\CreateFolders;use de\codenamephp\installer\steps\DeleteFilesAndFolders;
4040
use de\codenamephp\installer\steps\SequentialCollection;
4141
use de\codenamephp\installer\templateCopy\directoryHandler\CreateDirectoryWithSymfonyFilesystem;
4242
use de\codenamephp\installer\templateCopy\fileHandler\RenderWithTwigAndDumpWithSymfonyFilesystem;
@@ -51,7 +51,12 @@ return call_user_func(static function() {
5151
$filesystem = new Filesystem();
5252
$variableReplacer = new FramedStringReplace();
5353
$componentName = basename(shell_exec("git config --get remote.origin.url"), '.git');
54-
54+
$variables = [
55+
'vendor' => 'codenamephp',
56+
'componentName' => $componentName,
57+
'namespace' => implode('\\', array_merge(['de', 'codenamephp'], explode('.', $componentName)))
58+
];
59+
5560
(new StepExecutor(
5661
new SequentialCollection(
5762
new CopyTemplateFolder(
@@ -61,12 +66,9 @@ return call_user_func(static function() {
6166
),
6267
__DIR__ . '/templates',
6368
__DIR__ . '/..',
64-
[
65-
'vendor' => 'codenamephp',
66-
'componentName' => $componentName,
67-
'namespace' => implode('\\', array_merge(['de', 'codenamephp'], explode('.', $componentName)))
68-
]
69+
$variables
6970
),
71+
new CreateFolders($variableReplacer, $filesystem, [__DIR__ . '/../src', __DIR__ . '/../test'], $variables),
7072
new DeleteFilesAndFolders($filesystem, [
7173
__DIR__
7274
]),

src/steps/CreateFolders.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright 2021 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
namespace de\codenamephp\installer\steps;
20+
21+
use de\codenamephp\installer\templateCopy\variableReplacer\iVariableReplacer;
22+
use Symfony\Component\Filesystem\Exception\IOException;
23+
use Symfony\Component\Filesystem\Filesystem;
24+
25+
/**
26+
* Step to create folders since git doesn't commit empty folders. This way we don't have to create folders in the base template and place .gitkeep files in them
27+
*/
28+
final class CreateFolders implements iStep {
29+
30+
/**
31+
* @param iVariableReplacer $variableReplacer Used to replace variables in the folder paths
32+
* @param Filesystem $filesystem Used to create the folders
33+
* @param array<string> $folders The absolute paths of the folders to be created
34+
* @param array<string, string> $variables Used in variable replacement in the folder paths
35+
*/
36+
public function __construct(public iVariableReplacer $variableReplacer, public Filesystem $filesystem, public array $folders, public array $variables) { }
37+
38+
/**
39+
* Iterates over all folders, replaces the variables in each path using the variables and the variableReplacer passes each path to symfony filesystem to
40+
* create the folders
41+
*
42+
* @inheritDoc
43+
* @throws IOException
44+
*/
45+
public function run() : void {
46+
foreach($this->folders as $folder) {
47+
$this->filesystem->mkdir($this->variableReplacer->replace($folder, $this->variables));
48+
}
49+
}
50+
}

test/steps/CreateFoldersTest.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php declare(strict_types=1);
2+
/**
3+
* Copyright 2021 Bastian Schwarz <bastian@codename-php.de>.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*
17+
*/
18+
19+
namespace de\codenamephp\installer\test\steps;
20+
21+
use de\codenamephp\installer\steps\CreateFolders;
22+
use de\codenamephp\installer\templateCopy\variableReplacer\iVariableReplacer;
23+
use PHPUnit\Framework\TestCase;
24+
use Symfony\Component\Filesystem\Filesystem;
25+
26+
class CreateFoldersTest extends TestCase {
27+
28+
private CreateFolders $sut;
29+
30+
protected function setUp() : void {
31+
$variableReplacer = $this->createMock(iVariableReplacer::class);
32+
$filesystem = $this->createMock(Filesystem::class);
33+
34+
$this->sut = new CreateFolders($variableReplacer, $filesystem, [], []);
35+
}
36+
37+
public function testRun() : void {
38+
$this->sut->variables = ['some', 'vars'];
39+
$this->sut->folders = ['folder1', 'folder2', 'folder3'];
40+
41+
$this->sut->variableReplacer = $this->createMock(iVariableReplacer::class);
42+
$this->sut->variableReplacer
43+
->expects(self::exactly(3))
44+
->method('replace')
45+
->withConsecutive(
46+
[$this->sut->folders[0], $this->sut->variables],
47+
[$this->sut->folders[1], $this->sut->variables],
48+
[$this->sut->folders[2], $this->sut->variables],
49+
)
50+
->willReturnOnConsecutiveCalls('replaced1', 'replaced2', 'replaced3');
51+
52+
$this->sut->filesystem = $this->createMock(Filesystem::class);
53+
$this->sut->filesystem
54+
->expects(self::exactly(3))
55+
->method('mkdir')
56+
->withConsecutive(['replaced1'], ['replaced2'], ['replaced3']);
57+
58+
$this->sut->run();
59+
}
60+
}

0 commit comments

Comments
 (0)