Skip to content

Commit

Permalink
Merge branch '2.12.x' into 3.0.x
Browse files Browse the repository at this point in the history
* 2.12.x:
  Introduce DoctrineSetup as a replacement for Setup (doctrine#9443)
  Introduce __unserialize behaviour in docs (doctrine#9390)
  Adapt test logic to PHP and SQLite II (doctrine#9442)
  Use the identify generator strategy
  Added php 8.1 to CI
  Psalm 4.19.0, PHPStan 1.4.3 (doctrine#9438)
  Ignore PHPUnit result cache everywhere (doctrine#9425)
  • Loading branch information
derrabus committed Jan 30, 2022
2 parents 31b7d78 + f81980e commit 9872d1c
Show file tree
Hide file tree
Showing 23 changed files with 489 additions and 88 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ jobs:
uses: "shivammathur/setup-php@v2"
with:
php-version: "${{ matrix.php-version }}"
extensions: "pdo, pdo_sqlite"
extensions: "apcu, pdo, pdo_sqlite"
coverage: "pcov"
ini-values: "zend.assertions=1"

Expand Down Expand Up @@ -73,7 +73,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.0"
- "8.1"
dbal-version:
- "default"
postgres-version:
Expand Down Expand Up @@ -130,7 +130,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.0"
- "8.1"
dbal-version:
- "default"
mariadb-version:
Expand Down Expand Up @@ -191,7 +191,7 @@ jobs:
strategy:
matrix:
php-version:
- "8.0"
- "8.1"
dbal-version:
- "default"
mysql-version:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ vendor/
/tests/Doctrine/Performance/history.db
/.phpcs-cache
composer.lock
/.phpunit.result.cache
.phpunit.result.cache
/*.phpunit.xml
14 changes: 14 additions & 0 deletions UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,20 @@ Use `toIterable()` instead.

# Upgrade to 2.12

## Deprecate `Doctrine\ORM\Configuration::newDefaultAnnotationDriver`

This functionality has been moved to the new `DoctrineSetup` class. Call
`Doctrine\ORM\Tools\DoctrineSetup::createDefaultAnnotationDriver()` to create
a new annotation driver.

## Deprecate `Doctrine\ORM\Tools\Setup`

In our effort to migrate from Doctrine Cache to PSR-6, the `Setup` class which
accepted a Doctrine Cache instance in each method has been deprecated.

The replacement is `Doctrine\ORM\Tools\DoctrineSetup` which accepts a PSR-6
cache instead.

## Deprecate `Doctrine\ORM\Cache\MultiGetRegion`

The interface will be merged with `Doctrine\ORM\Cache\Region` in 3.0.
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@
"doctrine/annotations": "^1.13",
"doctrine/coding-standard": "^9.0",
"phpbench/phpbench": "^1.0",
"phpstan/phpstan": "1.4.1",
"phpstan/phpstan": "1.4.3",
"phpunit/phpunit": "^9.5",
"psr/log": "^1 || ^2 || ^3",
"squizlabs/php_codesniffer": "3.6.2",
"symfony/cache": "^4.4 || ^5.4 || ^6.0",
"vimeo/psalm": "4.18.1"
"vimeo/psalm": "4.19.0"
},
"conflict": {
"doctrine/annotations": "<1.13 || >= 2.0"
Expand Down
13 changes: 9 additions & 4 deletions docs/en/reference/architecture.rst
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,13 @@ be any regular PHP class observing the following restrictions:
:doc:`do so safely <../cookbook/implementing-wakeup-or-clone>`.
- An entity class must not implement ``__wakeup`` or
:doc:`do so safely <../cookbook/implementing-wakeup-or-clone>`.
Also consider implementing
`Serializable <https://php.net/manual/en/class.serializable.php>`_
instead.
You can also consider implementing
`Serializable <https://php.net/manual/en/class.serializable.php>`_,
but be aware that it is deprecated since PHP 8.1. We do not recommend its usage.
- PHP 7.4 introduces :doc:`the new magic method <https://php.net/manual/en/language.oop5.magic.php#object.unserialize>`
``__unserialize``, which changes the execution priority between
``__wakeup`` and itself when used. This can cause unexpected behaviour in
an Entity.
- Any two entity classes in a class hierarchy that inherit
directly or indirectly from one another must not have a mapped
property with the same name. That is, if B inherits from A then B
Expand Down Expand Up @@ -162,7 +166,8 @@ possible for ``__sleep`` to return names of private properties in
parent classes. On the other hand it is not a solution for proxy
objects to implement ``Serializable`` because Serializable does not
work well with any potential cyclic object references (at least we
did not find a way yet, if you did, please contact us).
did not find a way yet, if you did, please contact us). The
``Serializable`` interface is also deprecated beginning with PHP 8.1.

The EntityManager
~~~~~~~~~~~~~~~~~
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/ORM/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use Doctrine\ORM\Mapping\QuoteStrategy;
use Doctrine\ORM\Repository\DefaultRepositoryFactory;
use Doctrine\ORM\Repository\RepositoryFactory;
use Doctrine\ORM\Tools\DoctrineSetup;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Doctrine\Persistence\ObjectRepository;
use Psr\Cache\CacheItemPoolInterface;
Expand Down Expand Up @@ -144,6 +145,8 @@ public function setMetadataDriverImpl(MappingDriver $driverImpl)
* Adds a new default annotation driver with a correctly configured annotation reader. If $useSimpleAnnotationReader
* is true, the notation `@Entity` will work, otherwise, the notation `@ORM\Entity` will be supported.
*
* @deprecated Use {@see DoctrineSetup::createDefaultAnnotationDriver()} instead.
*
* @param string|string[] $paths
* @param bool $useSimpleAnnotationReader
* @psalm-param string|list<string> $paths
Expand All @@ -152,6 +155,14 @@ public function setMetadataDriverImpl(MappingDriver $driverImpl)
*/
public function newDefaultAnnotationDriver($paths = [], $useSimpleAnnotationReader = true)
{
Deprecation::trigger(
'doctrine/orm',
'https://github.com/doctrine/orm/pull/9443',
'%s is deprecated, call %s::createDefaultAnnotationDriver() instead.',
__METHOD__,
DoctrineSetup::class
);

AnnotationRegistry::registerFile(__DIR__ . '/Mapping/Driver/DoctrineAnnotations.php');

if ($useSimpleAnnotationReader) {
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/ORM/PersistentCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,7 @@ public function offsetExists($offset): bool
/**
* {@inheritdoc}
*/
#[ReturnTypeWillChange]
public function offsetGet($offset)
{
return $this->get($offset);
Expand Down
172 changes: 172 additions & 0 deletions lib/Doctrine/ORM/Tools/DoctrineSetup.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
<?php

declare(strict_types=1);

namespace Doctrine\ORM\Tools;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\PsrCachedReader;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\ORM\Mapping\Driver\XmlDriver;
use Memcached;
use Psr\Cache\CacheItemPoolInterface;
use Redis;
use RuntimeException;
use Symfony\Component\Cache\Adapter\ApcuAdapter;
use Symfony\Component\Cache\Adapter\ArrayAdapter;
use Symfony\Component\Cache\Adapter\MemcachedAdapter;
use Symfony\Component\Cache\Adapter\RedisAdapter;

use function class_exists;
use function extension_loaded;
use function md5;
use function sys_get_temp_dir;

final class DoctrineSetup
{
/**
* Creates a configuration with an annotation metadata driver.
*
* @param string[] $paths
*/
public static function createAnnotationMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(self::createDefaultAnnotationDriver($paths));

return $config;
}

/**
* Adds a new default annotation driver with a correctly configured annotation reader.
*
* @param string[] $paths
*/
public static function createDefaultAnnotationDriver(
array $paths = [],
?CacheItemPoolInterface $cache = null
): AnnotationDriver {
$reader = new AnnotationReader();

if ($cache === null && class_exists(ArrayAdapter::class)) {
$cache = new ArrayAdapter();
}

if ($cache !== null) {
$reader = new PsrCachedReader($reader, $cache);
}

return new AnnotationDriver($reader, $paths);
}

/**
* Creates a configuration with an attribute metadata driver.
*
* @param string[] $paths
*/
public static function createAttributeMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new AttributeDriver($paths));

return $config;
}

/**
* Creates a configuration with an XML metadata driver.
*
* @param string[] $paths
*/
public static function createXMLMetadataConfiguration(
array $paths,
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
$config = self::createConfiguration($isDevMode, $proxyDir, $cache);
$config->setMetadataDriverImpl(new XmlDriver($paths));

return $config;
}

/**
* Creates a configuration without a metadata driver.
*/
public static function createConfiguration(
bool $isDevMode = false,
?string $proxyDir = null,
?CacheItemPoolInterface $cache = null
): Configuration {
$proxyDir = $proxyDir ?: sys_get_temp_dir();

$cache = self::createCacheInstance($isDevMode, $proxyDir, $cache);

$config = new Configuration();

$config->setMetadataCache($cache);
$config->setQueryCache($cache);
$config->setResultCache($cache);
$config->setProxyDir($proxyDir);
$config->setProxyNamespace('DoctrineProxies');
$config->setAutoGenerateProxyClasses($isDevMode);

return $config;
}

private static function createCacheInstance(
bool $isDevMode,
string $proxyDir,
?CacheItemPoolInterface $cache
): CacheItemPoolInterface {
if ($cache !== null) {
return $cache;
}

if (! class_exists(ArrayAdapter::class)) {
throw new RuntimeException(
'The Doctrine setup tool cannot configure caches without symfony/cache.'
. ' Please add symfony/cache as explicit dependency or pass your own cache implementation.'
);
}

if ($isDevMode) {
return new ArrayAdapter();
}

$namespace = 'dc2_' . md5($proxyDir);

if (extension_loaded('apcu')) {
return new ApcuAdapter($namespace);
}

if (extension_loaded('memcached')) {
$memcached = new Memcached();
$memcached->addServer('127.0.0.1', 11211);

return new MemcachedAdapter($memcached, $namespace);
}

if (extension_loaded('redis')) {
$redis = new Redis();
$redis->connect('127.0.0.1');

return new RedisAdapter($redis, $namespace);
}

return new ArrayAdapter();
}

private function __construct()
{
}
}

0 comments on commit 9872d1c

Please sign in to comment.