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

Add namespace loader #320

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 4 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
parameters:
excludePaths:
analyse:
- src/Latte/Compiler/TagParser.php

level: 5

paths:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Latte\Compiler\PrintContext;


class InRangeNode extends ExpressionNode
class InNode extends ExpressionNode
{
public function __construct(
public ExpressionNode $needle,
Expand All @@ -26,11 +26,11 @@ public function __construct(

public function print(PrintContext $context): string
{
return 'in_array('
return 'LR\Filters::contains('
. $this->needle->print($context)
. ', '
. $this->haystack->print($context)
. ', true)';
. ')';
}


Expand Down
1 change: 1 addition & 0 deletions src/Latte/Compiler/TagLexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ final class TagLexer
'null' => Token::Php_Null,
'true' => Token::Php_True,
'false' => Token::Php_False,
'not' => Token::Php_Not,
];

private const KeywordsFollowed = [ // must follows ( & =
Expand Down
511 changes: 256 additions & 255 deletions src/Latte/Compiler/TagParserData.php

Large diffs are not rendered by default.

122 changes: 62 additions & 60 deletions src/Latte/Compiler/Token.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,65 +64,66 @@ final class Token
Php_BooleanAnd = 276,
Php_AmpersandNotFollowed = 277,
Php_AmpersandFollowed = 278,
Php_IsEqual = 279,
Php_IsNotEqual = 280,
Php_IsIdentical = 281,
Php_IsNotIdentical = 282,
Php_Spaceship = 283,
Php_IsSmallerOrEqual = 284,
Php_IsGreaterOrEqual = 285,
Php_Sl = 286,
Php_Sr = 287,
Php_In = 288,
Php_Instanceof = 289,
Php_Inc = 290,
Php_Dec = 291,
Php_IntCast = 292,
Php_FloatCast = 293,
Php_StringCast = 294,
Php_ArrayCast = 295,
Php_ObjectCast = 296,
Php_BoolCast = 297,
Php_Pow = 298,
Php_New = 299,
Php_Clone = 300,
Php_Integer = 301,
Php_Float = 302,
Php_Identifier = 303,
Php_StringVarname = 304,
Php_Constant = 305,
Php_Variable = 306,
Php_NumString = 307,
Php_EncapsedAndWhitespace = 308,
Php_ConstantEncapsedString = 309,
Php_Match = 310,
Php_Default = 311,
Php_Function = 312,
Php_Fn = 313,
Php_Return = 314,
Php_Use = 315,
Php_Isset = 316,
Php_Empty = 317,
Php_ObjectOperator = 318,
Php_NullsafeObjectOperator = 319,
Php_UndefinedsafeObjectOperator = 320,
Php_List = 321,
Php_Array = 322,
Php_StartHeredoc = 323,
Php_EndHeredoc = 324,
Php_DollarOpenCurlyBraces = 325,
Php_CurlyOpen = 326,
Php_PaamayimNekudotayim = 327,
Php_NsSeparator = 328,
Php_Ellipsis = 329,
Php_ExpandCast = 330,
Php_NameFullyQualified = 331,
Php_NameQualified = 332,
Php_Whitespace = 333,
Php_Comment = 334,
Php_Null = 335,
Php_True = 336,
Php_False = 337;
Php_Not = 279,
Php_IsEqual = 280,
Php_IsNotEqual = 281,
Php_IsIdentical = 282,
Php_IsNotIdentical = 283,
Php_Spaceship = 284,
Php_IsSmallerOrEqual = 285,
Php_IsGreaterOrEqual = 286,
Php_In = 287,
Php_Sl = 288,
Php_Sr = 289,
Php_Instanceof = 290,
Php_Inc = 291,
Php_Dec = 292,
Php_IntCast = 293,
Php_FloatCast = 294,
Php_StringCast = 295,
Php_ArrayCast = 296,
Php_ObjectCast = 297,
Php_BoolCast = 298,
Php_Pow = 299,
Php_New = 300,
Php_Clone = 301,
Php_Integer = 302,
Php_Float = 303,
Php_Identifier = 304,
Php_StringVarname = 305,
Php_Constant = 306,
Php_Variable = 307,
Php_NumString = 308,
Php_EncapsedAndWhitespace = 309,
Php_ConstantEncapsedString = 310,
Php_Match = 311,
Php_Default = 312,
Php_Function = 313,
Php_Fn = 314,
Php_Return = 315,
Php_Use = 316,
Php_Isset = 317,
Php_Empty = 318,
Php_ObjectOperator = 319,
Php_NullsafeObjectOperator = 320,
Php_UndefinedsafeObjectOperator = 321,
Php_List = 322,
Php_Array = 323,
Php_StartHeredoc = 324,
Php_EndHeredoc = 325,
Php_DollarOpenCurlyBraces = 326,
Php_CurlyOpen = 327,
Php_PaamayimNekudotayim = 328,
Php_NsSeparator = 329,
Php_Ellipsis = 330,
Php_ExpandCast = 331,
Php_NameFullyQualified = 332,
Php_NameQualified = 333,
Php_Whitespace = 334,
Php_Comment = 335,
Php_Null = 336,
Php_True = 337,
Php_False = 338;

public const NAMES = [
self::End => '[EOF]',
Expand Down Expand Up @@ -169,16 +170,17 @@ final class Token
self::Php_BooleanAnd => "'&&'",
self::Php_AmpersandNotFollowed => "'&'",
self::Php_AmpersandFollowed => "'&'",
self::Php_Not => "'not'",
self::Php_IsEqual => "'=='",
self::Php_IsNotEqual => "'!='",
self::Php_IsIdentical => "'==='",
self::Php_IsNotIdentical => "'!=='",
self::Php_Spaceship => "'<=>'",
self::Php_IsSmallerOrEqual => "'<='",
self::Php_IsGreaterOrEqual => "'>='",
self::Php_In => "'in'",
self::Php_Sl => "'<<'",
self::Php_Sr => "'>>'",
self::Php_In => "'in'",
self::Php_Instanceof => "'instanceof'",
self::Php_Inc => "'++'",
self::Php_Dec => "'--'",
Expand Down
87 changes: 87 additions & 0 deletions src/Latte/Loaders/NamespaceLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/**
* This file is part of the Latte (https://latte.nette.org)
* Copyright (c) 2008 David Grudl (https://davidgrudl.com)
*/

declare(strict_types=1);

namespace Latte\Loaders;

use Latte;


/**
* Template loader.
*/
class NamespaceLoader implements Latte\Loader
{
/**
* @var Latte\Loader[]
*/
private array $loaders;

public function __construct(array $loaders)
{
$this->loaders = $loaders;
}

/**
* Returns template source code.
*/
public function getContent(string $name): string
{
[$loader, $name] = $this->extractLoaderAndName($name);

return $loader->getContent($name);
}


public function isExpired(string $file, int $time): bool
{
[$loader, $name] = $this->extractLoaderAndName($file);

return $loader->isExpired($name, $time);
}


/**
* Returns referred template name.
*/
public function getReferredName(string $name, string $referringName): string
{
[$loader, $name] = $this->extractLoaderAndName($name);

return $loader->getReferredName($name, $referringName);
}


/**
* Returns unique identifier for caching.
*/
public function getUniqueId(string $name): string
{
[$loader, $name] = $this->extractLoaderAndName($name);

return $loader->getUniqueId($name);
}


private function extractLoaderAndName(string $name): array
{
$namespaceParts = \explode('::', $name, 2);

if (count($namespaceParts) === 2) {
return [
$this->loaders[$namespaceParts[0]],
$namespaceParts[1],
];
}

return [
$this->loaders[''],
$name,
];
}
}
11 changes: 11 additions & 0 deletions src/Latte/Runtime/Filters.php
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,17 @@ public static function escapeHtmlRawText($s): string
}


/**
* Determine if a string or array contains a given needle.
*/
public static function contains(mixed $needle, array|string $haystack): bool
{
return is_array($haystack)
? in_array($needle, $haystack, true)
: str_contains($haystack, (string) $needle);
}


/**
* Converts ... to ...
*/
Expand Down
30 changes: 30 additions & 0 deletions tests/common/Loaders.NamespaceLoader.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* Test: StringLoader
*/

declare(strict_types=1);

use Latte\Loaders\StringLoader;
use Latte\Loaders\NamespaceLoader;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


test('', function () {
$defaultLoader = new StringLoader(['main' => 'defaultcontent']);
$appLoader = new StringLoader(['main' => 'appcontent']);
$otherLoader = new StringLoader(['main' => 'othercontent']);

$loader = new NamespaceLoader([
'' => $defaultLoader,
'app' => $appLoader,
'other' => $otherLoader,
]);

Assert::same('defaultcontent', $loader->getContent('main'));
Assert::same('appcontent', $loader->getContent('app::main'));
Assert::same('othercontent', $loader->getContent('other::main'));
});
8 changes: 4 additions & 4 deletions tests/common/TagParser.parseArguments().phpt
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ test('inline modifiers', function () {


test('in operator', function () {
Assert::same("in_array(\$a, ['a', 'b'], true), 1", formatArgs('$a in [a, b], 1'));
Assert::same('$a, in_array($b->func(), [1, 2], true)', formatArgs('$a, $b->func() in [1, 2]'));
Assert::same('$a, in_array($b[1], [1, 2], true)', formatArgs('$a, $b[1] in [1, 2]'));
Assert::same('in_array($b, [1, [2], 3], true)', formatArgs('$b in [1, [2], 3]'));
Assert::same("LR\\Filters::contains(\$a, ['a', 'b']), 1", formatArgs('$a in [a, b], 1'));
Assert::same('$a, LR\Filters::contains($b->func(), [1, 2])', formatArgs('$a, $b->func() in [1, 2]'));
Assert::same('$a, LR\Filters::contains($b[1], [1, 2])', formatArgs('$a, $b[1] in [1, 2]'));
Assert::same('LR\Filters::contains($b, [1, [2], 3])', formatArgs('$b in [1, [2], 3]'));
});


Expand Down
26 changes: 26 additions & 0 deletions tests/filters/contains.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

/**
* Test: Latte\Runtime\Filters::contains
*/

declare(strict_types=1);

use Latte\Runtime\Filters;
use Tester\Assert;

require __DIR__ . '/../bootstrap.php';


Assert::false(Filters::contains(null, []));
Assert::true(Filters::contains(null, [null]));
Assert::false(Filters::contains(1, ['1']));
Assert::true(Filters::contains(1, [1]));

Assert::true(Filters::contains('', ''));
Assert::true(Filters::contains('', 'abcd'));
Assert::false(Filters::contains('bc', ''));
Assert::true(Filters::contains('bc', 'abcd'));
Assert::true(Filters::contains(null, ''));
Assert::true(Filters::contains(1, '123'));
Assert::false(Filters::contains(1, '23'));
2 changes: 1 addition & 1 deletion tests/phpLexer/names.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ __halt_compiler();
#10:8 Php_Whitespace '\n'
#11:1 Php_Identifier 'aaa--bbb'
#11:9 Php_Whitespace '\n'
#12:1 Php_Identifier 'not'
#12:1 Php_Not 'not'
#12:4 Php_Dec '--'
#12:6 '-'
#12:7 Php_Identifier 'name'
Expand Down
14 changes: 13 additions & 1 deletion tests/phpLexer/operators.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ $test = <<<'XX'
$a xor $b;
$b && 2;
$b || 1;

!$a;
not $a;
XX;

$tokens = (new TagLexer)->tokenize($test);
Expand Down Expand Up @@ -268,4 +271,13 @@ __halt_compiler();
#34:6 Php_Whitespace ' '
#34:7 Php_Integer '1'
#34:8 ';'
#34:9 End ''
#34:9 Php_Whitespace '\n\n'
#36:1 '!'
#36:2 Php_Variable '$a'
#36:4 ';'
#36:5 Php_Whitespace '\n'
#37:1 Php_Not 'not'
#37:4 Php_Whitespace ' '
#37:5 Php_Variable '$a'
#37:7 ';'
#37:8 End ''
Loading