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

Feature: Latte #1

Merged
merged 4 commits into from
Nov 28, 2023
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 .github/workflows/coding-style.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ jobs:
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.4
php-version: 8.1
tools: composer:v2
extensions: uopz

Expand Down
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ cs.check:
docker exec 68publishers.amp-client-php.74 vendor/bin/php-cs-fixer fix -v --dry-run

stan:
PHP=74 make composer.update
docker exec 68publishers.amp-client-php.74 vendor/bin/phpstan analyse
PHP=81 make composer.update
docker exec 68publishers.amp-client-php.81 vendor/bin/phpstan analyse

coverage:
PHP=74 make composer.update
docker exec 68publishers.amp-client-php.74 vendor/bin/tester -C -s --coverage ./coverage.xml --coverage-src ./src ./tests
PHP=81 make composer.update
docker exec 68publishers.amp-client-php.81 vendor/bin/tester -C -s --coverage ./coverage.xml --coverage-src ./src ./tests

composer.update:
ifndef PHP
Expand Down
12 changes: 9 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,29 @@
"require-dev": {
"ext-dom": "*",
"ext-libxml": "*",
"dg/bypass-finals": "^1.5",
"friendsofphp/php-cs-fixer": "^3.17",
"kubawerlos/php-cs-fixer-custom-fixers": "^3.14",
"latte/latte": "^2.11",
"latte/latte": "^2.11 || ^3.0",
"mockery/mockery": "^1.6",
"nette/application": "^3.0.8",
"nette/bootstrap": "^3.1",
"nette/caching": "^3.1",
"nette/di": "^3.0",
"nette/di": "^3.0.13",
"nette/tester": "^2.4",
"phpstan/phpstan": "^1.10",
"phpstan/phpstan-nette": "^1.2",
"psr/log": "^1.1",
"roave/security-advisories": "dev-latest",
"slope-it/clock-mock": "^0.4.0",
"symplify/phpstan-rules": "12.0.2.72",
"wa72/html-pretty-min": "^0.2.0"
},
"conflict": {
"latte/latte": "<2.11",
"nette/application": "<3.0.8",
"nette/caching": "<3.1",
"nette/di": "<3.0.13"
},
"config": {
"sort-packages": true
},
Expand Down
11 changes: 5 additions & 6 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,6 @@ includes:
- vendor/phpstan/phpstan-nette/extension.neon
- vendor/phpstan/phpstan-nette/rules.neon

rules:
- Symplify\PHPStanRules\Rules\NoArrayAccessOnObjectRule

services:
- Symplify\PHPStanRules\Matcher\ArrayStringAndFnMatcher

parameters:
level: 8
paths:
Expand All @@ -17,3 +11,8 @@ parameters:
-
message: '#Anonymous function has an unused use \$.+#'
path: src/Renderer/Phtml/PhtmlRendererBridge.php

excludePaths:
analyse:
- src/Bridge/Latte/AmpClientLatte2Extension.php # "extension" for Latte v2
- src/Bridge/Nette/Application/RenderQueuedPositionsOnPresenterShutdownHandler.php # using old Nette interfaces prefixed with `I` for back compatibility
44 changes: 44 additions & 0 deletions src/Bridge/Latte/AmpClientLatte2Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Latte;

use Latte\CompileException;
use Latte\Compiler;
use Latte\Engine;
use Latte\Helpers;
use Latte\MacroNode;
use Latte\Macros\MacroSet;
use Latte\PhpWriter;

final class AmpClientLatte2Extension extends MacroSet
{
private function __construct(Compiler $compiler)
{
parent::__construct($compiler);
}

public static function register(Engine $latte, RendererProvider $rendererProvider, string $tagName = 'banner'): void
{
$latte->addProvider('ampClientRenderer', $rendererProvider);

$latte->onCompile[] = static function (Engine $latte) use ($tagName): void {
$macroSet = new self($latte->getCompiler());

$macroSet->addMacro($tagName, [$macroSet, 'macroBanner']);
};
}

/**
* @throws CompileException
*/
public function macroBanner(MacroNode $node, PhpWriter $writer): string
{
Helpers::removeFilter($node->modifiers, 'escape');

return $writer
->using($node)
->write('echo ($this->global->ampClientRenderer)($this->global, %node.word, %node.args?);');
}
}
43 changes: 43 additions & 0 deletions src/Bridge/Latte/AmpClientLatte3Extension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Latte;

