Skip to content

Commit

Permalink
Merge pull request #4 from Desure85/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
Desure85 authored May 8, 2019
2 parents 4e2e525 + 14e244f commit cde822f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 20 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.1",
"version": "1.0.2",
"description": "Allows you to use doctrine/annotations in Yii2 projects",
"type": "yii2-extension",
"require": {
Expand Down
8 changes: 4 additions & 4 deletions src/AnnotationCacheInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,19 @@ interface AnnotationCacheInterface
/**
*
*/
public const STATS_HITS = 'hits';
public const STATS_HITS = 'hits';
/**
*
*/
public const STATS_MISSES = 'misses';
public const STATS_MISSES = 'misses';
/**
*
*/
public const STATS_UPTIME = 'uptime';
public const STATS_UPTIME = 'uptime';
/**
*
*/
public const STATS_MEMORY_USAGE = 'memory_usage';
public const STATS_MEMORY_USAGE = 'memory_usage';
/**
*
*/
Expand Down
72 changes: 57 additions & 15 deletions src/Annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

/**
* Class Annotations
* @property CacheInterface|object $cacheComponent
* @property FileCache $defaultCache
*/
class Annotations extends Component implements AnnotationsInterface
{
Expand Down Expand Up @@ -41,6 +43,10 @@ class Annotations extends Component implements AnnotationsInterface
*/
public $ignoreAnnotations = [];

/**
* @var FileCache
*/
private const DEFAULT_CACHE = FileCache::class;
/**
*
*/
Expand All @@ -53,34 +59,70 @@ class Annotations extends Component implements AnnotationsInterface
public function init(): void
{
parent::init();
if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
AnnotationRegistry::registerLoader('class_exists');
}
$cacheComponent = is_string($this->cache) ? Instance::ensure($this->cache) : $this->cache;
if (!$cacheComponent) {
$cacheComponent = new FileCache();
}
if ($cacheComponent instanceof FileCache && $this->path !== null) {
$cacheComponent->cachePath = Yii::getAlias($this->path);
$cacheComponent->cacheFileSuffix = static::CACHE_PREFIX;
}
$this->registerLoader();
$cacheComponent = $this->getCacheComponent();
$this->configurationCache($cacheComponent);
$reader = new AnnotationReader();
foreach ($this->ignoreAnnotations as $annotation) {
$reader::addGlobalIgnoredName($annotation);
}
$this->configurationReader($reader);
$this->reader = new AnnotationCacheReader(
$reader,
new AnnotationCache($cacheComponent),
$this->debug
);
}


/**
* @return AnnotationCacheReader
*/
public function getReader(): AnnotationCacheReader
{
return $this->reader;
}

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

/**
* Return default cache
*/
private function getDefaultCache()
{
return self::DEFAULT_CACHE;
}

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

private function registerLoader()
{
if (method_exists(AnnotationRegistry::class, 'registerLoader')) {
AnnotationRegistry::registerLoader('class_exists');
}
}

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

0 comments on commit cde822f

Please sign in to comment.