22
33namespace 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+
516class 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
648730HTML ;
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}
0 commit comments