فهرست منبع

Fix error when a custom template is not in the default available templates list

Ribas160 1 سال پیش
والد
کامیت
44f8cfbfb8
4فایلهای تغییر یافته به همراه67 افزوده شده و 9 حذف شده
  1. 14 2
      lib/TemplateSwitcher.php
  2. 26 2
      lib/View.php
  3. 11 5
      tst/TemplateSwitcherTest.php
  4. 16 0
      tst/ViewTest.php

+ 14 - 2
lib/TemplateSwitcher.php

@@ -25,7 +25,7 @@ class TemplateSwitcher
      * @static
      * @var    string
      */
-    protected static $_templateFallback;
+    protected static $_templateFallback = "bootstrap";
 
     /**
      * available templates
@@ -59,6 +59,11 @@ class TemplateSwitcher
     {
         if (self::isTemplateAvailable($template)) {
             self::$_templateFallback = $template;
+
+            if (!in_array($template, self::getAvailableTemplates())) {
+                // Add custom template to the available templates list
+                self::$_availableTemplates[] = $template;
+            }
         }
     }
 
@@ -96,7 +101,14 @@ class TemplateSwitcher
      */
     public static function isTemplateAvailable(string $template): bool
     {
-        return in_array($template, self::getAvailableTemplates());
+        $available = in_array($template, self::getAvailableTemplates());
+
+        if (!$available && !View::isBootstrapTemplate($template)) {
+            $path      = View::getTemplateFilePath($template);
+            $available = file_exists($path);
+        }
+
+        return $available;
     }
 
     /**

+ 26 - 2
lib/View.php

@@ -49,8 +49,7 @@ class View
      */
     public function draw($template)
     {
-        $file = substr($template, 0, 10) === 'bootstrap-' ? 'bootstrap' : $template;
-        $path = PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php';
+        $path = self::getTemplateFilePath($template);
         if (!file_exists($path)) {
             throw new Exception('Template ' . $template . ' not found!', 80);
         }
@@ -58,6 +57,31 @@ class View
         include $path;
     }
 
+    /**
+     * Get template file path
+     *
+     * @access public
+     * @param  string $template
+     * @return string
+     */
+    public static function getTemplateFilePath(string $template): string
+    {
+        $file = self::isBootstrapTemplate($template) ? 'bootstrap' : $template;
+        return PATH . 'tpl' . DIRECTORY_SEPARATOR . $file . '.php';
+    }
+
+    /**
+     * Is the template a variation of the bootstrap template
+     *
+     * @access public
+     * @param  string $template
+     * @return bool
+     */
+    public static function isBootstrapTemplate(string $template): bool
+    {
+        return substr($template, 0, 10) === 'bootstrap-';
+    }
+
     /**
      * echo script tag incl. SRI hash for given script file
      *

+ 11 - 5
tst/TemplateSwitcherTest.php

@@ -10,15 +10,21 @@ class TemplateSwitcherTest extends TestCase
     {
         $conf = new Configuration;
 
-        $existingTemplateFallback = 'bootstrap-dark';
-        $wrongTemplateFallback    = 'bootstrap-wrong';
+        $defaultTemplateFallback        = 'bootstrap';
+        $existingTemplateFallback       = 'bootstrap-dark';
+        $wrongBootstrapTemplateFallback = 'bootstrap-wrong';
+        $wrongTemplateFallback          = 'wrong-template';
 
         TemplateSwitcher::setAvailableTemplates($conf->getKey('availabletemplates'));
-        TemplateSwitcher::setTemplateFallback($existingTemplateFallback);
-        $this->assertEquals($existingTemplateFallback, TemplateSwitcher::getTemplate(), 'Correct template fallback');
+
+        TemplateSwitcher::setTemplateFallback($wrongBootstrapTemplateFallback);
+        $this->assertEquals($defaultTemplateFallback, TemplateSwitcher::getTemplate(), 'Wrong bootstrap template fallback');
 
         TemplateSwitcher::setTemplateFallback($wrongTemplateFallback);
-        $this->assertEquals($existingTemplateFallback, TemplateSwitcher::getTemplate(), 'Wrong template fallback');
+        $this->assertEquals($defaultTemplateFallback, TemplateSwitcher::getTemplate(), 'Wrong template fallback');
+
+        TemplateSwitcher::setTemplateFallback($existingTemplateFallback);
+        $this->assertEquals($existingTemplateFallback, TemplateSwitcher::getTemplate(), 'Correct template fallback');
     }
 
     public function testSetAvailableTemplates()

+ 16 - 0
tst/ViewTest.php

@@ -142,4 +142,20 @@ class ViewTest extends TestCase
         $this->expectExceptionCode(80);
         $test->draw('123456789 does not exist!');
     }
+
+    public function testTemplateFilePath()
+    {
+        $template     = 'bootstrap';
+        $templatePath = PATH . 'tpl' . DIRECTORY_SEPARATOR . $template . '.php';
+        $path         = View::getTemplateFilePath($template);
+        $this->assertEquals($templatePath, $path, 'Template file path');
+    }
+
+    public function testIsBootstrapTemplate()
+    {
+        $bootstrapTemplate    = 'bootstrap-dark';
+        $nonBootstrapTemplate = 'page';
+        $this->assertTrue(View::isBootstrapTemplate($bootstrapTemplate), 'Is bootstrap template');
+        $this->assertFalse(View::isBootstrapTemplate($nonBootstrapTemplate), 'Is not bootstrap template');
+    }
 }