-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5 from DeGraciaMathieu/feat/add-render-module
feat : added render module
- Loading branch information
Showing
12 changed files
with
203 additions
and
35 deletions.
There are no files selected for viewing
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
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
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
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
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,8 @@ | ||
<?php | ||
|
||
namespace App\Modules\Render\Contracts; | ||
|
||
interface Renderer | ||
{ | ||
public function display(string $view, array $attributes): void; | ||
} |
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,19 @@ | ||
<?php | ||
|
||
namespace App\Modules\Render\Dtos; | ||
|
||
use App\Modules\Render\Enums\RenderType; | ||
|
||
class Option | ||
{ | ||
public function __construct( | ||
public RenderType $type, | ||
) {} | ||
|
||
public static function fromCommand(array $attributes): Option | ||
{ | ||
$type = $attributes['json'] ? RenderType::Json : RenderType::View; | ||
|
||
return new self($type); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
namespace App\Modules\Render\Enums; | ||
|
||
enum RenderType: string | ||
{ | ||
case Json = 'json'; | ||
case View = 'view'; | ||
} |
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,23 @@ | ||
<?php | ||
|
||
namespace App\Modules\Render; | ||
|
||
use App\Modules\Render\Dtos\Option; | ||
use App\Modules\Render\Contracts\Renderer; | ||
use App\Modules\Render\Renderers\JsonRenderer; | ||
use App\Modules\Render\Renderers\ViewRenderer; | ||
|
||
class RendererFactory | ||
{ | ||
private array $renderers = [ | ||
'json' => JsonRenderer::class, | ||
'view' => ViewRenderer::class, | ||
]; | ||
|
||
public function from(Option $option): Renderer | ||
{ | ||
$renderer = $this->renderers[$option->type->value]; | ||
|
||
return app($renderer); | ||
} | ||
} |
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,13 @@ | ||
<?php | ||
|
||
namespace App\Modules\Render\Renderers; | ||
|
||
use App\Modules\Render\Contracts\Renderer; | ||
|
||
class JsonRenderer implements Renderer | ||
{ | ||
public function display(string $view, array $attributes): void | ||
{ | ||
echo json_encode($attributes); | ||
} | ||
} |
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,34 @@ | ||
<?php | ||
|
||
namespace App\Modules\Render\Renderers; | ||
|
||
use App\Modules\Render\Contracts\Renderer; | ||
use Termwind\HtmlRenderer; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Illuminate\Contracts\View\View as ViewContract; | ||
use Illuminate\View\Factory as ViewFactory; | ||
|
||
class ViewRenderer implements Renderer | ||
{ | ||
public function __construct( | ||
private ViewFactory $view, | ||
private HtmlRenderer $htmlRenderer, | ||
) {} | ||
|
||
public function display(string $view, array $attributes): void | ||
{ | ||
$html = $this->makeHtml($view, $attributes); | ||
|
||
$this->renderHtml($html); | ||
} | ||
|
||
protected function makeHtml(string $view, array $attributes): ViewContract | ||
{ | ||
return $this->view->make($view, $attributes); | ||
} | ||
|
||
private function renderHtml(ViewContract $html): void | ||
{ | ||
$this->htmlRenderer->render($html, OutputInterface::OUTPUT_NORMAL); | ||
} | ||
} |
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
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,44 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Render; | ||
|
||
use Tests\TestCase; | ||
use App\Modules\Render\RendererFactory; | ||
use App\Modules\Render\Dtos\Option; | ||
use App\Modules\Render\Renderers\JsonRenderer; | ||
use App\Modules\Render\Renderers\ViewRenderer; | ||
|
||
class RendererFactoryTest extends TestCase | ||
{ | ||
/** | ||
* @test | ||
*/ | ||
public function it_able_to_instantiate_json_renderer(): void | ||
{ | ||
$factory = new RendererFactory(); | ||
|
||
$renderer = $factory->from( | ||
Option::fromCommand(attributes: [ | ||
'json' => true, | ||
]), | ||
); | ||
|
||
$this->assertInstanceOf(JsonRenderer::class, $renderer); | ||
} | ||
|
||
/** | ||
* @test | ||
*/ | ||
public function it_able_to_instantiate_view_renderer(): void | ||
{ | ||
$factory = new RendererFactory(); | ||
|
||
$renderer = $factory->from( | ||
Option::fromCommand(attributes: [ | ||
'json' => false, | ||
]), | ||
); | ||
|
||
$this->assertInstanceOf(ViewRenderer::class, $renderer); | ||
} | ||
} |