Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Downgrade class php 5.6 to 5.5

I founded a class to stack object layers and closures but my server isnt running on php 5.6 yet. And i was wondering how can i convert the ...$parameters because i cant fix it by replacing everything with call_user_func_array() then the buildCoreClosure() method will throw errors for example because a closure isnt an array...

class Stack
{
    /**
     * Method to call on the decoracted class.
     *
     * @var string
     */

    protected $method;

    /**
     * Container.
     */

    protected $container;

    /**
     * Middleware layers.
     *
     * @var array
     */

    protected $layers = [];

    public function __construct(Container $container = null, $method = null)
    {
        $this->container = $container ?: new Container;
        $this->method = $method ?: 'handle';
    }

    public function addLayer($class, $inner = true)
    {
        return $inner ? array_unshift($this->layers, $class) : array_push($this->layers, $class);
    }

    public function addInnerLayer($class)
    {
        return $this->addLayer($class);
    }

    public function addOuterLayer($class)
    {
        return $this->addLayer($class, false);
    }

    protected function buildCoreClosure($object)
    {
        return function(...$arguments) use ($object)
        {
            $callable = $object instanceof Closure ? $object : [$object, $this->method];

            return $callable(...$arguments);
        };
    }

    protected function buildLayerClosure($layer, Closure $next)
    {
        return function(...$arguments) use ($layer, $next)
        {
            return $layer->execute(...array_merge($arguments, [$next]));
        };
    }


    public function peel($object, array $parameters = [])
    {
        $next = $this->buildCoreClosure($object);

        foreach($this->layers as $layer)
        {
            $layer = $this->container->get($layer);

            $next = $this->buildLayerClosure($layer, $next);
        }

        return $next(...$parameters);
    }
}
like image 545
M Beers Avatar asked Jul 01 '15 21:07

M Beers


1 Answers

By removing

...$arguments

in the arguments list of each function and replacing it with the following (early inside the function)

$arguments = func_get_args();

You can achieve the same value of arguments. Arguments will still be passed to func_get_args() even when not defined in the function's argument list.

like image 65
ThePengwin Avatar answered Sep 23 '22 11:09

ThePengwin