Skip to content

Commit e737c1a

Browse files
authored
Merge pull request #627 from FriendsOfSymfony/final-polish
constructor argument promotion and cleanup test type declarations
2 parents 3f6fe79 + e726e95 commit e737c1a

File tree

55 files changed

+204
-328
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+204
-328
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ Changelog
1010
Remove all configuration you have at `fos_http_cache.tags.annotations`
1111
* Make `fastly` and `cloudflare` clients lazy loaded to support Symfony secrets that are only available at runtime, but
1212
not yet when the container is built.
13-
* Removed deprecated `FOS\HttpCacheBundle\UserContext\AnonymousRequestMatcher`, use the
14-
`FOS\HttpCache\UserContext\AnonymousRequestMatcher` class.
15-
* Removed deprecated `ContextInvalidationLogoutHandler`, use `ContextInvalidationSessionLogoutHandler` instead.
13+
* Removed deprecated `FOS\HttpCacheBundle\UserContext\AnonymousRequestMatcher`, use the class from the `FOSHttpCache`
14+
component instead: `FOS\HttpCache\UserContext\AnonymousRequestMatcher`.
15+
* Removed obsolete `ContextInvalidationLogoutHandler`, use `ContextInvalidationSessionLogoutHandler` instead.
1616
* Fixed service loading to not fail when Twig is not available.
1717

1818
2.x

