Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closes #388 [2] #401

Merged
merged 2 commits into from
Jul 15, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Node/GetAttrNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/
namespace TwigBridge\Node;

use ArrayAccess;
use Twig\Compiler;
use Twig\Environment;
use Twig\Error\RuntimeError;
Expand Down Expand Up @@ -129,7 +130,12 @@ public static function attribute(
$sandboxed = false,
int $lineno = -1
) {
if (Template::METHOD_CALL !== $type and is_a($object, 'Illuminate\Database\Eloquent\Model')) {
// Twig doesn't support sandboxing on objects that implement ArrayAccess
// https://github.com/twigphp/Twig/issues/106#issuecomment-583737
// https://github.com/twigphp/Twig/pull/1863
//
// https://github.com/twigphp/Twig/issues/2878
if (Template::METHOD_CALL !== $type and $object instanceof ArrayAccess) {
// We can't easily find out if an attribute actually exists, so return true
if ($isDefinedTest) {
return true;
Expand All @@ -139,8 +145,8 @@ public static function attribute(
$env->getExtension(SandboxExtension::class)->checkPropertyAllowed($object, $item);
}

// Call the attribute, the Model object does the rest of the magic
return $object->$item;
// Call the attribute, the object does the rest of the magic
return isset($object[$item]) ? $object[$item] : null;
}

return \twig_get_attribute(
Expand Down