8000 Merge branch '5.4' into 6.0 · marphi/symfony@34f2413 · GitHub
[go: up one dir, main page]

Skip to content

Commit 34f2413

Browse files
Merge branch '5.4' into 6.0
* 5.4: [Notifier] composer.json cleanup [HttpKernel] AbstractSessionListener should not override the cache lifetime for private responses [DependencyInjection] Shared private services becomes public after a public service is accessed [DI] Fix undefined class in test
2 parents a34f99d + 5069489 commit 34f2413

File tree

15 files changed

+117
-66
lines changed

15 files changed

+117
-66
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -505,8 +505,8 @@ public function has(string $id): bool
505505
*/
506506
public function get(string $id, int $invalidBehavior = ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE): ?object
507507
{
508-
if ($this->isCompiled() && isset($this->removedIds[$id]) && ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior) {
509-
return parent::get($id);
508+
if ($this->isCompiled() && isset($this->removedIds[$id])) {
509+
return ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE >= $invalidBehavior ? parent::get($id) : null;
510510
}
511511

512512
return $this->doGet($id, $invalidBehavior);
@@ -523,9 +523,9 @@ private function doGet(string $id, int $invalidBehavior = ContainerInterface::EX
523523
}
524524
try {
525525
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
526-
return parent::get($id, $invalidBehavior);
526+
return $this->privates[$id] ?? parent::get($id, $invalidBehavior);
527527
}
528-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
528+
if (null !== $service = $this->privates[$id] ?? parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
529529
return $service;
530530
}
531531
} catch (ServiceCircularReferenceException $e) {
@@ -1018,8 +1018,8 @@ private function createService(Definition $definition, array &$inlineServices, b
10181018
}
10191019
}
10201020

1021-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && ($tryProxy || !$definition->isLazy())) {
1022-
return $this->services[$id];
1021+
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && ($tryProxy || !$definition->isLazy())) {
1022+
return $this->services[$id] ?? $this->privates[$id];
10231023
}
10241024

10251025
if (null !== $factory) {
@@ -1575,7 +1575,11 @@ private function shareService(Definition $definition, mixed $service, ?string $i
15751575
$inlineServices[$id ?? spl_object_hash($definition)] = $service;
15761576

15771577
if (null !== $id && $definition->isShared()) {
1578-
$this->services[$id] = $service;
1578+
if ($definition->isPrivate() && $this->isCompiled()) {
1579+
$this->privates[$id] = $service;
1580+
} else {
1581+
$this->services[$id] = $service;
1582+
}
15791583
unset($this->loading[$id]);
15801584
}
15811585
}

src/Symfony/Component/DependencyInjection/ReverseContainer.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,6 @@ public function getId(object $service): ?string
6363
*/
6464
public function getService(string $id): object
6565
{
66-
if ($this->serviceContainer->has($id)) {
67-
return $this->serviceContainer->get($id);
68-
}
69-
7066
if ($this->reversibleLocator->has($id)) {
7167
return $this->reversibleLocator->get($id);
7268
}
@@ -75,7 +71,6 @@ public function getService(string $id): object
7571
throw new ServiceNotFoundException($id, null, null, [], sprintf('The "%s" service is private and cannot be accessed by reference. You should either make it public, or tag it as "%s".', $id, $this->tagName));
7672
}
7773

78-
// will throw a ServiceNotFoundException
79-
$this->serviceContainer->get($id);
74+
return $this->serviceContainer->get($id);
8075
}
8176
}

src/Symfony/Component/DependencyInjection/Tests/Compiler/ServiceLocatorTagPassTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,19 +184,19 @@ public function testIndexedByServiceIdWithDecoration()
184184

185185
$container->setDefinition(Service::class, $service);
186186

187-
$decorated = new Definition(Decorated::class);
187+
$decorated = new Definition(DecoratedService::class);
188188
$decorated->setPublic(true);
189189
$decorated->setDecoratedService(Service::class);
190190

191-
$container->setDefinition(Decorated::class, $decorated);
191+
$container->setDefinition(DecoratedService::class, $decorated);
192192

193193
$container->compile();
194194

195195
/** @var ServiceLocator $locator */
196196
$locator = $container->get(Locator::class)->locator;
197197
static::assertTrue($locator->has(Service::class));
198-
static::assertFalse($locator->has(Decorated::class));
199-
static::assertInstanceOf(Decorated::class, $locator->get(Service::class));
198+
static::assertFalse($locator->has(DecoratedService::class));
199+
static::assertInstanceOf(DecoratedService::class, $locator->get(Service::class));
200200
}
201201

202202
public function testDefinitionOrderIsTheSame()

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1066,10 +1066,19 @@ public function testPrivateServiceUser()
10661066
$container->addDefinitions([
10671067
'bar' => $fooDefinition,
10681068
'bar_user' => $fooUserDefinition->setPublic(true),
1069+
'bar_user2' => $fooUserDefinition->setPublic(true),
10691070
]);
10701071

10711072
$container->compile();
1073+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
10721074
$this->assertInstanceOf(\BarClass::class, $container->get('bar_user')->bar);
1075+
1076+
// Ensure that accessing a public service with a shared private service
1077+
// does not make the private service available.
1078+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
1079+
1080+
// Ensure the private service is still shared.
1081+
$this->assertSame($container->get('bar_user')->bar, $container->get('bar_user2')->bar);
10731082
}
10741083

10751084
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()

src/Symfony/Component/HttpKernel/EventListener/AbstractSessionListener.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,10 +195,11 @@ public function onKernelResponse(ResponseEvent $event)
195195
}
196196

