1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JK\Sam\Filter; |
4
|
|
|
|
5
|
|
|
use JK\Sam\Configuration\ConfigurationInterface; |
6
|
|
|
use JK\Sam\Event\NotificationEvent; |
7
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
8
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
9
|
|
|
use Symfony\Component\Finder\Finder; |
10
|
|
|
use SplFileInfo; |
11
|
|
|
|
12
|
|
|
abstract class Filter implements FilterInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Filter's name |
16
|
|
|
* |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
protected $name; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Filter's configuration |
23
|
|
|
* |
24
|
|
|
* @var ConfigurationInterface |
25
|
|
|
*/ |
26
|
|
|
protected $configuration; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Notification dispatcher |
30
|
|
|
* |
31
|
|
|
* @var EventDispatcherInterface |
32
|
|
|
*/ |
33
|
|
|
protected $eventDispatcher; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Apply a filter to a list of source files |
37
|
|
|
* |
38
|
|
|
* @param SplFileInfo[] $files |
39
|
|
|
* @param SplFileInfo[] $destinations |
40
|
|
|
* @return SplFileInfo[] |
41
|
|
|
*/ |
42
|
|
|
abstract public function run(array $files, array $destinations); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Filter constructor. |
46
|
|
|
* |
47
|
|
|
* @param string $name |
48
|
|
|
* @param ConfigurationInterface $configuration |
49
|
|
|
* @param EventDispatcherInterface $eventDispatcher |
50
|
|
|
*/ |
51
|
|
|
public function __construct( |
52
|
|
|
$name, |
53
|
|
|
ConfigurationInterface $configuration, |
54
|
|
|
EventDispatcherInterface $eventDispatcher |
55
|
|
|
) { |
56
|
|
|
$this->name = $name; |
57
|
|
|
$this->configuration = $configuration; |
58
|
|
|
$this->eventDispatcher = $eventDispatcher; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Use this method to check your filter requirements (for example, if the library required by this filter are |
63
|
|
|
* installed). |
64
|
|
|
*/ |
65
|
|
|
public function checkRequirements() |
66
|
|
|
{ |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Remove the assets cache directory. |
71
|
|
|
*/ |
72
|
|
|
public function clean() |
73
|
|
|
{ |
74
|
|
|
if (file_exists($this->getCacheDir())) { |
75
|
|
|
$fileSystem = new Filesystem(); |
76
|
|
|
$fileSystem->remove($this->getCacheDir()); |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Return the assets cache dir. |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function getCacheDir() |
86
|
|
|
{ |
87
|
|
|
return 'var/cache/assets/'.$this->name.'/'; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
|
|
public function getName() |
94
|
|
|
{ |
95
|
|
|
return $this->name; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return files matching $pattern in filters cache directory. |
100
|
|
|
* |
101
|
|
|
* @param string $pattern |
102
|
|
|
* @return Finder|SplFileInfo[] |
103
|
|
|
*/ |
104
|
|
|
protected function findFilesInCacheDir($pattern) |
105
|
|
|
{ |
106
|
|
|
$finder = new Finder(); |
107
|
|
|
|
108
|
|
|
return $finder |
109
|
|
|
->name($pattern) |
110
|
|
|
->in($this->getCacheDir()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Add a notification to the subscriber. |
115
|
|
|
* |
116
|
|
|
* @param $message |
117
|
|
|
*/ |
118
|
|
|
protected function addNotification($message) |
119
|
|
|
{ |
120
|
|
|
$event = new NotificationEvent(); |
121
|
|
|
$event->setMessage($message); |
122
|
|
|
|
123
|
|
|
$this |
124
|
|
|
->eventDispatcher |
125
|
|
|
->dispatch(NotificationEvent::NAME, $event); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|