diff --git a/src/Config.php b/src/Config.php index ab7c2d2..a1e1f66 100644 --- a/src/Config.php +++ b/src/Config.php @@ -2,26 +2,20 @@ namespace PHPForm; use PHPForm\Renderers\TwigRenderer; +use PHPForm\TemplatePacks\DefaultTemplatePack; class Config extends Singleton { /** - * @var string Directory with templates + * @var array Template packs to be used. The templates will be loaded + * accordingly to the order defined. */ - protected $templates_dir = __DIR__ . '/templates/'; + protected $template_packs = array( + DefaultTemplatePack::class, + ); /** - * @var string Default fallback template pack - */ - protected $fallback_template_pack = "default"; - - /** - * @var string Default template pack defined. If null, fallback is used. - */ - protected $template_pack = null; - - /** - * @var string Renderer class used to render html content + * @var string Renderer class used to render html content. */ protected $renderer_class = TwigRenderer::class; @@ -31,45 +25,52 @@ class Config extends Singleton private $renderer; /** - * Templates path accordingly with defined template pack. + * Return renderer class instantiated * - * @return string + * @return PHPForm\Renderers\Renderer */ - private function buildPath($template_pack) + public function getRenderer() { - $path = $this->templates_dir . $template_pack . "/"; - - if (!file_exists($path)) { - trigger_error("Template pack dir '$path' don't exists.", E_USER_ERROR); + if (is_null($this->renderer)) { + $this->renderer = new $this->renderer_class($this->getTemplatesDirs()); } - return $path; + return $this->renderer; } /** - * Return renderer class instantiated + * Set renderer class. * - * @return PHPForm\Renderers\Renderer + * @param string Class name of Renderer. */ - public function getRenderer() + public function setRenderer(string $renderer_class) { - if (is_null($this->renderer)) { - $fallback_pack_dir = $this->buildPath($this->fallback_template_pack); - $pack_dir = !is_null($this->template_pack) ? $this->buildPath($this->template_pack) : null; - - $this->renderer = new $this->renderer_class($fallback_pack_dir, $pack_dir); - } + $this->renderer_class = $renderer_class; + } - return $this->renderer; + /** + * Set template pack on top level. + * + * @param string Class name of TemplatePack. + */ + public function setTemplatePack(string $template_pack) + { + $this->template_packs = array_unshift($this->template_packs, $template_pack); } /** - * Define template pack + * Traverse all packs to extract template dir path. * - * @param string + * @return array */ - public function setTemplatePack($template_pack) + private function getTemplatesDirs() { - $this->template_pack = $template_pack; + $dirs = array(); + + foreach ($this->template_packs as $template_pack) { + $dirs[] = $template_pack::TEMPLATES_DIR; + } + + return $dirs; } } diff --git a/src/Forms/Form.php b/src/Forms/Form.php index 4fb5ae5..664a788 100644 --- a/src/Forms/Form.php +++ b/src/Forms/Form.php @@ -7,8 +7,6 @@ use Iterator; use UnexpectedValueException; -use Fleshgrinder\Core\Formatter; - use PHPForm\Errors\ErrorList; use PHPForm\Exceptions\ValidationError; use PHPForm\Fields\BoundField; diff --git a/src/Renderers/Renderer.php b/src/Renderers/Renderer.php index ed4a1ca..c3aabe1 100644 --- a/src/Renderers/Renderer.php +++ b/src/Renderers/Renderer.php @@ -6,7 +6,6 @@ interface Renderer { - public function __construct(string $fallback_templates_dir, string $templates_dir); - public function getTemplate(string $template_name); + public function __construct(array $templates_dirs); public function render(string $template_name, array $context); } diff --git a/src/Renderers/TwigRenderer.php b/src/Renderers/TwigRenderer.php index 5defe26..68337ac 100644 --- a/src/Renderers/TwigRenderer.php +++ b/src/Renderers/TwigRenderer.php @@ -10,26 +10,45 @@ class TwigRenderer implements Renderer { + /** + * @var Twig_Environment Twig instance. + */ private $twig; - public function __construct(string $fallback_templates_dir, string $templates_dir = null) + /** + * Instantiate twig and chain loaders. The chain loader will start from the + * first element in the array to load templates, traversing the array until + * it finds the template. + * + * @param array $template_dirs + */ + public function __construct(array $templates_dirs) { $loaders = new Twig_Loader_Chain(); - if (!is_null($templates_dir)) { - $loaders->addLoader(new Twig_Loader_Filesystem($templates_dir)); + foreach ($templates_dirs as $template_dir) { + $loaders->addLoader(new Twig_Loader_Filesystem($template_dir)); } - $loaders->addLoader(new Twig_Loader_Filesystem($fallback_templates_dir)); - $this->twig = new Twig_Environment($loaders); } - public function getTemplate(string $template_name) + /** + * @param string $template_name Template path to be loaded. + * + * @return Twig_TemplateWrapper + */ + private function getTemplate(string $template_name) { return $this->twig->load($template_name); } + /** + * @param string $template_name Template path to be loaded. + * @param array $context Data to be injected on template. + * + * @return string + */ public function render(string $template_name, array $context) { $template = $this->getTemplate($template_name); diff --git a/src/TemplatePacks/Bootstrap4TemplatePack.php b/src/TemplatePacks/Bootstrap4TemplatePack.php new file mode 100644 index 0000000..e59459c --- /dev/null +++ b/src/TemplatePacks/Bootstrap4TemplatePack.php @@ -0,0 +1,11 @@ +renderer = new TwigRenderer(__DIR__ . "/templates/pack1", __DIR__ . "/templates/pack2"); - } - - public function testGetTempate() - { - $result = $this->renderer->getTemplate("template.html"); - $this->assertInstanceOf(Twig_TemplateWrapper::class, $result); + $this->renderer = new TwigRenderer([__DIR__ . "/templates/pack2", __DIR__ . "/templates/pack1"]); } public function testRender() @@ -37,7 +31,7 @@ public function testRenderFallback() public function testRenderWithOnlyFallbackDefined() { - $renderer = new TwigRenderer(__DIR__ . "/templates/pack1"); + $renderer = new TwigRenderer([__DIR__ . "/templates/pack1"]); $result = $renderer->render("template.html", array("name" => "test")); $expected = "Pack1 template test"; diff --git a/tests/unit/TemplatePacks/Bootstrap4TemplatePackTest.php b/tests/unit/TemplatePacks/Bootstrap4TemplatePackTest.php new file mode 100644 index 0000000..0f31aa2 --- /dev/null +++ b/tests/unit/TemplatePacks/Bootstrap4TemplatePackTest.php @@ -0,0 +1,15 @@ +assertEquals("bootstrap4", Bootstrap4TemplatePack::NAME); + $this->assertContains("/TemplatePacks/templates/bootstrap4/", Bootstrap4TemplatePack::TEMPLATES_DIR); + } +} diff --git a/tests/unit/TemplatePacks/DefaultTemplatePackTest.php b/tests/unit/TemplatePacks/DefaultTemplatePackTest.php new file mode 100644 index 0000000..be9d6d3 --- /dev/null +++ b/tests/unit/TemplatePacks/DefaultTemplatePackTest.php @@ -0,0 +1,15 @@ +assertEquals("default", DefaultTemplatePack::NAME); + $this->assertContains("/TemplatePacks/templates/default/", DefaultTemplatePack::TEMPLATES_DIR); + } +}