Created
September 16, 2025 12:08
-
-
Save millancore/a6ac5ed1b102f9ffa73d716e2adb5bd3 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| <?php | |
| namespace App\Libraries; | |
| use CodeIgniter\Config\View as ViewConfig; | |
| use CodeIgniter\View\Exceptions\ViewException; | |
| use CodeIgniter\View\RendererInterface; | |
| use Millancore\Pesto\Environment as PestoEngine; | |
| use Millancore\Pesto\PestoFactory; | |
| use RuntimeException; | |
| class PestoAdapter implements RendererInterface | |
| { | |
| protected PestoEngine $pesto; | |
| protected array $data = []; | |
| /** | |
| * @var \Config\View | |
| */ | |
| protected $config; | |
| /** | |
| * The base directory to look in for our Views. | |
| * | |
| * @var string | |
| */ | |
| protected $viewPath; | |
| /** | |
| * Should we store performance info? | |
| * | |
| * @var bool | |
| */ | |
| protected $debug = false; | |
| /** | |
| * Data for rendering including Caching and Debug Toolbar data. | |
| * | |
| * @var array<string, mixed> | |
| */ | |
| protected $renderVars = []; | |
| /** | |
| * Cache stats about our performance here, | |
| * when CI_DEBUG = true | |
| * | |
| * @var list<array{start: float, end: float, view: string}> | |
| */ | |
| protected $performanceData = []; | |
| public function __construct( | |
| ViewConfig $config, | |
| ?string $viewPath = null, | |
| ?bool $debug = null, | |
| ) | |
| { | |
| $this->config = $config; | |
| $this->viewPath = rtrim($viewPath, '\\/ ') . DIRECTORY_SEPARATOR; | |
| $this->debug = $debug ?? CI_DEBUG; | |
| $this->pesto = PestoFactory::create( | |
| templatesPath: $viewPath, | |
| cachePath: WRITEPATH . 'cache/pesto/', | |
| ); | |
| } | |
| public function render(string $view, ?array $options = null, ?bool $saveData = null): string | |
| { | |
| $this->renderVars['start'] = microtime(true); | |
| $fileExt = pathinfo($view, PATHINFO_EXTENSION); | |
| $this->renderVars['view'] = ($fileExt === '') ? $view . '.php' : $view; | |
| if(!is_file($this->viewPath . DIRECTORY_SEPARATOR . $this->renderVars['view'])) { | |
| throw ViewException::forInvalidFile($this->viewPath . DIRECTORY_SEPARATOR . $this->renderVars['view']); | |
| } | |
| if ($this->debug) { | |
| $this->performanceData[] = [ | |
| 'start' => $this->renderVars['start'], | |
| 'end' => microtime(true), | |
| 'view' => $this->renderVars['view'], | |
| ]; | |
| } | |
| $output = $this->pesto->make($this->renderVars['view'], $this->data)->toHtml(); | |
| if (!$this->config->saveData || $saveData === false) { | |
| $this->data = []; | |
| } | |
| return $output; | |
| } | |
| public function renderString(string $string, ?array $options = null, ?bool $saveData = null): string | |
| { | |
| throw new RuntimeException('Not implemented'); | |
| } | |
| public function setVar(string $name, $value = null, ?string $context = null) | |
| { | |
| $this->data[$name] = $value; | |
| return $this; | |
| } | |
| public function setData(array $data = [], ?string $context = null): RendererInterface | |
| { | |
| $this->data = array_merge($this->data, $data); | |
| return $this; | |
| } | |
| public function getData(string $context = null): array | |
| { | |
| return $this->data; | |
| } | |
| public function resetData(): RendererInterface | |
| { | |
| $this->data = []; | |
| return $this; | |
| } | |
| public function getPerformanceData(): array | |
| { | |
| return $this->performanceData; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment