Skip to content

Commit 5d1f628

Browse files
authored
Merge pull request #563 from FriendsOfSymfony/fix-expression-language-dependency
do not fail when expression language is not needed
2 parents 488b9a7 + 7624127 commit 5d1f628

File tree

5 files changed

+45
-6
lines changed

5 files changed

+45
-6
lines changed

.github/workflows/php.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,11 @@ jobs:
5252
if: ${{ matrix.dependencies }}
5353
- name: Symfony version
5454
run: composer require --no-update symfony/flex && composer config extra.symfony.require ${{ matrix.symfony-version}}
55-
if: ${{ matrix.symfony-version }}
55+
if: ${{ matrix.symfony-version }}
5656
- name: Composer update
5757
run: composer update ${{ matrix.composer-flag }} --prefer-dist --no-interaction
5858
- name: Composer validate
5959
run: composer validate --strict --no-check-lock
60+
if: ${{ matrix.stability != 'dev' }}
6061
- name: Run tests
6162
run: php vendor/bin/simple-phpunit -v

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
Changelog
22
=========
33

4+
2.10.3
5+
------
6+
7+
### Fixed
8+
9+
* Do not error in `InvalidationListener` nor `TagListener` when `symfony/expression-language` is missing but no expression is used.
10+
* Properly report missing `symfony/expression-language` when an expression is used in response configuration.
11+
412
2.10.2
513
------
614

src/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,10 @@ private function addCacheableResponseSection(ArrayNodeDefinition $rootNode)
240240
return !empty($v['additional_status']) && !empty($v['expression']);
241241
})
242242
->thenInvalid('You may not set both additional_status and expression.')
243+
->ifTrue(function ($v) {
244+
return !empty($v['expression']) && !class_exists(ExpressionLanguage::class);
245+
})
246+
->thenInvalid('Configured a response.expression but ExpressionLanguage is not available')
243247
->end()
244248
->end()
245249
->end()

src/EventListener/InvalidationListener.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function __construct(
8080
) {
8181
$this->cacheManager = $cacheManager;
8282
$this->urlGenerator = $urlGenerator;
83-
$this->expressionLanguage = $expressionLanguage ?: new ExpressionLanguage();
83+
$this->expressionLanguage = $expressionLanguage;
8484
$this->mustInvalidateRule = $mustInvalidateRule;
8585
}
8686

@@ -217,7 +217,7 @@ private function invalidateRoutes(array $routes, Request $request)
217217
// Iterate over route params and try to evaluate their values
218218
foreach ($route->getParams() as $key => $value) {
219219
if (is_array($value)) {
220-
$value = $this->expressionLanguage->evaluate($value['expression'], $values);
220+
$value = $this->getExpressionLanguage()->evaluate($value['expression'], $values);
221221
}
222222

223223
$params[$key] = $value;
@@ -227,4 +227,17 @@ private function invalidateRoutes(array $routes, Request $request)
227227
$this->cacheManager->invalidateRoute($route->getName(), $params);
228228
}
229229
}
230+
231+
private function getExpressionLanguage(): ExpressionLanguage
232+
{
233+
if (!$this->expressionLanguage) {
234+
// the expression comes from controller annotations, we can't detect whether they use expressions while building the configuration
235+
if (!class_exists(ExpressionLanguage::class)) {
236+
throw new \RuntimeException('Invalidation rules with expressions require '.ExpressionLanguage::class.' to be available.');
237+
}
238+
$this->expressionLanguage = new ExpressionLanguage();
239+
}
240+
241+
return $this->expressionLanguage;
242+
}
230243
}

src/EventListener/TagListener.php

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class TagListener extends AbstractRuleListener implements EventSubscriberInterfa
4848
private $symfonyResponseTagger;
4949

5050
/**
51-
* @var ExpressionLanguage
51+
* @var ExpressionLanguage|null
5252
*/
5353
private $expressionLanguage;
5454

@@ -76,7 +76,7 @@ public function __construct(
7676
$this->symfonyResponseTagger = $tagHandler;
7777
$this->cacheableRule = $cacheableRule;
7878
$this->mustInvalidateRule = $mustInvalidateRule;
79-
$this->expressionLanguage = $expressionLanguage ?: new ExpressionLanguage();
79+
$this->expressionLanguage = $expressionLanguage;
8080
}
8181

8282
/**
@@ -173,6 +173,19 @@ private function evaluateTag($expression, Request $request)
173173
// if there is an attribute called "request", it needs to be accessed through the request.
174174
$values['request'] = $request;
175175

176-
return $this->expressionLanguage->evaluate($expression, $values);
176+
return $this->getExpressionLanguage()->evaluate($expression, $values);
177+
}
178+
179+
private function getExpressionLanguage(): ExpressionLanguage
180+
{
181+
if (!$this->expressionLanguage) {
182+
// the expression comes from controller annotations, we can't detect whether they use expressions while building the configuration
183+
if (!class_exists(ExpressionLanguage::class)) {
184+
throw new \RuntimeException('Using the tag annotation requires the '.ExpressionLanguage::class.' to be available.');
185+
}
186+
$this->expressionLanguage = new ExpressionLanguage();
187+
}
188+
189+
return $this->expressionLanguage;
177190
}
178191
}

0 commit comments

Comments
 (0)