use Latte\Extension;
use SixtyEightPublishers\AmpClient\Bridge\Latte\Node\BannerNode;

final class AmpClientLatte3Extension extends Extension
{
private RendererProvider $rendererProvider;

private string $tagName;

public function __construct(RendererProvider $rendererProvider, string $tagName = 'banner')
{
$this->rendererProvider = $rendererProvider;
$this->tagName = $tagName;
}

/**
* @return array{
* ampClientRenderer: RendererProvider,
* }
*/
public function getProviders(): array
{
return [
'ampClientRenderer' => $this->rendererProvider,
];
}

/**
* @return array<string, callable>
*/
public function getTags(): array
{
return [
$this->tagName => [BannerNode::class, 'create'],
];
}
}
22 changes: 22 additions & 0 deletions src/Bridge/Latte/AmpClientLatteExtension.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Latte;

use Latte\Engine;
use function version_compare;

final class AmpClientLatteExtension
{
private function __construct() {}

public static function register(Engine $latte, RendererProvider $rendererProvider, string $tagName = 'banner'): void
{
if (version_compare(Engine::VERSION, '3', '<')) { // @phpstan-ignore-line
AmpClientLatte2Extension::register($latte, $rendererProvider, $tagName);
} else {
$latte->addExtension(new AmpClientLatte3Extension($rendererProvider, $tagName));
}
}
}
28 changes: 28 additions & 0 deletions src/Bridge/Latte/Event/ConfigureClientEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Latte\Event;

use SixtyEightPublishers\AmpClient\AmpClientInterface;

final class ConfigureClientEvent
{
private AmpClientInterface $client;

public function __construct(
AmpClientInterface $client
) {
$this->client = $client;
}

public function getClient(): AmpClientInterface
{
return $this->client;
}

public function withClient(AmpClientInterface $client): self
{
return new self($client);
}
}
10 changes: 10 additions & 0 deletions src/Bridge/Latte/Event/ConfigureClientEventHandlerInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Latte\Event;

interface ConfigureClientEventHandlerInterface
{
public function __invoke(ConfigureClientEvent $event): ConfigureClientEvent;
}
63 changes: 63 additions & 0 deletions src/Bridge/Latte/Node/BannerNode.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Latte\Node;

use Generator;
use Latte\CompileException;
use Latte\Compiler\Nodes\Php\ExpressionNode;
use Latte\Compiler\Nodes\StatementNode;
use Latte\Compiler\PrintContext;
use Latte\Compiler\Tag;

/**
* {banner $positionCode [, $resources]}
*/
final class BannerNode extends StatementNode
{
public ExpressionNode $positionCode;

public ?ExpressionNode $resources = null;

/**
* @throws CompileException
*/
public static function create(Tag $tag): self
{
$tag->expectArguments();
$node = new self;
$node->positionCode = $tag->parser->parseUnquotedStringOrExpression();

if ($tag->parser->stream->tryConsume(',')) {
$node->resources = $tag->parser->parseExpression();
}

return $node;
}

public function print(PrintContext $context): string
{
if (null !== $this->resources) {
return $context->format(
'echo ($this->global->ampClientRenderer)($this->global, %node, %node?);',
$this->positionCode,
$this->resources,
);
}

return $context->format(
'echo ($this->global->ampClientRenderer)($this->global, %node);',
$this->positionCode,
);
}

public function &getIterator(): Generator
{
yield $this->positionCode;

if (null !== $this->resources) {
yield $this->resources;
}
}
}
Loading