197197
if ($autoCacheControl) {
198+
$maxAge = $response->headers->hasCacheControlDirective('public') ? 0 : (int) $response->getMaxAge();
198199
$response
199-
->setExpires(new \DateTime())
200+
->setExpires(new \DateTimeImmutable('+'.$maxAge.' seconds'))
200201
->setPrivate()
201-
->setMaxAge(0)
202+
->setMaxAge($maxAge)
202203
->headers->addCacheControlDirective('must-revalidate');
203204
}
204205

src/Symfony/Component/HttpKernel/Tests/EventListener/SessionListenerTest.php

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class SessionListenerTest extends TestCase
3838
{
3939
/**
4040
* @dataProvider provideSessionOptions
41+
*
4142
* @runInSeparateProcess
4243
*/
4344
public function testSessionCookieOptions(array $phpSessionOptions, array $sessionOptions, array $expectedSessionOptions)
@@ -556,6 +557,64 @@ public function testUninitializedSessionWithoutInitializedSession()
556557
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
557558
}
558559

560+
public function testResponseHeadersMaxAgeAndExpiresNotBeOverridenIfSessionStarted()
561+
{
562+
$session = $this->createMock(Session::class);
563+
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
564+
565+
$container = new Container();
566+
$container->set('initialized_session', $session);
567+
568+
$listener = new SessionListener($container);
569+
$kernel = $this->createMock(HttpKernelInterface::class);
570+
571+
$request = new Request();
572+
$listener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
573+
574+
$response = new Response();
575+
$response->setPrivate();
576+
$expiresHeader = gmdate('D, d M Y H:i:s', time() + 600).' GMT';
577+
$response->setMaxAge(600);
578+
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
579+
580+
$this->assertTrue($response->headers->has('expires'));
581+
$this->assertSame($expiresHeader, $response->headers->get('expires'));
582+
$this->assertFalse($response->headers->has('max-age'));
583+
$this->assertSame('600', $response->headers->getCacheControlDirective('max-age'));
584+
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
585+
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
586+
$this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate'));
587+
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
588+
}
589+
590+
public function testResponseHeadersMaxAgeAndExpiresDefaultValuesIfSessionStarted()
591+
{
592+
$session = $this->createMock(Session::class);
593+
$session->expects($this->exactly(2))->method('getUsageIndex')->will($this->onConsecutiveCalls(0, 1));
594+
595+
$container = new Container();
596+
$container->set('initialized_session', $session);
597+
598+
$listener = new SessionListener($container);
599+
$kernel = $this->createMock(HttpKernelInterface::class);
600+
601+
$request = new Request();
602+
$l 10000 istener->onKernelRequest(new RequestEvent($kernel, $request, HttpKernelInterface::MAIN_REQUEST));
603+
604+
$response = new Response();
605+
$expiresHeader = gmdate('D, d M Y H:i:s', time()).' GMT';
606+
$listener->onKernelResponse(new ResponseEvent($kernel, new Request(), HttpKernelInterface::MAIN_REQUEST, $response));
607+
608+
$this->assertTrue($response->headers->has('expires'));
609+
$this->assertSame($expiresHeader, $response->headers->get('expires'));
610+
$this->assertFalse($response->headers->has('max-age'));
611+
$this->assertSame('0', $response->headers->getCacheControlDirective('max-age'));
612+
$this->assertFalse($response->headers->hasCacheControlDirective('public'));
613+
$this->assertTrue($response->headers->hasCacheControlDirective('private'));
614+
$this->assertTrue($response->headers->hasCacheControlDirective('must-revalidate'));
615+
$this->assertFalse($response->headers->has(AbstractSessionListener::NO_AUTO_CACHE_CONTROL_HEADER));
616+
}
617+
559618
public function testSurrogateMainRequestIsPublic()
560619
{
561620
$session = $this->createMock(Session::class);

src/Symfony/Component/Notifier/Bridge/Clickatell/composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
"symfony/http-client": "^5.4|^6.0",
2525
"symfony/notifier": "^5.4|^6.0"
2626
},
27-
"require-dev": {
28-
"symfony/event-dispatcher": "^5.4|^6.0"
29-
},
3027
"autoload": {
3128
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Clickatell\\": "" },
3229
"exclude-from-classmap": [

src/Symfony/Component/Notifier/Bridge/Discord/composer.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,6 @@
2121
"symfony/notifier": "^5.4|^6.0",
2222
"symfony/polyfill-mbstring": "^1.0"
2323
},
24-
"require-dev": {
25-
"symfony/event-dispatcher": "^5.4|^6.0"
26-
},
2724
"autoload": {
2825
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\Discord\\": "" },
2926
"exclude-from-classmap": [

src/Symfony/Component/Notifier/Bridge/FakeChat/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"php": ">=8.0.2",
2525
"symfony/http-client": "^5.4|^6.0",
2626
"symfony/notifier": "^5.4|^6.0",
27-
"symfony/event-dispatcher-contracts": "^2|^3",
2827
"symfony/mailer": "^5.4|^6.0"
2928
},
3029
"autoload": {

src/Symfony/Component/Notifier/Bridge/FakeSms/composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
"php": ">=8.0.2",
2525
"symfony/http-client": "^5.4|^6.0",
2626
"symfony/notifier": "^5.4|^6.0",
27-
"symfony/event-dispatcher-contracts": "^2|^3",
2827
"symfony/mailer": "^5.4|^6.0"
2928
},
3029
"autoload": {

0 commit comments

Comments
 (0)
0