|
6 | 6 | class Config extends Singleton |
7 | 7 | { |
8 | 8 | /** |
9 | | - * Template packs available |
| 9 | + * @var string Directory with templates |
10 | 10 | */ |
11 | | - const TEMPLATE_PACKS = array("default", "bootstrap4"); |
| 11 | + protected $templates_dir = __DIR__ . '/templates/'; |
12 | 12 |
|
13 | 13 | /** |
14 | | - * Directory with templates |
15 | | - * @var string |
| 14 | + * @var string Default fallback template pack |
16 | 15 | */ |
17 | | - protected $templates_dir = __DIR__ . '/templates/'; |
| 16 | + protected $fallback_template_pack = "default"; |
18 | 17 |
|
19 | 18 | /** |
20 | | - * Template pack defined |
21 | | - * @var string |
| 19 | + * @var string Default template pack defined. If null, fallback is used. |
22 | 20 | */ |
23 | | - protected $template_pack = "default"; |
| 21 | + protected $template_pack = null; |
24 | 22 |
|
25 | 23 | /** |
26 | | - * Renderer class used to render html content |
27 | | - * @var string |
| 24 | + * @var string Renderer class used to render html content |
28 | 25 | */ |
29 | 26 | protected $renderer_class = TwigRenderer::class; |
30 | 27 |
|
31 | 28 | /** |
32 | | - * Renderer instance based on $renderer_class. |
33 | | - * @var PHPForm\Renderers\Renderer |
| 29 | + * @var PHPForm\Renderers\Renderer Renderer instance based on $renderer_class. |
34 | 30 | */ |
35 | 31 | private $renderer; |
36 | 32 |
|
37 | 33 | /** |
38 | 34 | * Templates path accordingly with defined template pack. |
| 35 | + * |
39 | 36 | * @return string |
40 | 37 | */ |
41 | | - private function getTemplatesPath() |
| 38 | + private function buildPath($template_pack) |
42 | 39 | { |
43 | | - $path = $this->templates_dir . $this->template_pack . "/"; |
| 40 | + $path = $this->templates_dir . $template_pack . "/"; |
44 | 41 |
|
45 | 42 | if (!file_exists($path)) { |
46 | | - trigger_error("Template dir '$path' don't exists.", E_USER_ERROR); |
| 43 | + trigger_error("Template pack dir '$path' don't exists.", E_USER_ERROR); |
47 | 44 | } |
48 | 45 |
|
49 | 46 | return $path; |
50 | 47 | } |
51 | 48 |
|
52 | 49 | /** |
53 | 50 | * Return renderer class instantiated |
| 51 | + * |
54 | 52 | * @return PHPForm\Renderers\Renderer |
55 | 53 | */ |
56 | 54 | public function getRenderer() |
57 | 55 | { |
58 | 56 | if (is_null($this->renderer)) { |
59 | | - $this->renderer = new $this->renderer_class($this->getTemplatesPath()); |
| 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); |
60 | 61 | } |
61 | 62 |
|
62 | 63 | return $this->renderer; |
63 | 64 | } |
64 | 65 |
|
65 | 66 | /** |
66 | 67 | * Define template pack |
| 68 | + * |
67 | 69 | * @param string |
68 | 70 | */ |
69 | 71 | public function setTemplatePack($template_pack) |
70 | 72 | { |
71 | | - if (!in_array($template_pack, self::TEMPLATE_PACKS)) { |
72 | | - $packs = implode(", ", self::TEMPLATE_PACKS); |
73 | | - trigger_error("Template pack '$template_pack' not valid. Available packs are: $packs", E_USER_ERROR); |
74 | | - } |
75 | | - |
76 | 73 | $this->template_pack = $template_pack; |
77 | 74 | } |
78 | 75 | } |
0 commit comments