Skip to content

Commit 387ea6c

Browse files
Added FramedStringReplace variable replacer
1 parent d78f60c commit 387ea6c

7 files changed

Lines changed: 90 additions & 5 deletions

File tree

src/steps/CopyTemplateFolder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class CopyTemplateFolder implements iStep {
2929
* @param iTemplateCopy $templateCopy Actually copies the files while treating them as templates
3030
* @param string $templateFolder The base folders where the templates are located
3131
* @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
32+
* @param array<string, string> $variables The variables used to replace the placeholders in paths and templates
3333
*/
3434
public function __construct(public iTemplateCopy $templateCopy, public string $templateFolder, public string $targetFolder, public array $variables) { }
3535

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 string[] $variables The variables to use to replace any variables in the path
30+
* @param array<string, 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 string[] $variables The variables that should be used to replace placeholders in the paths and the file
31+
* @param array<string, 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 string[] $variables An array of variables that will be used for replacing placeholders
33+
* @param array<string, 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: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
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+
* Frames the variable names with a pre- and suffix so common folder names like "vendor" are not replaced by accident
23+
*/
24+
final class FramedStringReplace implements iVariableReplacer {
25+
26+
public function __construct(public string $prefix = '__', public string $suffix = '__') { }
27+
28+
/**
29+
* Applies the prefix and suffix to all variable names (the array keys) and passes them as search to str_replace, the values as replacment and the path
30+
* as subject.
31+
*
32+
* @inheritDoc
33+
*/
34+
public function replace(string $path, array $variables) : string {
35+
return str_replace(array_map(fn($name) => $this->prefix . $name . $this->suffix, array_keys($variables)), $variables, $path);
36+
}
37+
}

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 string[] $variables The variables to use to replace the placeholders
30+
* @param array<string, 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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\FramedStringReplace;
22+
use PHPUnit\Framework\TestCase;
23+
24+
class FramedStringReplaceTest extends TestCase {
25+
26+
private FramedStringReplace $sut;
27+
28+
protected function setUp() : void {
29+
$this->sut = new FramedStringReplace();
30+
}
31+
32+
public function testReplace() : void {
33+
self::assertEquals('/some/not_placeholder/vendor/not_vendor/not_placeholder', $this->sut->replace(
34+
'/some/__placeholder__/vendor/__vendor__/__placeholder__',
35+
['vendor' => 'not_vendor', 'placeholder' => 'not_placeholder'])
36+
);
37+
}
38+
39+
public function testReplace_withCustomPrefixAndSuffix() : void {
40+
$this->sut->prefix = '++';
41+
$this->sut->suffix = '~~';
42+
43+
self::assertEquals('/some/not_placeholder/vendor/not_vendor/not_placeholder/__placeholder__', $this->sut->replace(
44+
'/some/++placeholder~~/vendor/++vendor~~/++placeholder~~/__placeholder__',
45+
['vendor' => 'not_vendor', 'placeholder' => 'not_placeholder'])
46+
);
47+
}
48+
}

0 commit comments

Comments
 (0)