@@ -22,6 +22,7 @@ class View
2222 */
2323 public function __construct (string $ viewName )
2424 {
25+ $ viewFile = ("$ defaultDir/ " ?? "/app/views/ " ) . static ::DEFAULT_DIR . "/ $ viewName.view.php " ;
2526 try { $ defaultViewDir = Config::get ('DEFAULT_VIEW_DIR ' ); }
2627 catch (ConfigNotFoundException ) {
2728 throw new ViewException ("Default view directory not found in app configuration " );
@@ -38,19 +39,77 @@ public function __construct(string $viewName)
3839 $ this ->viewFile = $ viewFile ;
3940 }
4041
42+ protected static function parseVariables (string $ template , array $ props ): string
43+ {
44+
45+ // Matches
46+ $ abc = "[a-zA-Z] " ;
47+ $ abc_ = "[a-zA-Z_] " ;
48+ $ abc123_ = "[a-zA-Z0-9_] " ;
49+ $ varName = "( {$ abc }{$ abc123_ }+) " ;
50+
51+ $ regex = "/\{\{ * $ varName(\. $ varName)? *\}\}/ " ;
52+ $ replace_callback = function (array $ match ) use ($ props ) {
53+ if (count ($ match ) === 4 ) {
54+ [ , $ propName , , $ propProperty ] = $ match ;
55+ $ value = $ props [$ propName ][$ propProperty ] ?? null ;
56+ } else {
57+ [ , $ propName ] = $ match ;
58+ $ value = $ props [$ propName ] ?? null ;
59+ }
60+
61+ if ($ value === null && false ) {
62+ throw new ViewException ("Undefined prop: {$ match [1 ]}" );
63+ }
64+
65+ return $ value ;
66+ return "<?php echo ' $ value'; ?> " ;
67+ };
68+
69+ return preg_replace_callback ($ regex , $ replace_callback , $ template );
70+ }
71+
72+ protected static function parseForLoops (string $ template , array $ props ): string
73+ {
74+
75+ // Matches
76+ $ abc = "[a-zA-Z] " ;
77+ $ abc_ = "[a-zA-Z_] " ;
78+ $ abc123_ = "[a-zA-Z0-9_] " ;
79+ $ varName = "( {$ abc }{$ abc123_ }*) " ;
80+
81+ $ regex = "/@for *\(( $ varName:)? $ varName of $ varName*\) \{.+\}/ " ;
82+ $ regex = "/@for *\(( $ varName\:)?/ " ;
83+ $ replace_callback = function (array $ match ) use ($ props ) {
84+ var_dump ($ match );
85+ if (count ($ match ) === 4 ) {
86+ [ , $ propName , , $ propProperty ] = $ match ;
87+ $ value = $ props [$ propName ][$ propProperty ] ?? null ;
88+ } else {
89+ [ , $ propName ] = $ match ;
90+ $ value = $ props [$ propName ] ?? null ;
91+ }
92+
93+ if ($ value === null && false ) {
94+ throw new ViewException ("Undefined prop: {$ match [1 ]}" );
95+ }
96+
97+ return $ value ;
98+ return "<?php echo ' $ value'; ?> " ;
99+ };
100+
101+ return preg_replace_callback ($ regex , $ replace_callback , $ template );
102+
103+ }
104+
41105 public function render (array $ props =[]): string
42106 {
43107 // Get the content of the file
44108 $ view = file_get_contents ($ this ->viewFile );
45109
46110 // Extract props
47- $ view = preg_replace_callback (
48- "/\{ *([a-zA-Z_][a-zA-Z0-9_]+) *\}/ " ,
49- fn (array $ match ) => $ props [$ match [1 ]] ?? throw new ViewException (
50- "Undefined prop: {$ match [1 ]}"
51- ),
52- $ view
53- );
111+ $ view = static ::parseForLoops ($ view , $ props );
112+ // $view = static::parseVariables($view, $props);
54113
55114 return $ view ;
56115 }
0 commit comments