/
DataManager.php
167 lines (143 loc) · 4.44 KB
/
DataManager.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
/*
* This file is part of the `liip/LiipImagineBundle` project.
*
* (c) https://github.com/liip/LiipImagineBundle/graphs/contributors
*
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/
namespace Liip\ImagineBundle\Imagine\Data;
use Liip\ImagineBundle\Binary\BinaryInterface;
use Liip\ImagineBundle\Binary\Loader\LoaderInterface;
use Liip\ImagineBundle\Binary\MimeTypeGuesserInterface;
use Liip\ImagineBundle\Imagine\Filter\FilterConfiguration;
use Liip\ImagineBundle\Model\Binary;
use Symfony\Component\HttpFoundation\File\MimeType\ExtensionGuesserInterface;
class DataManager
{
/**
* @var MimeTypeGuesserInterface
*/
protected $mimeTypeGuesser;
/**
* @var ExtensionGuesserInterface
*/
protected $extensionGuesser;
/**
* @var FilterConfiguration
*/
protected $filterConfig;
/**
* @var string|null
*/
protected $defaultLoader;
/**
* @var string|null
*/
protected $globalDefaultImage;
/**
* @var LoaderInterface[]
*/
protected $loaders = [];
/**
* @param MimeTypeGuesserInterface $mimeTypeGuesser
* @param ExtensionGuesserInterface $extensionGuesser
* @param FilterConfiguration $filterConfig
* @param string $defaultLoader
* @param string $globalDefaultImage
*/
public function __construct(
MimeTypeGuesserInterface $mimeTypeGuesser,
ExtensionGuesserInterface $extensionGuesser,
FilterConfiguration $filterConfig,
$defaultLoader = null,
$globalDefaultImage = null
) {
$this->mimeTypeGuesser = $mimeTypeGuesser;
$this->filterConfig = $filterConfig;
$this->defaultLoader = $defaultLoader;
$this->extensionGuesser = $extensionGuesser;
$this->globalDefaultImage = $globalDefaultImage;
}
/**
* Adds a loader to retrieve images for the given filter.
*
* @param string $filter
* @param LoaderInterface $loader
*/
public function addLoader($filter, LoaderInterface $loader)
{
$this->loaders[$filter] = $loader;
}
/**
* Returns a loader previously attached to the given filter.
*
* @param string $filter
*
* @throws \InvalidArgumentException
*
* @return LoaderInterface
*/
public function getLoader($filter)
{
$config = $this->filterConfig->get($filter);
$loaderName = empty($config['data_loader']) ? $this->defaultLoader : $config['data_loader'];
if (!isset($this->loaders[$loaderName])) {
throw new \InvalidArgumentException(sprintf(
'Could not find data loader "%s" for "%s" filter type',
$loaderName,
$filter
));
}
return $this->loaders[$loaderName];
}
/**
* Retrieves an image with the given filter applied.
*
* @param string $filter
* @param string $path
*
* @throws \LogicException
*
* @return BinaryInterface
*/
public function find($filter, $path)
{
$loader = $this->getLoader($filter);
$binary = $loader->find($path);
if (!$binary instanceof BinaryInterface) {
$mimeType = $this->mimeTypeGuesser->guess($binary);
$binary = new Binary(
$binary,
$mimeType,
$this->extensionGuesser->guess($mimeType)
);
}
if (null === $binary->getMimeType()) {
throw new \LogicException(sprintf('The mime type of image %s was not guessed.', $path));
}
if (0 !== strpos($binary->getMimeType(), 'image/')) {
throw new \LogicException(sprintf('The mime type of image %s must be image/xxx got %s.', $path, $binary->getMimeType()));
}
return $binary;
}
/**
* Get default image url with the given filter applied.
*
* @param string $filter
*
* @return string|null
*/
public function getDefaultImageUrl($filter)
{
$config = $this->filterConfig->get($filter);
$defaultImage = null;
if (false == empty($config['default_image'])) {
$defaultImage = $config['default_image'];
} elseif (!empty($this->globalDefaultImage)) {
$defaultImage = $this->globalDefaultImage;
}
return $defaultImage;
}
}