diff --git a/Controller/ArgumentResolver/DateTimeValueResolver.php b/Controller/ArgumentResolver/DateTimeValueResolver.php index d8953f564b..e9a7494a4c 100644 --- a/Controller/ArgumentResolver/DateTimeValueResolver.php +++ b/Controller/ArgumentResolver/DateTimeValueResolver.php @@ -60,8 +60,6 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable $format = $attribute->format; } - $date = false; - if (null !== $format) { $date = $class::createFromFormat($format, $value); @@ -73,7 +71,7 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable $value = '@'.$value; } try { - $date = new $class($value); + $date = new $class($value ?? 'now'); } catch (\Exception) { $date = false; } diff --git a/Controller/ArgumentResolver/NotTaggedControllerValueResolver.php b/Controller/ArgumentResolver/NotTaggedControllerValueResolver.php index 22a4d2ae74..ae2dbbb477 100644 --- a/Controller/ArgumentResolver/NotTaggedControllerValueResolver.php +++ b/Controller/ArgumentResolver/NotTaggedControllerValueResolver.php @@ -69,8 +69,9 @@ public function resolve(Request $request, ArgumentMetadata $argument): iterable } if (!$this->container->has($controller)) { - $i = strrpos($controller, ':'); - $controller = substr($controller, 0, $i).strtolower(substr($controller, $i)); + $controller = (false !== $i = strrpos($controller, ':')) + ? substr($controller, 0, $i).strtolower(substr($controller, $i)) + : $controller.'::__invoke'; } $what = sprintf('argument $%s of "%s()"', $argument->getName(), $controller); diff --git a/DependencyInjection/RegisterControllerArgumentLocatorsPass.php b/DependencyInjection/RegisterControllerArgumentLocatorsPass.php index 2e861f32cf..c0a97c650b 100644 --- a/DependencyInjection/RegisterControllerArgumentLocatorsPass.php +++ b/DependencyInjection/RegisterControllerArgumentLocatorsPass.php @@ -25,6 +25,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\TypedReference; use Symfony\Component\HttpFoundation\Request; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -154,7 +155,7 @@ public function process(ContainerBuilder $container) $invalidBehavior = ContainerInterface::RUNTIME_EXCEPTION_ON_INVALID_REFERENCE; } - if (Request::class === $type || SessionInterface::class === $type) { + if (Request::class === $type || SessionInterface::class === $type || Response::class === $type) { continue; } diff --git a/Kernel.php b/Kernel.php index 78193d31c6..f279be7534 100644 --- a/Kernel.php +++ b/Kernel.php @@ -78,11 +78,11 @@ abstract class Kernel implements KernelInterface, RebootableInterface, Terminabl */ private static array $freshCache = []; - public const VERSION = '6.1.7'; - public const VERSION_ID = 60107; + public const VERSION = '6.1.8'; + public const VERSION_ID = 60108; public const MAJOR_VERSION = 6; public const MINOR_VERSION = 1; - public const RELEASE_VERSION = 7; + public const RELEASE_VERSION = 8; public const EXTRA_VERSION = ''; public const END_OF_MAINTENANCE = '01/2023'; diff --git a/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php b/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php index e1c3d662c6..b438c527a3 100644 --- a/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php +++ b/Tests/Controller/ArgumentResolver/DateTimeValueResolverTest.php @@ -113,6 +113,26 @@ public function testNullableWithEmptyAttribute() $this->assertNull($results[0]); } + /** + * @dataProvider getTimeZones + */ + public function testNow(string $timezone) + { + date_default_timezone_set($timezone); + $resolver = new DateTimeValueResolver(); + + $argument = new ArgumentMetadata('dummy', \DateTime::class, false, false, null, false); + $request = self::requestWithAttributes(['dummy' => null]); + + /** @var \Generator $results */ + $results = $resolver->resolve($request, $argument); + $results = iterator_to_array($results); + + $this->assertCount(1, $results); + $this->assertEquals('0', $results[0]->diff(new \DateTimeImmutable())->format('%s')); + $this->assertSame($timezone, $results[0]->getTimezone()->getName(), 'Default timezone'); + } + public function testPreviouslyConvertedAttribute() { $resolver = new DateTimeValueResolver(); diff --git a/Tests/Controller/ArgumentResolver/NotTaggedControllerValueResolverTest.php b/Tests/Controller/ArgumentResolver/NotTaggedControllerValueResolverTest.php index 3cf2f0f185..4577b5a6d2 100644 --- a/Tests/Controller/ArgumentResolver/NotTaggedControllerValueResolverTest.php +++ b/Tests/Controller/ArgumentResolver/NotTaggedControllerValueResolverTest.php @@ -98,6 +98,17 @@ public function testControllerNameIsAnArray() $resolver->resolve($request, $argument); } + public function testInvokableController() + { + $this->expectException(RuntimeException::class); + $this->expectExceptionMessage('Could not resolve argument $dummy of "App\Controller\Mine::__invoke()", maybe you forgot to register the controller as a service or missed tagging it with the "controller.service_arguments"?'); + $resolver = new NotTaggedControllerValueResolver(new ServiceLocator([])); + $argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null); + $request = $this->requestWithAttributes(['_controller' => 'App\Controller\Mine']); + $this->assertTrue($resolver->supports($request, $argument)); + $resolver->resolve($request, $argument); + } + private function requestWithAttributes(array $attributes) { $request = Request::create('/'); diff --git a/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php b/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php index 85e057439c..302f9e35dd 100644 --- a/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php +++ b/Tests/DependencyInjection/RegisterControllerArgumentLocatorsPassTest.php @@ -24,6 +24,7 @@ use Symfony\Component\DependencyInjection\Reference; use Symfony\Component\DependencyInjection\ServiceLocator; use Symfony\Component\DependencyInjection\TypedReference; +use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpKernel\DependencyInjection\RegisterControllerArgumentLocatorsPass; use Symfony\Component\HttpKernel\Tests\Fixtures\Suit; @@ -443,6 +444,20 @@ public function testBindWithTarget() $this->assertEquals($expected, $locator->getArgument(0)); } + public function testResponseArgumentIsIgnored() + { + $container = new ContainerBuilder(); + $resolver = $container->register('argument_resolver.service', 'stdClass')->addArgument([]); + + $container->register('foo', WithResponseArgument::class) + ->addTag('controller.service_arguments'); + + (new RegisterControllerArgumentLocatorsPass())->process($container); + + $locator = $container->getDefinition((string) $resolver->getArgument(0))->getArgument(0); + $this->assertEmpty(array_keys($locator), 'Response typed argument is ignored'); + } + public function testAutowireAttribute() { if (!class_exists(Autowire::class)) { @@ -558,6 +573,13 @@ public function fooAction( } } +class WithResponseArgument +{ + public function fooAction(Response $response, ?Response $nullableResponse) + { + } +} + class WithAutowireAttribute { public function fooAction(