1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AssetManager\Core\Service; |
4
|
|
|
|
5
|
|
|
use Assetic\Asset\AssetCache; |
6
|
|
|
use Assetic\Asset\AssetInterface; |
7
|
|
|
use Assetic\Cache\CacheInterface; |
8
|
|
|
use Psr\Container\ContainerInterface; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Asset Cache Manager. Sets asset cache based on configuration. |
12
|
|
|
*/ |
13
|
|
|
class AssetCacheManager |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @var \Psr\Container\ContainerInterface; |
17
|
|
|
*/ |
18
|
|
|
protected $container; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array Cache configuration. |
22
|
|
|
*/ |
23
|
|
|
protected $config = array(); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Construct the AssetCacheManager |
27
|
|
|
* |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
* @param array $config |
30
|
|
|
*/ |
31
|
|
|
public function __construct( |
32
|
|
|
ContainerInterface $container, |
33
|
|
|
$config |
34
|
|
|
) { |
35
|
|
|
$this->container = $container; |
36
|
|
|
$this->config = $config; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Set the cache (if any) on the asset, and return the new AssetCache. |
41
|
|
|
* |
42
|
|
|
* @param string $path Path to asset |
43
|
|
|
* @param AssetInterface $asset Assetic Asset Interface |
44
|
|
|
* |
45
|
|
|
* @return AssetInterface|CacheInterface |
46
|
|
|
*/ |
47
|
|
|
public function setCache($path, AssetInterface $asset) |
48
|
|
|
{ |
49
|
|
|
$provider = $this->getProvider($path); |
50
|
|
|
|
51
|
|
|
if (!$provider instanceof CacheInterface) { |
52
|
|
|
return $asset; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$assetCache = new AssetCache($asset, $provider); |
56
|
|
|
$assetCache->mimetype = $asset->mimetype; |
|
|
|
|
57
|
|
|
|
58
|
|
|
return $assetCache; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Get the cache provider. First checks to see if the provider is callable, |
63
|
|
|
* then will attempt to get it from the service locator, finally will fallback |
64
|
|
|
* to a class mapper. |
65
|
|
|
* |
66
|
|
|
* @param $path |
67
|
|
|
* |
68
|
|
|
* @return array |
69
|
|
|
*/ |
70
|
|
|
private function getProvider($path) |
71
|
|
|
{ |
72
|
|
|
$cacheProvider = $this->getCacheProviderConfig($path); |
73
|
|
|
|
74
|
|
|
if (!$cacheProvider) { |
75
|
|
|
return null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (is_string($cacheProvider['cache']) && |
79
|
|
|
$this->container->has($cacheProvider['cache']) |
80
|
|
|
) { |
81
|
|
|
return $this->container->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); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Get the cache provider config. Use default values if defined. |
102
|
|
|
* |
103
|
|
|
* @param $path |
104
|
|
|
* |
105
|
|
|
* @return null|array Cache config definition. Returns null if not found in |
106
|
|
|
* config. |
107
|
|
|
*/ |
108
|
|
|
private function getCacheProviderConfig($path) |
109
|
|
|
{ |
110
|
|
|
$cacheProvider = null; |
111
|
|
|
|
112
|
|
|
if (!empty($this->config[$path]) && !empty($this->config[$path]['cache'])) { |
113
|
|
|
$cacheProvider = $this->config[$path]; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if (!$cacheProvider |
117
|
|
|
&& !empty($this->config['default']) |
118
|
|
|
&& !empty($this->config['default']['cache']) |
119
|
|
|
) { |
120
|
|
|
$cacheProvider = $this->config['default']; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $cacheProvider; |
124
|
|
|
} |
125
|
|
|
|
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\Core\Cache\FilePathCache'; |
147
|
|
|
break; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $class; |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.