Skip to content

Commit

Permalink
Merge branch 'release/5.0.1' into v5
Browse files Browse the repository at this point in the history
  • Loading branch information
khalwat committed Aug 6, 2024
2 parents 9773a46 + b531384 commit 662d6d6
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/code-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request: null
push:
branches:
- develop-v4
- develop-v5
workflow_dispatch:
permissions:
contents: read
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Template Comments Changelog

## 5.0.1 - 2024.08.06
### Fixed
* Fixed an issue where Template Comments would cause the Craft Closure `^1.0.6` package to not work

## 5.0.0 - 2024.04.18
### Added
* Stable release for Craft CMS 5
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "nystudio107/craft-templatecomments",
"description": "Adds a HTML comment with performance timings to demarcate `{% block %}`s and each Twig template that is included or extended.",
"type": "craft-plugin",
"version": "5.0.0",
"version": "5.0.1",
"keywords": [
"craftcms",
"craft-plugin",
Expand Down
60 changes: 60 additions & 0 deletions src/helpers/Reflection.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php
/**
* Template Comments plugin for Craft CMS
*
* Adds a HTML comment to demarcate each Twig template that is included or extended.
*
* @link https://nystudio107.com/
* @copyright Copyright (c) nystudio107
*/

namespace nystudio107\templatecomments\helpers;

use ReflectionException;
use ReflectionObject;
use ReflectionProperty;

/**
* @author nystudio107
* @package TemplateComments
* @since 4.0.2
*/
class Reflection
{
// Public static Methods
// =========================================================================
/**
* @param object $object
* @param string $propertyName
*
* @return ReflectionProperty
* @throws ReflectionException
*/
public static function getReflectionProperty(object $object, string $propertyName): ReflectionProperty
{
$reflectionObject = new ReflectionObject($object);

$reflectionProperty = null;

if ($reflectionObject->hasProperty($propertyName)) {
$reflectionProperty = $reflectionObject->getProperty($propertyName);
} else {

// This is needed for private parent properties only.
$parent = $reflectionObject->getParentClass();
while ($reflectionProperty === null && $parent !== false) {
if ($parent->hasProperty($propertyName)) {
$reflectionProperty = $parent->getProperty($propertyName);
}

$parent = $parent->getParentClass();
}
}

if (!$reflectionProperty) {
throw new ReflectionException("Property not found: " . $propertyName);
}

return $reflectionProperty;
}
}
26 changes: 25 additions & 1 deletion src/web/twig/TemplateCommentsParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@

namespace nystudio107\templatecomments\web\twig;

use nystudio107\templatecomments\helpers\Reflection as ReflectionHelper;
use ReflectionException;
use Twig\Environment;
use Twig\Error\SyntaxError;
use Twig\ExpressionParser;
Expand Down Expand Up @@ -85,10 +87,32 @@ class TemplateCommentsParser extends Parser
private $traits;
private $embeddedTemplates = [];
private $varNameSalt = 0;
private $expressionParserClass;

public function __construct(Environment $env)
{
$this->env = $env;
// Get the existing parser object used by the Twig $env
try {
$parserReflection = ReflectionHelper::getReflectionProperty($env, 'parser');
} catch (ReflectionException $e) {
return;
}
$parserReflection->setAccessible(true);
$parser = $parserReflection->getValue($env);
if ($parser === null) {
return;
}
// Get the expression parser used by the current parser
try {
$expressionParserReflection = ReflectionHelper::getReflectionProperty($parser, 'expressionParser');
} catch (ReflectionException $e) {
return;
}
// Preserve the existing expression parser and use it
$expressionParserReflection->setAccessible(true);
$expressionParser = $expressionParserReflection->getValue($parser);
$this->expressionParserClass = get_class($expressionParser);
}

public function getVarName(): string
Expand All @@ -108,7 +132,7 @@ public function parse(TokenStream $stream, $test = null, bool $dropNeedle = fals
}

if (null === $this->expressionParser) {
$this->expressionParser = new ExpressionParser($this, $this->env);
$this->expressionParser = new $this->expressionParserClass($this, $this->env);
}

$this->stream = $stream;
Expand Down

0 comments on commit 662d6d6

Please sign in to comment.