Skip to content

Commit

Permalink
refactor: allow whitespace, rename vars to replace
Browse files Browse the repository at this point in the history
  • Loading branch information
steffans committed Jul 26, 2024
1 parent 8d3c693 commit ebae5c5
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ tasks:
env:
TASK_CWD: '{{.cwd}}'
cmds:
- composer run:task -q -- placeholder {{q .src}} {{q (toRawJson .vars)}}
- composer run:task -q -- placeholder {{q .src}} {{q (toRawJson .replace)}} {{if .ignore}}{{.ignore}}{{end}}
requires:
vars: [src, vars]
vars: [src, replace]
internal: true

zip:
Expand Down
10 changes: 3 additions & 7 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,10 @@

class StringHelper
{
public static function placeholder(string $data, array $vars): string
public static function placeholder(string $str, array $replace): string
{
$replace = [];
$callback = fn($matches) => $replace[$matches[1]] ?? $matches[0];

foreach ($vars as $key => $value) {
$replace["{{ {$key} }}"] = $value;
}

return str_replace(array_keys($replace), array_values($replace), $data);
return preg_replace_callback('/{{\s*(\w+?)\s*}}/', $callback, $str);
}
}
6 changes: 3 additions & 3 deletions src/TaskHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ public function copy(string $src, string $dest, string $ignore = ''): void
" to '{$dest}'\n";
}

public function placeholder(string $src, string $vars): void
public function placeholder(string $src, string $replace, string $ignore = ''): void
{
$fs = new Filesystem();
$files = self::findFiles($this->cwd, $src);
$variables = json_decode($vars, true);
$files = self::findFiles($this->cwd, $src, $ignore);
$variables = json_decode($replace, true);

foreach ($files as $file) {
$fs->dumpFile($file->getPathname(), Str::placeholder($file->getContents(), $variables));
Expand Down

0 comments on commit ebae5c5

Please sign in to comment.