/
Blade.php
132 lines (105 loc) 路 3.35 KB
/
Blade.php
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
<?php
namespace Jenssegers\Blade;
use Illuminate\Container\Container;
use Illuminate\Contracts\Container\Container as ContainerInterface;
use Illuminate\Contracts\View\Factory as FactoryContract;
use Illuminate\Contracts\View\View;
use Illuminate\Events\Dispatcher;
use Illuminate\Filesystem\Filesystem;
use Illuminate\Support\Facades\Facade;
use Illuminate\View\Compilers\BladeCompiler;
use Illuminate\View\Factory;
use Illuminate\View\ViewServiceProvider;
class Blade implements FactoryContract
{
/**
* @var Container
*/
protected $container;
/**
* @var Factory
*/
private $factory;
/**
* @var BladeCompiler
*/
private $compiler;
public function __construct($viewPaths, string $cachePath, ContainerInterface $container = null)
{
$this->container = $container ?: new Container;
$this->setupContainer((array) $viewPaths, $cachePath);
(new ViewServiceProvider($this->container))->register();
$this->factory = $this->container->get('view');
$this->compiler = $this->container->get('blade.compiler');
}
public function render(string $view, array $data = [], array $mergeData = []): string
{
return $this->make($view, $data, $mergeData)->render();
}
public function make($view, $data = [], $mergeData = []): View
{
return $this->factory->make($view, $data, $mergeData);
}
public function compiler(): BladeCompiler
{
return $this->compiler;
}
public function directive(string $name, callable $handler)
{
$this->compiler->directive($name, $handler);
}
public function if($name, callable $callback)
{
$this->compiler->if($name, $callback);
}
public function exists($view): bool
{
return $this->factory->exists($view);
}
public function file($path, $data = [], $mergeData = []): View
{
return $this->factory->file($path, $data, $mergeData);
}
public function share($key, $value = null)
{
return $this->factory->share($key, $value);
}
public function composer($views, $callback): array
{
return $this->factory->composer($views, $callback);
}
public function creator($views, $callback): array
{
return $this->factory->creator($views, $callback);
}
public function addNamespace($namespace, $hints): self
{
$this->factory->addNamespace($namespace, $hints);
return $this;
}
public function replaceNamespace($namespace, $hints): self
{
$this->factory->replaceNamespace($namespace, $hints);
return $this;
}
public function __call(string $method, array $params)
{
return call_user_func_array([$this->factory, $method], $params);
}
protected function setupContainer(array $viewPaths, string $cachePath)
{
$this->container->bindIf('files', function () {
return new Filesystem;
}, true);
$this->container->bindIf('events', function () {
return new Dispatcher;
}, true);
$this->container->bindIf('config', function () use ($viewPaths, $cachePath) {
return [
'view.paths' => $viewPaths,
'view.compiled' => $cachePath,
];
}, true);
Facade::setFacadeApplication($this->container);
}
}