Skip to content

Commit

Permalink
added constant Template::Source
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Aug 27, 2023
1 parent 03f3baa commit 45cd10f
Show file tree
Hide file tree
Showing 23 changed files with 50 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/Latte/Compiler/TemplateGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ final class TemplateGenerator
public function generate(
Nodes\TemplateNode $node,
string $className,
?string $comment = null,
?string $sourceName = null,
bool $strictMode = false,
): string
{
Expand All @@ -57,6 +57,10 @@ public function generate(
$this->addConstant('ContentType', $node->contentType);
}

if ($sourceName !== null) {
$this->addConstant('Source', $sourceName);
}

$this->generateBlocks($context->blocks, $context);

$members = [];
Expand All @@ -79,7 +83,7 @@ public function generate(
$code = "<?php\n\n"
. ($strictMode ? "declare(strict_types=1);\n\n" : '')
. "use Latte\\Runtime as LR;\n\n"
. ($comment === null ? '' : '/** ' . str_replace('*/', '* /', $comment) . " */\n")
. ($sourceName === null ? '' : '/** source: ' . str_replace('*/', '* /', $sourceName) . " */\n")
. "final class $className extends Latte\\Runtime\\Template\n{\n"
. implode("\n\n", $members)
. "\n}\n";
Expand Down
4 changes: 2 additions & 2 deletions src/Latte/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -180,12 +180,12 @@ public function applyPasses(TemplateNode &$node): void
*/
public function generate(TemplateNode $node, string $name): string
{
$comment = preg_match('#\n|\?#', $name) ? null : "source: $name";
$sourceName = preg_match('#\n|\?#', $name) ? null : $name;
$generator = new Compiler\TemplateGenerator;
return $generator->generate(
$node,
$this->getTemplateClass($name),
$comment,
$sourceName,
$this->strictTypes,
);
}
Expand Down
2 changes: 2 additions & 0 deletions src/Latte/Runtime/Template.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ class Template

public const ContentType = Latte\ContentType::Html;

public const Source = null;

public const Blocks = [];

/** global accumulators for intermediate results */
Expand Down
2 changes: 2 additions & 0 deletions tests/common/expected/contentType.text.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ final class Template%a% extends Latte\Runtime\Template
{
public const ContentType = 'text';

public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/common/expected/contentType.xml.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ final class Template%a% extends Latte\Runtime\Template
{
public const ContentType = 'xml';

public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/filters/expected/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/define.args4.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/embed.block.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';

public const Blocks = [
1 => ['a' => 'blockA'],
2 => ['a' => 'blockA1'],
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/embed.file.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';

public const Blocks = [
1 => ['a' => 'blockA'],
2 => ['a' => 'blockA1'],
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/extends.1.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/extends.4.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';

public const Blocks = [
['content' => 'blockContent'],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/general.n-attributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';

public const Blocks = [
['bl' => 'blockBl'],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/general.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
/** source: %A% */
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';

public const Blocks = [
['menu' => 'blockMenu'],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/include.inc1.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/include.inc2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/include.inc3.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/include.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = '%a%.latte';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/include.with-blocks.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'inc';

public const Blocks = [
['test' => 'blockTest'],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/include.with-blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';


public function main(array $ʟ_args): void
{
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/inheritance.1.parent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'parent';

public const Blocks = [
['title' => 'blockTitle', 'sidebar' => 'blockSidebar'],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/inheritance.1.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';

public const Blocks = [
['title' => 'blockTitle', 'content' => 'blockContent'],
];
Expand Down
2 changes: 2 additions & 0 deletions tests/tags/expected/inheritance.2.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
%A%
final class Template%a% extends Latte\Runtime\Template
{
public const Source = 'main';

public const Blocks = [
['content' => 'blockContent', 'title' => 'blockTitle', 'sidebar' => 'blockSidebar'],
];
Expand Down

0 comments on commit 45cd10f

Please sign in to comment.