| 1 |
<?php |
| 2 |
|
| 3 |
namespace League\Glide; |
| 4 |
|
| 5 |
use Intervention\Image\ImageManager; |
| 6 |
use InvalidArgumentException; |
| 7 |
use League\Flysystem\Filesystem; |
| 8 |
use League\Flysystem\FilesystemOperator; |
| 9 |
use League\Flysystem\Local\LocalFilesystemAdapter; |
| 10 |
use League\Glide\Api\Api; |
| 11 |
use League\Glide\Manipulators\Background; |
| 12 |
use League\Glide\Manipulators\Blur; |
| 13 |
use League\Glide\Manipulators\Border; |
| 14 |
use League\Glide\Manipulators\Brightness; |
| 15 |
use League\Glide\Manipulators\Contrast; |
| 16 |
use League\Glide\Manipulators\Crop; |
| 17 |
use League\Glide\Manipulators\Encode; |
| 18 |
use League\Glide\Manipulators\Filter; |
| 19 |
use League\Glide\Manipulators\Flip; |
| 20 |
use League\Glide\Manipulators\Gamma; |
| 21 |
use League\Glide\Manipulators\Orientation; |
| 22 |
use League\Glide\Manipulators\Pixelate; |
| 23 |
use League\Glide\Manipulators\Sharpen; |
| 24 |
use League\Glide\Manipulators\Size; |
| 25 |
use League\Glide\Manipulators\Watermark; |
| 26 |
use League\Glide\Responses\ResponseFactoryInterface; |
| 27 |
|
| 28 |
class ServerFactory |
| 29 |
{ |
| 30 |
/** |
| 31 |
* Configuration parameters. |
| 32 |
* |
| 33 |
* @var array |
| 34 |
*/ |
| 35 |
protected $config; |
| 36 |
|
| 37 |
/** |
| 38 |
* Create ServerFactory instance. |
| 39 |
* |
| 40 |
* @param array $config Configuration parameters. |
| 41 |
*/ |
| 42 |
public function __construct(array $config = []) |
| 43 |
{ |
| 44 |
$this->config = $config; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Get configured server. |
| 49 |
* |
| 50 |
* @return Server Configured Glide server. |
| 51 |
*/ |
| 52 |
public function getServer() |
| 53 |
{ |
| 54 |
$server = new Server( |
| 55 |
$this->getSource(), |
| 56 |
$this->getCache(), |
| 57 |
$this->getApi() |
| 58 |
); |
| 59 |
|
| 60 |
$server->setSourcePathPrefix($this->getSourcePathPrefix() ?: ''); |
| 61 |
$server->setCachePathPrefix($this->getCachePathPrefix() ?: ''); |
| 62 |
$server->setGroupCacheInFolders($this->getGroupCacheInFolders()); |
| 63 |
$server->setCacheWithFileExtensions($this->getCacheWithFileExtensions()); |
| 64 |
$server->setDefaults($this->getDefaults()); |
| 65 |
$server->setPresets($this->getPresets()); |
| 66 |
$server->setBaseUrl($this->getBaseUrl() ?: ''); |
| 67 |
$server->setResponseFactory($this->getResponseFactory()); |
| 68 |
$server->setCachePathCallable($this->getCachePathCallable()); |
| 69 |
|
| 70 |
if ($this->getTempDir()) { |
| 71 |
$server->setTempDir($this->getTempDir()); |
| 72 |
} |
| 73 |
|
| 74 |
return $server; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Get source file system. |
| 79 |
* |
| 80 |
* @return FilesystemOperator Source file system. |
| 81 |
*/ |
| 82 |
public function getSource() |
| 83 |
{ |
| 84 |
if (!isset($this->config['source'])) { |
| 85 |
throw new InvalidArgumentException('A "source" file system must be set.'); |
| 86 |
} |
| 87 |
|
| 88 |
if (is_string($this->config['source'])) { |
| 89 |
return new Filesystem( |
| 90 |
new LocalFilesystemAdapter($this->config['source']) |
| 91 |
); |
| 92 |
} |
| 93 |
|
| 94 |
return $this->config['source']; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Get source path prefix. |
| 99 |
* |
| 100 |
* @return string|null Source path prefix. |
| 101 |
*/ |
| 102 |
public function getSourcePathPrefix() |
| 103 |
{ |
| 104 |
if (isset($this->config['source_path_prefix'])) { |
| 105 |
return $this->config['source_path_prefix']; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Get cache file system. |
| 111 |
* |
| 112 |
* @return FilesystemOperator Cache file system. |
| 113 |
*/ |
| 114 |
public function getCache() |
| 115 |
{ |
| 116 |
if (!isset($this->config['cache'])) { |
| 117 |
throw new InvalidArgumentException('A "cache" file system must be set.'); |
| 118 |
} |
| 119 |
|
| 120 |
if (is_string($this->config['cache'])) { |
| 121 |
return new Filesystem( |
| 122 |
new LocalFilesystemAdapter($this->config['cache']) |
| 123 |
); |
| 124 |
} |
| 125 |
|
| 126 |
return $this->config['cache']; |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get cache path prefix. |
| 131 |
* |
| 132 |
* @return string|null Cache path prefix. |
| 133 |
*/ |
| 134 |
public function getCachePathPrefix() |
| 135 |
{ |
| 136 |
if (isset($this->config['cache_path_prefix'])) { |
| 137 |
return $this->config['cache_path_prefix']; |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Get temporary EXIF data directory. |
| 143 |
* |
| 144 |
* @return string |
| 145 |
*/ |
| 146 |
public function getTempDir() |
| 147 |
{ |
| 148 |
if (isset($this->config['temp_dir'])) { |
| 149 |
return $this->config['temp_dir']; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
/** |
| 154 |
* Get cache path callable. |
| 155 |
* |
| 156 |
* @return \Closure|null Cache path callable. |
| 157 |
*/ |
| 158 |
public function getCachePathCallable() |
| 159 |
{ |
| 160 |
return $this->config['cache_path_callable'] ?? null; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* Get the group cache in folders setting. |
| 165 |
* |
| 166 |
* @return bool Whether to group cache in folders. |
| 167 |
*/ |
| 168 |
public function getGroupCacheInFolders() |
| 169 |
{ |
| 170 |
if (isset($this->config['group_cache_in_folders'])) { |
| 171 |
return $this->config['group_cache_in_folders']; |
| 172 |
} |
| 173 |
|
| 174 |
return true; |
| 175 |
} |
| 176 |
|
| 177 |
/** |
| 178 |
* Get the cache with file extensions setting. |
| 179 |
* |
| 180 |
* @return bool Whether to cache with file extensions. |
| 181 |
*/ |
| 182 |
public function getCacheWithFileExtensions() |
| 183 |
{ |
| 184 |
if (isset($this->config['cache_with_file_extensions'])) { |
| 185 |
return $this->config['cache_with_file_extensions']; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* Get watermarks file system. |
| 193 |
* |
| 194 |
* @return FilesystemOperator|null Watermarks file system. |
| 195 |
*/ |
| 196 |
public function getWatermarks() |
| 197 |
{ |
| 198 |
if (!isset($this->config['watermarks'])) { |
| 199 |
return; |
| 200 |
} |
| 201 |
|
| 202 |
if (is_string($this->config['watermarks'])) { |
| 203 |
return new Filesystem( |
| 204 |
new LocalFilesystemAdapter($this->config['watermarks']) |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
return $this->config['watermarks']; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* Get watermarks path prefix. |
| 213 |
* |
| 214 |
* @return string|null Watermarks path prefix. |
| 215 |
*/ |
| 216 |
public function getWatermarksPathPrefix() |
| 217 |
{ |
| 218 |
if (isset($this->config['watermarks_path_prefix'])) { |
| 219 |
return $this->config['watermarks_path_prefix']; |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Get image manipulation API. |
| 225 |
* |
| 226 |
* @return Api Image manipulation API. |
| 227 |
*/ |
| 228 |
public function getApi() |
| 229 |
{ |
| 230 |
return new Api( |
| 231 |
$this->getImageManager(), |
| 232 |
$this->getManipulators() |
| 233 |
); |
| 234 |
} |
| 235 |
|
| 236 |
/** |
| 237 |
* Get Intervention image manager. |
| 238 |
* |
| 239 |
* @return ImageManager Intervention image manager. |
| 240 |
*/ |
| 241 |
public function getImageManager() |
| 242 |
{ |
| 243 |
$driver = 'gd'; |
| 244 |
|
| 245 |
if (isset($this->config['driver'])) { |
| 246 |
$driver = $this->config['driver']; |
| 247 |
} |
| 248 |
|
| 249 |
return new ImageManager([ |
| 250 |
'driver' => $driver, |
| 251 |
]); |
| 252 |
} |
| 253 |
|
| 254 |
/** |
| 255 |
* Get image manipulators. |
| 256 |
* |
| 257 |
* @return array Image manipulators. |
| 258 |
*/ |
| 259 |
public function getManipulators() |
| 260 |
{ |
| 261 |
return [ |
| 262 |
new Orientation(), |
| 263 |
new Crop(), |
| 264 |
new Size($this->getMaxImageSize()), |
| 265 |
new Brightness(), |
| 266 |
new Contrast(), |
| 267 |
new Gamma(), |
| 268 |
new Sharpen(), |
| 269 |
new Filter(), |
| 270 |
new Flip(), |
| 271 |
new Blur(), |
| 272 |
new Pixelate(), |
| 273 |
new Watermark($this->getWatermarks(), $this->getWatermarksPathPrefix() ?: ''), |
| 274 |
new Background(), |
| 275 |
new Border(), |
| 276 |
new Encode(), |
| 277 |
]; |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* Get maximum image size. |
| 282 |
* |
| 283 |
* @return int|null Maximum image size. |
| 284 |
*/ |
| 285 |
public function getMaxImageSize() |
| 286 |
{ |
| 287 |
if (isset($this->config['max_image_size'])) { |
| 288 |
return $this->config['max_image_size']; |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
/** |
| 293 |
* Get default image manipulations. |
| 294 |
* |
| 295 |
* @return array Default image manipulations. |
| 296 |
*/ |
| 297 |
public function getDefaults() |
| 298 |
{ |
| 299 |
if (isset($this->config['defaults'])) { |
| 300 |
return $this->config['defaults']; |
| 301 |
} |
| 302 |
|
| 303 |
return []; |
| 304 |
} |
| 305 |
|
| 306 |
/** |
| 307 |
* Get preset image manipulations. |
| 308 |
* |
| 309 |
* @return array Preset image manipulations. |
| 310 |
*/ |
| 311 |
public function getPresets() |
| 312 |
{ |
| 313 |
if (isset($this->config['presets'])) { |
| 314 |
return $this->config['presets']; |
| 315 |
} |
| 316 |
|
| 317 |
return []; |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Get base URL. |
| 322 |
* |
| 323 |
* @return string|null Base URL. |
| 324 |
*/ |
| 325 |
public function getBaseUrl() |
| 326 |
{ |
| 327 |
if (isset($this->config['base_url'])) { |
| 328 |
return $this->config['base_url']; |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
/** |
| 333 |
* Get response factory. |
| 334 |
* |
| 335 |
* @return ResponseFactoryInterface|null Response factory. |
| 336 |
*/ |
| 337 |
public function getResponseFactory() |
| 338 |
{ |
| 339 |
if (isset($this->config['response'])) { |
| 340 |
return $this->config['response']; |
| 341 |
} |
| 342 |
} |
| 343 |
|
| 344 |
/** |
| 345 |
* Create configured server. |
| 346 |
* |
| 347 |
* @param array $config Configuration parameters. |
| 348 |
* |
| 349 |
* @return Server Configured server. |
| 350 |
*/ |
| 351 |
public static function create(array $config = []) |
| 352 |
{ |
| 353 |
return (new self($config))->getServer(); |
| 354 |
} |
| 355 |
} |
| 356 |
|