Skip to content

Commit

Permalink
Latte 2 integration
Browse files Browse the repository at this point in the history
  • Loading branch information
tg666 committed Nov 28, 2023
1 parent 869fafb commit 6ed7957
Show file tree
Hide file tree
Showing 47 changed files with 1,217 additions and 75 deletions.
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
10 changes: 8 additions & 2 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 || ^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?);');
}
}
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));
}
}
}
5 changes: 5 additions & 0 deletions src/Bridge/Latte/RendererProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,11 @@ private function addToQueue(RequestPosition $position): string
return $comment;
}

public function supportsQueues(): bool
{
return $this->renderingMode->supportsQueues();
}

public function isAnythingQueued(): bool
{
return 0 < count($this->queue);
Expand Down
5 changes: 5 additions & 0 deletions src/Bridge/Latte/RenderingMode/DirectRenderingMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

final class DirectRenderingMode implements RenderingModeInterface
{
public function supportsQueues(): bool
{
return false;
}

public function shouldBePositionQueued(Position $position, object $globals): bool
{
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

final class QueuedRenderingInPresenterContextMode implements RenderingModeInterface
{
public function supportsQueues(): bool
{
return true;
}

public function shouldBePositionQueued(Position $position, object $globals): bool
{
return isset($globals->uiPresenter) && $globals->uiPresenter instanceof Presenter;
Expand Down
5 changes: 5 additions & 0 deletions src/Bridge/Latte/RenderingMode/QueuedRenderingMode.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

final class QueuedRenderingMode implements RenderingModeInterface
{
public function supportsQueues(): bool
{
return true;
}

public function shouldBePositionQueued(Position $position, object $globals): bool
{
return true;
Expand Down
2 changes: 2 additions & 0 deletions src/Bridge/Latte/RenderingMode/RenderingModeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,7 @@

interface RenderingModeInterface
{
public function supportsQueues(): bool;

public function shouldBePositionQueued(Position $position, object $globals): bool;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\AmpClient\Bridge\Nette\Application;

use Nette\Application\Application;
use Nette\Application\IPresenter;
use Nette\Application\UI\Presenter;
use SixtyEightPublishers\AmpClient\Bridge\Latte\RendererProvider;

final class AttachPresenterHandlersOnApplicationHandler
{
private RendererProvider $rendererProvider;

public function __construct(RendererProvider $rendererProvider)
{
$this->rendererProvider = $rendererProvider;
}

public static function attach(Application $application, RendererProvider $rendererProvider): void
{
$application->onPresenter[] = new self($rendererProvider);
}

public function __invoke(Application $application, IPresenter $presenter): void
{
if ($presenter instanceof Presenter) {
RenderQueuedPositionsOnPresenterShutdownHandler::attach($presenter, $this->rendererProvider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
namespace SixtyEightPublishers\AmpClient\Bridge\Nette\Application;

use Nette\Application\AbortException;
use Nette\Application\Application;
use Nette\Application\IPresenter;
use Nette\Application\Response;
use Nette\Application\IResponse;
use Nette\Application\Responses\JsonResponse;
use Nette\Application\Responses\TextResponse;
use Nette\Application\UI\ITemplate;
use Nette\Application\UI\Presenter;
use Nette\Application\UI\Template;
use SixtyEightPublishers\AmpClient\Bridge\Latte\RendererProvider;
use SixtyEightPublishers\AmpClient\Exception\AmpExceptionInterface;
use SixtyEightPublishers\AmpClient\Renderer\OutputBuffer;
Expand All @@ -28,16 +26,7 @@ public function __construct(RendererProvider $rendererProvider)
$this->rendererProvider = $rendererProvider;
}

public static function attachOnApplication(Application $application, RendererProvider $rendererProvider): void
{
$application->onPresenter[] = static function (Application $application, IPresenter $presenter) use ($rendererProvider): void {
if ($presenter instanceof Presenter) {
self::attachOnPresenter($presenter, $rendererProvider);
}
};
}

public static function attachOnPresenter(Presenter $presenter, RendererProvider $rendererProvider): void
public static function attach(Presenter $presenter, RendererProvider $rendererProvider): void
{
$presenter->onShutdown[] = new self($rendererProvider);
}
Expand All @@ -46,8 +35,12 @@ public static function attachOnPresenter(Presenter $presenter, RendererProvider
* @throws AmpExceptionInterface
* @throws Throwable
*/
public function __invoke(Presenter $presenter, Response $response): void
public function __invoke(Presenter $presenter, IResponse $response): void
{
if (!$this->rendererProvider->supportsQueues()) {
return;
}

try {
if ($response instanceof TextResponse) {
$this->processTextResponse($presenter, $response);
Expand All @@ -67,7 +60,7 @@ private function processTextResponse(Presenter $presenter, TextResponse $respons
{
$source = $response->getSource();

if ($source instanceof Template) {
if ($source instanceof ITemplate) {
$output = OutputBuffer::capture(static function () use ($source): void {
$source->render();
});
Expand Down
43 changes: 29 additions & 14 deletions src/Bridge/Nette/DI/AmpClientLatteExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,26 @@
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use RuntimeException;
use SixtyEightPublishers\AmpClient\Bridge\Latte\AmpClientLatte3Extension;
use SixtyEightPublishers\AmpClient\Bridge\Latte\AmpClientLatteExtension as AmpClientLatteExtensionRegister;
use SixtyEightPublishers\AmpClient\Bridge\Latte\Event\ConfigureClientEventHandlerInterface;
use SixtyEightPublishers\AmpClient\Bridge\Latte\RendererProvider;
use SixtyEightPublishers\AmpClient\Bridge\Latte\RenderingMode\DirectRenderingMode;
use SixtyEightPublishers\AmpClient\Bridge\Nette\Application\RenderQueuedPositionsOnPresenterShutdownHandler;
use SixtyEightPublishers\AmpClient\Bridge\Latte\RenderingMode\QueuedRenderingInPresenterContextMode;
use SixtyEightPublishers\AmpClient\Bridge\Latte\RenderingMode\QueuedRenderingMode;
use SixtyEightPublishers\AmpClient\Bridge\Nette\Application\AttachPresenterHandlersOnApplicationHandler;
use SixtyEightPublishers\AmpClient\Bridge\Nette\DI\Config\AmpClientLatteConfig;
use function assert;
use function count;
use function sprintf;

final class AmpClientLatteExtension extends CompilerExtension
{
private const RenderingModes = [
'direct' => DirectRenderingMode::class,
'queued' => QueuedRenderingMode::class,
'queued_in_presenter_context' => QueuedRenderingInPresenterContextMode::class,
];

private bool $debugMode;

public function __construct(bool $debugMode = false)
Expand All @@ -41,10 +49,7 @@ public function getConfigSchema(): Schema
return Expect::structure([
'banner_macro_name' => Expect::string('banner'),
'rendering_mode' => Expect::anyOf(Expect::string(), Expect::type(Statement::class))
->default(new Statement(DirectRenderingMode::class))
->before(static function ($factory): Statement {
return $factory instanceof Statement ? $factory : new Statement($factory);
}),
->default('direct'),
])->castTo(AmpClientLatteConfig::class);
}

Expand All @@ -70,11 +75,21 @@ public function loadConfiguration(): void
$config = $this->getConfig();
assert($config instanceof AmpClientLatteConfig);

$renderingMode = $config->rendering_mode;

if (is_string($renderingMode) && isset(self::RenderingModes[$renderingMode])) {
$renderingMode = self::RenderingModes[$renderingMode];
}

if (!($renderingMode instanceof Statement)) {
$renderingMode = new Statement($renderingMode);
}

$builder->addDefinition($this->prefix('rendererProvider'))
->setAutowired(false)
->setFactory(RendererProvider::class)
->addSetup('setRenderingMode', [
'renderingMode' => $config->rendering_mode,
'renderingMode' => $renderingMode,
])
->addSetup('setDebugMode', [
'debugMode' => $this->debugMode,
Expand All @@ -91,11 +106,11 @@ public function beforeCompile(): void
assert($latteFactory instanceof FactoryDefinition);
$latteFactoryResultDefinition = $latteFactory->getResultDefinition();

$latteFactoryResultDefinition->addSetup('addExtension', [
new Statement(AmpClientLatte3Extension::class, [
'rendererProvider' => new Reference($this->prefix('rendererProvider')),
'tagName' => $config->banner_macro_name,
]),
$latteFactoryResultDefinition->addSetup('?::register(?, ?, ?)', [
ContainerBuilder::literal(AmpClientLatteExtensionRegister::class),
new Reference('self'),
new Reference($this->prefix('rendererProvider')),
$config->banner_macro_name,
]);

$rendererProviderDefinition = $builder->getDefinition($this->prefix('rendererProvider'));
Expand All @@ -112,8 +127,8 @@ public function beforeCompile(): void
$applicationDefinition = $builder->getDefinitionByType(Application::class);
assert($applicationDefinition instanceof ServiceDefinition);

$applicationDefinition->addSetup('?::attachOnApplication(?, ?)', [
ContainerBuilder::literal(RenderQueuedPositionsOnPresenterShutdownHandler::class),
$applicationDefinition->addSetup('?::attach(?, ?)', [
ContainerBuilder::literal(AttachPresenterHandlersOnApplicationHandler::class),
new Reference('self'),
new Reference($this->prefix('rendererProvider')),
]);
Expand Down
3 changes: 2 additions & 1 deletion src/Bridge/Nette/DI/Config/AmpClientLatteConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ final class AmpClientLatteConfig
{
public string $banner_macro_name;

public Statement $rendering_mode;
/** @var string|Statement|null */
public $rendering_mode;
}
Loading

0 comments on commit 6ed7957

Please sign in to comment.