|
5 | 5 | use Assetic\Asset\AssetInterface; |
6 | 6 | use Assetic\Asset\AssetCache; |
7 | 7 | use Assetic\Cache\CacheInterface; |
8 | | -use Assetic\Cache; |
9 | | -use AssetManager\Cache\FilePathCache; |
10 | | -use AssetManager\Exception; |
| 8 | +use Zend\ServiceManager\ServiceLocatorInterface; |
11 | 9 |
|
| 10 | +/** |
| 11 | + * Asset Cache Manager. Sets asset cache based on configuration. |
| 12 | + */ |
12 | 13 | class AssetCacheManager |
13 | 14 | { |
| 15 | + /** |
| 16 | + * @var \Zend\ServiceManager\ServiceLocatorInterface |
| 17 | + */ |
| 18 | + protected $serviceLocator; |
| 19 | + |
14 | 20 | /** |
15 | 21 | * @var array Cache configuration. |
16 | 22 | */ |
17 | | - protected $config; |
| 23 | + protected $config = array(); |
18 | 24 |
|
19 | 25 | /** |
20 | 26 | * Construct the AssetCacheManager |
21 | 27 | * |
22 | | - * @param array $config |
| 28 | + * @param ServiceLocatorInterface $serviceLocator |
| 29 | + * @param array $config |
| 30 | + * |
23 | 31 | * @return AssetCacheManager |
24 | 32 | */ |
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; |
28 | 39 | } |
29 | 40 |
|
30 | 41 | /** |
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 |
32 | 46 | * |
33 | | - * @return array |
| 47 | + * @return AssetCache |
34 | 48 | */ |
35 | | - protected function getConfig() |
| 49 | + public function setCache($path, AssetInterface $asset) |
36 | 50 | { |
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; |
38 | 61 | } |
39 | 62 |
|
40 | 63 | /** |
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 |
42 | 69 | * |
43 | | - * @param array $config |
| 70 | + * @return array |
44 | 71 | */ |
45 | | - protected function setConfig($config) |
| 72 | + private function getProvider($path) |
46 | 73 | { |
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); |
48 | 98 | } |
49 | 99 |
|
50 | 100 | /** |
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. |
52 | 102 | * |
53 | | - * @param string$path |
54 | | - * @param AssetInterface $asset |
| 103 | + * @param $path |
55 | 104 | * |
56 | | - * @return AssetCache |
| 105 | + * @return null|array Cache config definition. Returns null if not found in |
| 106 | + * config. |
57 | 107 | */ |
58 | | - public function setCache($path, AssetInterface $asset) |
| 108 | + private function getCacheProviderConfig($path) |
59 | 109 | { |
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; |
68 | 111 |
|
69 | | - if (null === $caching) { |
70 | | - return $asset; |
| 112 | + if (!empty($this->config[$path]) && !empty($this->config[$path]['cache'])) { |
| 113 | + $cacheProvider = $this->config[$path]; |
71 | 114 | } |
72 | 115 |
|
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']; |
75 | 121 | } |
76 | 122 |
|
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 | + } |
118 | 125 |
|
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; |
121 | 148 | } |
122 | 149 |
|
123 | | - $assetCache = new AssetCache($asset, $cacher); |
124 | | - $assetCache->mimetype = $asset->mimetype; |
125 | | - |
126 | | - return $assetCache; |
| 150 | + return $class; |
127 | 151 | } |
128 | 152 | } |
0 commit comments