InvokableComponentVariable.php
TLDR
This file contains the InvokableComponentVariable
class, which implements several interfaces (DeferringDisplayableValue
, IteratorAggregate
, and Stringable
) and provides methods for resolving and using variables.
Classes
InvokableComponentVariable
The InvokableComponentVariable
class is responsible for representing a variable that can be resolved using a callable. It implements the DeferringDisplayableValue
, IteratorAggregate
, and Stringable
interfaces.
__construct(Closure $callable)
This method is the constructor of the class. It initializes the callable
property with the given Closure
parameter.
resolveDisplayableValue()
This method resolves and returns the displayable value of the variable.
getIterator(): Traversable
This method returns an iterator instance for the variable. If the result of the __invoke()
method is an Enumerable
object, it converts it to an array using the all()
method before creating the ArrayIterator
.
__get($key)
This method dynamically proxies attribute access to the variable. It invokes the __invoke()
method and accesses the requested attribute on the result.
__call($method, $parameters)
This method dynamically proxies method access to the variable. It invokes the __invoke()
method and calls the requested method with the given parameters on the result.
__invoke()
This method resolves and returns the variable by calling the callable
property.
__toString()
This method resolves and returns the variable as a string by invoking the __invoke()
method and casting the result to a string.
<?php
namespace Illuminate\View;
use ArrayIterator;
use Closure;
use Illuminate\Contracts\Support\DeferringDisplayableValue;
use Illuminate\Support\Enumerable;
use IteratorAggregate;
use Stringable;
use Traversable;
class InvokableComponentVariable implements DeferringDisplayableValue, IteratorAggregate, Stringable
{
/**
* The callable instance to resolve the variable value.
*
* @var \Closure
*/
protected $callable;
/**
* Create a new variable instance.
*
* @param \Closure $callable
* @return void
*/
public function __construct(Closure $callable)
{
$this->callable = $callable;
}
/**
* Resolve the displayable value that the class is deferring.
*
* @return \Illuminate\Contracts\Support\Htmlable|string
*/
public function resolveDisplayableValue()
{
return $this->__invoke();
}
/**
* Get an iterator instance for the variable.
*
* @return \ArrayIterator
*/
public function getIterator(): Traversable
{
$result = $this->__invoke();
return new ArrayIterator($result instanceof Enumerable ? $result->all() : $result);
}
/**
* Dynamically proxy attribute access to the variable.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->__invoke()->{$key};
}
/**
* Dynamically proxy method access to the variable.
*
* @param string $method
* @param array $parameters
* @return mixed
*/
public function __call($method, $parameters)
{
return $this->__invoke()->{$method}(...$parameters);
}
/**
* Resolve the variable.
*
* @return mixed
*/
public function __invoke()
{
return call_user_func($this->callable);
}
/**
* Resolve the variable as a string.
*
* @return mixed
*/
public function __toString()
{
return (string) $this->__invoke();
}
}