Skip to content

Commit ea2563a

Browse files
committed
feat: switch from jens blade to local version
1 parent c5abeb3 commit ea2563a

File tree

3 files changed

+146
-13
lines changed

3 files changed

+146
-13
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
],
2323
"require": {
2424
"php": ">=7.4|^8.0",
25-
"jenssegers/blade": "^1.4"
25+
"illuminate/view": "^8.0|^10.0|^12.0",
26+
"illuminate/config": "^8.0|^10.0|^12.0"
2627
},
2728
"autoload": {
2829
"psr-4": {

src/Blade.php

Lines changed: 119 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,39 @@
22

33
namespace Leaf;
44

5+
use Illuminate\Config\Repository;
6+
use Illuminate\Contracts\Foundation\Application;
7+
use Illuminate\Contracts\View\View;
8+
use Illuminate\Events\Dispatcher;
9+
use Illuminate\Filesystem\Filesystem;
10+
use Illuminate\Support\Facades\Facade;
11+
use Illuminate\View\Compilers\BladeCompiler;
12+
use Illuminate\View\Factory;
13+
use Illuminate\View\ViewServiceProvider;
14+
use Leaf\Blade\Container;
15+
516
class Blade
617
{
7-
protected $blade;
18+
/**
19+
* @var Application
20+
*/
21+
protected $container;
22+
23+
/**
24+
* @var Factory
25+
*/
26+
private $factory;
27+
28+
/**
29+
* @var BladeCompiler
30+
*/
31+
private $compiler;
832

933
public function __construct(?string $viewPaths = null, ?string $cachePath = null)
1034
{
11-
// Just to maintain compatibility with the original Leaf Blade
35+
if ($viewPaths) {
36+
$this->configure($viewPaths, $cachePath);
37+
}
1238
}
1339

1440
/**
@@ -17,7 +43,7 @@ public function __construct(?string $viewPaths = null, ?string $cachePath = null
1743
* @param string|array $viewPaths The path to your view or an array of paths
1844
* @param string|null $cachePath The path to your cache directory
1945
*
20-
* @return \Jenssegers\Blade\Blade
46+
* @return \Leaf\Blade
2147
*/
2248
public function configure($viewPaths, ?string $cachePath = null)
2349
{
@@ -26,10 +52,17 @@ public function configure($viewPaths, ?string $cachePath = null)
2652
$viewPaths = $viewPaths['views'] ?? null;
2753
}
2854

29-
$this->blade = new \Jenssegers\Blade\Blade($viewPaths, $cachePath);
55+
$this->container = Container::getInstance();
56+
57+
$this->setupContainer((array) $viewPaths, $cachePath);
58+
(new ViewServiceProvider($this->container))->register();
59+
60+
$this->factory = $this->container->get('view');
61+
$this->compiler = $this->container->get('blade.compiler');
62+
3063
$this->setupDefaultDirectives();
3164

32-
return $this->blade;
65+
return $this;
3366
}
3467

3568
/**
@@ -38,7 +71,7 @@ public function configure($viewPaths, ?string $cachePath = null)
3871
* @param string|array $viewPaths The path to your view or an array of paths
3972
* @param string|null $cachePath The path to your cache directory
4073
*
41-
* @return \Jenssegers\Blade\Blade
74+
* @return \Leaf\Blade
4275
*/
4376
public function config($viewPaths, ?string $cachePath = null)
4477
{
@@ -51,7 +84,7 @@ public function config($viewPaths, ?string $cachePath = null)
5184
* A shorter version of the original `make` command.
5285
* You can optionally pass data into the view as a second parameter
5386
*/
54-
public function render(string $view, $data = [], $mergeData = [])
87+
public function render(string $view, $data = [], $mergeData = []): string
5588
{
5689
return $this->make($view, $data, $mergeData);
5790
}
@@ -64,32 +97,81 @@ public function render(string $view, $data = [], $mergeData = [])
6497
*/
6598
public function make(string $view, $data = [], $mergeData = []): string
6699
{
67-
return $this->blade->make($view, $data, $mergeData)->render();
100+
return $this->factory->make($view, $data, $mergeData)->render();
68101
}
69102

70103
/**
71104
* Add a new namespace to the loader
72105
*/
73106
public function directive(string $name, callable $handler)
74107
{
75-
$this->blade->directive($name, $handler);
108+
$this->compiler()->directive($name, $handler);
76109
}
77110

78111
/**
79112
* Return actual blade instance
80-
* @return \Jenssegers\Blade\Blade
113+
* @return \Leaf\Blade
81114
*/
82115
public function blade()
83116
{
84-
return $this->blade;
117+
return $this;
85118
}
86119

87120
/**
88121
* Hook into the blade compiler
89122
*/
90123
public function compiler()
91124
{
92-
return $this->blade->compiler();
125+
return $this->compiler;
126+
}
127+
128+
public function if($name, callable $callback)
129+
{
130+
$this->compiler->if($name, $callback);
131+
}
132+
133+
public function exists($view): bool
134+
{
135+
return $this->factory->exists($view);
136+
}
137+
138+
public function file($path, $data = [], $mergeData = []): View
139+
{
140+
return $this->factory->file($path, $data, $mergeData);
141+
}
142+
143+
public function share($key, $value = null)
144+
{
145+
return $this->factory->share($key, $value);
146+
}
147+
148+
public function composer($views, $callback): array
149+
{
150+
return $this->factory->composer($views, $callback);
151+
}
152+
153+
public function creator($views, $callback): array
154+
{
155+
return $this->factory->creator($views, $callback);
156+
}
157+
158+
public function addNamespace($namespace, $hints): self
159+
{
160+
$this->factory->addNamespace($namespace, $hints);
161+
162+
return $this;
163+
}
164+
165+
public function replaceNamespace($namespace, $hints): self
166+
{
167+
$this->factory->replaceNamespace($namespace, $hints);
168+
169+
return $this;
170+
}
171+
172+
public function __call(string $method, array $params)
173+
{
174+
return call_user_func_array([$this->factory, $method], $params);
93175
}
94176

95177
/**
@@ -648,4 +730,29 @@ class="absolute right-0 p-1.5 mr-2.5 text-gray-400 duration-100 ease-in-out roun
648730
HTML;
649731
});
650732
}
733+
734+
protected function setupContainer(array $viewPaths, string $cachePath)
735+
{
736+
$this->container->bindIf('files', function () {
737+
return new Filesystem;
738+
}, true);
739+
740+
$this->container->bindIf('events', function () {
741+
return new Dispatcher;
742+
}, true);
743+
744+
$this->container->bindIf('config', function () use ($viewPaths, $cachePath) {
745+
return new Repository([
746+
'view.paths' => $viewPaths,
747+
'view.compiled' => $cachePath,
748+
]);
749+
}, true);
750+
751+
$this->container->bindIf('blade.compiler', function ($app) use ($cachePath) {
752+
return new BladeCompiler($app['files'], $cachePath);
753+
});
754+
755+
Container::setInstance($this->container);
756+
Facade::setFacadeApplication($this->container);
757+
}
651758
}

src/Blade/Container.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Leaf\Blade;
4+
5+
use Closure;
6+
use Illuminate\Container\Container as BaseContainer;
7+
8+
class Container extends BaseContainer
9+
{
10+
protected array $terminatingCallbacks = [];
11+
12+
public function terminating(Closure $callback)
13+
{
14+
$this->terminatingCallbacks[] = $callback;
15+
16+
return $this;
17+
}
18+
19+
public function terminate()
20+
{
21+
foreach ($this->terminatingCallbacks as $terminatingCallback) {
22+
$terminatingCallback();
23+
}
24+
}
25+
}

0 commit comments

Comments
 (0)