... | ... |
@@ -63,8 +63,15 @@ class View{ |
63 | 63 |
|
64 | 64 |
|
65 | 65 |
if($this->viewFile = $this->find($view)){ |
66 |
- if(!isset($this->viewFileExtension)) |
|
67 |
- $this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION); |
|
66 |
+ if(!isset($this->viewFileExtension)){ |
|
67 |
+ foreach($this->_L3->viewEngines->getRegisteredFileExtensions() as $extension){ |
|
68 |
+ if(substr($this->viewFile, -strlen($extension)) == $extension){ |
|
69 |
+ $this->viewFileExtension = $extension; |
|
70 |
+ break; |
|
71 |
+ //$this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION); |
|
72 |
+ } |
|
73 |
+ } |
|
74 |
+ } |
|
68 | 75 |
} |
69 | 76 |
else{ |
70 | 77 |
if(is_object($view)){ |
... | ... |
@@ -83,15 +83,15 @@ class View{ |
83 | 83 |
|
84 | 84 |
$viewEngineClass = $viewEngine->class; |
85 | 85 |
if(isset($viewEngine->config)){ |
86 |
- $view = new $viewEngineClass($viewEngine->config); |
|
86 |
+ $view = new $viewEngineClass($this->_L3, $viewEngine->config); |
|
87 | 87 |
} |
88 | 88 |
else{ |
89 | 89 |
$refl = new \ReflectionClass($viewEngineClass); |
90 | 90 |
if($refl->inNamespace()&&class_exists($refl->getNamespaceName().'\\Config')){ |
91 |
- $view = new $viewEngineClass($refl->getNamespaceName().'\\Config'); |
|
91 |
+ $view = new $viewEngineClass($this->_L3, $refl->getNamespaceName().'\\Config'); |
|
92 | 92 |
} |
93 | 93 |
else{ |
94 |
- $view = new $viewEngineClass(); |
|
94 |
+ $view = new $viewEngineClass($this->_L3); |
|
95 | 95 |
} |
96 | 96 |
} |
97 | 97 |
return $view->render($this->viewFile, $this->context); |
... | ... |
@@ -143,12 +143,12 @@ class View{ |
143 | 143 |
|
144 | 144 |
foreach($dirs as $dir){ |
145 | 145 |
// check if the file exists |
146 |
- if(is_file($found = $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view)){ |
|
146 |
+ if(is_file($found = str_replace('\\', DIRECTORY_SEPARATOR, $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view))){ |
|
147 | 147 |
return $found; |
148 | 148 |
} |
149 | 149 |
// try to add extension and check again |
150 | 150 |
else foreach($this->_L3->viewEngines->getRegisteredFileExtensions() as $registeredExtension){ |
151 |
- if(is_file($found = $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view.$registeredExtension)){ |
|
151 |
+ if(is_file($found = str_replace('\\', DIRECTORY_SEPARATOR, $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view.$registeredExtension))){ |
|
152 | 152 |
$this->viewFileExtension = $registeredExtension; |
153 | 153 |
return $found; |
154 | 154 |
} |
... | ... |
@@ -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 |
} |
... | ... |
@@ -66,7 +66,13 @@ class View{ |
66 | 66 |
$this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION); |
67 | 67 |
} |
68 | 68 |
else{ |
69 |
- throw new \Exception("View file:\"$view\" not found!"); |
|
69 |
+ if(is_object($view)){ |
|
70 |
+ $string_view = 'matched for class: '.get_class($view); |
|
71 |
+ } |
|
72 |
+ else{ |
|
73 |
+ $string_view = $view; |
|
74 |
+ } |
|
75 |
+ throw new \Exception("View file:\"$string_view\" not found!"); |
|
70 | 76 |
} |
71 | 77 |
$this->context = $context; |
72 | 78 |
} |
... | ... |
@@ -72,7 +72,7 @@ class View{ |
72 | 72 |
} |
73 | 73 |
|
74 | 74 |
public function render(){ |
75 |
- if ($viewEngineClass = $this->_L3->viewEngines::get($this->viewFileExtension)){ |
|
75 |
+ if ($viewEngineClass = $this->_L3->viewEngines->get($this->viewFileExtension)){ |
|
76 | 76 |
$view = new $viewEngineClass(); |
77 | 77 |
return $view->render($this->viewFile, $this->context); |
78 | 78 |
} |
1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,141 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+namespace elanpl\L3; |
|
4 |
+ |
|
5 |
+class View{ |
|
6 |
+ protected $viewFileExtension; |
|
7 |
+ protected $viewFile; |
|
8 |
+ protected $context; |
|
9 |
+ public $_L3; //The L3 instance |
|
10 |
+ |
|
11 |
+ public function __construct(){ |
|
12 |
+ $numargs = func_num_args(); |
|
13 |
+ |
|
14 |
+ if ($numargs == 0) { |
|
15 |
+ $view = ""; |
|
16 |
+ $context = array(); |
|
17 |
+ } |
|
18 |
+ else if ($numargs == 1) { |
|
19 |
+ $arg = func_get_arg(0); |
|
20 |
+ if(is_object($arg) && $arg instanceof ViewModel){ |
|
21 |
+ if(method_exists($arg, 'getContext')){ |
|
22 |
+ $context = $arg->getContext(); |
|
23 |
+ } |
|
24 |
+ else{ |
|
25 |
+ $context = array(); |
|
26 |
+ $context['vm'] = $arg; |
|
27 |
+ } |
|
28 |
+ |
|
29 |
+ if(method_exists($arg, 'getView')){ |
|
30 |
+ $view = $arg->getView(); |
|
31 |
+ } |
|
32 |
+ else{ |
|
33 |
+ $view = $arg; |
|
34 |
+ } |
|
35 |
+ |
|
36 |
+ if(isset($arg->_L3)){ |
|
37 |
+ $this->_L3 = $arg->_L3; |
|
38 |
+ } |
|
39 |
+ } |
|
40 |
+ else{ |
|
41 |
+ $view = ""; |
|
42 |
+ $context = $arg; |
|
43 |
+ } |
|
44 |
+ } |
|
45 |
+ else { |
|
46 |
+ $arg = func_get_arg(0); |
|
47 |
+ if($arg instanceof Application){ |
|
48 |
+ $this->_L3 = $arg; |
|
49 |
+ $view = func_get_arg(1); |
|
50 |
+ $context = func_get_arg(2); |
|
51 |
+ } |
|
52 |
+ else{ |
|
53 |
+ $view = func_get_arg(0); |
|
54 |
+ $context = func_get_arg(1); |
|
55 |
+ } |
|
56 |
+ } |
|
57 |
+ |
|
58 |
+ if(!isset($this->_L3)){ |
|
59 |
+ // if an Application object was not injected, take it form the global context... |
|
60 |
+ global $_L3; |
|
61 |
+ $this->_L3 = $_L3; |
|
62 |
+ } |
|
63 |
+ |
|
64 |
+ |
|
65 |
+ if($this->viewFile = $this->find($view)){ |
|
66 |
+ $this->viewFileExtension = ".".pathinfo($this->viewFile, PATHINFO_EXTENSION); |
|
67 |
+ } |
|
68 |
+ else{ |
|
69 |
+ throw new \Exception("View file:\"$view\" not found!"); |
|
70 |
+ } |
|
71 |
+ $this->context = $context; |
|
72 |
+ } |
|
73 |
+ |
|
74 |
+ public function render(){ |
|
75 |
+ if ($viewEngineClass = $this->_L3->viewEngines::get($this->viewFileExtension)){ |
|
76 |
+ $view = new $viewEngineClass(); |
|
77 |
+ return $view->render($this->viewFile, $this->context); |
|
78 |
+ } |
|
79 |
+ else{ |
|
80 |
+ throw new \Exception("View Engine not found for the view file extension:\"".$this->viewFileExtension."\"!"); |
|
81 |
+ } |
|
82 |
+ } |
|
83 |
+ |
|
84 |
+ public function find($view){ |
|
85 |
+ //find the view file |
|
86 |
+ if(is_string($view) && is_file($view)){ |
|
87 |
+ // full path was provided... |
|
88 |
+ return $view; |
|
89 |
+ } |
|
90 |
+ else{ |
|
91 |
+ // search for the view file |
|
92 |
+ |
|
93 |
+ //search for controller, action and module if present |
|
94 |
+ if(is_object($view) && $view instanceof ViewModel){ |
|
95 |
+ $controller = $view->getController(); |
|
96 |
+ $module = $view->getModule(); |
|
97 |
+ $action = $view->getAction(); |
|
98 |
+ $view = ""; |
|
99 |
+ } |
|
100 |
+ else{ |
|
101 |
+ |
|
102 |
+ $match = $this->_L3->findControllerInCallStack(); |
|
103 |
+ |
|
104 |
+ $controller = $match['controller']; |
|
105 |
+ $module = $match['module']; |
|
106 |
+ $action = $match['action']; |
|
107 |
+ } |
|
108 |
+ //locate the view file |
|
109 |
+ |
|
110 |
+ // The search order: |
|
111 |
+ // check [module/]views/controller/action/ directory (file) |
|
112 |
+ // check [module/]views/controller/ directory (file) |
|
113 |
+ // check [module/]views/ |
|
114 |
+ |
|
115 |
+ $viewsPath = $this->_L3->baseDirectory.$this->_L3->applicationDirectory.DIRECTORY_SEPARATOR |
|
116 |
+ .($module!=""?$module.DIRECTORY_SEPARATOR:"").$this->_L3->viewsDirectory.DIRECTORY_SEPARATOR; |
|
117 |
+ |
|
118 |
+ $dirs = array(); |
|
119 |
+ |
|
120 |
+ $dirs[] = $viewsPath.$controller.DIRECTORY_SEPARATOR.$action; |
|
121 |
+ $dirs[] = $viewsPath.$controller.DIRECTORY_SEPARATOR; |
|
122 |
+ $dirs[] = $viewsPath; |
|
123 |
+ |
|
124 |
+ foreach($dirs as $dir){ |
|
125 |
+ // check if the file exists |
|
126 |
+ if(is_file($found = $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view)){ |
|
127 |
+ return $found; |
|
128 |
+ } |
|
129 |
+ // try to add extension and check again |
|
130 |
+ else foreach($this->_L3->viewEngines->getRegisteredFileExtensions() as $registeredExtension){ |
|
131 |
+ if(is_file($found = $dir.($action!=""&&$view!=""?DIRECTORY_SEPARATOR:"").$view.$registeredExtension)){ |
|
132 |
+ return $found; |
|
133 |
+ } |
|
134 |
+ } |
|
135 |
+ } |
|
136 |
+ |
|
137 |
+ |
|
138 |
+ } |
|
139 |
+ return null; |
|
140 |
+ } |
|
141 |
+} |
|
0 | 142 |
\ No newline at end of file |