src/CacheManager.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,16 @@
2424
*/
2525
class CacheManager extends CacheInvalidator
2626
{
27-
private ProxyClient $cache;
28-
29-
private UrlGeneratorInterface $urlGenerator;
30-
3127
/**
3228
* What type of urls to generate.
3329
*/
3430
private int $generateUrlType = UrlGeneratorInterface::ABSOLUTE_PATH;
3531

36-
/**
37-
* Constructor.
38-
*
39-
* @param ProxyClient $cache HTTP cache proxy client
40-
* @param UrlGeneratorInterface $urlGenerator Symfony route generator
41-
*/
42-
public function __construct(ProxyClient $cache, UrlGeneratorInterface $urlGenerator)
43-
{
32+
public function __construct(
33+
private readonly ProxyClient $cache,
34+
private readonly UrlGeneratorInterface $urlGenerator
35+
) {
4436
parent::__construct($cache);
45-
$this->cache = $cache;
46-
$this->urlGenerator = $urlGenerator;
4737
}
4838

4939
/**

src/DependencyInjection/Compiler/HashGeneratorPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function process(ContainerBuilder $container): void
4646
}
4747

4848
krsort($prioritisedTags, SORT_NUMERIC);
49-
$prioritisedProviders = call_user_func_array('array_merge', $prioritisedTags);
49+
$prioritisedProviders = array_merge(...$prioritisedTags);
5050

5151
$providers = [];
5252
foreach ($prioritisedProviders as $id) {

src/DependencyInjection/Configuration.php

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
namespace FOS\HttpCacheBundle\DependencyInjection;
1313

14+
use DateTime;
1415
use FOS\HttpCache\ProxyClient\Varnish;
1516
use FOS\HttpCache\SymfonyCache\PurgeListener;
1617
use FOS\HttpCache\SymfonyCache\PurgeTagsListener;
@@ -35,17 +36,9 @@
3536
*/
3637
final class Configuration implements ConfigurationInterface
3738
{
38-
/**
39-
* @var bool
40-
*/
41-
private $debug;
42-
43-
/**
44-
* @param bool $debug Whether to use the debug mode
45-
*/
46-
public function __construct($debug)
47-
{
48-
$this->debug = $debug;
39+
public function __construct(
40+
private readonly bool $debug
41+
) {
4942
}
5043

5144
public function getConfigTreeBuilder(): TreeBuilder
@@ -113,7 +106,7 @@ function ($v) {
113106
}
114107

115108
if (isset($v['proxy_client']['default'])
116-
&& in_array($v['proxy_client']['default'], ['varnish', 'symfony', 'noop'])
109+
&& \in_array($v['proxy_client']['default'], ['varnish', 'symfony', 'noop'])
117110
) {
118111
$v['user_context']['logout_handler']['enabled'] = true;
119112

@@ -325,7 +318,7 @@ private function addCacheControlSection(ArrayNodeDefinition $rootNode): void
325318
->scalarNode('last_modified')
326319
->validate()
327320
->ifTrue(function ($v) {
328-
if (is_string($v)) {
321+
if (\is_string($v)) {
329322
new \DateTime($v);
330323
}
331324

src/EventListener/AttributesListener.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
final class AttributesListener implements EventSubscriberInterface
1919
{
2020
public function __construct(
21-
private ControllerResolverInterface $controllerResolver
21+
private readonly ControllerResolverInterface $controllerResolver
2222
) {
2323
}
2424

src/EventListener/CacheControlListener.php

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,15 @@ final class CacheControlListener implements EventSubscriberInterface
4848
*/
4949
private array $rulesMap = [];
5050

51-
/**
52-
* If not empty, add a debug header with that name to all responses,
53-
* telling the cache proxy to add debug output.
54-
*
55-
* @var string|false Name of the header or false to add no header
56-
*/
57-
private string|false $debugHeader;
58-
59-
/**
60-
* @param string|false $debugHeader Header to set to trigger debugging, or false to send no header
61-
*/
62-
public function __construct(string|false $debugHeader = false)
63-
{
64-
$this->debugHeader = $debugHeader;
51+
public function __construct(
52+
/**
53+
* If not empty, add a debug header with that name to all responses,
54+
* telling the cache proxy to add debug output.
55+
*
56+
* @var string|false Name of the header or false to add no header
57+
*/
58+
private readonly string|false $debugHeader = false
59+
) {
6560
}
6661

6762
public static function getSubscribedEvents(): array

src/EventListener/InvalidationListener.php

Lines changed: 6 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,12 @@
3535
*/
3636
final class InvalidationListener extends AbstractRuleListener implements EventSubscriberInterface
3737
{
38-
private CacheManager $cacheManager;
39-
private UrlGeneratorInterface $urlGenerator;
40-
private ?ExpressionLanguage $expressionLanguage;
41-
private RuleMatcherInterface $mustInvalidateRule;
42-
43-
/**
44-
* Constructor.
45-
*/
4638
public function __construct(
47-
CacheManager $cacheManager,
48-
UrlGeneratorInterface $urlGenerator,
49-
RuleMatcherInterface $mustInvalidateRule,
50-
?ExpressionLanguage $expressionLanguage = null
39+
private readonly CacheManager $cacheManager,
40+
private readonly UrlGeneratorInterface $urlGenerator,
41+
private readonly RuleMatcherInterface $mustInvalidateRule,
42+
private ?ExpressionLanguage $expressionLanguage = null
5143
) {
52-
$this->cacheManager = $cacheManager;
53-
$this->urlGenerator = $urlGenerator;
54-
$this->mustInvalidateRule = $mustInvalidateRule;
55-
$this->expressionLanguage = $expressionLanguage;
5644
}
5745

5846
/**
@@ -76,7 +64,7 @@ public function onKernelTerminate(TerminateEvent $event): void
7664

7765
try {
7866
$this->cacheManager->flush();
79-
} catch (ExceptionCollection $e) {
67+
} catch (ExceptionCollection) {
8068
// swallow exception
8169
// there is the fos_http_cache.event_listener.log to log them
8270
}
@@ -89,7 +77,7 @@ public function onKernelException(): void
8977
{
9078
try {
9179
$this->cacheManager->flush();
92-
} catch (ExceptionCollection $e) {
80+
} catch (ExceptionCollection) {
9381
// swallow exception
9482
// there is the fos_http_cache.event_listener.log to log them
9583
}

src/EventListener/SwitchUserListener.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,9 @@
1818

1919
final class SwitchUserListener implements EventSubscriberInterface
2020
{
21-
private UserContextInvalidator $invalidator;
22-
23-
public function __construct(UserContextInvalidator $invalidator)
24-
{
25-
$this->invalidator = $invalidator;
21+
public function __construct(
22+
private readonly UserContextInvalidator $invalidator
23+
) {
2624
}
2725

2826
public static function getSubscribedEvents(): array

src/EventListener/TagListener.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,13 @@
2929
*/
3030
final class TagListener extends AbstractRuleListener implements EventSubscriberInterface
3131
{
32-
private CacheManager $cacheManager;
33-
private SymfonyResponseTagger $symfonyResponseTagger;
34-
private ?ExpressionLanguage $expressionLanguage;
35-
private RuleMatcherInterface $mustInvalidateRule;
36-
private RuleMatcherInterface $cacheableRule;
37-
38-
/**
39-
* Constructor.
40-
*/
4132
public function __construct(
42-
CacheManager $cacheManager,
43-
SymfonyResponseTagger $tagHandler,
44-
RuleMatcherInterface $cacheableRule,
45-
RuleMatcherInterface $mustInvalidateRule,
46-
?ExpressionLanguage $expressionLanguage = null
33+
private readonly CacheManager $cacheManager,
34+
private readonly SymfonyResponseTagger $symfonyResponseTagger,
35+
private readonly RuleMatcherInterface $cacheableRule,
36+
private readonly RuleMatcherInterface $mustInvalidateRule,
37+
private ?ExpressionLanguage $expressionLanguage = null
4738
) {
48-
$this->cacheManager = $cacheManager;
49-
$this->symfonyResponseTagger = $tagHandler;
50-
$this->cacheableRule = $cacheableRule;
51-
$this->mustInvalidateRule = $mustInvalidateRule;
52-
$this->expressionLanguage = $expressionLanguage;
5339
}
5440

5541
/**
@@ -115,19 +101,19 @@ private function getAttributeTags(Request $request): array
115101
return [];
116102
}
117103

118-
$tags = [];
104+
$tagArrays = [];
119105
foreach ($tagConfigurations as $tagConfiguration) {
120106
if (null !== $tagConfiguration->getExpression()) {
121-
$tags[] = $this->evaluateTag(
107+
$tagArrays[] = [$this->evaluateTag(
122108
$tagConfiguration->getExpression(),
123109
$request
124-
);
110+
)];
125111
} else {
126-
$tags = array_merge($tags, $tagConfiguration->getTags());
112+
$tagArrays[] = $tagConfiguration->getTags();
127113
}
128114
}
129115

130-
return $tags;
116+
return array_merge(...$tagArrays);
131117
}
132118

133119
/**

src/EventListener/UserContextListener.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -36,45 +36,30 @@
3636
*/
3737
final class UserContextListener implements EventSubscriberInterface
3838
{
39-
private RequestMatcherInterface $requestMatcher;
40-
private HashGenerator $hashGenerator;
41-
42-
/**
43-
* If the response tagger is set, the hash lookup response is tagged with the session id for later invalidation.
44-
*/
45-
private ?ResponseTagger $responseTagger;
46-
4739
private array $options;
4840

49-
/**
50-
* Whether the application has a session listener and therefore could
51-
* require the AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER.
52-
*/
53-
private bool $hasSessionListener;
54-
5541
private bool $wasAnonymous = false;
5642

57-
/**
58-
* Used to exclude anonymous requests (no authentication nor session) from user hash sanity check.
59-
* It prevents issues when the hash generator that is used returns a customized value for anonymous users,
60-
* that differs from the documented, hardcoded one.
61-
*/
62-
private ?RequestMatcherInterface $anonymousRequestMatcher;
63-
6443
public function __construct(
65-
RequestMatcherInterface $requestMatcher,
66-
HashGenerator $hashGenerator,
67-
?RequestMatcherInterface $anonymousRequestMatcher = null,
68-
?ResponseTagger $responseTagger = null,
44+
private readonly RequestMatcherInterface $requestMatcher,
45+
private readonly HashGenerator $hashGenerator,
46+
/**
47+
* Used to exclude anonymous requests (no authentication nor session) from user hash sanity check.
48+
* It prevents issues when the hash generator that is used returns a customized value for anonymous users,
49+
* that differs from the documented, hardcoded one.
50+
*/
51+
private readonly ?RequestMatcherInterface $anonymousRequestMatcher = null,
52+
/**
53+
* If the response tagger is set, the hash lookup response is tagged with the session id for later invalidation.
54+
*/
55+
private readonly ?ResponseTagger $responseTagger = null,
6956
array $options = [],
70-
bool $hasSessionListener = true
57+
/**
58+
* Whether the application has a session listener and therefore could
59+
* require the AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER.
60+
*/
61+
private readonly bool $hasSessionListener = true
7162
) {
72-
$this->requestMatcher = $requestMatcher;
73-
$this->hashGenerator = $hashGenerator;
74-
$this->anonymousRequestMatcher = $anonymousRequestMatcher;
75-
$this->responseTagger = $responseTagger;
76-
$this->hasSessionListener = $hasSessionListener;
77-
7863
$resolver = new OptionsResolver();
7964
$resolver->setDefaults([
8065
'user_identifier_headers' => ['Cookie', 'Authorization'],

0 commit comments

Comments
 (0)