Upgrade to 4.0.0

This commit is contained in:
Bastian Allgeier
2023-11-28 09:33:56 +01:00
parent f96b96af76
commit 3b0b6546ca
480 changed files with 21371 additions and 13327 deletions

View File

@@ -27,80 +27,44 @@ class Component
{
/**
* Registry for all component mixins
*
* @var array
*/
public static $mixins = [];
public static array $mixins = [];
/**
* Registry for all component types
*
* @var array
*/
public static $types = [];
public static array $types = [];
/**
* An array of all passed attributes
*
* @var array
*/
protected $attrs = [];
protected array $attrs = [];
/**
* An array of all computed properties
*
* @var array
*/
protected $computed = [];
protected array $computed = [];
/**
* An array of all registered methods
*
* @var array
*/
protected $methods = [];
protected array $methods = [];
/**
* An array of all component options
* from the component definition
*
* @var array
*/
protected $options = [];
protected array|string $options = [];
/**
* An array of all resolved props
*
* @var array
*/
protected $props = [];
protected array $props = [];
/**
* The component type
*
* @var string
*/
protected $type;
/**
* Magic caller for defined methods and properties
*/
public function __call(string $name, array $arguments = [])
{
if (array_key_exists($name, $this->computed) === true) {
return $this->computed[$name];
}
if (array_key_exists($name, $this->props) === true) {
return $this->props[$name];
}
if (array_key_exists($name, $this->methods) === true) {
return $this->methods[$name]->call($this, ...$arguments);
}
return $this->$name;
}
protected string $type;
/**
* Creates a new component for the given type
@@ -133,8 +97,29 @@ class Component
$this->type = $type;
}
/**
* Magic caller for defined methods and properties
*/
public function __call(string $name, array $arguments = [])
{
if (array_key_exists($name, $this->computed) === true) {
return $this->computed[$name];
}
if (array_key_exists($name, $this->props) === true) {
return $this->props[$name];
}
if (array_key_exists($name, $this->methods) === true) {
return $this->methods[$name]->call($this, ...$arguments);
}
return $this->$name;
}
/**
* Improved `var_dump` output
* @codeCoverageIgnore
*/
public function __debugInfo(): array
{
@@ -167,24 +152,29 @@ class Component
*/
protected function applyProps(array $props): void
{
foreach ($props as $propName => $propFunction) {
if ($propFunction instanceof Closure) {
if (isset($this->attrs[$propName]) === true) {
foreach ($props as $name => $function) {
if ($function instanceof Closure) {
if (isset($this->attrs[$name]) === true) {
try {
$this->$propName = $this->props[$propName] = $propFunction->call($this, $this->attrs[$propName]);
$this->$name = $this->props[$name] = $function->call(
$this,
$this->attrs[$name]
);
continue;
} catch (TypeError) {
throw new TypeError('Invalid value for "' . $propName . '"');
}
} else {
try {
$this->$propName = $this->props[$propName] = $propFunction->call($this);
} catch (ArgumentCountError) {
throw new ArgumentCountError('Please provide a value for "' . $propName . '"');
throw new TypeError('Invalid value for "' . $name . '"');
}
}
} else {
$this->$propName = $this->props[$propName] = $propFunction;
try {
$this->$name = $this->props[$name] = $function->call($this);
continue;
} catch (ArgumentCountError) {
throw new ArgumentCountError('Please provide a value for "' . $name . '"');
}
}
$this->$name = $this->props[$name] = $function;
}
}
@@ -194,9 +184,9 @@ class Component
*/
protected function applyComputed(array $computed): void
{
foreach ($computed as $computedName => $computedFunction) {
if ($computedFunction instanceof Closure) {
$this->$computedName = $this->computed[$computedName] = $computedFunction->call($this);
foreach ($computed as $name => $function) {
if ($function instanceof Closure) {
$this->$name = $this->computed[$name] = $function->call($this);
}
}
}
@@ -214,7 +204,10 @@ class Component
throw new Exception('Component definition ' . $definition . ' does not exist');
}
static::$types[$type] = $definition = F::load($definition, allowOutput: false);
static::$types[$type] = $definition = F::load(
$definition,
allowOutput: false
);
}
return $definition;
@@ -244,20 +237,21 @@ class Component
}
// inject mixins
if (isset($options['mixins']) === true) {
foreach ($options['mixins'] as $mixin) {
if (isset(static::$mixins[$mixin]) === true) {
if (is_string(static::$mixins[$mixin]) === true) {
// resolve a path to a mixin on demand
foreach ($options['mixins'] ?? [] as $mixin) {
if (isset(static::$mixins[$mixin]) === true) {
if (is_string(static::$mixins[$mixin]) === true) {
// resolve a path to a mixin on demand
static::$mixins[$mixin] = F::load(static::$mixins[$mixin], allowOutput: false);
}
$options = array_replace_recursive(
static::$mixins[$mixin] = F::load(
static::$mixins[$mixin],
$options
allowOutput: false
);
}
$options = array_replace_recursive(
static::$mixins[$mixin],
$options
);
}
}
@@ -269,8 +263,10 @@ class Component
*/
public function toArray(): array
{
if (($this->options['toArray'] ?? null) instanceof Closure) {
return $this->options['toArray']->call($this);
$closure = $this->options['toArray'] ?? null;
if ($closure instanceof Closure) {
return $closure->call($this);
}
$array = array_merge($this->attrs, $this->props, $this->computed);