From 935de072bbdfd0a3c5513491b26a68af1b73b3ec Mon Sep 17 00:00:00 2001 From: Jens Segers Date: Sat, 25 May 2019 09:30:44 +0200 Subject: [PATCH] :art: Use more typehinting --- src/Blade.php | 94 +++++++++++++++++++++++++++++++++------------------ 1 file changed, 61 insertions(+), 33 deletions(-) diff --git a/src/Blade.php b/src/Blade.php index 51749ee..e4d3875 100644 --- a/src/Blade.php +++ b/src/Blade.php @@ -4,6 +4,7 @@ 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; @@ -11,18 +12,8 @@ use Illuminate\View\Factory; use Illuminate\View\ViewServiceProvider; -class Blade +class Blade implements FactoryContract { - /** - * @var array|string - */ - private $viewPaths; - - /** - * @var string - */ - private $cachePath; - /** * @var Container */ @@ -40,36 +31,16 @@ class Blade public function __construct($viewPaths, string $cachePath, ContainerInterface $container = null) { - $this->viewPaths = $viewPaths; - $this->cachePath = $cachePath; $this->container = $container ?: new Container; - $this->setupContainer(); + $this->setupContainer((array) $viewPaths, $cachePath); (new ViewServiceProvider($this->container))->register(); $this->factory = $this->container->get('view'); $this->compiler = $this->container->get('blade.compiler'); } - protected function setupContainer() - { - $this->container->bindIf('files', function () { - return new Filesystem; - }, true); - - $this->container->bindIf('events', function () { - return new Dispatcher; - }, true); - - $this->container->bindIf('config', function () { - return [ - 'view.paths' => (array) $this->viewPaths, - 'view.compiled' => $this->cachePath, - ]; - }, true); - } - - public function make(string $view, array $data = [], array $mergeData = []): View + public function make($view, $data = [], $mergeData = []): View { return $this->factory->make($view, $data, $mergeData); } @@ -84,8 +55,65 @@ public function directive(string $name, callable $handler) $this->compiler->directive($name, $handler); } + 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->shared($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); + } }