Skip to content

Commit

Permalink
Merge pull request #6 from Desure85/develop
Browse files Browse the repository at this point in the history
getReader() return CacheReader with new AnnotationReader
  • Loading branch information
Desure85 authored May 8, 2019
2 parents cde822f + 46f633b commit 1a71ff2
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "egorov/yii2-annotations",
"version": "1.0.2",
"version": "1.0.3",
"description": "Allows you to use doctrine/annotations in Yii2 projects",
"type": "yii2-extension",
"require": {
Expand Down
55 changes: 31 additions & 24 deletions src/Annotations.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
namespace yii\annotations;

use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Yii;
Expand Down Expand Up @@ -43,6 +42,11 @@ class Annotations extends Component implements AnnotationsInterface
*/
public $ignoreAnnotations = [];

/**
* @var CacheInterface
*/
private $cacheComponent;

/**
* @var FileCache
*/
Expand All @@ -53,39 +57,38 @@ class Annotations extends Component implements AnnotationsInterface
protected const CACHE_PREFIX = '.annotations';

/**
* @throws AnnotationException
* @throws InvalidConfigException
*/
public function init(): void
{
parent::init();
$this->registerLoader();
$cacheComponent = $this->getCacheComponent();
$this->configurationCache($cacheComponent);
$reader = new AnnotationReader();
$this->configurationReader($reader);
$this->reader = new AnnotationCacheReader(
$reader,
new AnnotationCache($cacheComponent),
$this->debug
);
$this->enableCacheComponent();
}

/**
* @return AnnotationCacheReader
*/
public function getReader(): AnnotationCacheReader
{
return $this->reader;
$this->enableNewReader();
return new AnnotationCacheReader(
$this->reader,
new AnnotationCache($this->cacheComponent),
$this->debug
);
}

/**
* @return object|CacheInterface
* @return void
* @throws InvalidConfigException
*/
private function getCacheComponent(): CacheInterface
private function enableCacheComponent(): void
{
return (is_string($this->cache) ? Instance::ensure($this->cache) : $this->cache) ?? $this->getDefaultCache();
$this->cacheComponent = (is_string($this->cache)
? Instance::ensure($this->cache)
: $this->cache) ?? $this->getDefaultCache();
$this->configurationCache();
}

/**
Expand All @@ -97,18 +100,21 @@ private function getDefaultCache()
}

/**
* @param $cacheComponent
*
*/
private function configurationCache(CacheInterface $cacheComponent)
private function configurationCache()
{
if (property_exists($cacheComponent, 'cachePath') && $this->path !== null) {
$cacheComponent->cachePath = Yii::getAlias($this->path);
if (property_exists($this->cacheComponent, 'cachePath') && $this->path !== null) {
$this->cacheComponent->cachePath = Yii::getAlias($this->path);
}
if (property_exists($cacheComponent, 'cacheFileSuffix')) {
$cacheComponent->cacheFileSuffix = static::CACHE_PREFIX;
if (property_exists($this->cacheComponent, 'cacheFileSuffix')) {
$this->cacheComponent->cacheFileSuffix = static::CACHE_PREFIX;
}
}

/**
*
*/
private function registerLoader()
{
if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
Expand All @@ -117,12 +123,13 @@ private function registerLoader()
}

/**
* @param $reader
*
*/
private function configurationReader(AnnotationReader $reader)
private function enableNewReader()
{
$this->reader = new AnnotationReader();
foreach ($this->ignoreAnnotations as $annotation) {
$reader::addGlobalIgnoredName($annotation);
$this->reader::addGlobalIgnoredName($annotation);
}
}
}

0 comments on commit 1a71ff2

Please sign in to comment.