Archived
1
0
Fork 0
This repository has been archived on 2026-04-03. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
httpcb/app/library/ViewHelper/Service.php
2023-04-30 16:57:42 +02:00

77 lines
1.6 KiB
PHP

<?php
namespace Httpcb\ViewHelper;
use Phalcon\Di\DiInterface,
Phalcon\Di\InjectionAwareInterface;
class Service implements InjectionAwareInterface
{
protected $_helpers = array();
protected $_di;
/**
* Sets the dependency injector
*
* @param DiInterface $container
*/
public function setDI(DiInterface $container): void
{
$this->_di = $container;
}
/**
* Returns the internal dependency injector
*
* @return DiInterface
*/
public function getDI(): DiInterface
{
return $this->_di;
}
public function set($name, AbstractHelper $helper)
{
$helper->setDI($this->getDI());
$this->_helpers[$name] = $helper;
}
public function has($name)
{
return $this->_locateHelper($name) !== false;
}
public function get($name)
{
if ($this->has($name)) {
return $this->_helpers[$name];
}
return false;
}
public function __call($name, $args)
{
$helper = $this->get($name);
if ($helper) {
return call_user_func_array(array($helper, $name), $args);
}
return false;
}
protected function _locateHelper($name)
{
if (array_key_exists($name, $this->_helpers)) {
return $this->_helpers[$name];
}
$class = 'Httpcb\\ViewHelper\\' . ucfirst($name);
if (class_exists($class)) {
$helper = new $class();
$this->set($name, $helper);
return $helper;
}
return false;
}
}