|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace CodexShaper\Blade; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Container\Container; |
|
6
|
|
|
use Illuminate\Contracts\Container\Container as ContainerContract; |
|
7
|
|
|
use Illuminate\Events\Dispatcher; |
|
8
|
|
|
use Illuminate\Filesystem\Filesystem; |
|
9
|
|
|
use Illuminate\Support\Facades\Facade; |
|
10
|
|
|
use Illuminate\View\ViewServiceProvider; |
|
11
|
|
|
|
|
12
|
|
|
class View |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* @var |
|
16
|
|
|
*/ |
|
17
|
|
|
protected $app; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* @var |
|
21
|
|
|
*/ |
|
22
|
|
|
private $view; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var |
|
26
|
|
|
*/ |
|
27
|
|
|
private $viewPaths; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* @var |
|
31
|
|
|
*/ |
|
32
|
|
|
private $cachePath; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Create view factory instance if not exists. |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $views |
|
38
|
|
|
* @param string $cache |
|
39
|
|
|
* @param \Illuminate\Contracts\Container\Container|null $container |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
|
|
|
|
|
42
|
|
|
*/ |
|
43
|
|
|
public function __construct($views = [], $cache = '', ContainerContract $container = null) |
|
44
|
|
|
{ |
|
45
|
|
|
$this->app = $container; |
|
46
|
|
|
|
|
47
|
|
|
if (is_null($this->app)) { |
|
48
|
|
|
if (!is_array($views)) { |
|
49
|
|
|
$views = (array) $views; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
$this->viewPaths = $views; |
|
53
|
|
|
$this->cachePath = $cache; |
|
54
|
|
|
|
|
55
|
|
|
$this->app = $container ?? new Container(); |
|
56
|
|
|
Facade::setFacadeApplication($this->app); |
|
|
|
|
|
|
57
|
|
|
|
|
58
|
|
|
// Check this files, events and config is binding with shared or not. If not then bind |
|
59
|
|
|
$this->registerFileSystem(); |
|
60
|
|
|
$this->registerEvents(); |
|
61
|
|
|
$this->registerConfig(); |
|
62
|
|
|
|
|
63
|
|
|
// Make sure files, events and config are register before call register |
|
64
|
|
|
with(new ViewServiceProvider($this->app))->register(); |
|
|
|
|
|
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$this->view = $this->app['view']; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Bind files calss if not exists. |
|
72
|
|
|
* |
|
73
|
|
|
* @return void |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function registerFileSystem() |
|
76
|
|
|
{ |
|
77
|
|
|
$this->app->bindIf('files', function () { |
|
78
|
|
|
return new Filesystem(); |
|
79
|
|
|
}, true); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Bind events calss if not exists. |
|
84
|
|
|
* |
|
85
|
|
|
* @return void |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function registerEvents() |
|
88
|
|
|
{ |
|
89
|
|
|
$this->app->bindIf('events', function () { |
|
90
|
|
|
return new Dispatcher(); |
|
91
|
|
|
}, true); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Bind config calss if not exists. |
|
96
|
|
|
* |
|
97
|
|
|
* @return void |
|
98
|
|
|
*/ |
|
99
|
|
|
protected function registerConfig() |
|
100
|
|
|
{ |
|
101
|
|
|
if (!is_array($this->viewPaths)) { |
|
102
|
|
|
throw new \Exception('Views path must be array'); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$self = $this; |
|
106
|
|
|
|
|
107
|
|
|
$this->app->bindIf('config', function () use ($self) { |
|
108
|
|
|
return [ |
|
109
|
|
|
'view.paths' => $self->viewPaths, |
|
110
|
|
|
'view.compiled' => $self->cachePath, |
|
111
|
|
|
]; |
|
112
|
|
|
}, true); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Get blade compiler. |
|
117
|
|
|
* |
|
118
|
|
|
* @return \Illuminate\View\Compilers\BladeCompiler |
|
119
|
|
|
*/ |
|
120
|
|
|
public function blade() |
|
121
|
|
|
{ |
|
122
|
|
|
return $this->app['blade.compiler']; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Call view factory methods dynamically. |
|
127
|
|
|
* |
|
128
|
|
|
* @param string $method |
|
129
|
|
|
* @param array $parameters |
|
130
|
|
|
* |
|
131
|
|
|
* @return \Illuminate\View\View|\BadMethodCallException |
|
132
|
|
|
*/ |
|
133
|
|
|
public function __call($method, $parameters) |
|
134
|
|
|
{ |
|
135
|
|
|
if (! method_exists(new self, $method) && method_exists($this->view, $method)) { |
|
136
|
|
|
return call_user_func_array([$this->view, $method], $parameters); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
return $this->$method(...$parameters); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Call view factory methods statically. |
|
144
|
|
|
* |
|
145
|
|
|
* @param string $method |
|
146
|
|
|
* @param array $parameters |
|
147
|
|
|
* |
|
148
|
|
|
* @return \Illuminate\View\View|\BadMethodCallException |
|
149
|
|
|
*/ |
|
150
|
|
|
public static function __callStatic($method, $parameters) |
|
151
|
|
|
{ |
|
152
|
|
|
if (! method_exists(new static(), $method) && method_exists($this->view, $method)) { |
|
153
|
|
|
return forward_static_call_array([$this->view, $method], $parameters); |
|
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return (new static() )->$method(...$parameters); |
|
157
|
|
|
} |
|
158
|
|
|
} |
|
159
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.