Helper refactoring
This commit is contained in:
@@ -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->helpers[$name];
|
||||
return $this->getHelper($helper);
|
||||
}
|
||||
|
||||
/**
|
||||
* HTML escaping
|
||||
* Expose helpers with method
|
||||
*
|
||||
* @param string $value Value to escape
|
||||
* @return string
|
||||
* @access public
|
||||
* @param string $helper
|
||||
* @return mixed
|
||||
*/
|
||||
public function e($value)
|
||||
public function getHelper($helper)
|
||||
{
|
||||
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', false);
|
||||
return $this->helpers[$helper];
|
||||
}
|
||||
|
||||
/**
|
||||
* Register a new Helper
|
||||
*
|
||||
* @access public
|
||||
* @param string $property
|
||||
* @param string $className
|
||||
* @return Helper
|
||||
*/
|
||||
public function register($property, $className)
|
||||
{
|
||||
$container = $this->container;
|
||||
|
||||
$this->helpers[$property] = function() use($className, $container) {
|
||||
return new $className($container);
|
||||
};
|
||||
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,13 +3,21 @@
|
||||
namespace Kanboard\Core;
|
||||
|
||||
/**
|
||||
* Template class
|
||||
* Template
|
||||
*
|
||||
* @package core
|
||||
* @author Frederic Guillot
|
||||
*/
|
||||
class Template extends Helper
|
||||
class Template
|
||||
{
|
||||
/**
|
||||
* Helper object
|
||||
*
|
||||
* @access private
|
||||
* @var Helper
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
/**
|
||||
* List of template overrides
|
||||
*
|
||||
@@ -19,47 +27,26 @@ class Template extends Helper
|
||||
private $overrides = array();
|
||||
|
||||
/**
|
||||
* Rendering start time
|
||||
* Template constructor
|
||||
*
|
||||
* @access private
|
||||
* @var float
|
||||
* @access public
|
||||
* @param Helper $helper
|
||||
*/
|
||||
private $startTime = 0;
|
||||
|
||||
/**
|
||||
* Total rendering time
|
||||
*
|
||||
* @access private
|
||||
* @var float
|
||||
*/
|
||||
private $renderingTime = 0;
|
||||
|
||||
/**
|
||||
* Method executed before the rendering
|
||||
*
|
||||
* @access protected
|
||||
* @param string $template
|
||||
*/
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
Reference in New Issue
Block a user