1 | <?php |
||
32 | trait FileUploadTrait |
||
33 | { |
||
34 | |||
35 | /** |
||
36 | * The directory with the Magento media files => target directory for images (relative to the root directory). |
||
37 | * |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $mediaDir; |
||
41 | |||
42 | /** |
||
43 | * The directory with the images that have to be imported (relative to the root directory). |
||
44 | * |
||
45 | * @var string |
||
46 | */ |
||
47 | protected $imagesFileDir; |
||
48 | |||
49 | /** |
||
50 | * Contains the mappings for the image names that has been uploaded (old => new image name). |
||
51 | * |
||
52 | * @var array |
||
53 | */ |
||
54 | protected $imageMappings = array(); |
||
55 | |||
56 | /** |
||
57 | * The flag whether to copy the images or not. |
||
58 | * |
||
59 | * @var boolean |
||
60 | */ |
||
61 | protected $copyImages = false; |
||
62 | |||
63 | /** |
||
64 | * Whether or not to override images with the same name. |
||
65 | * |
||
66 | * @var bool |
||
67 | */ |
||
68 | protected $overrideImages = false; |
||
69 | |||
70 | /** |
||
71 | * Sets whether or not to override images with the same name. |
||
72 | * |
||
73 | * @param $overrideImages |
||
74 | * |
||
75 | * @return void |
||
76 | */ |
||
77 | public function setOverrideImages($overrideImages) |
||
81 | |||
82 | /** |
||
83 | * Returns whether or not we should override images with the same name. |
||
84 | * |
||
85 | * @return bool |
||
86 | */ |
||
87 | public function shouldOverride() |
||
91 | |||
92 | /** |
||
93 | * Set's the flag to copy the images or not. |
||
94 | * |
||
95 | * @param boolean $copyImages The flag |
||
96 | * |
||
97 | * @return void |
||
98 | */ |
||
99 | public function setCopyImages($copyImages) |
||
103 | |||
104 | /** |
||
105 | * Return's the flag to copy images or not. |
||
106 | * |
||
107 | * @return boolean The flag |
||
108 | */ |
||
109 | public function hasCopyImages() |
||
113 | |||
114 | /** |
||
115 | * Set's directory with the Magento media files => target directory for images. |
||
116 | * |
||
117 | * @param string $mediaDir The directory with the Magento media files => target directory for images |
||
118 | * |
||
119 | * @return void |
||
120 | */ |
||
121 | public function setMediaDir($mediaDir) |
||
125 | |||
126 | /** |
||
127 | * Return's the directory with the Magento media files => target directory for images. |
||
128 | * |
||
129 | * @return string The directory with the Magento media files => target directory for images |
||
130 | */ |
||
131 | public function getMediaDir() |
||
135 | |||
136 | /** |
||
137 | * Set's directory with the images that have to be imported. |
||
138 | * |
||
139 | * @param string $imagesFileDir The directory with the images that have to be imported |
||
140 | * |
||
141 | * @return void |
||
142 | */ |
||
143 | public function setImagesFileDir($imagesFileDir) |
||
147 | |||
148 | /** |
||
149 | * Return's the directory with the images that have to be imported. |
||
150 | * |
||
151 | * @return string The directory with the images that have to be imported |
||
152 | */ |
||
153 | public function getImagesFileDir() |
||
157 | |||
158 | /** |
||
159 | * Adds the mapping from the filename => new filename. |
||
160 | * |
||
161 | * @param string $filename The filename |
||
162 | * @param string $newFilename The new filename |
||
163 | * |
||
164 | * @return void |
||
165 | */ |
||
166 | public function addImageMapping($filename, $newFilename) |
||
170 | |||
171 | /** |
||
172 | * Returns the mapped filename (which is the new filename). |
||
173 | * |
||
174 | * @param string $filename The filename to map |
||
175 | * |
||
176 | * @return string The mapped filename |
||
177 | */ |
||
178 | public function getImageMapping($filename) |
||
189 | |||
190 | /** |
||
191 | * Returns TRUE, if the passed filename has already been mapped. |
||
192 | * |
||
193 | * @param string $filename The filename to query for |
||
194 | * |
||
195 | * @return boolean TRUE if the filename has already been mapped, else FALSE |
||
196 | */ |
||
197 | public function imageHasBeenMapped($filename) |
||
201 | |||
202 | /** |
||
203 | * Returns TRUE, if the passed filename has NOT been mapped yet. |
||
204 | * |
||
205 | * @param string $filename The filename to query for |
||
206 | * |
||
207 | * @return boolean TRUE if the filename has NOT been mapped yet, else FALSE |
||
208 | */ |
||
209 | public function imageHasNotBeenMapped($filename) |
||
213 | |||
214 | /** |
||
215 | * Returns the original filename for passed one (which is the new filename). |
||
216 | * |
||
217 | * @param string $newFilename The new filename to return the original one for |
||
218 | * |
||
219 | * @return string The original filename |
||
220 | */ |
||
221 | public function getInversedImageMapping($newFilename) |
||
232 | |||
233 | /** |
||
234 | * Get new file name, if a filename with the same name already exists. |
||
235 | * |
||
236 | * @param string $targetFilename The name of target file |
||
237 | * |
||
238 | * @return string The new filename |
||
239 | */ |
||
240 | public function getNewFileName($targetFilename) |
||
268 | |||
269 | /** |
||
270 | * Upload's the file with the passed name to the Magento |
||
271 | * media directory. If the file already exists, the will |
||
272 | * be given a new name that will be returned. |
||
273 | * |
||
274 | * @param string $filename The name of the file to be uploaded |
||
275 | * |
||
276 | * @return string The name of the uploaded file |
||
277 | * @throws \Exception Is thrown, if the file with the passed name is not available |
||
278 | */ |
||
279 | public function uploadFile($filename) |
||
318 | |||
319 | /** |
||
320 | * Delete the file with the passed name. |
||
321 | * |
||
322 | * @param string $filename The name of the file to be deleted |
||
323 | * |
||
324 | * @return boolean TRUE on success, else FALSE |
||
325 | */ |
||
326 | public function deleteFile($filename) |
||
344 | } |
||
345 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.