diff --git a/src/Blade.php b/src/Blade.php index 6fb75f4..00f87e9 100644 --- a/src/Blade.php +++ b/src/Blade.php @@ -4,10 +4,12 @@ use Illuminate\Container\Container; use Illuminate\Contracts\Container\Container as ContainerInterface; +use Illuminate\Contracts\Foundation\Application; 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; @@ -15,7 +17,7 @@ class Blade implements FactoryContract { /** - * @var Container + * @var Application */ protected $container; @@ -59,6 +61,11 @@ 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 { @@ -120,5 +127,7 @@ protected function setupContainer(array $viewPaths, string $cachePath) 'view.compiled' => $cachePath, ]; }, true); + + Facade::setFacadeApplication($this->container); } } diff --git a/tests/BladeTest.php b/tests/BladeTest.php index 946f512..a8f7e27 100644 --- a/tests/BladeTest.php +++ b/tests/BladeTest.php @@ -1,5 +1,9 @@ blade->directive('datetime', function ($expression) { return "format('F d, Y g:i a'); ?>"; }); + + $this->blade->if('ifdate', function ($date) { + return $date instanceof DateTime; + }); + } + + public function testCompilerGetter() + { + $this->assertInstanceOf(BladeCompiler::class, $this->blade->compiler()); } public function testBasic() @@ -25,6 +38,11 @@ public function testBasic() $this->assertEquals('hello world', trim($output)); } + public function testExists() + { + $this->assertFalse($this->blade->exists('nonexistentview')); + } + public function testVariables() { $output = $this->blade->make('variables', ['name' => 'John Doe']); @@ -34,12 +52,49 @@ public function testVariables() public function testNonBlade() { $output = $this->blade->make('plain'); - $this->assertEquals('this is plain php', trim($output)); + $this->assertEquals('{{ this is plain php }}', trim($output)); + } + + public function testFile() + { + $output = $this->blade->file('tests/views/basic.blade.php'); + $this->assertEquals('hello world', trim($output)); + } + + public function testShare() + { + $this->blade->share('name', 'John Doe'); + + $output = $this->blade->make('variables'); + $this->assertEquals('hello John Doe', trim($output)); + } + + public function testComposer() + { + $this->blade->composer('variables', function (View $view) { + $view->with('name', 'John Doe and ' . $view->offsetGet('name')); + }); + + $output = $this->blade->make('variables', ['name' => 'Jane Doe']); + $this->assertEquals('hello John Doe and Jane Doe', trim($output)); + } + + public function testCreator() + { + $this->blade->creator('variables', function (View $view) { + $view->with('name', 'John Doe'); + }); + $this->blade->composer('variables', function (View $view) { + $view->with('name', 'Jane Doe and ' . $view->offsetGet('name')); + }); + + $output = $this->blade->make('variables'); + $this->assertEquals('hello Jane Doe and John Doe', trim($output)); } public function testRenderAlias() { - $output = $this->blade->make('basic'); + $output = $this->blade->render('basic'); $this->assertEquals('hello world', trim($output)); } @@ -49,6 +104,37 @@ public function testDirective() $this->assertEquals('Your birthday is August 19, 1989 12:00 am', trim($output)); } + public function testIf() + { + $output = $this->blade->make('if', ['birthday' => new DateTime('1989/08/19')]); + $this->assertEquals('Birthday August 19, 1989 12:00 am detected', trim($output)); + } + + public function testAddNamespace() + { + $this->blade->addNamespace('other', 'tests/views/other'); + + $output = $this->blade->make('other::basic'); + $this->assertEquals('hello other world', trim($output)); + } + + public function testReplaceNamespace() + { + $this->blade->addNamespace('other', 'tests/views/other'); + $this->blade->replaceNamespace('other', 'tests/views/another'); + + $output = $this->blade->make('other::basic'); + $this->assertEquals('hello another world', trim($output)); + } + + public function testViewGetter() + { + /** @var Factory $view */ + $view = $this->blade; + + $this->assertInstanceOf(ViewFinderInterface::class, $view->getFinder()); + } + public function testOther() { $users = [ @@ -75,31 +161,20 @@ public function testOther() 'authenticated' => false, ]); - $this->write($output, 'other'); - $this->assertEquals($output, $this->expected('other')); } - public function testExtends() - { - $output = $this->blade->make('extends'); - - $this->write($output, 'extends'); - - $this->assertEquals($output, $this->expected('extends')); - } - - private function write(string $output, string $file) + private function expected(string $file): string { $file_path = __DIR__ . '/expected/' . $file . '.html'; - file_put_contents($file_path, $output); + return file_get_contents($file_path); } - private function expected(string $file): string + public function testExtends() { - $file_path = __DIR__ . '/expected/' . $file . '.html'; + $output = $this->blade->make('extends'); - return file_get_contents($file_path); + $this->assertEquals($output, $this->expected('extends')); } } diff --git a/tests/views/another/basic.blade.php b/tests/views/another/basic.blade.php new file mode 100644 index 0000000..b95610a --- /dev/null +++ b/tests/views/another/basic.blade.php @@ -0,0 +1 @@ +hello another world diff --git a/tests/views/if.blade.php b/tests/views/if.blade.php new file mode 100644 index 0000000..e63304c --- /dev/null +++ b/tests/views/if.blade.php @@ -0,0 +1,3 @@ +@ifdate($birthday) +Birthday {{ $birthday->format('F d, Y g:i a') }} detected +@endifdate \ No newline at end of file diff --git a/tests/views/other/basic.blade.php b/tests/views/other/basic.blade.php new file mode 100644 index 0000000..8b0ca7c --- /dev/null +++ b/tests/views/other/basic.blade.php @@ -0,0 +1 @@ +hello other world \ No newline at end of file diff --git a/tests/views/plain.php b/tests/views/plain.php index ff3b69f..7fb4e6d 100644 --- a/tests/views/plain.php +++ b/tests/views/plain.php @@ -1 +1 @@ -this is plain php +{{ this is plain }}