@@ -13,41 +13,17 @@ class Blueprint
1313
1414 private array $ generators = [];
1515
16- public static function relativeNamespace (string $ fullyQualifiedClassName ): string
17- {
18- $ namespace = config ('blueprint.namespace ' ) . '\\' ;
19- $ reference = ltrim ($ fullyQualifiedClassName , '\\' );
20-
21- if (Str::startsWith ($ reference , $ namespace )) {
22- return Str::after ($ reference , $ namespace );
23- }
16+ private array $ shorthands = [];
2417
25- return $ reference ;
26- }
27-
28- public static function appPath ()
18+ public function registerShorthand (string $ shorthand , \Closure $ callback ): void
2919 {
30- return str_replace ( '\\' , ' / ' , config ( ' blueprint.app_path ' )) ;
20+ $ this -> shorthands [ $ shorthand ] = $ callback ;
3121 }
3222
33- public function parse ( $ content , $ strip_dashes = true )
23+ private function expandShorthands ( string $ content ): string
3424 {
35- $ content = str_replace (["\r\n" , "\r" ], "\n" , $ content );
36-
37- if ($ strip_dashes ) {
38- $ content = preg_replace ('/^(\s*)-\s*/m ' , '\1 ' , $ content );
39- }
40-
41- $ content = $ this ->transformDuplicatePropertyKeys ($ content );
42-
4325 $ content = preg_replace_callback (
44- '/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)$/mi ' ,
45- fn ($ matches ) => $ matches [1 ] . strtolower ($ matches [2 ]) . ': ' . $ matches [2 ],
46- $ content
47- );
48-
49- $ content = preg_replace_callback (
50- '/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?): true$/mi ' ,
26+ '/^(\s+)(id|timestamps(Tz)?|softDeletes(Tz)?)(: true)?$/mi ' ,
5127 fn ($ matches ) => $ matches [1 ] . strtolower ($ matches [2 ]) . ': ' . $ matches [2 ],
5228 $ content
5329 );
@@ -70,6 +46,45 @@ public function parse($content, $strip_dashes = true)
7046 $ content
7147 );
7248
49+ foreach ($ this ->shorthands as $ shorthand => $ callback ) {
50+ $ content = preg_replace_callback (
51+ '/^(\s+) ' . preg_quote ($ shorthand , '/ ' ) . '$/mi ' ,
52+ $ callback ,
53+ $ content
54+ );
55+ }
56+
57+ return $ content ;
58+ }
59+
60+ public static function relativeNamespace (string $ fullyQualifiedClassName ): string
61+ {
62+ $ namespace = config ('blueprint.namespace ' ) . '\\' ;
63+ $ reference = ltrim ($ fullyQualifiedClassName , '\\' );
64+
65+ if (Str::startsWith ($ reference , $ namespace )) {
66+ return Str::after ($ reference , $ namespace );
67+ }
68+
69+ return $ reference ;
70+ }
71+
72+ public static function appPath ()
73+ {
74+ return str_replace ('\\' , '/ ' , config ('blueprint.app_path ' ));
75+ }
76+
77+ public function parse ($ content , $ strip_dashes = true )
78+ {
79+ $ content = str_replace (["\r\n" , "\r" ], "\n" , $ content );
80+
81+ if ($ strip_dashes ) {
82+ $ content = preg_replace ('/^(\s*)-\s*/m ' , '\1 ' , $ content );
83+ }
84+
85+ $ content = $ this ->transformDuplicatePropertyKeys ($ content );
86+ $ content = $ this ->expandShorthands ($ content );
87+
7388 return Yaml::parse ($ content );
7489 }
7590
0 commit comments