This repository has been archived by the owner on Sep 4, 2018. It is now read-only.
/
Media.php
111 lines (93 loc) · 2.39 KB
/
Media.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
<?php
/**
* this file is part of the pok package.
*
* (c) florent denis <florentdenisp@gmail.com>
*
* for the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Pok\Media;
use Pok\Media\Cache\CacheInterface;
use Pok\Media\Cache\ArrayCache;
use Pok\Media\ServiceManager;
use Pok\Media\MediaInterface;
use Zend\Uri\Uri;
/**
* @author Florent Denis <florentdenisp@gmail.com>
*/
class Media implements MediaInterface
{
/**
* @var \Pok\Media\ServiceManager
*/
protected $serviceManager;
/**
* @var \Pok\Media\Cache\CacheInterface
*/
protected $cache;
/**
* Constructor.
*
* @param null|\Pok\Media\Cache\CacheInterface $cache
*/
public function __construct(CacheInterface $cache = null)
{
$this->serviceManager = new ServiceManager();
if (null === $cache) {
$cache = new ArrayCache();
}
$this->cache = $cache;
}
/**
* @return \Pok\Media\ServiceManager
*/
public function getServiceManager()
{
return $this->serviceManager;
}
/**
* @param \Pok\Media\Cache\CacheInterface $cache
*/
public function setCache(CacheInterface $cache)
{
$this->cache = $cache;
}
/**
* @return \Pok\Media\Cache\CacheInterface
*/
public function getCache()
{
return $this->cache;
}
/**
* @param \Zend\Uri\Uri $uri
*
* @return mixed False if service or namespace nofound
*/
public function analyse(Uri $uri)
{
$service = $this->serviceManager->searchService($uri->toString());
if (false === $service) {
return false;
}
$namespace = $this->serviceManager->getServiceNamespace($service);
if (null === $namespace || $namespace === '') {
return false;
}
$clear = $namespace::clearUri($uri);
if (false === $clear) {
return false;
}
$keyCache = $uri->toString();
// cache because the analyse process requires a lot of resources
$analyser = $this->cache->get($keyCache);
if (null !== $analyser) {
return $analyser;
}
// analyse process
$analyser = new $namespace();
$analyser->parse($uri);
return $this->cache->set($keyCache, $analyser);
}
}