Source of file Helper.php
Size: 3,279 Bytes - Last Modified: 2017-11-08T13:54:24+00:00
/home/travis/build/bluzphp/framework/src/Common/Helper.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 | <?php /** * Bluz Framework Component * * @copyright Bluz PHP Team * @link https://github.com/bluzphp/framework */ declare(strict_types=1); namespace Bluz\Common; use Bluz\Common\Exception\CommonException; /** * Helper trait * * @package Bluz\Common * @author Anton Shevchuk * @link https://github.com/bluzphp/framework/wiki/Trait-Helper */ trait Helper { /** * @var array[] list of helpers */ protected static $helpers = []; /** * @var array[] list of helpers paths */ protected static $helpersPath = []; /** * Add helper path * * @param string $path * * @return void * @throws CommonException */ public function addHelperPath(string $path) { $class = static::class; $realPath = realpath($path); if (false === $realPath) { throw new CommonException("Invalid Helper path `$path` for class `$class`"); } // create store of helpers if (!isset(static::$helpersPath[$class])) { static::$helpersPath[$class] = []; } if (!in_array($realPath, static::$helpersPath[$class])) { static::$helpersPath[$class][] = $realPath; } } /** * Call magic helper * * @param string $method * @param array $arguments * * @return mixed * @throws CommonException */ public function __call($method, $arguments) { $class = static::class; // Call callable helper structure (function or class) if (!isset(static::$helpers[$class], static::$helpers[$class][$method])) { $this->loadHelper($method); } /** @var \Closure $helper */ $helper = static::$helpers[$class][$method]; return $helper->call($this, ...$arguments); } /** * Call helper * * @param string $name * * @return void * @throws CommonException */ private function loadHelper(string $name) { $class = static::class; // Somebody forgot to call `addHelperPath` if (!isset(static::$helpersPath[$class])) { throw new CommonException("Helper path not found for class `$class`"); } // Try to find helper file foreach (static::$helpersPath[$class] as $path) { if ($helperPath = realpath($path . '/' . ucfirst($name) . '.php')) { $this->addHelper($name, $helperPath); return; } } throw new CommonException("Helper `$name` not found for class `$class`"); } /** * Add helper callable * * @param string $name * @param string $path * * @return void * @throws CommonException */ private function addHelper(string $name, string $path) { $class = static::class; // create store of helpers for this class if (!isset(static::$helpers[$class])) { static::$helpers[$class] = []; } $helper = include $path; if (is_callable($helper)) { static::$helpers[$class][$name] = $helper; } else { throw new CommonException("Helper `$name` not found in file `$path`"); } } } |