8000 Merge branch '6.2' into 6.3 · symfony/symfony@ad3424c · GitHub
[go: up one dir, main page]

Skip to content

Commit ad3424c

Browse files
Merge branch '6.2' into 6.3
* 6.2: [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 eed0ae4 + 6a3d23b commit ad3424c

File tree

18 files changed

+153
-102
lines changed

18 files changed

+153
-102
lines changed

src/Symfony/Component/DependencyInjection/ContainerBuilder.php

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

513513
return $this->doGet($id, $invalidBehavior);
@@ -524,9 +524,9 @@ private function doGet(string $id, int $invalidBehavior = ContainerInterface::EX
524524
}
525525
try {
526526
if (ContainerInterface::IGNORE_ON_UNINITIALIZED_REFERENCE === $invalidBehavior) {
527-
return parent::get($id, $invalidBehavior);
527+
return $this->privates[$id] ?? parent::get($id, $invalidBehavior);
528528
}
529-
if ($service = parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
529+
if (null !== $service = $this->privates[$id] ?? parent::get($id, ContainerInterface::NULL_ON_INVALID_REFERENCE)) {
530530
return $service;
531531
}
532532
} catch (ServiceCircularReferenceException $e) {
@@ -1029,8 +1029,8 @@ private function createService(Definition $definition, array &$inlineServices, b
10291029

10301030
$arguments = $this->doResolveServices($parameterBag->unescapeValue($parameterBag->resolveValue($arguments)), $inlineServices, $isConstructorArgument);
10311031

1032-
if (null !== $id && $definition->isShared() && isset($this->services[$id]) && (true === $tryProxy || !$definition->isLazy())) {
1033-
return $this->services[$id];
1032+
if (null !== $id && $definition->isShared() && (isset($this->services[$id]) || isset($this->privates[$id])) && (true === $tryProxy || !$definition->isLazy())) {
1033+
return $this->services[$id] ?? $this->privates[$id];
10341034
}
10351035

10361036
if (null !== $factory) {
@@ -1587,7 +1587,11 @@ private function shareService(Definition $definition, mixed $service, ?string $i
15871587
$inlineServices[$id ?? spl_object_hash($definition)] = $service;
15881588

15891589
if (null !== $id && $definition->isShared()) {
1590-
$this->services[$id] = $service;
1590+
if ($definition->isPrivate() && $this->isCompiled()) {
1591+
$this->privates[$id] = $service;
1592+
} else {
1593+
$this->services[$id] = $service;
1594+
}
15911595
unset($this->loading[$id]);
15921596
}
15931597
}

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
@@ -1127,10 +1127,19 @@ public function testPrivateServiceUser()
11271127
$container->addDefinitions([
11281128
'bar' => $fooDefinition,
11291129
'bar_user' => $fooUserDefinition->setPublic(true),
1130+
'bar_user2' => $fooUserDefinition->setPublic(true),
11301131
]);
11311132

11321133
$container->compile();
1134+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
11331135
$this->assertInstanceOf(\BarClass::class, $container->get('bar_user')->bar);
1136+
1137+
// Ensure that accessing a public service with a shared private service
1138+
// does not make the private service available.
1139+
$this->assertNull($container->get('bar', $container::NULL_ON_INVALID_REFERENCE));
1140+
1141+
// Ensure the private service is still shared.
1142+
$this->assertSame($container->get('bar_user')->bar, $container->get('bar_user2')->bar);
11341143
}
11351144

11361145
public function testThrowsExceptionWhenSetServiceOnACompiledContainer()

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

Lines changed: 3 additions & 2 deletions
4473
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 \DateTimeImmutable())
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)
@@ -554,6 +555,64 @@ public function testUninitializedSessionWithoutInitializedSession()
554555
$this->assertSame('60', $response->headers->getCacheControlDirective('s-maxage'));
555556
}
556557

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
}
1717
],
1818
"require": {
19-
"php": ">=7.2.5",
20-
"symfony/http-client": "^4.3|^5.0|^6.0",
19+
"php": ">=8.1",
20+
"symfony/http-client": "^5.4|^6.0",
2121
"symfony/notifier": "^6.2"
2222
},
2323
"autoload": {

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,8 @@
2626
"symfony/notifier": "^6.2"
2727
},
2828
"require-dev": {
29-
"symfony/mailer": "^5.4|^6.0",
30-
"symfony/event-dispatcher": "^5.4|^6.0",
31-
"psr/log": "^1|^2|^3"
29+
"psr/log": "^1|^2|^3",
30+
"symfony/mailer": "^5.4|^6.0"
3231
},
3332
"autoload": {
3433
"psr-4": { "Symfony\\Component\\Notifier\\Bridge\\FakeChat\\": "" },

0 commit comments

Comments
 (0)
0