-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #758 from hydephp/Create-class-AnonymousViewCompiler
Add simple class to compile any Blade file without having to register it as a view path
- Loading branch information
Showing
2 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
38 changes: 38 additions & 0 deletions
38
packages/framework/src/Framework/Actions/AnonymousViewCompiler.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Actions; | ||
|
||
use Hyde\Facades\Filesystem; | ||
use Hyde\Framework\Concerns\InvokableAction; | ||
use Hyde\Framework\Exceptions\FileNotFoundException; | ||
use Illuminate\Support\Facades\Blade; | ||
|
||
/** | ||
* Compile any Blade file using the Blade facade as it allows us to render | ||
* it without having to register the directory with the view finder. | ||
*/ | ||
class AnonymousViewCompiler extends InvokableAction | ||
{ | ||
protected string $viewPath; | ||
protected array $data; | ||
|
||
public function __construct(string $viewPath, array $data = []) | ||
{ | ||
$this->viewPath = $viewPath; | ||
$this->data = $data; | ||
} | ||
|
||
public function __invoke(): string | ||
{ | ||
if (Filesystem::missing($this->viewPath)) { | ||
throw new FileNotFoundException($this->viewPath); | ||
} | ||
|
||
return Blade::render( | ||
Filesystem::getContents($this->viewPath), | ||
$this->data | ||
); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/framework/tests/Feature/Actions/AnonymousViewCompilerTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Framework\Testing\Feature\Actions; | ||
|
||
use Hyde\Framework\Actions\AnonymousViewCompiler; | ||
use Hyde\Framework\Exceptions\FileNotFoundException; | ||
use Hyde\Testing\TestCase; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Actions\AnonymousViewCompiler | ||
*/ | ||
class AnonymousViewCompilerTest extends TestCase | ||
{ | ||
public function testCanCompileBladeFile() | ||
{ | ||
$this->file('foo.blade.php', "{{ 'Hello World' }}"); | ||
|
||
$this->assertSame('Hello World', AnonymousViewCompiler::call('foo.blade.php')); | ||
} | ||
|
||
public function testCanCompileBladeFileWithData() | ||
{ | ||
$this->file('foo.blade.php', '{{ $foo }}'); | ||
|
||
$this->assertSame('bar', AnonymousViewCompiler::call('foo.blade.php', ['foo' => 'bar'])); | ||
} | ||
|
||
public function testWithMissingView() | ||
{ | ||
$this->expectException(FileNotFoundException::class); | ||
$this->expectExceptionMessage('File foo.blade.php not found.'); | ||
|
||
AnonymousViewCompiler::call('foo.blade.php'); | ||
} | ||
} |