22namespace PHPForm ;
33
44use PHPForm \Renderers \TwigRenderer ;
5+ use PHPForm \TemplatePacks \DefaultTemplatePack ;
56
67class 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}
0 commit comments