Skip to content

Commit

Permalink
automatically resolve parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
david-d-h committed Apr 20, 2024
1 parent 0ba2a55 commit 64944b5
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/CachedComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,19 @@ abstract class CachedComponent extends Component
{
use CachesValue;

protected function registerParameters(array $args): void
{
$parameters = (new \ReflectionClass(static::class))->getConstructor()->getParameters();
$this->parameters = collect($args)->mapWithKeys(fn ($v, $k) => [$parameters[$k]->name => $v])->toArray();
}

/** {@inheritdoc} */
public function resolveView()
{
$this->parameters = collect((new \ReflectionClass(static::class))
->getProperties(\ReflectionProperty::IS_PUBLIC))
->filter(fn (\ReflectionProperty $p) => $p->class === static::class)
->mapWithKeys(fn (\ReflectionProperty $p) => [$p->name => $p->getValue($this)])
->toArray();

if ($this->isUpdating()) {
return $this->get($this->parameters) ?? parent::resolveView();
$value = $this->get($this->parameters);

return is_null($value) ? parent::resolveView() : new HtmlString($value);
}

if (null !== $cache = $this->get($this->parameters, update: true)) {
Expand Down

0 comments on commit 64944b5

Please sign in to comment.