Skip to content
Permalink
Browse files

OrmAnnotationsExtension: requires generic annotations integration

  • Loading branch information
mabar authored and f3l1x committed Nov 25, 2019
1 parent 3cf7ef3 commit 1c8b5ac9295fc36fd8fcfb3f3861db96502ac2d1
@@ -114,7 +114,7 @@ class Category
{
```

You will need the `OrmAnnotationsExtension`. This is the default configuration, it uses the `filesystem` cache driver.
You will need the `OrmAnnotationsExtension`. This is the default configuration, it uses an autowired cache driver.

```yaml
extensions:
@@ -124,30 +124,6 @@ extensions:
orm.annotations:
paths: []
excludePaths: []
ignore: []
defaultCache: filesystem
```

Available drivers:

- `apc` - `ApcCache`
- `apcu` - `ApcuCache`
- `array` - `ArrayCache`
- `filesystem` - `FilesystemCache`
- `memcache` - `MemcacheCache`
- `memcached` - `MemcachedCache`
- `redis` - `RedisCache`
- `void` - `VoidCache`
- `xcache` - `XcacheCache`

You can change cache settings for the annotation reader.

```yaml
orm.annotations:
defaultCache: apcu
# or directly
cache: @cacheReader
```

### XML Bridge
@@ -27,6 +27,8 @@
},
"require-dev": {
"mockery/mockery": "^1.2.2",
"nettrine/annotations": "^0.6.0",
"nettrine/cache": "^0.1.0",
"ninjify/qa": "^0.9.0",
"phpstan/extension-installer": "^1.0",
"phpstan/phpstan-deprecation-rules": "^0.11.0",
@@ -2,7 +2,5 @@ parameters:
ignoreErrors:
# PHPStan bug, there is no Doctrine\Orm\Configuration assignment
- '#^Parameter \#1 \$type of method Nette\\DI\\Definitions\\ServiceDefinition::setType\(\) expects string|null, Doctrine\\ORM\\Configuration|string given$#'
# No replacement available yet
- '#^Call to deprecated method registerUniqueLoader\(\) of class Doctrine\\Common\\Annotations\\AnnotationRegistry.+#'
# We will replace it once, for sure
- '#Fetching class constant .+ of deprecated class Doctrine\\Common\\Proxy\\AbstractProxyFactory.+#'
@@ -2,16 +2,9 @@

namespace Nettrine\ORM\DI;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Nette\PhpGenerator\ClassType;
use Nette\PhpGenerator\PhpLiteral;
use Doctrine\Common\Persistence\Mapping\Driver\MappingDriver;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nettrine\ORM\DI\Helpers\CacheBuilder;
use Nettrine\ORM\Exception\Logical\InvalidStateException;
use Nettrine\ORM\Mapping\AnnotationDriver;
use stdClass;

@@ -24,12 +17,8 @@ class OrmAnnotationsExtension extends AbstractExtension
public function getConfigSchema(): Schema
{
return Expect::structure([
'debug' => Expect::bool(false),
'cache' => Expect::string()->nullable(),
'defaultCache' => Expect::string('filesystem')->nullable(),
'paths' => Expect::listOf('string'),
'excludePaths' => Expect::listOf('string'),
'ignore' => Expect::listOf('string'),
]);
}

@@ -44,52 +33,13 @@ public function loadConfiguration(): void
$builder = $this->getContainerBuilder();
$config = $this->config;

$reader = $builder->addDefinition($this->prefix('annotationReader'))
->setType(AnnotationReader::class)
->setAutowired(false);

foreach ($config->ignore as $annotationName) {
$reader->addSetup('addGlobalIgnoredName', [$annotationName]);
AnnotationReader::addGlobalIgnoredName($annotationName);
}

if ($config->cache === null && $config->defaultCache !== null) {
CacheBuilder::of($this)
->withDefault($config->defaultCache)
->getDefinition('annotationsCache');
} elseif ($config->cache !== null) {
$builder->addDefinition($this->prefix('annotationsCache'))
->setFactory($config->cache)
->setAutowired(false);
} else {
throw new InvalidStateException('Cache or defaultCache must be provided');
}

$builder->addDefinition($this->prefix('reader'))
->setType(Reader::class)
->setFactory(CachedReader::class, [
$this->prefix('@annotationReader'),
$this->prefix('@annotationsCache'),
$config->debug,
]);

$builder->addDefinition($this->prefix('annotationDriver'))
->setFactory(AnnotationDriver::class, [$this->prefix('@reader'), $config->paths])
->setFactory(AnnotationDriver::class, [1 => $config->paths])
->setType(MappingDriver::class)
->addSetup('addExcludePaths', [$config->excludePaths]);

$configurationDef = $this->getConfigurationDef();
$configurationDef->addSetup('setMetadataDriverImpl', [$this->prefix('@annotationDriver')]);

// Just for runtime
AnnotationRegistry::registerUniqueLoader('class_exists');
}

public function afterCompile(ClassType $classType): void
{
$initialize = $classType->getMethod('initialize');
$original = (string) $initialize->getBody();
$initialize->setBody('?::registerUniqueLoader("class_exists");' . "\n", [new PhpLiteral(AnnotationRegistry::class)]);
$initialize->addBody($original);
}

}
@@ -2,23 +2,27 @@

namespace Tests\Cases\Unit\DI;

use Doctrine\Common\Cache\FilesystemCache;
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\DI\ServiceCreationException;
use Nettrine\Annotations\DI\AnnotationsExtension;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\ORM\DI\OrmAnnotationsExtension;
use Nettrine\ORM\DI\OrmExtension;
use Nettrine\ORM\Exception\Logical\InvalidStateException;
use Nettrine\ORM\Mapping\AnnotationDriver;
use Tests\Toolkit\TestCase;

final class OrmAnnotationsExtensionTest extends TestCase
{

public function testDefaultCache(): void
public function testOk(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('annotations', new AnnotationsExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addExtension('orm', new OrmExtension());
$compiler->addExtension('orm.annotations', new OrmAnnotationsExtension());
@@ -33,16 +37,17 @@ public function testDefaultCache(): void
/** @var Container $container */
$container = new $class();

$this->assertInstanceOf(FilesystemCache::class, $container->getService('orm.annotations.annotationsCache'));
$this->assertInstanceOf(AnnotationDriver::class, $container->getService('orm.annotations.annotationDriver'));
}

public function testNoCache(): void
public function testNoReader(): void
{
$this->expectException(InvalidStateException::class);
$this->expectExceptionMessage('Cache or defaultCache must be provided');
$this->expectException(ServiceCreationException::class);
$this->expectExceptionMessage('Service \'orm.annotations.annotationDriver\' (type of Doctrine\Common\Persistence\Mapping\Driver\MappingDriver): Service of type Doctrine\Common\Annotations\Reader needed by $reader in Nettrine\ORM\Mapping\AnnotationDriver::__construct() not found. Did you register it in configuration file?');

$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addExtension('orm', new OrmExtension());
$compiler->addExtension('orm.annotations', new OrmAnnotationsExtension());
@@ -51,10 +56,6 @@ public function testNoCache(): void
'tempDir' => TEMP_PATH,
'appDir' => __DIR__,
],
'orm.annotations' => [
'cache' => null,
'defaultCache' => null,
],
]);
}, self::class . __METHOD__);

@@ -6,6 +6,8 @@
use Nette\DI\Compiler;
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nettrine\Annotations\DI\AnnotationsExtension;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\ORM\DI\OrmAnnotationsExtension;
use Nettrine\ORM\DI\OrmCacheExtension;
@@ -20,6 +22,8 @@ public function testCacheDrivers(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('annotations', new AnnotationsExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addExtension('orm', new OrmExtension());
$compiler->addExtension('orm.annotations', new OrmAnnotationsExtension());
@@ -6,6 +6,8 @@
use Nette\DI\Container;
use Nette\DI\ContainerLoader;
use Nette\InvalidArgumentException;
use Nettrine\Annotations\DI\AnnotationsExtension;
use Nettrine\Cache\DI\CacheExtension;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\ORM\DI\OrmAnnotationsExtension;
use Nettrine\ORM\DI\OrmExtension;
@@ -23,6 +25,8 @@ public function testRegisterAnnotations(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('annotations', new AnnotationsExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addExtension('orm', new OrmExtension());
$compiler->addExtension('orm.annotations', new OrmAnnotationsExtension());
@@ -43,6 +47,8 @@ public function testOwnEntityManager(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('annotations', new AnnotationsExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addExtension('orm', new OrmExtension());
$compiler->addExtension('orm.annotations', new OrmAnnotationsExtension());
@@ -94,6 +100,8 @@ public function testResetEntityManager(): void
{
$loader = new ContainerLoader(TEMP_PATH, true);
$class = $loader->load(function (Compiler $compiler): void {
$compiler->addExtension('annotations', new AnnotationsExtension());
$compiler->addExtension('cache', new CacheExtension());
$compiler->addExtension('dbal', new DbalExtension());
$compiler->addExtension('orm', new OrmExtension());
$compiler->addExtension('orm.annotations', new OrmAnnotationsExtension());

0 comments on commit 1c8b5ac

Please sign in to comment.
You can’t perform that action at this time.