Merge branch '5.1'

* 5.1:
  [FrameworkBundle] Fix MicroKernelTrait for php 8
  [DependencyInjection] Fixed tests for wither with static return type.
  [HttpFoundation] Avoid TypeError when calling \SessionHandlerInterface::gc().
  Don't call method_exists() with non-objects.
  [Lock] skip tests when ext-mongo is not installed
  [HttpClient] Adjust AmpResponse to the stricter trait handling in php 8.
  [FrameworkBundle] don't use abstract methods in MicroKernelTrait, their semantics changed in PHP 8
This commit is contained in:
Nicolas Grekas 2020-05-25 00:00:42 +02:00
commit 99ae043165
11 changed files with 161 additions and 42 deletions

View File

@ -31,8 +31,6 @@ matrix:
- php: nightly - php: nightly
services: [memcached] services: [memcached]
fast_finish: true fast_finish: true
allow_failures:
- php: nightly
cache: cache:
directories: directories:

View File

@ -36,7 +36,7 @@ class AddDebugLogProcessorPass implements CompilerPassInterface
public static function configureLogger($logger) public static function configureLogger($logger)
{ {
if (method_exists($logger, 'removeDebugLogger') && \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) { if (\is_object($logger) && method_exists($logger, 'removeDebugLogger') && \in_array(\PHP_SAPI, ['cli', 'phpdbg'], true)) {
$logger->removeDebugLogger(); $logger->removeDebugLogger();
} }
} }

View File

