Skip to content

Commit 9e1761e

Browse files
committed
Support Factory dependency injection
1 parent 67fd019 commit 9e1761e

1 file changed

Lines changed: 46 additions & 9 deletions

File tree

src/ArrayView/Factory.php

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class Factory
1313
*/
1414
protected $viewPaths = [];
1515

16+
/**
17+
* Factory dependency injection
18+
*/
19+
protected $factoryDI = null;
20+
1621
/**
1722
* The extension to engine bindings.
1823
*
@@ -33,9 +38,10 @@ class Factory
3338
* @param array $viewPaths
3439
* @return void
3540
*/
36-
public function __construct($viewPaths = [])
41+
public function __construct($viewPaths = [], $factoryDI = null)
3742
{
3843
$this->viewPaths = $viewPaths;
44+
$this->factoryDI = $factoryDI;
3945
}
4046

4147
/**
@@ -89,19 +95,34 @@ public function render($view, $data = [], $mergeData = [])
8995
return $this->results;
9096
}
9197

98+
/**
99+
* Get results
100+
*
101+
* @return array
102+
*/
92103
public function getResults()
93104
{
94105
return $this->results;
95106
}
96107

108+
/**
109+
* Set result
110+
*
111+
* @param array $results
112+
*/
113+
public function setResults($results = [])
114+
{
115+
$this->results = $results;
116+
}
117+
97118
/**
98119
* Get path of view
99120
*
100121
* @param string $view View
101122
* @return string View Path
102123
* @author HuyTBT <huytbt@gmail.com>
103124
*/
104-
protected function getViewPath($view)
125+
public function getViewPath($view)
105126
{
106127
if (file_exists($view)) {
107128
return $view;
@@ -125,7 +146,7 @@ protected function getViewPath($view)
125146
* @return string View Path
126147
* @author HuyTBT <huytbt@gmail.com>
127148
*/
128-
protected function getHelperPath($helper)
149+
public function getHelperPath($helper)
129150
{
130151
if (file_exists($helper)) {
131152
return $helper;
@@ -150,7 +171,7 @@ protected function getHelperPath($helper)
150171
* @return ChickenCoder\ArrayView\Factory $this
151172
* @author HuyTBT <huytbt@gmail.com>
152173
*/
153-
protected function set($key, $value = null)
174+
public function set($key, $value = null)
154175
{
155176
if (func_num_args() === 1) {
156177
$key === '{}' && $key = json_decode('{}');
@@ -159,7 +180,7 @@ protected function set($key, $value = null)
159180
}
160181

161182
if ($value instanceof Closure) {
162-
$factory = new Factory($this->viewPaths);
183+
$factory = $this->getFactory();
163184
$value($factory);
164185
$this->results[$key] = $factory->getResults();
165186
return $this;
@@ -179,9 +200,9 @@ protected function set($key, $value = null)
179200
* @return array
180201
* @author HuyTBT <huytbt@gmail.com>
181202
*/
182-
protected function each($data = [], Closure $callback)
203+
public function each($data = [], Closure $callback)
183204
{
184-
$factory = new Factory($this->viewPaths);
205+
$factory = $this->getFactory();
185206
$results = array();
186207
foreach ($data as $item) {
187208
$callback($factory, $item);
@@ -200,9 +221,9 @@ protected function each($data = [], Closure $callback)
200221
* @return ChickenCoder\ArrayView\Factory
201222
* @author HuyTBT <huytbt@gmail.com>
202223
*/
203-
protected function partial($partialView, $data = [], $mergeData = [])
224+
public function partial($partialView, $data = [], $mergeData = [])
204225
{
205-
$factory = new Factory($this->viewPaths);
226+
$factory = $this->getFactory();
206227

207228
return $factory->render($partialView, $data, $mergeData);
208229
}
@@ -252,4 +273,20 @@ public function helper($helper)
252273

253274
return call_user_func_array($callback, $args);
254275
}
276+
277+
/**
278+
* Get factory
279+
* @return Factory
280+
*/
281+
private function getFactory()
282+
{
283+
if (!empty($this->factoryDI)) {
284+
$factory = clone $this->factoryDI;
285+
} else {
286+
$factory = clone $this;
287+
$factory->setResults([]);
288+
}
289+
290+
return $factory;
291+
}
255292
}

0 commit comments

Comments
 (0)