From 8f3e2b2e5c62a6130f6c8867ab335fb4c1a32c5c Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Fri, 4 Mar 2016 20:10:34 -0500 Subject: Helper refactoring --- app/Core/Helper.php | 74 +++++++++++++++++++++++++++++++-------------------- app/Core/Template.php | 59 ++++++++++++++-------------------------- 2 files changed, 65 insertions(+), 68 deletions(-) (limited to 'app/Core') diff --git a/app/Core/Helper.php b/app/Core/Helper.php index 759209f6..3764a67c 100644 --- a/app/Core/Helper.php +++ b/app/Core/Helper.php @@ -10,18 +10,18 @@ use Pimple\Container; * @package core * @author Frederic Guillot * - * @property \Kanboard\Helper\App $app - * @property \Kanboard\Helper\Asset $asset - * @property \Kanboard\Helper\Dt $dt - * @property \Kanboard\Helper\File $file - * @property \Kanboard\Helper\Form $form - * @property \Kanboard\Helper\Subtask $subtask - * @property \Kanboard\Helper\Task $task - * @property \Kanboard\Helper\Text $text - * @property \Kanboard\Helper\Url $url - * @property \Kanboard\Helper\User $user - * @property \Kanboard\Helper\Layout $layout - * @property \Kanboard\Helper\Model $model + * @property \Kanboard\Helper\AppHelper $app + * @property \Kanboard\Helper\AssetHelper $asset + * @property \Kanboard\Helper\DateHelper $dt + * @property \Kanboard\Helper\FileHelper $file + * @property \Kanboard\Helper\FormHelper $form + * @property \Kanboard\Helper\ModelHelper $model + * @property \Kanboard\Helper\SubtaskHelper $subtask + * @property \Kanboard\Helper\TaskHelper $task + * @property \Kanboard\Helper\TextHelper $text + * @property \Kanboard\Helper\UrlHelper $url + * @property \Kanboard\Helper\UserHelper $user + * @property \Kanboard\Helper\LayoutHelper $layout */ class Helper { @@ -29,17 +29,17 @@ class Helper * Helper instances * * @access private - * @var array + * @var \Pimple\Container */ - private $helpers = array(); + private $helpers; /** * Container instance * - * @access protected + * @access private * @var \Pimple\Container */ - protected $container; + private $container; /** * Constructor @@ -50,33 +50,49 @@ class Helper public function __construct(Container $container) { $this->container = $container; + $this->helpers = new Container; } /** - * Load automatically helpers + * Expose helpers with magic getter * * @access public - * @param string $name Helper name + * @param string $helper * @return mixed */ - public function __get($name) + public function __get($helper) { - if (! isset($this->helpers[$name])) { - $class = '\Kanboard\Helper\\'.ucfirst($name); - $this->helpers[$name] = new $class($this->container); - } + return $this->getHelper($helper); + } - return $this->helpers[$name]; + /** + * Expose helpers with method + * + * @access public + * @param string $helper + * @return mixed + */ + public function getHelper($helper) + { + return $this->helpers[$helper]; } /** - * HTML escaping + * Register a new Helper * - * @param string $value Value to escape - * @return string + * @access public + * @param string $property + * @param string $className + * @return Helper */ - public function e($value) + public function register($property, $className) { - return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false); + $container = $this->container; + + $this->helpers[$property] = function() use($className, $container) { + return new $className($container); + }; + + return $this; } } diff --git a/app/Core/Template.php b/app/Core/Template.php index 8ded6f7c..fa7b65ec 100644 --- a/app/Core/Template.php +++ b/app/Core/Template.php @@ -3,63 +3,50 @@ namespace Kanboard\Core; /** - * Template class + * Template * * @package core * @author Frederic Guillot */ -class Template extends Helper +class Template { /** - * List of template overrides + * Helper object * * @access private - * @var array + * @var Helper */ - private $overrides = array(); + private $helper; /** - * Rendering start time - * - * @access private - * @var float - */ - private $startTime = 0; - - /** - * Total rendering time + * List of template overrides * * @access private - * @var float + * @var array */ - private $renderingTime = 0; + private $overrides = array(); /** - * Method executed before the rendering + * Template constructor * - * @access protected - * @param string $template + * @access public + * @param Helper $helper */ - protected function beforeRender($template) + public function __construct(Helper $helper) { - if (DEBUG) { - $this->startTime = microtime(true); - } + $this->helper = $helper; } /** - * Method executed after the rendering + * Expose helpers with magic getter * - * @access protected - * @param string $template + * @access public + * @param string $helper + * @return mixed */ - protected function afterRender($template) + public function __get($helper) { - if (DEBUG) { - $duration = microtime(true) - $this->startTime; - $this->renderingTime += $duration; - $this->container['logger']->debug('Rendering '.$template.' in '.$duration.'s, total='.$this->renderingTime); - } + return $this->helper->getHelper($helper); } /** @@ -76,16 +63,10 @@ class Template extends Helper */ public function render($__template_name, array $__template_args = array()) { - $this->beforeRender($__template_name); - extract($__template_args); ob_start(); include $this->getTemplateFile($__template_name); - $html = ob_get_clean(); - - $this->afterRender($__template_name); - - return $html; + return ob_get_clean(); } /** -- cgit v1.2.3