Skip to content

Instantly share code, notes, and snippets.

@enumag
Created February 9, 2017 17:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enumag/5a10e5d223513920c71c9e9ae80bdcd5 to your computer and use it in GitHub Desktop.
Save enumag/5a10e5d223513920c71c9e9ae80bdcd5 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
namespace Kdyby\Annotations\DI;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\Reader;
use Kdyby\DoctrineCache\DI\DoctrineCacheExtension;
use Nette\DI\CompilerExtension;
use Nette\PhpGenerator\ClassType;
use Nette\Utils\Validators;
/**
* @author Jáchym Toušek <enumag@gmail.com>
*/
class AnnotationsExtension extends CompilerExtension
{
public const CACHE_NAMESPACE = 'kdyby.annotations';
/**
* @var array
*/
public $defaults = [
'ignore' => [],
];
public function __construct(bool $debugMode = false)
{
$this->defaults['debug'] = $debugMode;
}
public function loadConfiguration()
{
$this->validateConfig($this->defaults);
Validators::assertField($config, 'ignore', 'array');
$readerDefinition = $builder
->addDefinition($this->prefix('annotationReader'))
->setClass(AnnotationReader::class)
->setAutowired(false);
foreach ($config['ignore'] as $annotationName) {
$readerDefinition->addSetup('addGlobalIgnoredName', [$annotationName]);
}
$builder
->addDefinition($this->prefix('reader'))
->setClass(Reader::class)
->setFactory(
CachedReader::class,
[
$this->prefix('@annotationReader'),
// This would of course require entirely new DoctrineCacheExtension. Below is an example what the configuration of this extension would look like.
$this->getExtension(DoctrineCacheExtension::class)->getCacheService(self::CACHE_NAMESPACE)
$config['debug']
]
);
}
public function afterCompile(ClassType $class)
{
$initialize = $class->getMethod('initialize');
$originalBody = $init->getBody();
$initialize->setBody("\\Doctrine\\Common\\Annotations\\AnnotationRegistry::registerLoader('class_exists');\n");
$initialize->addBody($originalBody);
}
}
/*
extensions:
kdyby.doctrineCache: Kdyby\DoctrineCache\DI\DoctrineCacheExtension(%tempDir%/doctrine-cache, %debugMode%)
kdyby.annotations: Kdyby\Annotations\DI\AnnotationsExtension(%debugMode%)
kdyby.doctrineCache:
defaultDriver: filesystem
namespaces:
kdyby.annotations: apcu
kdyby.doctrine.queryCache: apcu
kdyby.doctrine.resultCache: apcu
kdyby.annotations:
ignore:
- persistent
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment