Skip to content

Commit

Permalink
Merge pull request #801 from demiankatz/annotations-v2-compatibility
Browse files Browse the repository at this point in the history
Adjust dependencies/code for doctrine/annotations 2 compatibility
  • Loading branch information
TomHAnderson committed Apr 3, 2023
2 parents 6e180ed + 45f3d01 commit 3c86ba9
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 38 deletions.
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"homepage": "http://www.doctrine-project.org/",
"require": {
"php": "~7.4.0 || ~8.0.0 || ~8.1.0 || ~8.2.0",
"doctrine/annotations": "^1.13.3",
"doctrine/annotations": "^1.13.3 || ^2",
"doctrine/cache": "^1.13.0",
"doctrine/collections": "^1.8.0",
"doctrine/doctrine-laminas-hydrator": "^3.2.0",
Expand All @@ -59,7 +59,7 @@
},
"require-dev": {
"doctrine/coding-standard": "^9.0.2",
"doctrine/mongodb-odm": "^2.4.2",
"doctrine/mongodb-odm": "^2.5.0",
"doctrine/orm": "^2.13.4",
"jangregor/phpstan-prophecy": "^1.0.0",
"laminas/laminas-cache-storage-adapter-blackhole": "^2.0.0",
Expand Down Expand Up @@ -101,7 +101,7 @@
"dealerdirect/phpcodesniffer-composer-installer": true
},
"platform": {
"ext-mongodb": "1.8.0"
"ext-mongodb": "1.11.0"
},
"sort-packages": true
},
Expand Down
16 changes: 1 addition & 15 deletions src/Module.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,13 @@

namespace DoctrineModule;

use Doctrine\Common\Annotations\AnnotationRegistry;
use Laminas\ModuleManager\Feature\ConfigProviderInterface;
use Laminas\ModuleManager\Feature\InitProviderInterface;
use Laminas\ModuleManager\ModuleManagerInterface;

use function class_exists;

/**
* Base module for integration of Doctrine projects with Laminas applications
*/
final class Module implements ConfigProviderInterface, InitProviderInterface
final class Module implements ConfigProviderInterface
{
public function init(ModuleManagerInterface $manager): void
{
AnnotationRegistry::registerLoader(
static function ($className) {
return class_exists($className);
}
);
}

/**
* @return array<string, mixed>
*/
Expand Down
25 changes: 20 additions & 5 deletions src/Service/DriverFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Doctrine\Persistence\Mapping\Driver\MappingDriverChain;
use DoctrineModule\Options\Driver;
use InvalidArgumentException;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Container\ContainerInterface;
use RuntimeException;

Expand Down Expand Up @@ -80,11 +81,25 @@ protected function createDriver(ContainerInterface $container, Driver $options):
is_a($class, MongoODMAnnotationDriver::class, true)
)
) {
$reader = new Annotations\AnnotationReader();
$reader = new Annotations\CachedReader(
new Annotations\IndexedReader($reader),
$container->get($options->getCache())
);
$reader = new Annotations\IndexedReader(new Annotations\AnnotationReader());

// Decorate reader with cache behavior if available:
if (class_exists(Annotations\CachedReader::class)) {
// For Doctrine Annotations 1.x, use the old CachedReader; this can
// be removed when Annotations 1.x support is dropped.
$reader = new Annotations\CachedReader(
$reader,
$container->get($options->getCache())
);
} elseif (class_exists(Annotations\PsrCachedReader::class)) {
// For Doctrine Annotations 2.x, we can use the PsrCachedReader if
// the cache supports the appropriate interface.
$cache = $container->get($options->getCache());
if ($cache instanceof CacheItemPoolInterface) {
$reader = new Annotations\PsrCachedReader($reader, $cache);
}
}

$driver = new $class($reader, $paths);
} else {
$driver = new $class($paths);
Expand Down
16 changes: 1 addition & 15 deletions tests/Service/CacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
use DoctrineModule\Service\CacheFactory;
use Laminas\Cache\ConfigProvider;
use Laminas\Cache\Storage\Adapter\BlackHole;
use Laminas\Cache\Storage\AdapterPluginManager;
use Laminas\ServiceManager\Factory\InvokableFactory;
use Laminas\ServiceManager\ServiceManager;
use PHPUnit\Framework\TestCase as BaseTestCase;

Expand Down Expand Up @@ -77,19 +75,7 @@ public function testCreateLaminasCache(): void
$serviceManager->configure((new BlackHole\ConfigProvider())->getServiceDependencies());
$serviceManager->setService('config', $config);
} else {
// setup for laminas-cache 2 and 3 with blackhole adapter 1
$config['caches']['my-laminas-cache']['name'] = 'blackhole';
$pluginManager = $serviceManager->get(AdapterPluginManager::class);
assert($pluginManager instanceof AdapterPluginManager);
$pluginManager->configure([
'factories' => [
BlackHole::class => InvokableFactory::class,
],
'aliases' => [
'blackhole' => BlackHole::class,
],
]);
$serviceManager->setService('config', $config);
$this->markTestSkipped('Test requires blackhole adapter 2');
}

$cache = $factory->__invoke($serviceManager, LaminasStorageCache::class);
Expand Down

0 comments on commit 3c86ba9

Please sign in to comment.