1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AssetManager\Service; |
4
|
|
|
|
5
|
|
|
use AssetManager\Core\Exception\InvalidArgumentException; |
6
|
|
|
use AssetManager\Core\Resolver\ResolverInterface; |
7
|
|
|
use AssetManager\Core\Service\AssetManager; |
8
|
|
|
use AssetManager\View\Helper\Asset; |
9
|
|
|
use Interop\Container\ContainerInterface; |
10
|
|
|
use Zend\Cache\Storage\Adapter\AbstractAdapter as AbstractCacheAdapter; |
11
|
|
|
|
12
|
|
|
class AssetViewHelperFactory |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @inheritDoc |
16
|
|
|
*/ |
17
|
1 |
|
public function __invoke(ContainerInterface $container) |
18
|
|
|
{ |
19
|
1 |
|
$config = $container->get('config')['asset_manager']; |
20
|
|
|
|
21
|
|
|
/** @var ResolverInterface $assetManagerResolver */ |
22
|
1 |
|
$assetManagerResolver = $container->get(AssetManager::class)->getResolver(); |
23
|
|
|
|
24
|
|
|
/** @var AbstractCacheAdapter|null $cache */ |
25
|
1 |
|
$cache = $this->loadCache($container, $config); |
26
|
|
|
|
27
|
1 |
|
return new Asset($assetManagerResolver, $cache, $config); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param ContainerInterface $container |
32
|
|
|
* @param array $config |
33
|
|
|
* |
34
|
|
|
* @return null |
35
|
|
|
*/ |
36
|
1 |
|
private function loadCache(ContainerInterface $container, $config) |
37
|
|
|
{ |
38
|
|
|
// check if the cache is configured |
39
|
1 |
|
if (!isset($config['view_helper']['cache']) || $config['view_helper']['cache'] === null) { |
40
|
1 |
|
return null; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
// get the cache, if it's a string, search it among services |
44
|
|
|
$cache = $config['view_helper']['cache']; |
45
|
|
|
if (is_string($cache)) { |
46
|
|
|
$cache = $container->get($cache); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// exception in case cache is not an Adapter that extend the AbstractAdapter of Zend\Cache\Storage |
50
|
|
|
if ($cache !== null && !($cache instanceof AbstractCacheAdapter)) { |
51
|
|
|
throw new InvalidArgumentException( |
52
|
|
|
'Invalid cache provided, you must pass a Cache Adapter that extend |
53
|
|
|
Zend\Cache\Storage\Adapter\AbstractAdapter' |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $cache; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|