Skip to content

Commit

Permalink
Migrate to static helper storage
Browse files Browse the repository at this point in the history
  • Loading branch information
Anton Shevchuk authored and Anton Shevchuk committed Dec 26, 2016
1 parent 8990fe3 commit 1ee3f9f
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 92 deletions.
127 changes: 86 additions & 41 deletions src/Common/Helper.php
Expand Up @@ -6,9 +6,8 @@
* @link https://github.com/bluzphp/framework
*/

/**
* @namespace
*/
declare(strict_types=1);

namespace Bluz\Common;

use Bluz\Common\Exception\CommonException;
Expand All @@ -23,54 +22,103 @@
trait Helper
{
/**
* @var array list of helpers
* @var array[] list of helpers
*/
protected $helpers = [];
protected static $helpers = [];

/**
* @var array list of helpers paths
* @var array[] list of helpers paths
*/
protected $helpersPath = [];
protected static $helpersPath = [];

/**
* Add helper path
* Add helper callable
*
* @param string $name
* @param string $path
* @return self
* @return void
* @throws CommonException
*/
public function addHelperPath($path)
private function addHelper(string $name, string $path)
{
$path = rtrim(realpath($path), '/');
if (false !== $path && !in_array($path, $this->helpersPath)) {
$this->helpersPath[] = $path;
$class = static::class;
$path = realpath($path);

if (!$path) {
throw new CommonException("Helper `$name` not found for class `$class`");
}

// create store of helpers
if (!isset(static::$helpers[$class])) {
static::$helpers[$class] = [];
}

return $this;
$helper = include $path;

if (is_callable($helper)) {
static::$helpers[$class][$name] = $helper;
} else {
throw new CommonException("Helper `$name` not found in file `$path`");
}
}

/**
* Set helpers path
* Call helper
*
* @param array $helpersPath
* @return self
* @param string $name
* @param array $arguments
* @return mixed
* @throws CommonException
*/
public function setHelpersPath(array $helpersPath)
private function callHelper(string $name, array $arguments)
{
foreach ($helpersPath as $path) {
$this->addHelperPath((string)$path);
$class = static::class;
if (isset(static::$helpers[$class], static::$helpers[$class][$name])) {
/** @var \Closure $helper */
$helper = static::$helpers[$class][$name];
return $helper->call($this, ...$arguments);
} else {
throw new CommonException("Helper `$name` not registered for class `$class`");
}
}

/**
* Add helper path
*
* @param string $path
* @return void
* @throws CommonException
*/
public function addHelperPath(string $path)
{
$class = static::class;
$path = realpath($path);

if (!$path) {
throw new CommonException("Invalid Helper path for class `$class`");
}

// create store of helpers
if (!isset(static::$helpersPath[$class])) {
static::$helpersPath[$class] = [];
}

if (!in_array($path, static::$helpersPath[$class])) {
static::$helpersPath[$class][] = $path;
}
return $this;
}

/**
* Reset helpers path
* Set helpers path
*
* @return self
* @param array $helpersPath
* @return void
*/
public function resetHelpersPath()
public function setHelpersPath(array $helpersPath)
{
$this->helpersPath = [];
return $this;
foreach ($helpersPath as $path) {
$this->addHelperPath($path);
}
}

/**
Expand All @@ -83,27 +131,24 @@ public function resetHelpersPath()
*/
public function __call($method, $args)
{
// Setup key
$key = static::class .':'. $method;
$class = static::class;

// Call callable helper structure (function or class)
if (isset($this->helpers[$key]) && is_callable($this->helpers[$key])) {
return $this->helpers[$key](...$args);
if (isset(static::$helpers[$class], static::$helpers[$class][$method])) {
return $this->callHelper($method, $args);
}

if (!isset(static::$helpersPath[$class])) {
throw new CommonException("Helper path not found for class `$class`");
}

// Try to find helper file
foreach ($this->helpersPath as $helperPath) {
$helperPath = realpath($helperPath . '/' . ucfirst($method) . '.php');
if ($helperPath) {
$helperInclude = include $helperPath;
if (is_callable($helperInclude)) {
$this->helpers[$key] = $helperInclude;
return $this->helpers[$key](...$args);
} else {
throw new CommonException("Helper '$method' not found in file '$helperPath'");
}
foreach (static::$helpersPath[$class] as $path) {
if (realpath($path . '/' . ucfirst($method) . '.php')) {
$this->addHelper($method, $path . '/' . ucfirst($method) . '.php');
return $this->callHelper($method, $args);
}
}
throw new CommonException("Helper '$method' not found for '" . __CLASS__ . "'");
throw new CommonException("Helper `$method` not found for `$class`");
}
}
32 changes: 0 additions & 32 deletions tests/src/Common/Fixtures/Helper/HelperClass.php

This file was deleted.

19 changes: 0 additions & 19 deletions tests/src/Common/HelperTest.php
Expand Up @@ -75,10 +75,6 @@ public function testAddHelperPath()
$this->class->helper2Function(self::MAGIC_NUMBER),
self::MAGIC_NUMBER
);
self::assertEquals(
$this->class->helperClass(self::MAGIC_NUMBER),
self::MAGIC_NUMBER
);
}

/**
Expand All @@ -96,19 +92,4 @@ public function testSetHelperPaths()
self::MAGIC_NUMBER
);
}

/**
* test Reset Helper Path
* @expectedException \Bluz\Common\Exception\CommonException
*/
public function testResetHelperPath()
{
$this->class->addHelperPath(dirname(__FILE__) .'/Fixtures/Helper');
$this->class->resetHelpersPath();

self::assertEquals(
$this->class->helperFunction(self::MAGIC_NUMBER),
self::MAGIC_NUMBER
);
}
}

0 comments on commit 1ee3f9f

Please sign in to comment.