Skip to content

Commit

Permalink
Drop BC layers for old Symfony versions
Browse files Browse the repository at this point in the history
  • Loading branch information
alcaeus committed Dec 11, 2018
1 parent 4456983 commit 166b85e
Show file tree
Hide file tree
Showing 18 changed files with 62 additions and 203 deletions.
13 changes: 5 additions & 8 deletions DataCollector/DoctrineDataCollector.php
Expand Up @@ -136,14 +136,11 @@ public function collect(Request $request, Response $response, Exception $excepti
}
}

// HttpKernel < 3.2 compatibility layer
if (method_exists($this, 'cloneVar')) {
// Might be good idea to replicate this block in doctrine bridge so we can drop this from here after some time.
// This code is compatible with such change, because cloneVar is supposed to check if input is already cloned.
foreach ($this->data['queries'] as &$queries) {
foreach ($queries as &$query) {
$query['params'] = $this->cloneVar($query['params']);
}
// Might be good idea to replicate this block in doctrine bridge so we can drop this from here after some time.
// This code is compatible with such change, because cloneVar is supposed to check if input is already cloned.
foreach ($this->data['queries'] as &$queries) {
foreach ($queries as &$query) {
$query['params'] = $this->cloneVar($query['params']);
}
}

Expand Down
Expand Up @@ -23,15 +23,6 @@ public function process(ContainerBuilder $container)

$repoServiceIds = array_keys($container->findTaggedServiceIds(self::REPOSITORY_SERVICE_TAG));

// Symfony 3.2 and lower sanity check
if (! class_exists(ServiceLocatorTagPass::class)) {
if (! empty($repoServiceIds)) {
throw new RuntimeException(sprintf('The "%s" tag can only be used with Symfony 3.3 or higher. Remove the tag from the following services (%s) or upgrade to Symfony 3.3 or higher.', self::REPOSITORY_SERVICE_TAG, implode(', ', $repoServiceIds)));
}

return;
}

$repoReferences = array_map(static function ($id) {
return new Reference($id);
}, $repoServiceIds);
Expand Down
50 changes: 13 additions & 37 deletions DependencyInjection/DoctrineExtension.php
Expand Up @@ -25,6 +25,7 @@
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;

/**
Expand Down Expand Up @@ -65,7 +66,7 @@ public function load(array $configs, ContainerBuilder $container)
throw new LogicException('Configuring the ORM layer requires to configure the DBAL layer as well.');
}

if (! class_exists('Doctrine\ORM\Version')) {
if (! class_exists(Version::class)) {
throw new LogicException('To configure the ORM layer, you must first install the doctrine/orm package.');
}

Expand Down Expand Up @@ -123,23 +124,20 @@ protected function dbalLoad(array $config, ContainerBuilder $container)
*/
protected function loadDbalConnection($name, array $connection, ContainerBuilder $container)
{
// configuration
$definitionClassname = $this->getDefinitionClassname();

$configuration = $container->setDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name), new $definitionClassname('doctrine.dbal.connection.configuration'));
$configuration = $container->setDefinition(sprintf('doctrine.dbal.%s_connection.configuration', $name), new ChildDefinition('doctrine.dbal.connection.configuration'));
$logger = null;
if ($connection['logging']) {
$logger = new Reference('doctrine.dbal.logger');
}
unset($connection['logging']);
if ($connection['profiling']) {
$profilingLoggerId = 'doctrine.dbal.logger.profiling.' . $name;
$container->setDefinition($profilingLoggerId, new $definitionClassname('doctrine.dbal.logger.profiling'));
$container->setDefinition($profilingLoggerId, new ChildDefinition('doctrine.dbal.logger.profiling'));
$profilingLogger = new Reference($profilingLoggerId);
$container->getDefinition('data_collector.doctrine')->addMethodCall('addLogger', [$name, $profilingLogger]);

if ($logger !== null) {
$chainLogger = new $definitionClassname('doctrine.dbal.logger.chain');
$chainLogger = new ChildDefinition('doctrine.dbal.logger.chain');
$chainLogger->addMethodCall('addLogger', [$profilingLogger]);

$loggerId = 'doctrine.dbal.logger.chain.' . $name;
Expand Down Expand Up @@ -168,13 +166,13 @@ protected function loadDbalConnection($name, array $connection, ContainerBuilder
}

// event manager
$container->setDefinition(sprintf('doctrine.dbal.%s_connection.event_manager', $name), new $definitionClassname('doctrine.dbal.connection.event_manager'));
$container->setDefinition(sprintf('doctrine.dbal.%s_connection.event_manager', $name), new ChildDefinition('doctrine.dbal.connection.event_manager'));

// connection
$options = $this->getConnectionOptions($connection);

$def = $container
->setDefinition(sprintf('doctrine.dbal.%s_connection', $name), new $definitionClassname('doctrine.dbal.connection'))
->setDefinition(sprintf('doctrine.dbal.%s_connection', $name), new ChildDefinition('doctrine.dbal.connection'))
->setPublic(true)
->setArguments([
$options,
Expand Down Expand Up @@ -328,10 +326,6 @@ protected function ormLoad(array $config, ContainerBuilder $container)
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('orm.xml');

if (class_exists(AbstractType::class) && method_exists(DoctrineType::class, 'reset')) {
$container->getDefinition('form.type.entity')->addTag('kernel.reset', ['method' => 'reset']);
}

$entityManagers = [];
foreach (array_keys($config['entity_managers']) as $name) {
$entityManagers[$name] = sprintf('doctrine.orm.%s_entity_manager', $name);
Expand All @@ -354,17 +348,10 @@ protected function ormLoad(array $config, ContainerBuilder $container)

$config['entity_managers'] = $this->fixManagersAutoMappings($config['entity_managers'], $container->getParameter('kernel.bundles'));

$loadPropertyInfoExtractor = interface_exists('Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface')
&& class_exists('Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor');

foreach ($config['entity_managers'] as $name => $entityManager) {
$entityManager['name'] = $name;
$this->loadOrmEntityManager($entityManager, $container);

if (! $loadPropertyInfoExtractor) {
continue;
}

$this->loadPropertyInfoExtractor($name, $container);
}

Expand All @@ -381,11 +368,8 @@ protected function ormLoad(array $config, ContainerBuilder $container)
$def->addTag('doctrine.event_subscriber');
}

// if is for Symfony 3.2 and lower compat
if (method_exists($container, 'registerForAutoconfiguration')) {
$container->registerForAutoconfiguration(ServiceEntityRepositoryInterface::class)
->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG);
}
$container->registerForAutoconfiguration(ServiceEntityRepositoryInterface::class)
->addTag(ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG);

// If the Messenger component is installed and the doctrine transaction middleware is available, wire it:
if (interface_exists(MessageBusInterface::class) && class_exists(DoctrineTransactionMiddleware::class)) {
Expand All @@ -401,6 +385,7 @@ protected function ormLoad(array $config, ContainerBuilder $container)
* This is replaced with a true locator by ServiceRepositoryCompilerPass.
* This makes that pass technically optional (good for tests).
*/
// @todo cleanup
if (! class_exists(ServiceLocator::class)) {
return;
}
Expand All @@ -417,8 +402,7 @@ protected function ormLoad(array $config, ContainerBuilder $container)
*/
protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $container)
{
$definitionClassname = $this->getDefinitionClassname();
$ormConfigDef = $container->setDefinition(sprintf('doctrine.orm.%s_configuration', $entityManager['name']), new $definitionClassname('doctrine.orm.configuration'));
$ormConfigDef = $container->setDefinition(sprintf('doctrine.orm.%s_configuration', $entityManager['name']), new ChildDefinition('doctrine.orm.configuration'));

$this->loadOrmEntityManagerMappingInformation($entityManager, $ormConfigDef, $container);
$this->loadOrmCacheDrivers($entityManager, $container);
Expand Down Expand Up @@ -498,7 +482,7 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $

$managerConfiguratorName = sprintf('doctrine.orm.%s_manager_configurator', $entityManager['name']);
$container
->setDefinition($managerConfiguratorName, new $definitionClassname('doctrine.orm.manager_configurator.abstract'))
->setDefinition($managerConfiguratorName, new ChildDefinition('doctrine.orm.manager_configurator.abstract'))
->replaceArgument(0, $enabledFilters)
->replaceArgument(1, $filtersParameters);

Expand All @@ -507,7 +491,7 @@ protected function loadOrmEntityManager(array $entityManager, ContainerBuilder $
}

$container
->setDefinition(sprintf('doctrine.orm.%s_entity_manager', $entityManager['name']), new $definitionClassname('doctrine.orm.entity_manager.abstract'))
->setDefinition(sprintf('doctrine.orm.%s_entity_manager', $entityManager['name']), new ChildDefinition('doctrine.orm.entity_manager.abstract'))
->setPublic(true)
->setArguments([
new Reference(sprintf('doctrine.dbal.%s_connection', $entityManager['connection'])),
Expand Down Expand Up @@ -827,12 +811,4 @@ public function getConfiguration(array $config, ContainerBuilder $container)
{
return new Configuration($container->getParameter('kernel.debug'));
}

/**
* @return string
*/
private function getDefinitionClassname()
{
return class_exists(ChildDefinition::class) ? ChildDefinition::class : DefinitionDecorator::class;
}
}
4 changes: 2 additions & 2 deletions DoctrineBundle.php
Expand Up @@ -105,7 +105,7 @@ public function shutdown()
// Clear all entity managers to clear references to entities for GC
if ($this->container->hasParameter('doctrine.entity_managers')) {
foreach ($this->container->getParameter('doctrine.entity_managers') as $id) {
if (method_exists($this->container, 'initialized') && ! $this->container->initialized($id)) {
if (! $this->container->initialized($id)) {
continue;
}

Expand All @@ -119,7 +119,7 @@ public function shutdown()
}

foreach ($this->container->getParameter('doctrine.connections') as $id) {
if (method_exists($this->container, 'initialized') && ! $this->container->initialized($id)) {
if (! $this->container->initialized($id)) {
continue;
}

Expand Down
7 changes: 1 addition & 6 deletions Repository/ContainerRepositoryFactory.php
Expand Up @@ -30,7 +30,7 @@ public function __construct(ContainerInterface $container = null)
{
// When DoctrineBundle requires Symfony 3.3+, this can be removed
// and the $container argument can become required.
if ($container === null && class_exists(ServiceLocatorTagPass::class)) {
if ($container === null) {
throw new InvalidArgumentException(sprintf('The first argument of %s::__construct() is required on Symfony 3.3 or higher.', self::class));
}

Expand Down Expand Up @@ -60,11 +60,6 @@ public function getRepository(EntityManagerInterface $entityManager, $entityName

// if not in the container but the class/id implements the interface, throw an error
if (is_a($customRepositoryName, ServiceEntityRepositoryInterface::class, true)) {
// can be removed when DoctrineBundle requires Symfony 3.3
if ($this->container === null) {
throw new RuntimeException(sprintf('Support for loading entities from the service container only works for Symfony 3.3 or higher. Upgrade your version of Symfony or make sure your "%s" class does not implement "%s"', $customRepositoryName, ServiceEntityRepositoryInterface::class));
}

throw new RuntimeException(sprintf('The "%s" entity repository implements "%s", but its service could not be found. Make sure the service exists and is tagged with "%s".', $customRepositoryName, ServiceEntityRepositoryInterface::class, ServiceRepositoryCompilerPass::REPOSITORY_SERVICE_TAG));
}

Expand Down
6 changes: 5 additions & 1 deletion Resources/config/orm.xml
Expand Up @@ -103,7 +103,11 @@
</service>

<service id="doctrine.orm.container_repository_factory" class="Doctrine\Bundle\DoctrineBundle\Repository\ContainerRepositoryFactory" public="false">
<argument>null</argument> <!-- repository locator -->
<argument type="service">
<service class="Symfony\Component\DependencyInjection\ServiceLocator">
<argument type="collection" />
</service>
</argument>
</service>

<!-- The configurator cannot be a private service -->
Expand Down
3 changes: 2 additions & 1 deletion Tests/ConnectionFactoryTest.php
Expand Up @@ -6,6 +6,7 @@
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\ORM\Version;
use Exception;

class ConnectionFactoryTest extends TestCase
Expand All @@ -14,7 +15,7 @@ protected function setUp()
{
parent::setUp();

if (class_exists('Doctrine\\ORM\\Version')) {
if (class_exists(Version::class)) {
return;
}

Expand Down
16 changes: 7 additions & 9 deletions Tests/ContainerTest.php
Expand Up @@ -3,6 +3,9 @@
namespace Doctrine\Bundle\DoctrineBundle\Tests;

use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Version;
use Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyInitializableExtractorInterface;

class ContainerTest extends TestCase
Expand All @@ -11,7 +14,7 @@ protected function setUp()
{
parent::setUp();

if (class_exists('Doctrine\\ORM\\Version')) {
if (class_exists(Version::class)) {
return;
}

Expand Down Expand Up @@ -48,14 +51,9 @@ public function testContainer()

$this->assertFalse($container->has('doctrine.dbal.default_connection.events.mysqlsessioninit'));

if (interface_exists('Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface') && class_exists('Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor')) {
if (! interface_exists(PropertyInitializableExtractorInterface::class)) {
$this->assertInstanceOf('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory', $container->get('doctrine.orm.default_entity_manager.metadata_factory'));
}
$this->assertInstanceOf('Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor', $container->get('doctrine.orm.default_entity_manager.property_info_extractor'));
} else {
$this->assertFalse($container->has('doctrine.orm.default_entity_manager.metadata_factory'));
$this->assertFalse($container->has('doctrine.orm.default_entity_manager.property_info_extractor'));
if (! interface_exists(PropertyInitializableExtractorInterface::class)) {
$this->assertInstanceOf('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory', $container->get('doctrine.orm.default_entity_manager.metadata_factory'));
}
$this->assertInstanceOf('Symfony\Bridge\Doctrine\PropertyInfo\DoctrineExtractor', $container->get('doctrine.orm.default_entity_manager.property_info_extractor'));
}
}
8 changes: 3 additions & 5 deletions Tests/DataCollector/DoctrineDataCollectorTest.php
Expand Up @@ -29,11 +29,9 @@ public function testCollectEntities()
->method('getConfiguration')
->will($this->returnValue($config));

if (method_exists($config, 'isSecondLevelCacheEnabled')) {
$config->expects($this->once())
->method('isSecondLevelCacheEnabled')
->will($this->returnValue(false));
}
$config->expects($this->once())
->method('isSecondLevelCacheEnabled')
->will($this->returnValue(false));

$metadatas = [
$this->createEntityMetadata(self::FIRST_ENTITY),
Expand Down

0 comments on commit 166b85e

Please sign in to comment.