Skip to content

Commit d54b03b

Browse files
committed
add support for global defaults
1 parent 7230d8f commit d54b03b

File tree

2 files changed

+34
-6
lines changed

2 files changed

+34
-6
lines changed

src/Mtrgen/Template/Generator.php

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ public static function parseAnyFile(string $path, array $arguments = [], bool $u
2323
throw new \RuntimeException(sprintf('File "%s" was not found', $path));
2424
}
2525

26+
$defaults = static::getDefaultArguments($file);
27+
foreach ($defaults as $key => $value) {
28+
$arguments["default:$key"] = $value;
29+
}
30+
2631
$parsed = Parser::parseString($file, $arguments, false, new PatternsOption(null, null, $useCommentSyntax !== false ? self::COMMENT_PATTERN : null));
2732

2833
$header = static::getTemplateHeader($parsed);
@@ -54,7 +59,8 @@ private static function write(GenericFileObject $file): void
5459
throw new \RuntimeException(sprintf('Directory "%s" was not created', $concurrentDirectory));
5560
}
5661

57-
file_put_contents(Path::safe(str_ends_with($file->directory, DIRECTORY_SEPARATOR)
62+
file_put_contents(Path::safe(
63+
str_ends_with($file->directory, DIRECTORY_SEPARATOR)
5864
? $file->directory . $file->filename
5965
: $file->directory . DIRECTORY_SEPARATOR . $file->filename), $file->contents);
6066
}
@@ -89,19 +95,39 @@ public static function getTemplateHeader(string $content): TemplateHeader
8995
if ($line === '') {
9096
continue;
9197
}
92-
$keyValue = explode(':', $line);
98+
$keyValue = explode(':', $line, 2);
9399
$key = trim($keyValue[0]);
94-
$value = trim($keyValue[1]);
95-
$info[$key] = $value;
100+
if ($key !== 'defaults') {
101+
$value = trim($keyValue[1]);
102+
$info[$key] = $value;
103+
} else {
104+
$info['defaults'] = array_slice($lines, array_search($line, $lines) + 1);
105+
break;
106+
}
96107
}
97108

98109
if (!isset($info['name']) || !isset($info['filename']) || !isset($info['path'])) {
99110
throw new \RuntimeException('Template header is missing some required properties (name, filename, path).');
100111
}
101112

113+
if (isset($info['defaults'])) {
114+
foreach ($info['defaults'] as $value) {
115+
$keyValue = explode(':', $value, 2);
116+
$key = trim($keyValue[0]);
117+
$value = trim($keyValue[1]);
118+
$info['defaults'][$key] = $value;
119+
}
120+
}
121+
102122
return TemplateHeader::fromArray($info);
103123
}
104124

125+
public static function getDefaultArguments(string $content): array
126+
{
127+
$header = static::getTemplateHeader($content);
128+
return $header->defaults;
129+
}
130+
105131
public static function removeHeader(string $content): string
106132
{
107133
return ltrim(preg_replace(self::HEADER_PATTERN, '', $content));

src/Mtrgen/Template/TemplateHeader.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,18 @@ class TemplateHeader
99
public string $name;
1010
public string $filename;
1111
public string $path;
12+
public array $defaults = [];
1213

13-
public function __construct(string $name, string $filename, string $path)
14+
public function __construct(string $name, string $filename, string $path, array $defaults = [])
1415
{
1516
$this->name = $name;
1617
$this->filename = $filename;
1718
$this->path = $path;
19+
$this->defaults = $defaults;
1820
}
1921

2022
public static function fromArray(array $array): static
2123
{
22-
return new static($array['name'], $array['filename'], $array['path']);
24+
return new static($array['name'], $array['filename'], $array['path'], $array['defaults'] ?? []);
2325
}
2426
}

0 commit comments

Comments
 (0)