Skip to content

Commit

Permalink
Merge pull request #13 from merkle-open/feat/php-8-1-support
Browse files Browse the repository at this point in the history
Add support for PHP 8.0;8.1 and Twig 3.x.
  • Loading branch information
rdamjanov authored Oct 27, 2023
2 parents 0bd3b4f + f07f34d commit f30c2b8
Show file tree
Hide file tree
Showing 20 changed files with 87 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
php_version: ['7.3', '7.4']
php_version: ['8.0', '8.1']

steps:
- name: Setup PHP ${{ matrix.php_version }}
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ $ composer require namics/terrific-twig
## Requirements

The following versions of PHP are currently supported.
* 7.3
* 7.4
* 8.0
* 8.1

## Setup
Step 1: Implement `TemplateInformationProvider`
Expand All @@ -46,7 +46,7 @@ Step 2: Implement `ContextProviderInterface`
```php
class ContextProvider implements ContextProviderInterface
{
public function compile(Twig_Compiler $compiler, Twig_Node $component, Twig_Node $dataVariant, $only) {
public function compile(\Twig\Compiler $compiler, \Twig\Node\Node $component, \Twig\Node\Node $dataVariant, $only) {
// ...
}
}
Expand All @@ -55,13 +55,13 @@ class ContextProvider implements ContextProviderInterface
Step 3: Add `TerrificLoader`
```php
$loader = ...;
$chain = new Twig_Loader_Chain([$loader, new TerrificLoader(new TemplateInformationProvider)]);
$twig = new Twig_Environment($chain);
$chain = new \Twig\Loader\ChainLoader([$loader, new TerrificLoader(new TemplateInformationProvider)]);
$twig = new \Twig\Environment($chain);
```

Step 4: Add `TerrificExtension`
```php
$twig = new Twig_Environment($chain);
$twig = new \Twig\Environment($chain);
$twig->addExtension(new TerrificExtension(new ContextProvider));
```

Expand Down
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@
}
},
"require": {
"php": ">=7.3",
"twig/twig": "^2.13"
"php": ">=8.0",
"twig/twig": "^3.6"
},
"require-dev": {
"phpunit/phpunit": "^6.0"
"phpunit/phpunit": "^9.0"
},
"scripts": {
"tests": [
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="vendor/autoload.php"
>
<testsuites>
Expand Down
11 changes: 7 additions & 4 deletions src/Config/ConfigReader.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Config;

use DomainException;
Expand All @@ -26,7 +28,7 @@ final class ConfigReader {
*
* @var array
*/
private $config = [];
private array $config = [];

/**
* ConfigReader constructor.
Expand All @@ -36,7 +38,7 @@ final class ConfigReader {
*
* @throws DomainException Thrown if Configuration could not be loaded correctly.
*/
public function __construct($terrificDir) {
public function __construct(string $terrificDir) {
$path = $terrificDir . '/' . self::TERRIFIC_NITRO_FILE_NAME;
$this->readConfig($path);
}
Expand All @@ -56,8 +58,9 @@ public function getConfig(): array {
*
* @param string $path
* The path to the config file.
* @return void
*/
protected function readConfig(string $path) {
protected function readConfig(string $path): void {
if (is_readable($path)) {
try {
$this->config = json_decode(file_get_contents($path), TRUE);
Expand All @@ -67,7 +70,7 @@ protected function readConfig(string $path) {
}
}
catch (DomainException $e) {
throw $e;
throw new DomainException($e->getMessage());
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/Provider/ContextProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Provider;

use Twig\Compiler;
Expand Down
2 changes: 2 additions & 0 deletions src/Provider/TemplateInformationProviderInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Provider;

/**
Expand Down
7 changes: 5 additions & 2 deletions src/Twig/Data/VariableNameAndArrayKeysPair.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig\Data;

use Twig\Error\Error;
Expand All @@ -16,7 +18,7 @@ class VariableNameAndArrayKeysPair {
*
* @var string
*/
protected $variableName;
protected string $variableName;

/**
* Array of array keys for given variableName variable.
Expand All @@ -25,7 +27,7 @@ class VariableNameAndArrayKeysPair {
*
* @var string[]
*/
protected $arrayKeys;
protected array $arrayKeys;

/**
* VariableNameAndArrayKeysPair constructor.
Expand Down Expand Up @@ -137,6 +139,7 @@ public function toTwigContextArrayKeysString(): string {
*
* @return string
* E.g. myVariable['aKey']['anotherKey'].
* @throws Error
*/
public function toTwigVariableString(): string {
if (!$this->isVariableNameEmpty()) {
Expand Down
4 changes: 3 additions & 1 deletion src/Twig/Extension/TerrificExtension.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig\Extension;

use Namics\Terrific\Provider\ContextProviderInterface;
Expand All @@ -25,7 +27,7 @@ final class TerrificExtension extends AbstractExtension {
*
* @var \Namics\Terrific\Provider\ContextProviderInterface
*/
private $ctxProvider;
private ContextProviderInterface $ctxProvider;

/**
* TerrificExtension constructor.
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/Loader/TerrificLoader.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig\Loader;

use Namics\Terrific\Provider\TemplateInformationProviderInterface;
Expand Down
19 changes: 11 additions & 8 deletions src/Twig/Node/ComponentNode.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig\Node;

use Namics\Terrific\Provider\ContextProviderInterface;
Expand Down Expand Up @@ -28,7 +30,7 @@ final class ComponentNode extends Node implements NodeOutputInterface {
*
* @var \Namics\Terrific\Provider\ContextProviderInterface
*/
private $ctxProvider;
private ContextProviderInterface $ctxProvider;

/**
* The expression handler.
Expand All @@ -38,7 +40,7 @@ final class ComponentNode extends Node implements NodeOutputInterface {
*
* @var \Namics\Terrific\Twig\Utility\ExpressionHandler
*/
private $expressionHandler;
private ExpressionHandler $expressionHandler;

/**
* ComponentNode constructor.
Expand All @@ -60,9 +62,9 @@ public function __construct(
Node $component,
ContextProviderInterface $ctxProvider,
Node $data,
$only = FALSE,
$lineno,
$tag = NULL
int $lineno,
bool $only = FALSE,
string $tag = NULL
) {
parent::__construct(
['component' => $component, 'data' => $data],
Expand Down Expand Up @@ -129,8 +131,9 @@ protected function createTerrificContext(TerrificCompilerInterface $terrificComp
* IMPORTANT: Has to be executed after the Terrific context was created
* (ComponentNode::createTerrificContext).
*
* @param \Namics\Terrific\Twig\TerrificCompilerInterface $terrificCompiler
* @param TerrificCompilerInterface $terrificCompiler
* The Terrific Twig compiler.
* @throws SyntaxError
*/
protected function addGetTemplate(TerrificCompilerInterface $terrificCompiler): void {
$twigCompiler = $terrificCompiler->getTwigCompiler();
Expand Down Expand Up @@ -161,7 +164,7 @@ protected function compileComponentName(TerrificCompilerInterface $terrificCompi
$node = $this->getNode('component');

/* If a variable is used for component name,
use it's value (is inside Terrifc context "$tContext") for template name.
use its value (is inside Terrifc context "$tContext") for template name.
E.g. {% component myTwigComponentName { dataItem: 'my string' } %} */
if ($node instanceof NameExpression) {
$terrificCompiler->compileNameExpressionAsContextVariable($node);
Expand All @@ -173,7 +176,7 @@ protected function compileComponentName(TerrificCompilerInterface $terrificCompi
$terrificCompiler->getTwigCompiler()->subcompile($node);
}
/* If an object object/array used for component name,
use it's value (is inside Terrifc context "$tContext") for template name.
use its value (is inside Terrifc context "$tContext") for template name.
E.g. {% component myTwigObject.anObjectProperty.myTwigComponentName { dataItem: 'my string' } %} */
elseif ($node instanceof GetAttrExpression) {
$terrificCompiler->compileGetAttrExpressionAsContextVariable($node);
Expand Down
13 changes: 9 additions & 4 deletions src/Twig/TerrificCompiler.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig;

use Namics\Terrific\Provider\ContextProviderInterface;
use Namics\Terrific\Twig\Data\VariableNameAndArrayKeysPair;
use Namics\Terrific\Twig\Extension\TerrificExtension;
use Namics\Terrific\Twig\Utility\ExpressionHandler;
use Twig\Compiler;
use Twig\Error\Error;
use Twig\Node\Expression\GetAttrExpression;
use Twig\Node\Expression\NameExpression;

Expand All @@ -22,7 +25,7 @@ class TerrificCompiler implements TerrificCompilerInterface {
*
* @var \Twig\Compiler
*/
protected $compiler;
protected Compiler $compiler;

/**
* The expression handler.
Expand All @@ -32,7 +35,7 @@ class TerrificCompiler implements TerrificCompilerInterface {
*
* @var \Namics\Terrific\Twig\Utility\ExpressionHandler
*/
private $expressionHandler;
private ExpressionHandler $expressionHandler;

/**
* TerrificCompiler constructor.
Expand Down Expand Up @@ -62,6 +65,7 @@ public static function create(Compiler $compiler): self {
* @param string $contextVariable
* The PHP variable to use as base for adding the array keys.
* Defaults to the Terrific context.
* @throws Error
*/
public function compileNameExpressionAsContextVariable(NameExpression $expression, string $contextVariable = ContextProviderInterface::TERRIFIC_CONTEXT_VARIABLE): void {
$variableName = $this->getExpressionHandler()->getVariableNameFromNameExpression($expression);
Expand Down Expand Up @@ -138,13 +142,14 @@ public function compileAndMergeGetAttrExpressionToContext(GetAttrExpression $exp
/**
* Checks if given variable exists, and adds it to the context.
*
* So it it's data is available in the compiled component.
* So it its data is available in the compiled component.
*
* @param \Namics\Terrific\Twig\Data\VariableNameAndArrayKeysPair $variableNameAndArrayKeysPair
* @param VariableNameAndArrayKeysPair $variableNameAndArrayKeysPair
* Object containing the variable name and optionally array keys.
* @param string|null $variableDoesNotExistErrorMessage
* Custom error message that is used as exception message
* when the given variable does not exist.
* @throws Error
*/
public function compileAndMergeVariableToContext(VariableNameAndArrayKeysPair $variableNameAndArrayKeysPair, ?string $variableDoesNotExistErrorMessage = NULL): void {
$twigCompiler = $this->getTwigCompiler();
Expand Down
2 changes: 2 additions & 0 deletions src/Twig/TerrificCompilerInterface.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig;

use Namics\Terrific\Twig\Utility\ExpressionHandler;
Expand Down
6 changes: 4 additions & 2 deletions src/Twig/TokenParser/ComponentTokenParser.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig\TokenParser;

use Namics\Terrific\Provider\ContextProviderInterface;
Expand All @@ -21,7 +23,7 @@ final class ComponentTokenParser extends AbstractTokenParser {
*
* @var \Namics\Terrific\Provider\ContextProviderInterface
*/
private $ctxProvider;
private ContextProviderInterface $ctxProvider;

/**
* ComponentTokenParser constructor.
Expand All @@ -44,7 +46,7 @@ public function parse(Token $token): ComponentNode {
$component = $this->parser->getExpressionParser()->parseExpression();
list($data, $only) = $this->parseArguments();

return new ComponentNode($component, $this->ctxProvider, $data, $only, $token->getLine(), $this->getTag());
return new ComponentNode($component, $this->ctxProvider, $data, $token->getLine(), $only, $this->getTag());
}

/**
Expand Down
8 changes: 4 additions & 4 deletions src/Twig/Utility/ExpressionHandler.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Terrific\Twig\Utility;

use Namics\Terrific\Twig\Data\VariableNameAndArrayKeysPair;
Expand Down Expand Up @@ -88,12 +90,10 @@ public function buildGetAttrExpressionArrayKeys(Node $expression): array {
*
* @return bool
* TRUE if nested object.
* Otherwise FALSE.
* Otherwise, FALSE.
*/
public function isNestedObject(Node $expression): bool {
$isNestedObject = $expression->hasNode('node') && $expression->getNode('node') instanceof GetAttrExpression;

return $isNestedObject;
return $expression->hasNode('node') && $expression->getNode('node') instanceof GetAttrExpression;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions test/Twig/Extension/TerrificExtensionTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

namespace Namics\Test\Terrific\Twig\Extension;

use Namics\Terrific\Twig\TokenParser\ComponentTokenParser;
Expand Down
Loading

0 comments on commit f30c2b8

Please sign in to comment.