@ -27,6 +27,9 @@ use Symfony\Component\Routing\RouteCollectionBuilder;
* *
* @author Ryan Weaver <ryan@knpuniversity.com> * @author Ryan Weaver <ryan@knpuniversity.com>
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*
* @method void configureRoutes(RoutingConfigurator $routes)
* @method void configureContainer(ContainerConfigurator $c)
*/ */
trait MicroKernelTrait trait MicroKernelTrait
{ {
@ -39,7 +42,7 @@ trait MicroKernelTrait
* ->controller('App\Controller\AdminController::dashboard') * ->controller('App\Controller\AdminController::dashboard')
* ; * ;
*/ */
abstract protected function configureRoutes(RoutingConfigurator $routes); //abstract protected function configureRoutes(RoutingConfigurator $routes): void;
/** /**
* Configures the container. * Configures the container.
@ -58,7 +61,7 @@ trait MicroKernelTrait
* *
* $c->parameters()->set('halloween', 'lot of fun'); * $c->parameters()->set('halloween', 'lot of fun');
*/ */
abstract protected function configureContainer(ContainerConfigurator $c); //abstract protected function configureContainer(ContainerConfigurator $c): void;
/** /**
* {@inheritdoc} * {@inheritdoc}
@ -87,8 +90,10 @@ trait MicroKernelTrait
], ],
]); ]);
$kernelClass = false !== strpos(static::class, "@anonymous\0") ? parent::class : static::class;
if (!$container->hasDefinition('kernel')) { if (!$container->hasDefinition('kernel')) {
$container->register('kernel', static::class) $container->register('kernel', $kernelClass)
->addTag('controller.service_arguments') ->addTag('controller.service_arguments')
->setAutoconfigured(true) ->setAutoconfigured(true)
->setSynthetic(true) ->setSynthetic(true)
@ -103,20 +108,22 @@ trait MicroKernelTrait
$container->fileExists($this->getProjectDir().'/config/bundles.php'); $container->fileExists($this->getProjectDir().'/config/bundles.php');
try { try {
$configureContainer = new \ReflectionMethod($this, 'configureContainer');
} catch (\ReflectionException $e) {
throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureContainer(ContainerConfigurator $c): void".', get_debug_type($this), MicroKernelTrait::class), 0, $e);
}
$configuratorClass = $configureContainer->getNumberOfParameters() > 0 && ($type = $configureContainer->getParameters()[0]->getType()) && !$type->isBuiltin() ? $type->getName() : null;
if ($configuratorClass && !is_a(ContainerConfigurator::class, $configuratorClass, true)) {
$this->configureContainer($container, $loader); $this->configureContainer($container, $loader);
return; return;
} catch (\TypeError $e) {
$file = $e->getFile();
if (0 !== strpos($e->getMessage(), sprintf('Argument 1 passed to %s::configureContainer() must be an instance of %s,', static::class, ContainerConfigurator::class))) {
throw $e;
}
} }
// the user has opted into using the ContainerConfigurator // the user has opted into using the ContainerConfigurator
/* @var ContainerPhpFileLoader $kernelLoader */ /* @var ContainerPhpFileLoader $kernelLoader */
$kernelLoader = $loader->getResolver()->resolve($file); $kernelLoader = $loader->getResolver()->resolve($file = $configureContainer->getFileName());
$kernelLoader->setCurrentDir(\dirname($file)); $kernelLoader->setCurrentDir(\dirname($file));
$instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)(); $instanceof = &\Closure::bind(function &() { return $this->instanceof; }, $kernelLoader, $kernelLoader)();
@ -133,12 +140,14 @@ trait MicroKernelTrait
AbstractConfigurator::$valuePreProcessor = $valuePreProcessor; AbstractConfigurator::$valuePreProcessor = $valuePreProcessor;
} }
$container->setAlias(static::class, 'kernel')->setPublic(true); $container->setAlias($kernelClass, 'kernel')->setPublic(true);
}); });
} }
/** /**
* @internal * @internal
*
* @return RouteCollection
*/ */
public function loadRoutes(LoaderInterface $loader) public function loadRoutes(LoaderInterface $loader)
{ {
@ -149,28 +158,32 @@ trait MicroKernelTrait
$collection = new RouteCollection(); $collection = new RouteCollection();
try { try {
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file)); $configureRoutes = new \ReflectionMethod($this, 'configureRoutes');
} catch (\ReflectionException $e) {
throw new \LogicException(sprintf('"%s" uses "%s", but does not implement the required method "protected function configureRoutes(RoutingConfigurator $routes): void".', get_debug_type($this), MicroKernelTrait::class), 0, $e);
}
foreach ($collection as $route) { $configuratorClass = $configureRoutes->getNumberOfParameters() > 0 && ($type = $configureRoutes->getParameters()[0]->getType()) && !$type->isBuiltin() ? $type->getName() : null;
$controller = $route->getDefault('_controller');
if (\is_array($controller) && [0, 1] === array_keys($controller) && $this === $controller[0]) { if ($configuratorClass && !is_a(RoutingConfigurator::class, $configuratorClass, true)) {
$route->setDefault('_controller', ['kernel', $controller[1]]); trigger_deprecation('symfony/framework-bundle', '5.1', 'Using type "%s" for argument 1 of method "%s:configureRoutes()" is deprecated, use "%s" instead.', RouteCollectionBuilder::class, self::class, RoutingConfigurator::class);
}
}
return $collection; $routes = new RouteCollectionBuilder($loader);
} catch (\TypeError $e) { $this->configureRoutes($routes);
if (0 !== strpos($e->getMessage(), sprintf('Argument 1 passed to %s::configureRoutes() must be an instance of %s,', static::class, RouteCollectionBuilder::class))) {
throw $e; return $routes->build();
}
$this->configureRoutes(new RoutingConfigurator($collection, $kernelLoader, $file, $file));
foreach ($collection as $route) {
$controller = $route->getDefault('_controller');
if (\is_array($controller) && [0, 1] === array_keys($controller) && $this === $controller[0]) {
$route->setDefault('_controller', ['kernel', $controller[1]]);
} }
} }
trigger_deprecation('symfony/framework-bundle', '5.1', 'Using type "%s" for argument 1 of method "%s:configureRoutes()" is deprecated, use "%s" instead.', RouteCollectionBuilder::class, self::class, RoutingConfigurator::class); return $collection;
$routes = new RouteCollectionBuilder($loader);
$this->configureRoutes($routes);
return $routes->build();
} }
} }

View File

