-
Notifications
You must be signed in to change notification settings - Fork 2
/
ImageEntityNameResolver.php
91 lines (73 loc) · 2.38 KB
/
ImageEntityNameResolver.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
<?php
/**
* This file is part of the IrishDan\ResponsiveImageBundle package.
*
* (c) Daniel Byrne <danielbyrne@outlook.com>
*
* For the full copyright and license information, please view the LICENSE file that was distributed with this source
* code.
*/
namespace IrishDan\ResponsiveImageBundle;
use Symfony\Component\Cache\Simple\FilesystemCache;
class ImageEntityNameResolver
{
const CACHE_KEY = 'responsive_image.image_entity';
protected $className = null;
protected $classLocator;
protected $cache;
protected $imageEntityParameter;
public function __construct(ImageEntityClassLocator $classLocator, $imageEntityParameter = '')
{
$this->classLocator = $classLocator;
$this->imageEntityParameter = $imageEntityParameter;
}
public function classExists()
{
$class = $this->getClassName();
$locatedClass = $this->classLocator->getClassName();
return ($class === $locatedClass) ? true : false;
}
/**
* @return mixed
*/
public function getClassName()
{
// If it's set here just return that.
if (!empty($this->className)) {
return $this->className;
}
// Use class name parameter if its been set
if (!empty($this->imageEntityParameter)) {
$this->className = $this->imageEntityParameter;
return $this->className;
}
// Load it from cached data if it exists
// as a last resort use the Class locator service.
if (empty($this->cache)) {
$this->cache = new FilesystemCache();
}
$cached = $this->cache->has(self::CACHE_KEY);
if ($cached) {
$classname = $this->cache->get(self::CACHE_KEY);
}
// LAt resort use the Class locator service
if (empty($classname)) {
$classname = $this->classLocator->getClassName();
}
// Set the value in as a property in this class
// and set as a cached value for next time.
if (!empty($classname)) {
$this->className = $classname;
if (!$cached) {
// @TODO: Should set cache expiration
$this->cache->set(self::CACHE_KEY, $classname);
}
return $this->className;
}
return false;
}
public function getCache()
{
return $this->cache;
}
}