Skip to content

Commit 93db1d3

Browse files
committed
add annotation caching
1 parent 1e021b6 commit 93db1d3

File tree

7 files changed

+94
-0
lines changed

7 files changed

+94
-0
lines changed

src/ArthurH/SphringCache/Bean/CacheBean.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ class CacheBean extends AbstractBean
3434
* @var bool
3535
*/
3636
private $cacheSphringContext = true;
37+
/**
38+
* @var bool
39+
*/
40+
private $cacheSphringAnnotation = true;
3741
/**
3842
* @var bool
3943
*/
@@ -50,6 +54,7 @@ public function inject()
5054
$this->object->setCacheSphringProxies($this->cacheSphringProxies);
5155
$this->object->setCacheSphringContext($this->cacheSphringContext);
5256
$this->object->setCacheSphringBean($this->cacheSphringBean);
57+
$this->object->setCacheSphringAnnotation($this->cacheSphringAnnotation);
5358
SphringCacheRunnerPlugin::getInstance()->setCacheManager($this->object);
5459
}
5560

@@ -122,4 +127,20 @@ public function setCacheSphringBean($cacheSphringBean)
122127
$this->cacheSphringBean = $cacheSphringBean;
123128
}
124129

130+
/**
131+
* @return boolean
132+
*/
133+
public function isCacheSphringAnnotation()
134+
{
135+
return $this->cacheSphringAnnotation;
136+
}
137+
138+
/**
139+
* @param boolean $cacheSphringAnnotation
140+
*/
141+
public function setCacheSphringAnnotation($cacheSphringAnnotation)
142+
{
143+
$this->cacheSphringAnnotation = $cacheSphringAnnotation;
144+
}
145+
125146
}

src/ArthurH/SphringCache/CacheManager/AbstractCacheManager.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ abstract class AbstractCacheManager
3434
* @var bool
3535
*/
3636
protected $cacheSphringContext = true;
37+
/**
38+
* @var bool
39+
*/
40+
private $cacheSphringAnnotation = true;
3741
/**
3842
* @var bool
3943
*/
@@ -119,4 +123,20 @@ public function setCacheSphringBean($cacheSphringBean)
119123
$this->cacheSphringBean = $cacheSphringBean;
120124
}
121125

126+
/**
127+
* @return boolean
128+
*/
129+
public function isCacheSphringAnnotation()
130+
{
131+
return $this->cacheSphringAnnotation;
132+
}
133+
134+
/**
135+
* @param boolean $cacheSphringAnnotation
136+
*/
137+
public function setCacheSphringAnnotation($cacheSphringAnnotation)
138+
{
139+
$this->cacheSphringAnnotation = $cacheSphringAnnotation;
140+
}
141+
122142
}

src/ArthurH/SphringCache/Enum/SphringCacheEnum.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ class SphringCacheEnum extends Enum
2121
const CACHE_FILE_BEAN = '.cache-sphring-bean-%s';
2222
const CACHE_FOLDER = 'sphring';
2323
const CACHE_FOLDER_PROXIES = 'proxies';
24+
const CACHE_FOLDER_ANNOTATIONS = 'annotations';
2425
}

src/ArthurH/SphringCache/GlobalEvent/CacheSphringContext.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@
1818
use Arthurh\Sphring\Model\SphringGlobal;
1919
use Arthurh\Sphring\ProxyGenerator\ProxyGenerator;
2020
use ArthurH\SphringCache\Enum\SphringCacheEnum;
21+
use Doctrine\Common\Annotations\AnnotationReader;
22+
use Doctrine\Common\Annotations\CachedReader;
23+
use Doctrine\Common\Cache\FilesystemCache;
2124
use ProxyManager\Configuration;
2225
use ProxyManager\Factory\AccessInterceptorValueHolderFactory;
2326

@@ -41,6 +44,7 @@ public function run()
4144
$this->cacheProxy();
4245
$this->loadContextFromCache();
4346
$this->loadBeansFromCache();
47+
$this->loadAnnotationFromCache();
4448
}
4549

4650
private function cacheProxy()
@@ -74,6 +78,20 @@ private function loadContextFromCache()
7478
$this->getSphring()->setContext($context);
7579
}
7680

