@@ -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 ));
0 commit comments