1 | <?php |
||
15 | class CacheManager |
||
16 | { |
||
17 | /** |
||
18 | * @var FilterConfiguration |
||
19 | */ |
||
20 | protected $filterConfig; |
||
21 | |||
22 | /** |
||
23 | * @var RouterInterface |
||
24 | */ |
||
25 | protected $router; |
||
26 | |||
27 | /** |
||
28 | * @var ResolverInterface[] |
||
29 | */ |
||
30 | protected $resolvers = array(); |
||
31 | |||
32 | /** |
||
33 | * @var SignerInterface |
||
34 | */ |
||
35 | protected $signer; |
||
36 | |||
37 | /** |
||
38 | * @var \Symfony\Component\EventDispatcher\EventDispatcherInterface |
||
39 | */ |
||
40 | protected $dispatcher; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $defaultResolver; |
||
46 | |||
47 | /** |
||
48 | * Constructs the cache manager to handle Resolvers based on the provided FilterConfiguration. |
||
49 | * |
||
50 | * @param FilterConfiguration $filterConfig |
||
51 | * @param RouterInterface $router |
||
52 | * @param SignerInterface $signer |
||
53 | * @param EventDispatcherInterface $dispatcher |
||
54 | * @param string $defaultResolver |
||
55 | */ |
||
56 | public function __construct( |
||
57 | FilterConfiguration $filterConfig, |
||
58 | RouterInterface $router, |
||
59 | SignerInterface $signer, |
||
60 | EventDispatcherInterface $dispatcher, |
||
61 | $defaultResolver = null |
||
62 | ) { |
||
63 | $this->filterConfig = $filterConfig; |
||
64 | $this->router = $router; |
||
65 | $this->signer = $signer; |
||
66 | $this->dispatcher = $dispatcher; |
||
67 | $this->defaultResolver = $defaultResolver ?: 'default'; |
||
68 | } |
||
69 | |||
70 | /** |
||
71 | * Adds a resolver to handle cached images for the given filter. |
||
72 | * |
||
73 | * @param string $filter |
||
74 | * @param ResolverInterface $resolver |
||
75 | */ |
||
76 | public function addResolver($filter, ResolverInterface $resolver) |
||
77 | { |
||
78 | $this->resolvers[$filter] = $resolver; |
||
79 | |||
80 | if ($resolver instanceof CacheManagerAwareInterface) { |
||
81 | $resolver->setCacheManager($this); |
||
82 | } |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Gets a resolver for the given filter. |
||
87 | * |
||
88 | * In case there is no specific resolver, but a default resolver has been configured, the default will be returned. |
||
89 | * |
||
90 | * @param string $filter |
||
91 | * @param string $resolver |
||
92 | * |
||
93 | * @return ResolverInterface |
||
94 | * |
||
95 | * @throws \OutOfBoundsException If neither a specific nor a default resolver is available. |
||
96 | */ |
||
97 | protected function getResolver($filter, $resolver) |
||
98 | { |
||
99 | // BC |
||
100 | if (false == $resolver) { |
||
|
|||
101 | $config = $this->filterConfig->get($filter); |
||
102 | |||
103 | $resolverName = empty($config['cache']) ? $this->defaultResolver : $config['cache']; |
||
104 | } else { |
||
105 | $resolverName = $resolver; |
||
106 | } |
||
107 | |||
108 | if (!isset($this->resolvers[$resolverName])) { |
||
109 | throw new \OutOfBoundsException(sprintf( |
||
110 | 'Could not find resolver "%s" for "%s" filter type', |
||
111 | $resolverName, |
||
112 | $filter |
||
113 | )); |
||
114 | } |
||
115 | |||
116 | return $this->resolvers[$resolverName]; |
||
117 | } |
||
118 | |||
119 | /** |
||
120 | * Gets filtered path for rendering in the browser. |
||
121 | * It could be the cached one or an url of filter action. |
||
122 | * |
||
123 | * @param string $path The path where the resolved file is expected. |
||
124 | * @param string $filter |
||
125 | * @param array $runtimeConfig |
||
126 | * @param string $resolver |
||
127 | * |
||
128 | * @return string |
||
129 | */ |
||
130 | public function getBrowserPath($path, $filter, array $runtimeConfig = array(), $resolver = null) |
||
131 | { |
||
132 | if (!empty($runtimeConfig)) { |
||
133 | $rcPath = $this->getRuntimePath($path, $runtimeConfig); |
||
134 | |||
135 | return $this->isStored($rcPath, $filter, $resolver) ? |
||
136 | $this->resolve($rcPath, $filter, $resolver) : |
||
137 | $this->generateUrl($path, $filter, $runtimeConfig, $resolver) |
||
138 | ; |
||
139 | } |
||
140 | |||
141 | return $this->isStored($path, $filter, $resolver) ? |
||
142 | $this->resolve($path, $filter, $resolver) : |
||
143 | $this->generateUrl($path, $filter, array(), $resolver) |
||
144 | ; |
||
145 | } |
||
146 | |||
147 | /** |
||
148 | * Get path to runtime config image. |
||
149 | * |
||
150 | * @param string $path |
||
151 | * @param array $runtimeConfig |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getRuntimePath($path, array $runtimeConfig) |
||
159 | |||
160 | /** |
||
161 | * Returns a web accessible URL. |
||
162 | * |
||
163 | * @param string $path The path where the resolved file is expected. |
||
164 | * @param string $filter The name of the imagine filter in effect. |
||
165 | * @param array $runtimeConfig |
||
166 | * @param string $resolver |
||
167 | * |
||
168 | * @return string |
||
169 | */ |
||
170 | public function generateUrl($path, $filter, array $runtimeConfig = array(), $resolver = null) |
||
192 | |||
193 | /** |
||
194 | * Checks whether the path is already stored within the respective Resolver. |
||
195 | * |
||
196 | * @param string $path |
||
197 | * @param string $filter |
||
198 | * @param string $resolver |
||
199 | * |
||
200 | * @return bool |
||
201 | */ |
||
202 | public function isStored($path, $filter, $resolver = null) |
||
206 | |||
207 | /** |
||
208 | * Resolves filtered path for rendering in the browser. |
||
209 | * |
||
210 | * @param string $path |
||
211 | * @param string $filter |
||
212 | * @param string $resolver |
||
213 | * |
||
214 | * @return string The url of resolved image. |
||
215 | * |
||
216 | * @throws NotFoundHttpException if the path can not be resolved |
||
217 | */ |
||
218 | public function resolve($path, $filter, $resolver = null) |
||
219 | { |
||
220 | if (false !== strpos($path, '/../') || 0 === strpos($path, '../')) { |
||
221 | throw new NotFoundHttpException(sprintf("Source image was searched with '%s' outside of the defined root path", $path)); |
||
222 | } |
||
223 | |||
224 | $preEvent = new CacheResolveEvent($path, $filter); |
||
225 | $this->dispatcher->dispatch(ImagineEvents::PRE_RESOLVE, $preEvent); |
||
226 | |||
227 | $url = $this->getResolver($preEvent->getFilter(), $resolver)->resolve($preEvent->getPath(), $preEvent->getFilter()); |
||
228 | |||
229 | $postEvent = new CacheResolveEvent($preEvent->getPath(), $preEvent->getFilter(), $url); |
||
230 | $this->dispatcher->dispatch(ImagineEvents::POST_RESOLVE, $postEvent); |
||
231 | |||
232 | return $postEvent->getUrl(); |
||
233 | } |
||
234 | |||
235 | /** |
||
236 | * @see ResolverInterface::store |
||
237 | * |
||
238 | * @param BinaryInterface $binary |
||
239 | * @param string $path |
||
240 | * @param string $filter |
||
241 | * @param string $resolver |
||
242 | */ |
||
243 | public function store(BinaryInterface $binary, $path, $filter, $resolver = null) |
||
247 | |||
248 | /** |
||
249 | * @param string|string[]|null $paths |
||
250 | * @param string|string[]|null $filters |
||
251 | */ |
||
252 | public function remove($paths = null, $filters = null) |
||
282 | } |
||
283 |