Advertisement
kwasinski

PHP Template Trait

Jun 29th, 2022
638
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php declare(strict_types = 1);
  2.  
  3. namespace Xxxxxx\Support\Unit\Traits;
  4.  
  5. use Jenssegers\Blade\Blade;
  6.  
  7. use Exception;
  8. use ReflectionObject;
  9.  
  10. trait TemplateTrait
  11. {
  12.     protected ?Blade $template = null;
  13.  
  14.     protected ?string $dir = null;
  15.  
  16.     protected ?string $templateDir = null;
  17.  
  18.     protected ?string $templateCacheDir = null;
  19.  
  20.     protected string $viewName = '';
  21.  
  22.     private static $instance = null;
  23.  
  24.  
  25.     /**
  26.      * getDir
  27.      *
  28.      * @return string
  29.      */
  30.     public function getDir(): string
  31.     {
  32.         return $this->dir;
  33.     }
  34.  
  35.     /**
  36.      * getTemplateDir
  37.      *
  38.      * @return string
  39.      */
  40.     public function getTemplateDir(): string
  41.     {
  42.         return $this->templateDir;
  43.     }
  44.  
  45.     /**
  46.      * setViewName
  47.      *
  48.      * @return string
  49.      */
  50.     public function setViewName(string $viewName): void
  51.     {
  52.         $this->viewName = $viewName;
  53.     }
  54.  
  55.     /**
  56.      * getViewName
  57.      *
  58.      * @return string
  59.      */
  60.     public function getViewName(): string
  61.     {
  62.         return $this->viewName;
  63.     }
  64.  
  65.  
  66.     /**
  67.      * instance
  68.      *
  69.      * @return static::class
  70.      */
  71.     public static function instance()
  72.     {
  73.         if (static::$instance === null) {
  74.             $instance = new static();
  75.  
  76.             static::$instance = $instance;
  77.         }
  78.  
  79.         return static::$instance;
  80.     }
  81.  
  82.     /**
  83.      * view
  84.      *
  85.      * @param  mixed $viewName
  86.      * @return void
  87.      */
  88.     public function view(string $viewName)
  89.     {
  90.         $this->setViewName($viewName);
  91.  
  92.         if (!file_exists($this->getTemplateDir() . '/'. $this->getViewName())) throw new Exception('Template  '.$viewName.' does not exists.');
  93.  
  94.         return $this;
  95.     }
  96.  
  97.     /**
  98.      * getInstanceObjectFilePath
  99.      *
  100.      * @return string
  101.      */
  102.     public function getInstanceObjectFilePath(): string
  103.     {
  104.         return plugin_dir_path((new ReflectionObject($this))->getFilename());
  105.     }
  106.  
  107.     /**
  108.      * template
  109.      *
  110.      * @return void
  111.      */
  112.     public function template()
  113.     {
  114.         $this->dir = $this->getInstanceObjectFilePath();
  115.         $this->templateDir = $this->dir . 'Views';
  116.         $this->templateCacheDir = $this->dir . 'Views/Cache';
  117.  
  118.         if (!is_dir($this->templateDir))        throw new Exception('Views directory does not exists at: ' . $this->dir);
  119.         if (!is_dir($this->templateCacheDir))   throw new Exception('Views/Cache directory does not exists at: ' . $this->dir);
  120.  
  121.         $this->template = new Blade(
  122.             $this->templateDir,
  123.             $this->templateCacheDir,
  124.         );
  125.  
  126.         if ($this->template === null) throw new Exception('Template was not initialized');
  127.  
  128.         return $this;
  129.     }
  130.  
  131.     /**
  132.      * render
  133.      *
  134.      * @param  mixed $arguments
  135.      * @return void
  136.      */
  137.     public function render(array $arguments): void
  138.     {
  139.         print $this->template->render(
  140.             $this->getViewName(),
  141.             $arguments,
  142.         );
  143.  
  144.         return;
  145.     }
  146. }
  147.  
  148.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement