Skip to content

Commit 7f1c4f3

Browse files
committed
Merge pull request #128 from wshafer/refactor-asset-cache
Refactor asset cache Manager + Zend Cache Adaptor
2 parents bfba7e3 + 87f8e18 commit 7f1c4f3

14 files changed

+724
-289
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ clover.xml
22
php-cs-fixer.phar
33
composer.json
44
composer.lock
5-
vendor
5+
vendor
6+
.idea

config/module.config.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,15 @@
1111
'AssetManager\Resolver\PrioritizedPathsResolver' => 'AssetManager\Service\PrioritizedPathsResolverServiceFactory',
1212
'AssetManager\Resolver\CollectionResolver' => 'AssetManager\Service\CollectionResolverServiceFactory',
1313
),
14+
1415
'invokables' => array(
15-
'mime_resolver' => 'AssetManager\Service\MimeResolver',
16+
'AssetManager\Service\MimeResolver' => 'AssetManager\Service\MimeResolver',
1617
),
18+
19+
'aliases' => array(
20+
//Alias left here for BC
21+
'mime_resolver' => 'AssetManager\Service\MimeResolver',
22+
)
1723
),
1824
'asset_manager' => array(
1925
'resolvers' => array(
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace AssetManager\Cache;
4+
5+
use Assetic\Cache\CacheInterface;
6+
use Zend\Cache\Storage\StorageInterface;
7+
8+
/**
9+
* Zend Cache Storage Adapter for Assetic
10+
*/
11+
class ZendCacheAdapter implements CacheInterface
12+
{
13+
14+
/** @var StorageInterface */
15+
protected $zendCache;
16+
17+
/**
18+
* Constructor
19+
*
20+
* @param StorageInterface $zendCache Zend Configured Cache Storage
21+
*/
22+
public function __construct(StorageInterface $zendCache)
23+
{
24+
$this->zendCache = $zendCache;
25+
}
26+
27+
/**
28+
* {@inheritDoc}
29+
*/
30+
public function has($key)
31+
{
32+
return $this->zendCache->hasItem($key);
33+
}
34+
35+
/**
36+
* {@inheritDoc}
37+
*/
38+
public function get($key)
39+
{
40+
return $this->zendCache->getItem($key);
41+
}
42+
43+
/**
44+
* {@inheritDoc}
45+
*/
46+
public function set($key, $value)
47+
{
48+
return $this->zendCache->setItem($key, $value);
49+
}
50+
51+
/**
52+
* {@inheritDoc}
53+
*/
54+
public function remove($key)
55+
{
56+
return $this->zendCache->removeItem($key);
57+
}
58+
}

src/AssetManager/Service/AggregateResolverServiceFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
4949
}
5050

5151
if ($resolverService instanceof MimeResolverAwareInterface) {
52-
$resolverService->setMimeResolver($serviceLocator->get('mime_resolver'));
52+
$resolverService->setMimeResolver($serviceLocator->get('AssetManager\Service\MimeResolver'));
5353
}
5454

5555
if ($resolverService instanceof AssetFilterManagerAwareInterface) {

src/AssetManager/Service/AssetCacheManager.php

Lines changed: 104 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -5,124 +5,148 @@
55
use Assetic\Asset\AssetInterface;
66
use Assetic\Asset\AssetCache;
77
use Assetic\Cache\CacheInterface;
8-
use Assetic\Cache;
9-
use AssetManager\Cache\FilePathCache;
10-
use AssetManager\Exception;
8+
use Zend\ServiceManager\ServiceLocatorInterface;
119

10+
/**
11+
* Asset Cache Manager. Sets asset cache based on configuration.
12+
*/
1213
class AssetCacheManager
1314
{
15+
/**
16+
* @var \Zend\ServiceManager\ServiceLocatorInterface
17+
*/
18+
protected $serviceLocator;
19+
1420
/**
1521
* @var array Cache configuration.
1622
*/
17-
protected $config;
23+
protected $config = array();
1824

1925
/**
2026
* Construct the AssetCacheManager
2127
*
22-
* @param array $config
28+
* @param ServiceLocatorInterface $serviceLocator
29+
* @param array $config
30+
*
2331
* @return AssetCacheManager
2432
*/
25-
public function __construct(array $config = array())
26-
{
27-
$this->setConfig($config);
33+
public function __construct(
34+
ServiceLocatorInterface $serviceLocator,
35+
$config
36+
) {
37+
$this->serviceLocator = $serviceLocator;
38+
$this->config = $config;
2839
}
2940

3041
/**
31-
* Get the cache configuration.
42+
* Set the cache (if any) on the asset, and return the new AssetCache.
43+
*
44+
* @param string $path Path to asset
45+
* @param AssetInterface $asset Assetic Asset Interface
3246
*
33-
* @return array
47+
* @return AssetCache
3448
*/
35-
protected function getConfig()
49+
public function setCache($path, AssetInterface $asset)
3650
{
37-
return $this->config;
51+
$provider = $this->getProvider($path);
52+
53+
if (!$provider instanceof CacheInterface) {
54+
return $asset;
55+
}
56+
57+
$assetCache = new AssetCache($asset, $provider);
58+
$assetCache->mimetype = $asset->mimetype;
59+
60+
return $assetCache;
3861
}
3962

4063
/**
41-
* Set the cache configuration.
64+
* Get the cache provider. First checks to see if the provider is callable,
65+
* then will attempt to get it from the service locator, finally will fallback
66+
* to a class mapper.
67+
*
68+
* @param $path
4269
*
43-
* @param array $config
70+
* @return array
4471
*/
45-
protected function setConfig($config)
72+
private function getProvider($path)
4673
{
47-
$this->config = $config;
74+
$cacheProvider = $this->getCacheProviderConfig($path);
75+
76+
if (!$cacheProvider) {
77+
return null;
78+
}
79+
80+
if ($this->serviceLocator->has($cacheProvider['cache'])) {
81+
return $this->serviceLocator->get($cacheProvider['cache']);
82+
}
83+
84+
// Left here for BC. Please consider defining a ZF2 service instead.
85+
if (is_callable($cacheProvider['cache'])) {
86+
return call_user_func($cacheProvider['cache'], $path);
87+
}
88+
89+
$dir = '';
90+
$class = $cacheProvider['cache'];
91+
92+
if (!empty($cacheProvider['options']['dir'])) {
93+
$dir = $cacheProvider['options']['dir'];
94+
}
95+
96+
$class = $this->classMapper($class);
97+
return new $class($dir, $path);
4898
}
4999

50100
/**
51-
* Set the cache (if any) on the asset, and return the new AssetCache.
101+
* Get the cache provider config. Use default values if defined.
52102
*
53-
* @param string$path
54-
* @param AssetInterface $asset
103+
* @param $path
55104
*
56-
* @return AssetCache
105+
* @return null|array Cache config definition. Returns null if not found in
106+
* config.
57107
*/
58-
public function setCache($path, AssetInterface $asset)
108+
private function getCacheProviderConfig($path)
59109
{
60-
$caching = null;
61-
$config = $this->getConfig();
62-
63-
if (!empty($config[$path])) {
64-
$caching = $config[$path];
65-
} elseif (!empty($config['default'])) {
66-
$caching = $config['default'];
67-
}
110+
$cacheProvider = null;
68111

69-
if (null === $caching) {
70-
return $asset;
112+
if (!empty($this->config[$path]) && !empty($this->config[$path]['cache'])) {
113+
$cacheProvider = $this->config[$path];
71114
}
72115

73-
if (empty($caching['cache'])) {
74-
return $asset;
116+
if (!$cacheProvider
117+
&& !empty($this->config['default'])
118+
&& !empty($this->config['default']['cache'])
119+
) {
120+
$cacheProvider = $this->config['default'];
75121
}
76122

77-
$cacher = null;
78-
79-
if (is_callable($caching['cache'])) {
80-
$cacher = $caching['cache']($path);
81-
} else {
82-
// @codeCoverageIgnoreStart
83-
$factories = array(
84-
'FilesystemCache' => function ($options) {
85-
if (empty($options['dir'])) {
86-
throw new Exception\RuntimeException(
87-
'FilesystemCache expected dir entry.'
88-
);
89-
}
90-
$dir = $options['dir'];
91-
return new Cache\FilesystemCache($dir);
92-
},
93-
'ApcCache' => function () {
94-
return new Cache\ApcCache();
95-
},
96-
'FilePathCache' => function ($options) use ($path) {
97-
if (empty($options['dir'])) {
98-
throw new Exception\RuntimeException(
99-
'FilePathCache expected dir entry.'
100-
);
101-
}
102-
$dir = $options['dir'];
103-
return new FilePathCache($dir, $path);
104-
}
105-
);
106-
// @codeCoverageIgnoreEnd
107-
108-
$type = $caching['cache'];
109-
$type .= (substr($type, -5) === 'Cache') ? '' : 'Cache';
110-
111-
if (!isset($factories[$type])) {
112-
return $asset;
113-
}
114-
115-
$options = empty($caching['options']) ? array() : $caching['options'];
116-
$cacher = $factories[$type]($options);
117-
}
123+
return $cacheProvider;
124+
}
118125

119-
if (!$cacher instanceof CacheInterface) {
120-
return $asset;
126+
/**
127+
* Class mapper to provide backwards compatibility
128+
*
129+
* @param $class
130+
*
131+
* @return string
132+
*/
133+
private function classMapper($class)
134+
{
135+
$classToCheck = $class;
136+
$classToCheck .= (substr($class, -5) === 'Cache') ? '' : 'Cache';
137+
138+
switch ($classToCheck) {
139+
case 'ApcCache':
140+
$class = 'Assetic\Cache\ApcCache';
141+
break;
142+
case 'FilesystemCache':
143+
$class = 'Assetic\Cache\FilesystemCache';
144+
break;
145+
case 'FilePathCache':
146+
$class = 'AssetManager\Cache\FilePathCache';
147+
break;
121148
}
122149

123-
$assetCache = new AssetCache($asset, $cacher);
124-
$assetCache->mimetype = $asset->mimetype;
125-
126-
return $assetCache;
150+
return $class;
127151
}
128152
}

src/AssetManager/Service/AssetCacheManagerServiceFactory.php

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
use Zend\ServiceManager\FactoryInterface;
66
use Zend\ServiceManager\ServiceLocatorInterface;
77

8+
/**
9+
* Factory for the Asset Cache Manager Service
10+
*
11+
* @package AssetManager\Service
12+
*/
813
class AssetCacheManagerServiceFactory implements FactoryInterface
914
{
1015
/**
@@ -14,15 +19,14 @@ class AssetCacheManagerServiceFactory implements FactoryInterface
1419
*/
1520
public function createService(ServiceLocatorInterface $serviceLocator)
1621
{
17-
$filters = array();
18-
$config = $serviceLocator->get('Config');
22+
$config = array();
1923

20-
if (!empty($config['asset_manager']['caching'])) {
21-
$filters = $config['asset_manager']['caching'];
22-
}
24+
$globalConfig = $serviceLocator->get('config');
2325

24-
$assetCacheManager = new AssetCacheManager($filters);
26+
if (!empty($globalConfig['asset_manager']['caching'])) {
27+
$config = $globalConfig['asset_manager']['caching'];
28+
}
2529

26-
return $assetCacheManager;
30+
return new AssetCacheManager($serviceLocator, $config);
2731
}
2832
}

src/AssetManager/Service/AssetFilterManagerServiceFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function createService(ServiceLocatorInterface $serviceLocator)
2424
$assetFilterManager = new AssetFilterManager($filters);
2525

2626
$assetFilterManager->setServiceLocator($serviceLocator);
27-
$assetFilterManager->setMimeResolver($serviceLocator->get('mime_resolver'));
27+
$assetFilterManager->setMimeResolver($serviceLocator->get('AssetManager\Service\MimeResolver'));
2828

2929
return $assetFilterManager;
3030
}

0 commit comments

Comments
 (0)