mirror of
https://github.com/ad-aures/castopod.git
synced 2026-04-13 11:37:46 +02:00
refactor(view-components): use CI4's View Decorators to render components
This commit is contained in:
parent
a182d96f18
commit
a3ebd6c9a4
6 changed files with 42 additions and 181 deletions
|
|
@ -14,23 +14,11 @@ class ComponentRenderer
|
|||
{
|
||||
protected ViewComponents $config;
|
||||
|
||||
/**
|
||||
* File name of the view source
|
||||
*/
|
||||
protected string $currentView;
|
||||
|
||||
public function __construct()
|
||||
{
|
||||
$this->config = config('ViewComponents');
|
||||
}
|
||||
|
||||
public function setCurrentView(string $view): self
|
||||
{
|
||||
$this->currentView = $view;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function render(string $output): string
|
||||
{
|
||||
// Try to locate any custom tags, with PascalCase names like: Button, Label, etc.
|
||||
|
|
@ -139,10 +127,7 @@ class ComponentRenderer
|
|||
// TODO: Is there a better way to locate components local to current module?
|
||||
$pathsToDiscover = [];
|
||||
$lookupPaths = $this->config->lookupPaths;
|
||||
$pathsToDiscover = array_filter($lookupPaths, function ($path): bool {
|
||||
return str_starts_with($this->currentView, $path);
|
||||
});
|
||||
$pathsToDiscover = array_values($pathsToDiscover);
|
||||
$pathsToDiscover = array_values($lookupPaths);
|
||||
$pathsToDiscover[] = $this->config->defaultLookupPath;
|
||||
|
||||
$namePath = str_replace('.', '/', $name);
|
||||
|
|
|
|||
38
app/Libraries/ViewComponents/Decorator.php
Normal file
38
app/Libraries/ViewComponents/Decorator.php
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ViewComponents;
|
||||
|
||||
use CodeIgniter\View\ViewDecoratorInterface;
|
||||
|
||||
/**
|
||||
* Class Decorator
|
||||
*
|
||||
* Enables rendering of View Components into the views.
|
||||
*
|
||||
* Borrowed and adapted from https://github.com/lonnieezell/Bonfire2/
|
||||
*/
|
||||
class Decorator implements ViewDecoratorInterface
|
||||
{
|
||||
private static ?ComponentRenderer $components = null;
|
||||
|
||||
public static function decorate(string $html): string
|
||||
{
|
||||
$components = self::factory();
|
||||
|
||||
return $components->render($html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Factory method to create a new instance of ComponentRenderer
|
||||
*/
|
||||
private static function factory(): ComponentRenderer
|
||||
{
|
||||
if (self::$components === null) {
|
||||
self::$components = new ComponentRenderer();
|
||||
}
|
||||
|
||||
return self::$components;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue