Skip to content

Commit

Permalink
Bring back the reflection stuff - that'll happen for tag pairs.
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonvarga committed Dec 5, 2024
1 parent 752f419 commit 5f1f6a2
Showing 1 changed file with 39 additions and 3 deletions.
42 changes: 39 additions & 3 deletions src/View/Antlers/Language/Runtime/PathDataManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Exception;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
Expand Down Expand Up @@ -642,10 +643,12 @@ public function getData(VariableReference $path, $data, $isForArrayIndex = false
}
}

if (count($path->pathParts) > 1 && $this->isPair == false) {
if (count($path->pathParts) > 1 && $this->isPair == false && ! $this->reducedVar instanceof Model) {
// If we have more steps in the path to take, but we are
// not a tag pair, we need to reduce anyway so we
// can descend further into the nested values.
// We skip this step for Models to prevent
// some of the reflection stuff below.
$this->lockData();
$this->reducedVar = self::reduce($this->reducedVar, true, $this->shouldDoValueIntercept);
$this->unlockData();
Expand Down Expand Up @@ -779,6 +782,10 @@ public function getData(VariableReference $path, $data, $isForArrayIndex = false
$this->compact(true);
}

if ($this->reducedVar instanceof Model && $this->isPair) {
$this->reducedVar = self::reduce($this->reducedVar, true, true, false);
}

$this->namedSlotsInScope = false;
$this->resetInternalState();

Expand Down Expand Up @@ -914,6 +921,10 @@ private function reduceVar($path, $processorData = [])
*/
private function compact($isFinal)
{
if (! $isFinal && $this->reducedVar instanceof Model) {
return;
}

if ($this->isForArrayIndex && $isFinal && is_object($this->reducedVar) && method_exists($this->reducedVar, '__toString')) {
$this->reducedVar = (string) $this->reducedVar;

Expand Down Expand Up @@ -956,14 +967,15 @@ protected static function guardRuntimeReturnValue($value)
* @param mixed $value The value to reduce.
* @param bool $isPair Indicates if the path belongs to a node pair.
* @param bool $reduceBuildersAndAugmentables Indicates if Builder and Augmentable instances should be resolved.
* @param bool $leaveModelsAlone
* @return array|string
*/
public static function reduce($value, $isPair = true, $reduceBuildersAndAugmentables = true)
public static function reduce($value, $isPair = true, $reduceBuildersAndAugmentables = true, $leaveModelsAlone = true)
{
$reductionStack = [$value];
$returnValue = $value;

if ($value instanceof Model) {
if ($value instanceof Model && $leaveModelsAlone) {
return $value;
}

Expand Down Expand Up @@ -1023,6 +1035,26 @@ public static function reduce($value, $isPair = true, $reduceBuildersAndAugmenta
$reductionStack[] = $reductionValue->all();
GlobalRuntimeState::$isEvaluatingData = false;

continue;
} elseif ($reductionValue instanceof Model) {
GlobalRuntimeState::$isEvaluatingData = true;
$data = $reductionValue->toArray();

foreach (get_class_methods($reductionValue) as $method) {
if ((new \ReflectionMethod($reductionValue, $method))->getReturnType()?->getName() === Attribute::class) {
$method = Str::snake($method);
$data[$method] = $reductionValue->$method;
}

if (Str::startsWith($method, 'get') && Str::endsWith($method, 'Attribute')) {
$method = Str::of($method)->after('get')->before('Attribute')->snake()->__toString();
$data[$method] = $reductionValue->getAttribute($method);
}
}

$reductionStack[] = $data;
GlobalRuntimeState::$isEvaluatingData = false;

continue;
} elseif ($reductionValue instanceof Arrayable) {
GlobalRuntimeState::$isEvaluatingData = true;
Expand Down Expand Up @@ -1058,6 +1090,10 @@ public static function reduceForAntlers($value, Parser $parser, $data, $isPair =
GlobalRuntimeState::$isEvaluatingUserData = true;
GlobalRuntimeState::$isEvaluatingData = true;

if ($value instanceof Model) {
return $value;
}

if ($value instanceof Collection) {
$value = $value->all();
}
Expand Down

0 comments on commit 5f1f6a2

Please sign in to comment.