@ -12,10 +12,18 @@
namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel; namespace Symfony\Bundle\FrameworkBundle\Tests\Kernel;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Bundle\FrameworkBundle\FrameworkBundle;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; use Symfony\Component\DependencyInjection\Extension\ExtensionInterface;
use Symfony\Component\DependencyInjection\Loader\ClosureLoader; use Symfony\Component\DependencyInjection\Loader\ClosureLoader;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
require_once __DIR__.'/flex-style/src/FlexStyleMicroKernel.php'; require_once __DIR__.'/flex-style/src/FlexStyleMicroKernel.php';
@ -77,4 +85,93 @@ class MicroKernelTraitTest extends TestCase
self::assertSame('$ecret', $kernel->getContainer()->getParameter('kernel.secret')); self::assertSame('$ecret', $kernel->getContainer()->getParameter('kernel.secret'));
} }
public function testAnonymousMicroKernel()
{
$kernel = new class('anonymous_kernel') extends MinimalKernel {
public function helloAction(): Response
{
return new Response('Hello World!');
}
protected function configureContainer(ContainerConfigurator $c): void
{
$c->extension('framework', [
'router' => ['utf8' => true],
]);
$c->services()->set('logger', NullLogger::class);
}
protected function configureRoutes(RoutingConfigurator $routes): void
{
$routes->add('hello', '/')->controller([$this, 'helloAction']);
}
};
$request = Request::create('/');
$response = $kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, false);
$this->assertSame('Hello World!', $response->getContent());
}
public function testMissingConfigureContainer()
{
$kernel = new class('missing_configure_container') extends MinimalKernel {
protected function configureRoutes(RoutingConfigurator $routes): void
{
}
};
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('"Symfony\Bundle\FrameworkBundle\Tests\Kernel\MinimalKernel@anonymous" uses "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait", but does not implement the required method "protected function configureContainer(ContainerConfigurator $c): void".');
$kernel->boot();
}
public function testMissingConfigureRoutes()
{
$kernel = new class('missing_configure_routes') extends MinimalKernel {
protected function configureContainer(ContainerConfigurator $c): void
{
$c->extension('framework', [
'router' => ['utf8' => true],
]);
}
};
$this->expectException(\LogicException::class);
$this->expectExceptionMessage('"Symfony\Bundle\FrameworkBundle\Tests\Kernel\MinimalKernel@anonymous" uses "Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait", but does not implement the required method "protected function configureRoutes(RoutingConfigurator $routes): void".');
$request = Request::create('/');
$kernel->handle($request, HttpKernelInterface::MASTER_REQUEST, false);
}
}
abstract class MinimalKernel extends Kernel
{
use MicroKernelTrait;
private $cacheDir;
public function __construct(string $cacheDir)
{
parent::__construct('test', false);
$this->cacheDir = sys_get_temp_dir().'/'.$cacheDir;
}
public function registerBundles(): iterable
{
yield new FrameworkBundle();
}
public function getCacheDir(): string
{
return $this->cacheDir;
}
public function getLogDir(): string
{
return $this->cacheDir;
}
} }

View File