81+
private function loadAnnotationFromCache()
82+
{
83+
$annotationsFolder = new Folder(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
84+
SphringCacheEnum::CACHE_FOLDER . DIRECTORY_SEPARATOR .
85+
SphringCacheEnum::CACHE_FOLDER_ANNOTATIONS);
86+
$annotationsFolder->create();
87+
$reader = new CachedReader(
88+
new AnnotationReader(),
89+
new FilesystemCache($annotationsFolder->absolute())
90+
);
91+
$sphringBoot = $this->sphring->getSphringEventDispatcher()->getSphringBoot();
92+
$sphringBoot->getSphringAnnotationReader()->setReader($reader);
93+
}
94+
7795
private function loadBeansFromCache()
7896
{
7997

src/ArthurH/SphringCache/GlobalEvent/CacheSphringFinished.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function run()
4545
$this->cacheSphringContext();
4646
$this->cacheSphringBean();
4747
$this->removeProxies();
48+
$this->removeAnnotation();
4849
}
4950

5051
private function cacheSphringContext()
@@ -103,10 +104,24 @@ private function removeProxies()
103104
$proxiesFolder = new Folder(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
104105
SphringCacheEnum::CACHE_FOLDER . DIRECTORY_SEPARATOR .
105106
SphringCacheEnum::CACHE_FOLDER_PROXIES);
107+
106108
$proxiesFolder->removeFiles('#.*#i', true);
107109
$proxiesFolder->remove();
108110
}
109111

112+
private function removeAnnotation()
113+
{
114+
if ($this->cacheManager->isCacheSphringAnnotation()) {
115+
return;
116+
}
117+
$annotationsFolder = new Folder(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
118+
SphringCacheEnum::CACHE_FOLDER . DIRECTORY_SEPARATOR .
119+
SphringCacheEnum::CACHE_FOLDER_ANNOTATIONS);
120+
$annotationsFolder->removeFiles('#.*#i', true);
121+
$annotationsFolder->removeFolders('#.*#i', true);
122+
$annotationsFolder->remove();
123+
}
124+
110125
/**
111126
* @return AbstractCacheManager
112127
*/

src/ArthurH/SphringCache/Validation/cachebean.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ root:
1010
cacheSphringContext:
1111
_type: text
1212
_required: false
13+
cacheSphringAnnotation:
14+
_type: text
15+
_required: false
1316
cacheSphringBean:
1417
_type: text
1518
_required: false

tests/ArthurH/SphringCache/SphringCacheTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ class SphringCacheTest extends \PHPUnit_Framework_TestCase
3737
* @var Folder
3838
*/
3939
private $proxiesFolder;
40+
/**
41+
* @var Folder
42+
*/
43+
private $annotationFolder;
4044

4145
public function testCachedContextFileLoaded()
4246
{
@@ -63,6 +67,9 @@ public function setUp()
6367
$this->proxiesFolder = new Folder(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
6468
SphringCacheEnum::CACHE_FOLDER . DIRECTORY_SEPARATOR .
6569
SphringCacheEnum::CACHE_FOLDER_PROXIES);
70+
$this->annotationFolder = new Folder(sys_get_temp_dir() . DIRECTORY_SEPARATOR .
71+
SphringCacheEnum::CACHE_FOLDER . DIRECTORY_SEPARATOR .
72+
SphringCacheEnum::CACHE_FOLDER_ANNOTATIONS);
6673
$this->sphring = new Sphring(__DIR__ . '/Resources/mainSimpleTest.yml');
6774
$this->sphring->setComposerLockFile(__DIR__ . '/Resources/composer.lock');
6875
$this->sphring->loadContext();
@@ -72,6 +79,9 @@ public function tearDown()
7279
{
7380
$this->contextCacheFile->remove();
7481
$this->beanCacheFile->remove();
82+
$this->annotationFolder->removeFiles('#.*#i', true);
83+
$this->annotationFolder->removeFolders('#.*#i', true);
84+
$this->annotationFolder->remove();
7585
}
7686

7787
public function testCacheContextFile()
@@ -89,6 +99,12 @@ public function testCacheProxies()
8999
$this->assertCount(3, $files);
90100
}
91101

102+
public function testCacheAnnotations()
103+
{
104+
$files = $this->annotationFolder->getFiles('#.*$#i', true);
105+
$this->assertCount(24, $files);
106+
}
107+
92108
public function testCacheBean()
93109
{
94110
$contextFile = new File(__DIR__ . '/Resources/mainSimpleTest.yml');

0 commit comments

Comments
 (0)