Skip to content

Commit f498c3c

Browse files
committed
Added TemplatePack classing.
1 parent 1bf3b41 commit f498c3c

36 files changed

+126
-55
lines changed

src/Config.php

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,20 @@
22
namespace PHPForm;
33

44
use PHPForm\Renderers\TwigRenderer;
5+
use PHPForm\TemplatePacks\DefaultTemplatePack;
56

67
class Config extends Singleton
78
{
89
/**
9-
* @var string Directory with templates
10+
* @var array Template packs to be used. The templates will be loaded
11+
* accordingly to the order defined.
1012
*/
11-
protected $templates_dir = __DIR__ . '/templates/';
13+
protected $template_packs = array(
14+
DefaultTemplatePack::class,
15+
);
1216

1317
/**
14-
* @var string Default fallback template pack
15-
*/
16-
protected $fallback_template_pack = "default";
17-
18-
/**
19-
* @var string Default template pack defined. If null, fallback is used.
20-
*/
21-
protected $template_pack = null;
22-
23-
/**
24-
* @var string Renderer class used to render html content
18+
* @var string Renderer class used to render html content.
2519
*/
2620
protected $renderer_class = TwigRenderer::class;
2721

@@ -31,45 +25,52 @@ class Config extends Singleton
3125
private $renderer;
3226

3327
/**
34-
* Templates path accordingly with defined template pack.
28+
* Return renderer class instantiated
3529
*
36-
* @return string
30+
* @return PHPForm\Renderers\Renderer
3731
*/
38-
private function buildPath($template_pack)
32+
public function getRenderer()
3933
{
40-
$path = $this->templates_dir . $template_pack . "/";
41-
42-
if (!file_exists($path)) {
43-
trigger_error("Template pack dir '$path' don't exists.", E_USER_ERROR);
34+
if (is_null($this->renderer)) {
35+
$this->renderer = new $this->renderer_class($this->getTemplatesDirs());
4436
}
4537

46-
return $path;
38+
return $this->renderer;
4739
}
4840

4941
/**
50-
* Return renderer class instantiated
42+
* Set renderer class.
5143
*
52-
* @return PHPForm\Renderers\Renderer
44+
* @param string Class name of Renderer.
5345
*/
54-
public function getRenderer()
46+
public function setRenderer(string $renderer_class)
5547
{
56-
if (is_null($this->renderer)) {
57-
$fallback_pack_dir = $this->buildPath($this->fallback_template_pack);
58-
$pack_dir = !is_null($this->template_pack) ? $this->buildPath($this->template_pack) : null;
59-
60-
$this->renderer = new $this->renderer_class($fallback_pack_dir, $pack_dir);
61-
}
48+
$this->renderer_class = $renderer_class;
49+
}
6250

63-
return $this->renderer;
51+
/**
52+
* Set template pack on top level.
53+
*
54+
* @param string Class name of TemplatePack.
55+
*/
56+
public function setTemplatePack(string $template_pack)
57+
{
58+
$this->template_packs = array_unshift($this->template_packs, $template_pack);
6459
}
6560

6661
/**
67-
* Define template pack
62+
* Traverse all packs to extract template dir path.
6863
*
69-
* @param string
64+
* @return array
7065
*/
71-
public function setTemplatePack($template_pack)
66+
private function getTemplatesDirs()
7267
{
73-
$this->template_pack = $template_pack;
68+
$dirs = array();
69+
70+
foreach ($this->template_packs as $template_pack) {
71+
$dirs[] = $template_pack::TEMPLATES_DIR;
72+
}
73+
74+
return $dirs;
7475
}
7576
}

src/Forms/Form.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77
use Iterator;
88
use UnexpectedValueException;
99

10-
use Fleshgrinder\Core\Formatter;
11-
1210
use PHPForm\Errors\ErrorList;
1311
use PHPForm\Exceptions\ValidationError;
1412
use PHPForm\Fields\BoundField;

src/Renderers/Renderer.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
interface Renderer
88
{
9-
public function __construct(string $fallback_templates_dir, string $templates_dir);
10-
public function getTemplate(string $template_name);
9+
public function __construct(array $templates_dirs);
1110
public function render(string $template_name, array $context);
1211
}

src/Renderers/TwigRenderer.php

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,26 +10,45 @@
1010

1111
class TwigRenderer implements Renderer
1212
{
13+
/**
14+
* @var Twig_Environment Twig instance.
15+
*/
1316
private $twig;
1417

15-
public function __construct(string $fallback_templates_dir, string $templates_dir = null)
18+
/**
19+
* Instantiate twig and chain loaders. The chain loader will start from the
20+
* first element in the array to load templates, traversing the array until
21+
* it finds the template.
22+
*
23+
* @param array $template_dirs
24+
*/
25+
public function __construct(array $templates_dirs)
1626
{
1727
$loaders = new Twig_Loader_Chain();
1828

19-
if (!is_null($templates_dir)) {
20-
$loaders->addLoader(new Twig_Loader_Filesystem($templates_dir));
29+
foreach ($templates_dirs as $template_dir) {
30+
$loaders->addLoader(new Twig_Loader_Filesystem($template_dir));
2131
}
2232

23-
$loaders->addLoader(new Twig_Loader_Filesystem($fallback_templates_dir));
24-
2533
$this->twig = new Twig_Environment($loaders);
2634
}
2735

28-
public function getTemplate(string $template_name)
36+
/**
37+
* @param string $template_name Template path to be loaded.
38+
*
39+
* @return Twig_TemplateWrapper
40+
*/
41+
private function getTemplate(string $template_name)
2942
{
3043
return $this->twig->load($template_name);
3144
}
3245

46+
/**
47+
* @param string $template_name Template path to be loaded.
48+
* @param array $context Data to be injected on template.
49+
*
50+
* @return string
51+
*/
3352
public function render(string $template_name, array $context)
3453
{
3554
$template = $this->getTemplate($template_name);
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
/**
3+
* Bootstrap v4.0.0-beta.2 template pack
4+
*/
5+
namespace PHPForm\TemplatePacks;
6+
7+
class Bootstrap4TemplatePack extends TemplatePack
8+
{
9+
const NAME = "bootstrap4";
10+
const TEMPLATES_DIR = __DIR__ . '/templates/bootstrap4/';
11+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace PHPForm\TemplatePacks;
3+
4+
/**
5+
* Default template pack
6+
*/
7+
class DefaultTemplatePack extends TemplatePack
8+
{
9+
const TEMPLATES_DIR = __DIR__ . '/templates/default/';
10+
}

src/TemplatePacks/TemplatePack.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
namespace PHPForm\TemplatePacks;
3+
4+
/**
5+
* Base class to define template packs
6+
*/
7+
abstract class TemplatePack
8+
{
9+
const NAME = 'default';
10+
const TEMPLATES_DIR = null;
11+
}

0 commit comments

Comments
 (0)