Browse code

ViewEngine configs + viewFileExtension fix

RafaƂ Szklarczyk authored on 17/06/2019 12:33:37
Showing 3 changed files
... ...
@@ -63,7 +63,8 @@ class View{
63 63
         
64 64
 
65 65
         if($this->viewFile = $this->find($view)){
66
-            $this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION);
66
+            if(!isset($this->viewFileExtension))
67
+                $this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION);
67 68
         }
68 69
         else{
69 70
             if(is_object($view)){
... ...
@@ -78,8 +79,21 @@ class View{
78 79
     }
79 80
 
80 81
     public function render(){
81
-        if ($viewEngineClass = $this->_L3->viewEngines->get($this->viewFileExtension)){
82
-            $view = new $viewEngineClass();
82
+        if ($viewEngine = $this->_L3->viewEngines->get($this->viewFileExtension)){
83
+
84
+            $viewEngineClass = $viewEngine->class;
85
+            if(isset($viewEngine->config)){
86
+                $view = new $viewEngineClass($viewEngine->config);
87
+            }
88
+            else{
89
+                $refl = new \ReflectionClass($viewEngineClass);
90
+                if($refl->inNamespace()&&class_exists($refl->getNamespaceName().'\\Config')){
91
+                    $view = new $viewEngineClass($refl->getNamespaceName().'\\Config');
92
+                }
93
+                else{
94
+                    $view = new $viewEngineClass();
95
+                }
96
+            }    
83 97
             return $view->render($this->viewFile, $this->context);
84 98
         }
85 99
         else{
... ...
@@ -135,6 +149,7 @@ class View{
135 149
                 // try to add extension and check again
136 150
                 else foreach($this->_L3->viewEngines->getRegisteredFileExtensions() as $registeredExtension){
137 151
                     if(is_file($found = $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view.$registeredExtension)){
152
+                        $this->viewFileExtension = $registeredExtension;
138 153
                         return $found;
139 154
                     }
140 155
                 }
... ...
@@ -10,7 +10,7 @@ class ViewEngine{
10 10
         $this->viewengines = array(); 
11 11
     }
12 12
 
13
-    public function register($fileExtension, $viewEngine){
13
+    public function register($fileExtension, $viewEngine, $config = null){
14 14
         if(!class_exists($viewEngine,true)){
15 15
             throw new \Exception("Class \"$viewEngine\" not found!");
16 16
         }
... ...
@@ -18,7 +18,9 @@ class ViewEngine{
18 18
             throw new \Exception("Class \"$viewEngine\" does not implement IViewEngine interface!");
19 19
         }
20 20
 
21
-        $this->viewengines[$fileExtension] = $viewEngine;
21
+        $vn = new ViewEngineInfo($fileExtension, $viewEngine, $config);
22
+
23
+        $this->viewengines[$fileExtension] = $vn;
22 24
     }
23 25
 
24 26
     public function get($fileExtension){
25 27
new file mode 100644
... ...
@@ -0,0 +1,16 @@
1
+<?php
2
+
3
+namespace elanpl\L3;
4
+
5
+class ViewEngineInfo{
6
+    public $extension;
7
+    public $class;
8
+    public $config;
9
+
10
+    public function __construct($extension, $class, $config)
11
+    {
12
+        $this->extension = $extension;
13
+        $this->class = $class;
14
+        $this->config = $config;
15
+    }
16
+}
0 17
\ No newline at end of file