-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathViewRenderer.php
More file actions
142 lines (118 loc) · 3.01 KB
/
Copy pathViewRenderer.php
File metadata and controls
142 lines (118 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace cyneek\yii2\blade;
use Yii;
use yii\base\InvalidParamException;
use yii\base\ViewRenderer as BaseViewRenderer;
use \Philo\Blade\Blade;
class ViewRenderer extends BaseViewRenderer
{
/**
* Extension the special view files will use
*
* @var string
*/
public $extension = 'blade';
/**
* Path where view cache will be located
* Warning: this path must be writable by PHP.
*
* @var String
*/
public $cachePath;
/**
* List of view paths
*
* @var String[]
*/
public $viewPaths = [];
/**
* Blade object used to make the dawgh
*
* @var \Philo\Blade\Blade
*/
public $blade;
/**
* Main view that will be used as layout for the rest of
* views in this render.
*
* If it's null the layout will be the view sent in the
* render.
*
* @var string
*/
protected $layoutView;
/**
* @inheritdoc
*/
public function init()
{
if (is_null($this->cachePath) || !is_string($this->cachePath))
{
throw new InvalidParamException('Cache path must be a declared string');
}
foreach ($this->viewPaths as $key => $path)
{
$this->viewPaths[$key] = Yii::getAlias($path);
}
$this->blade = new Blade($this->viewPaths, Yii::getAlias($this->cachePath));
$this->blade->view()->addExtension($this->extension, $this->extension);
}
/**
* Renders the view file sent by /Yii/base/View
*
* @param \yii\base\View $view
* @param string $viewFile
* @param array $params
* @return mixed
*/
public function render($view, $viewFile, $params)
{
$viewFile = $this->normalizeView($viewFile);
if (is_null($this->layoutView))
{
$base_view = $viewFile;
$viewFile = NULL;
}
else
{
$base_view = $this->layoutView;
$this->layoutView = NULL;
}
$viewObject = $this->blade->view()->make($base_view, $params)->with('view', $view);
if (!is_null($viewFile))
{
$params['view'] = $view;
$viewObject->nest($viewFile . '_view', $viewFile, $params);
}
return $viewObject->render();
}
/**
* Returns View blade Object
*
* @return \Philo\Blade\Illuminate\View\Factory
*/
public function view()
{
return $this->blade->view();
}
/**
* Adds a layout to the Blade System to use as a base view
*
* @param $layout
*/
public function addLayout($layout)
{
$this->layoutView = $this->normalizeView($layout);
}
/**
* @param String $view
* @return mixed
*/
protected function normalizeView($view)
{
$directory = pathinfo($view, PATHINFO_DIRNAME);
$viewFile = pathinfo($view, PATHINFO_FILENAME);
$this->blade->view()->getFinder()->addLocation($directory . '/');
return $viewFile;
}
}