@ -2,6 +2,8 @@
namespace Symfony\Component\DependencyInjection\Tests\Fixtures; namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
use Symfony\Component\DependencyInjection\Tests\Compiler\Foo;
class WitherStaticReturnType class WitherStaticReturnType
{ {
public $foo; public $foo;

View File

@ -48,11 +48,11 @@ class Symfony_DI_PhpDumper_Service_WitherStaticReturnType extends Container
/** /**
* Gets the public 'wither' shared autowired service. * Gets the public 'wither' shared autowired service.
* *
* @return \Symfony\Component\DependencyInjection\Tests\Compiler\WitherStaticReturnType * @return \Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType
*/ */
protected function getWitherService() protected function getWitherService()
{ {
$instance = new \Symfony\Component\DependencyInjection\Tests\Compiler\WitherStaticReturnType(); $instance = new \Symfony\Component\DependencyInjection\Tests\Fixtures\WitherStaticReturnType();
$a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo(); $a = new \Symfony\Component\DependencyInjection\Tests\Compiler\Foo();

View File

@ -25,6 +25,7 @@ use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpClient\HttpClientTrait; use Symfony\Component\HttpClient\HttpClientTrait;
use Symfony\Component\HttpClient\Internal\AmpBody; use Symfony\Component\HttpClient\Internal\AmpBody;
use Symfony\Component\HttpClient\Internal\AmpClientState; use Symfony\Component\HttpClient\Internal\AmpClientState;
use Symfony\Component\HttpClient\Internal\ClientState;
use Symfony\Contracts\HttpClient\ResponseInterface; use Symfony\Contracts\HttpClient\ResponseInterface;
/** /**
@ -143,8 +144,10 @@ final class AmpResponse implements ResponseInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @param AmpClientState $multi
*/ */
private static function perform(AmpClientState $multi, array &$responses = null): void private static function perform(ClientState $multi, array &$responses = null): void
{ {
if ($responses) { if ($responses) {
foreach ($responses as $response) { foreach ($responses as $response) {
@ -163,8 +166,10 @@ final class AmpResponse implements ResponseInterface
/** /**
* {@inheritdoc} * {@inheritdoc}
*
* @param AmpClientState $multi
*/ */
private static function select(AmpClientState $multi, float $timeout): int private static function select(ClientState $multi, float $timeout): int
{ {
$selected = 1; $selected = 1;
$delay = Loop::delay(1000 * $timeout, static function () use (&$selected) { $delay = Loop::delay(1000 * $timeout, static function () use (&$selected) {

View File

@ -29,13 +29,13 @@ final class InputBag extends ParameterBag
*/ */
public function get(string $key, $default = null) public function get(string $key, $default = null)
{ {
if (null !== $default && !is_scalar($default) && !method_exists($default, '__toString')) { if (null !== $default && !is_scalar($default) && !(\is_object($default) && method_exists($default, '__toString'))) {
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead.', __METHOD__); trigger_deprecation('symfony/http-foundation', '5.1', 'Passing a non-string value as 2nd argument to "%s()" is deprecated, pass a string or null instead.', __METHOD__);
} }
$value = parent::get($key, $this); $value = parent::get($key, $this);
if (null !== $value && $this !== $value && !is_scalar($value) && !method_exists($value, '__toString')) { if (null !== $value && $this !== $value && !is_scalar($value) && !(\is_object($value) && method_exists($value, '__toString'))) {
trigger_deprecation('symfony/http-foundation', '5.1', 'Retrieving a non-string value from "%s()" is deprecated, and will throw a "%s" exception in Symfony 6.0, use "%s::all()" instead.', __METHOD__, BadRequestException::class, __CLASS__); trigger_deprecation('symfony/http-foundation', '5.1', 'Retrieving a non-string value from "%s()" is deprecated, and will throw a "%s" exception in Symfony 6.0, use "%s::all()" instead.', __METHOD__, BadRequestException::class, __CLASS__);
} }
@ -87,7 +87,7 @@ final class InputBag extends ParameterBag
*/ */
public function set(string $key, $value) public function set(string $key, $value)
{ {
if (!is_scalar($value) && !method_exists($value, '__toString') && !\is_array($value)) { if (!is_scalar($value) && !\is_array($value) && !method_exists($value, '__toString')) {
trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string or an array instead.', get_debug_type($value), __METHOD__); trigger_deprecation('symfony/http-foundation', '5.1', 'Passing "%s" as a 2nd Argument to "%s()" is deprecated, pass a string or an array instead.', get_debug_type($value), __METHOD__);
} }

View File

@ -72,9 +72,9 @@ class MarshallingSessionHandlerTest extends TestCase
$marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller); $marshallingSessionHandler = new MarshallingSessionHandler($this->handler, $this->marshaller);
$this->handler->expects($this->once())->method('gc') $this->handler->expects($this->once())->method('gc')
->with('maxlifetime')->willReturn(true); ->with(4711)->willReturn(true);
$marshallingSessionHandler->gc('maxlifetime'); $marshallingSessionHandler->gc(4711);
} }
public function testRead() public function testRead()

View File

@ -22,7 +22,7 @@ use Symfony\Component\Lock\Store\MongoDbStore;
/** /**
* @author Joe Bennett <joe@assimtech.com> * @author Joe Bennett <joe@assimtech.com>
* *
* @requires function \MongoDB\Client::__construct * @requires extension mongodb
* @group integration * @group integration
*/ */
class MongoDbStoreTest extends AbstractStoreTest class MongoDbStoreTest extends AbstractStoreTest
@ -31,6 +31,10 @@ class MongoDbStoreTest extends AbstractStoreTest
public static function setupBeforeClass(): void public static function setupBeforeClass(): void
{ {
if (!class_exists(\MongoDB\Client::class)) {
$this->markTestSkipped('The mongodb/mongodb package is required.');
}
$client = self::getMongoClient(); $client = self::getMongoClient();
try { try {
$client->listDatabases(); $client->listDatabases();

View File

@ -47,7 +47,7 @@ class DoctrineTransportFactory implements TransportFactoryInterface
throw new TransportException(sprintf('Could not find Doctrine connection from Messenger DSN "%s".', $dsn), 0, $e); throw new TransportException(sprintf('Could not find Doctrine connection from Messenger DSN "%s".', $dsn), 0, $e);
} }
if ($useNotify && method_exists($driverConnection->getWrappedConnection(), 'pgsqlGetNotify')) { if ($useNotify && ($wrappedConnection = $driverConnection->getWrappedConnection()) && method_exists($wrappedConnection, 'pgsqlGetNotify')) {
$connection = new PostgreSqlConnection($configuration, $driverConnection); $connection = new PostgreSqlConnection($configuration, $driverConnection);
} else { } else {
$connection = new Connection($configuration, $driverConnection); $connection = new Connection($configuration, $driverConnection);