Skip to content
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "newtron/core",
"version": "0.1.2",
"version": "0.1.3",
"type": "library",
"description": "Core framework package for Newtron",
"homepage": "https://github.com/newtron-framework/core",
Expand Down
49 changes: 41 additions & 8 deletions src/Quark/QuarkCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,30 @@ private function registerBuiltinDirectives(): void {
};

$this->directives['include'] = function ($args) {
$template = trim($args, '"\'');
return "\$__quark->skipRootLayout();\necho \$__quark->render('{$template}');\n";
if (preg_match('/^["\']?([^"\'\s]+)["\']?(?:\s*\[(.+)\])?$/', $args, $matches)) {
$template = trim($matches[1], '"\'');
$data = '[]';
if (count($matches) > 2) {
$variables = array_map('trim', explode(',', $matches[2]));

$cleanVars = [];
foreach ($variables as $var) {
$var = ltrim($var, '$');

if (preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $var)) {
$cleanVars[] = $var;
}
}

$quotedVars = array_map(function($var) {
return "'" . $var . "'";
}, $cleanVars);

$data = "compact(" . implode(', ', $quotedVars) . ")";
}
return "echo \$__quark->render('{$template}', {$data});\n";
}
throw new \Exception("Invalid include syntax: {$args}");
};

$this->directives['if'] = fn($args) => "if ({$args}) {\n";
Expand Down Expand Up @@ -215,16 +237,27 @@ private function normalizeVariable(string $expression): string {
return $expression;
}

if (strpos($expression, '.') !== false) {
if (preg_match('/^(\w+)\.(.+)$/', $expression, $matches)) {
return '$' . $matches[1] . '->' . str_replace('.', '->', $matches[2]);
if (strpos($expression, '->') !== false) {
if (preg_match('/^(\w+)->(.+)$/', $expression, $matches)) {
return '$' . $matches[1] . '->' . $matches[2];
}
}

if (strpos($expression, '->') !== false) {
if (preg_match('/^(\w+)->(.+)$/', $expression, $matches)) {
return '$' . $matches[1] . '->' . str_replace('.', '->', $matches[2]);
if (strpos($expression, '.') !== false) {
$parts = explode('.', $expression);
$variable = '$' . array_shift($parts);
foreach ($parts as $part) {
if (preg_match('/^(\w+)\((.*)\)$/', $part, $matches)) {
$method = $matches[1];
$args = $matches[2];
$variable .= '->' . $method . '(' . $args . ')';
} elseif (is_numeric($part)) {
$variable .= '[' . $part . ']';
} else {
$variable = "\$__quark->access({$variable}, '{$part}')";
}
}
return $variable;
}

if (preg_match('/^\w+$/', $expression)) {
Expand Down
33 changes: 33 additions & 0 deletions src/Quark/QuarkEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,39 @@ public function escape(mixed $value, string $context = 'html'): string {
}
}

/**
* Smart accessor for arrays, objects, and ArrayAccess
*
* @param mixed $value The value to access on
* @param string $key The member to access
*/
public function access(mixed $value, string $key): mixed {
if ($value === null) {
return null;
}

if (is_array($value) || $value instanceof \ArrayAccess) {
return $value[$key] ?? null;
}

if (is_object($value)) {
if (property_exists($value, $key)) {
return $value->$key;
}

$getter = 'get' . ucfirst($key);
if (method_exists($value, $getter)) {
return $value->$getter();
}

if (method_exists($value, '__get')) {
return $value->__get($key);
}
}

return null;
}

/**
* Apply a filter to a value
*
Expand Down
12 changes: 12 additions & 0 deletions tests/Mocks/Quark/NestedObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
declare(strict_types=1);

namespace Tests\Mocks\Quark;

class NestedObject {
public string $property = 'Nested Property';

public function method(): string {
return 'Nested Method';
}
}
21 changes: 21 additions & 0 deletions tests/Mocks/Quark/SimpleObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Tests\Mocks\Quark;

class SimpleObject {
public string $property = 'Test Property';
public NestedObject $nestedObject;

public function __construct() {
$this->nestedObject = new NestedObject();
}

public function method(): string {
return 'Test Method';
}

public function getNested(): NestedObject {
return $this->nestedObject;
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Quark/QuarkCompilerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public function testNormalizeVariableDotToProperty(): void {
$method->setAccessible(true);

$this->assertEquals(
'$test->property',
'$__quark->access($test, \'property\')',
$method->invoke($this->compiler, 'test.property')
);
}
Expand Down
46 changes: 46 additions & 0 deletions tests/Unit/Quark/QuarkEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

namespace Tests\Unit\Quark;

use Tests\Mocks\Quark\SimpleObject;
use Tests\QuarkTestCase;

class QuarkEngineTest extends QuarkTestCase {
Expand Down Expand Up @@ -214,6 +215,51 @@ public function testEscapeRaw(): void {
);
}

public function testDotWithArray(): void {
$this->createTestTemplate('test', '<div>{{ test.title }}</div>');

$this->assertEquals(
'<div>Test Value</div>',
$this->engine->render('test', ['test' => ['title' => 'Test Value']])
);
}

public function testDotWithObject(): void {
$this->createTestTemplate('test', '<div>{{ test.property }}</div>');

$this->assertEquals(
'<div>Test Property</div>',
$this->engine->render('test', ['test' => new SimpleObject()])
);
}

public function testDotWithMethod(): void {
$this->createTestTemplate('test', '<div>{{ test.method() }}</div>');

$this->assertEquals(
'<div>Test Method</div>',
$this->engine->render('test', ['test' => new SimpleObject()])
);
}

public function testDotWithMixed(): void {
$this->createTestTemplate('test', '<div>{{ test.nested.method() }}</div>');

$this->assertEquals(
'<div>Nested Method</div>',
$this->engine->render('test', ['test' => new SimpleObject()])
);
}

public function testDotWithNestedMethods(): void {
$this->createTestTemplate('test', '<div>{{ test.getNested().method() }}</div>');

$this->assertEquals(
'<div>Nested Method</div>',
$this->engine->render('test', ['test' => new SimpleObject()])
);
}

public function testFilterUpper(): void {
$this->assertEquals(
'TEST',
Expand Down