1 | <?php |
||
13 | class FilterManager |
||
14 | { |
||
15 | /** |
||
16 | * @var FilterConfiguration |
||
17 | */ |
||
18 | protected $filterConfig; |
||
19 | |||
20 | /** |
||
21 | * @var ImagineInterface |
||
22 | */ |
||
23 | protected $imagine; |
||
24 | |||
25 | /** |
||
26 | * @var MimeTypeGuesserInterface |
||
27 | */ |
||
28 | protected $mimeTypeGuesser; |
||
29 | |||
30 | /** |
||
31 | * @var LoaderInterface[] |
||
32 | */ |
||
33 | protected $loaders = array(); |
||
34 | |||
35 | /** |
||
36 | * @var PostProcessorInterface[] |
||
37 | */ |
||
38 | protected $postProcessors = array(); |
||
39 | |||
40 | /** |
||
41 | * @param FilterConfiguration $filterConfig |
||
42 | * @param ImagineInterface $imagine |
||
43 | * @param MimeTypeGuesserInterface $mimeTypeGuesser |
||
44 | */ |
||
45 | public function __construct( |
||
54 | |||
55 | /** |
||
56 | * Adds a loader to handle the given filter. |
||
57 | * |
||
58 | * @param string $filter |
||
59 | * @param LoaderInterface $loader |
||
60 | */ |
||
61 | public function addLoader($filter, LoaderInterface $loader) |
||
65 | |||
66 | /** |
||
67 | * Adds a post-processor to handle binaries. |
||
68 | * |
||
69 | * @param string $name |
||
70 | * @param PostProcessorInterface $postProcessor |
||
71 | */ |
||
72 | public function addPostProcessor($name, PostProcessorInterface $postProcessor) |
||
76 | |||
77 | /** |
||
78 | * @return FilterConfiguration |
||
79 | */ |
||
80 | public function getFilterConfiguration() |
||
84 | |||
85 | /** |
||
86 | * @param BinaryInterface $binary |
||
87 | * @param array $config |
||
88 | * |
||
89 | * @throws \InvalidArgumentException |
||
90 | * |
||
91 | * @return Binary |
||
92 | */ |
||
93 | public function apply(BinaryInterface $binary, array $config) |
||
94 | { |
||
95 | $config = array_replace( |
||
96 | array( |
||
97 | 'filters' => array(), |
||
98 | 'quality' => 100, |
||
99 | 'animated' => false, |
||
100 | ), |
||
101 | $config |
||
102 | ); |
||
103 | |||
104 | if ($binary instanceof FileBinaryInterface) { |
||
105 | $image = $this->imagine->open($binary->getPath()); |
||
106 | } else { |
||
107 | $image = $this->imagine->load($binary->getContent()); |
||
108 | } |
||
109 | |||
110 | foreach ($config['filters'] as $eachFilter => $eachOptions) { |
||
111 | if (!isset($this->loaders[$eachFilter])) { |
||
112 | throw new \InvalidArgumentException(sprintf( |
||
113 | 'Could not find filter loader for "%s" filter type', $eachFilter |
||
114 | )); |
||
115 | } |
||
116 | |||
117 | $prevImage = $image; |
||
118 | $image = $this->loaders[$eachFilter]->load($image, $eachOptions); |
||
119 | |||
120 | // If the filter returns a different image object destruct the old one because imagick keeps consuming memory if we don't |
||
121 | // See https://github.com/liip/LiipImagineBundle/pull/682 |
||
122 | if ($prevImage !== $image && method_exists($prevImage, '__destruct')) { |
||
123 | $prevImage->__destruct(); |
||
|
|||
124 | } |
||
125 | } |
||
126 | |||
127 | $options = array( |
||
128 | 'quality' => $config['quality'], |
||
129 | ); |
||
130 | |||
131 | if (isset($config['jpeg_quality'])) { |
||
132 | $options['jpeg_quality'] = $config['jpeg_quality']; |
||
133 | } |
||
134 | if (isset($config['png_compression_level'])) { |
||
135 | $options['png_compression_level'] = $config['png_compression_level']; |
||
136 | } |
||
137 | if (isset($config['png_compression_filter'])) { |
||
138 | $options['png_compression_filter'] = $config['png_compression_filter']; |
||
139 | } |
||
140 | |||
141 | if ($binary->getFormat() === 'gif' && $config['animated']) { |
||
142 | $options['animated'] = $config['animated']; |
||
143 | } |
||
144 | |||
145 | $filteredFormat = isset($config['format']) ? $config['format'] : $binary->getFormat(); |
||
146 | $filteredContent = $image->get($filteredFormat, $options); |
||
147 | $filteredMimeType = $filteredFormat === $binary->getFormat() ? $binary->getMimeType() : $this->mimeTypeGuesser->guess($filteredContent); |
||
148 | |||
149 | // We are done with the image object so we can destruct the this because imagick keeps consuming memory if we don't |
||
150 | // See https://github.com/liip/LiipImagineBundle/pull/682 |
||
151 | if (method_exists($image, '__destruct')) { |
||
152 | $image->__destruct(); |
||
153 | } |
||
154 | |||
155 | return $this->applyPostProcessors(new Binary($filteredContent, $filteredMimeType, $filteredFormat), $config); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * @param BinaryInterface $binary |
||
160 | * @param array $config |
||
161 | * |
||
162 | * @throws \InvalidArgumentException |
||
163 | * |
||
164 | * @return BinaryInterface |
||
165 | */ |
||
166 | public function applyPostProcessors(BinaryInterface $binary, $config) |
||
180 | |||
181 | /** |
||
182 | * Apply the provided filter set on the given binary. |
||
183 | * |
||
184 | * @param BinaryInterface $binary |
||
185 | * @param string $filter |
||
186 | * @param array $runtimeConfig |
||
187 | * |
||
188 | * @throws \InvalidArgumentException |
||
189 | * |
||
190 | * @return BinaryInterface |
||
191 | */ |
||
192 | public function applyFilter(BinaryInterface $binary, $filter, array $runtimeConfig = array()) |
||
201 | } |
||
202 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: