Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 36 additions & 35 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
}
}
2 changes: 0 additions & 2 deletions src/Forms/Form.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
use Iterator;
use UnexpectedValueException;

use Fleshgrinder\Core\Formatter;

use PHPForm\Errors\ErrorList;
use PHPForm\Exceptions\ValidationError;
use PHPForm\Fields\BoundField;
Expand Down
3 changes: 1 addition & 2 deletions src/Renderers/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
31 changes: 25 additions & 6 deletions src/Renderers/TwigRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 11 additions & 0 deletions src/TemplatePacks/Bootstrap4TemplatePack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
/**
* Bootstrap v4.0.0-beta.2 template pack
*/
namespace PHPForm\TemplatePacks;

class Bootstrap4TemplatePack extends TemplatePack
{
const NAME = "bootstrap4";
const TEMPLATES_DIR = __DIR__ . '/templates/bootstrap4/';
}
10 changes: 10 additions & 0 deletions src/TemplatePacks/DefaultTemplatePack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php
namespace PHPForm\TemplatePacks;

/**
* Default template pack
*/
class DefaultTemplatePack extends TemplatePack
{
const TEMPLATES_DIR = __DIR__ . '/templates/default/';
}
11 changes: 11 additions & 0 deletions src/TemplatePacks/TemplatePack.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace PHPForm\TemplatePacks;

/**
* Base class to define template packs
*/
abstract class TemplatePack
{
const NAME = 'default';
const TEMPLATES_DIR = null;
}
2 changes: 0 additions & 2 deletions src/Widgets/ChoiceWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
*/
namespace PHPForm\Widgets;

use PHPForm\Utils\Attributes;

abstract class ChoiceWidget extends Widget
{
const TEMPLATE_CHOICE = '';
Expand Down
10 changes: 2 additions & 8 deletions tests/unit/Renderers/TwigRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,7 @@ class TwigRendererTest extends TestCase
{
public function setUp()
{
$this->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()
Expand All @@ -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";
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/TemplatePacks/Bootstrap4TemplatePackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace PHPForm\Unit\TemplatePacks;

use PHPUnit\Framework\TestCase;

use PHPForm\TemplatePacks\Bootstrap4TemplatePack;

class Bootstrap4TemplatePackTest extends TestCase
{
public function testConstants()
{
$this->assertEquals("bootstrap4", Bootstrap4TemplatePack::NAME);
$this->assertContains("/TemplatePacks/templates/bootstrap4/", Bootstrap4TemplatePack::TEMPLATES_DIR);
}
}
15 changes: 15 additions & 0 deletions tests/unit/TemplatePacks/DefaultTemplatePackTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
namespace PHPForm\Unit\TemplatePacks;

use PHPUnit\Framework\TestCase;

use PHPForm\TemplatePacks\DefaultTemplatePack;

class DefaultTemplatePackTest extends TestCase
{
public function testConstants()
{
$this->assertEquals("default", DefaultTemplatePack::NAME);
$this->assertContains("/TemplatePacks/templates/default/", DefaultTemplatePack::TEMPLATES_DIR);
}
}