1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the IrishDan\ResponsiveImageBundle package. |
4
|
|
|
* |
5
|
|
|
* (c) Daniel Byrne <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE file that was distributed with this source |
8
|
|
|
* code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace IrishDan\ResponsiveImageBundle\EventSubscriber; |
12
|
|
|
|
13
|
|
|
use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvent; |
14
|
|
|
use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvents; |
15
|
|
|
use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper; |
16
|
|
|
use League\Flysystem\FilesystemInterface; |
17
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class StyledImagesSubscriber |
21
|
|
|
* |
22
|
|
|
* @package IrishDan\ResponsiveImageBundle\EventSubscriber |
23
|
|
|
*/ |
24
|
|
|
class StyledImagesSubscriber implements EventSubscriberInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @var FilesystemInterface |
28
|
|
|
*/ |
29
|
|
|
private $temporaryFileSystem; |
30
|
|
|
/** |
31
|
|
|
* @var FilesystemInterface|null |
32
|
|
|
*/ |
33
|
|
|
private $primaryFileSystem; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* StyledImagesSubscriber constructor. |
37
|
|
|
* |
38
|
|
|
* @param PrimaryFileSystemWrapper $PrimaryFileSystemWrapper |
39
|
|
|
* @param FilesystemInterface|null $temporaryFileSystem |
40
|
|
|
*/ |
41
|
|
|
public function __construct(PrimaryFileSystemWrapper $PrimaryFileSystemWrapper, FilesystemInterface $temporaryFileSystem = null) |
42
|
|
|
{ |
43
|
|
|
$this->temporaryFileSystem = $temporaryFileSystem; |
44
|
|
|
$this->primaryFileSystem = $PrimaryFileSystemWrapper->getFileSystem(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @return array |
49
|
|
|
*/ |
50
|
|
|
public static function getSubscribedEvents() |
51
|
|
|
{ |
52
|
|
|
return [ |
53
|
|
|
StyledImagesEvents::STYLED_IMAGES_GENERATED => 'onImagesGenerated', |
54
|
|
|
StyledImagesEvents::STYLED_IMAGES_DELETED => 'onImagesDeleted', |
55
|
|
|
]; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param StyledImagesEvent $event |
60
|
|
|
*/ |
61
|
|
|
public function onImagesGenerated(StyledImagesEvent $event) |
62
|
|
|
{ |
63
|
|
|
$image = $event->getImage(); |
64
|
|
|
$this->temporaryFileSystem->delete($image->getPath()); |
65
|
|
|
|
66
|
|
|
$generatedImages = $event->getStyleImageLocationArray(); |
67
|
|
|
|
68
|
|
|
foreach ($generatedImages as $style => $relativePath) { |
69
|
|
|
$contents = $this->temporaryFileSystem->read($relativePath); |
70
|
|
|
if (!empty($contents)) { |
71
|
|
|
$this->primaryFileSystem->put($relativePath, $contents); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$this->temporaryFileSystem->delete($relativePath); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @param StyledImagesEvent $event |
80
|
|
|
*/ |
81
|
|
|
public function onImagesDeleted(StyledImagesEvent $event) |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
// @TODO: Implement. |
84
|
|
|
} |
85
|
|
|
} |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.