Skip to content

Commit d78f60c

Browse files
- Declaring the variables array as string array
- Added StringReplace variable replacer
1 parent cdb2341 commit d78f60c

7 files changed

Lines changed: 82 additions & 4 deletions

File tree

src/steps/CopyTemplateFolder.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
*/
2626
final class CopyTemplateFolder implements iStep {
2727

28+
/**
29+
* @param iTemplateCopy $templateCopy Actually copies the files while treating them as templates
30+
* @param string $templateFolder The base folders where the templates are located
31+
* @param string $targetFolder The target folder where the template structure will be recreated
32+
* @param string[] $variables The variables used to replace the placeholders in paths and templates
33+
*/
2834
public function __construct(public iTemplateCopy $templateCopy, public string $templateFolder, public string $targetFolder, public array $variables) { }
2935

3036
public function run() : void {

src/templateCopy/directoryHandler/iDirectoryHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface iDirectoryHandler {
2727
* correctly
2828
*
2929
* @param string $target The target path of the folder, e.g. the folder where the templates are installed to
30-
* @param array $variables The variables to use to replace any variables in the path
30+
* @param string[] $variables The variables to use to replace any variables in the path
3131
*/
3232
public function handle(string $target, array $variables) : void;
3333
}

src/templateCopy/fileHandler/iFileHandler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface iFileHandler {
2828
*
2929
* @param string $source The source path of the template being copied
3030
* @param string $target The target path to where the template should be rendered to
31-
* @param array $variables The variables that should be used to replace placeholders in the paths and the file
31+
* @param string[] $variables The variables that should be used to replace placeholders in the paths and the file
3232
*/
3333
public function handle(string $source, string $target, array $variables) : void;
3434
}

src/templateCopy/iTemplateCopy.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ interface iTemplateCopy {
3030
*
3131
* @param string $source The absolute path to the source file or folder
3232
* @param string $target The absolute path to the target file or folder
33-
* @param array $variables An array of variables that will be used for replacing placeholders
33+
* @param string[] $variables An array of variables that will be used for replacing placeholders
3434
*/
3535
public function copy(string $source, string $target, array $variables) : void;
3636
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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\templateCopy\variableReplacer;
20+
21+
/**
22+
* Uses str_replace to replace all placeholders in the path with the variables array the name matching must be 1:1
23+
*/
24+
final class StringReplace implements iVariableReplacer {
25+
/**
26+
* Passes the array kes of the variables which are the names of the placeholders as search, the values as replace and the path as subject and returns
27+
* the result.
28+
*
29+
* @inheritDoc
30+
*/
31+
public function replace(string $path, array $variables) : string {
32+
return str_replace(array_keys($variables), $variables, $path);
33+
}
34+
}

src/templateCopy/variableReplacer/iVariableReplacer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ interface iVariableReplacer {
2727
* Implementations MUST make sure that all variables are replaced and return the final path
2828
*
2929
* @param string $path The path containing the placeholders
30-
* @param array $variables The variables to use to replace the placeholders
30+
* @param string[] $variables The variables to use to replace the placeholders
3131
* @return string The path with the placeholders replaced
3232
*/
3333
public function replace(string $path, array $variables) : string;
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\templateCopy\variableReplacer;
20+
21+
use de\codenamephp\installer\templateCopy\variableReplacer\StringReplace;
22+
use PHPUnit\Framework\TestCase;
23+
24+
class StringReplaceTest extends TestCase {
25+
26+
private StringReplace $sut;
27+
28+
protected function setUp() : void {
29+
$this->sut = new StringReplace();
30+
}
31+
32+
public function testReplace() : void {
33+
self::assertEquals('/some/path/without/placeholders/path', $this->sut->replace(
34+
'/some/__placeholder1__/without/__placeholder2__/__placeholder1__',
35+
['__placeholder1__' => 'path', '__placeholder2__' => 'placeholders'])
36+
);
37+
}
38+
}

0 commit comments

Comments
 (0)