|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TechDivision\Import\Subjects\FileUploadTrait |
|
5
|
|
|
* |
|
6
|
|
|
* NOTICE OF LICENSE |
|
7
|
|
|
* |
|
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
|
9
|
|
|
* that is available through the world-wide-web at this URL: |
|
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
|
11
|
|
|
* |
|
12
|
|
|
* PHP version 5 |
|
13
|
|
|
* |
|
14
|
|
|
* @author Tim Wagner <[email protected]> |
|
15
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
17
|
|
|
* @link https://github.com/techdivision/import |
|
18
|
|
|
* @link http://www.techdivision.com |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
namespace TechDivision\Import\Subjects; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* The trait implementation for the file upload functionality. |
|
25
|
|
|
* |
|
26
|
|
|
* @author Tim Wagner <[email protected]> |
|
27
|
|
|
* @copyright 2016 TechDivision GmbH <[email protected]> |
|
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
|
29
|
|
|
* @link https://github.com/techdivision/import |
|
30
|
|
|
* @link http://www.techdivision.com |
|
31
|
|
|
*/ |
|
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
|
|
|
* The flag whether to copy the images or not. |
|
51
|
|
|
* |
|
52
|
|
|
* @var boolean |
|
53
|
|
|
*/ |
|
54
|
|
|
protected $copyImages = false; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Set's the flag to copy the images or not. |
|
58
|
|
|
* |
|
59
|
|
|
* @param boolean $copyImages The flag |
|
60
|
|
|
* |
|
61
|
|
|
* @return void |
|
62
|
|
|
*/ |
|
63
|
1 |
|
public function setCopyImages($copyImages) |
|
64
|
|
|
{ |
|
65
|
1 |
|
$this->copyImages = $copyImages; |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Return's the flag to copy images or not. |
|
70
|
|
|
* |
|
71
|
|
|
* @return boolean The flag |
|
72
|
|
|
*/ |
|
73
|
1 |
|
public function hasCopyImages() |
|
74
|
|
|
{ |
|
75
|
1 |
|
return $this->copyImages; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Set's directory with the Magento media files => target directory for images. |
|
80
|
|
|
* |
|
81
|
|
|
* @param string $mediaDir The directory with the Magento media files => target directory for images |
|
82
|
|
|
* |
|
83
|
|
|
* @return void |
|
84
|
|
|
*/ |
|
85
|
3 |
|
public function setMediaDir($mediaDir) |
|
86
|
|
|
{ |
|
87
|
3 |
|
$this->mediaDir = $mediaDir; |
|
88
|
3 |
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Return's the directory with the Magento media files => target directory for images. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string The directory with the Magento media files => target directory for images |
|
94
|
|
|
*/ |
|
95
|
3 |
|
public function getMediaDir() |
|
96
|
|
|
{ |
|
97
|
3 |
|
return $this->mediaDir; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* Set's directory with the images that have to be imported. |
|
102
|
|
|
* |
|
103
|
|
|
* @param string $imagesFileDir The directory with the images that have to be imported |
|
104
|
|
|
* |
|
105
|
|
|
* @return void |
|
106
|
|
|
*/ |
|
107
|
3 |
|
public function setImagesFileDir($imagesFileDir) |
|
108
|
|
|
{ |
|
109
|
3 |
|
$this->imagesFileDir = $imagesFileDir; |
|
110
|
3 |
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Return's the directory with the images that have to be imported. |
|
114
|
|
|
* |
|
115
|
|
|
* @return string The directory with the images that have to be imported |
|
116
|
|
|
*/ |
|
117
|
3 |
|
public function getImagesFileDir() |
|
118
|
|
|
{ |
|
119
|
3 |
|
return $this->imagesFileDir; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Get new file name if the same is already exists. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $targetFilename The name of the exisising files |
|
126
|
|
|
* |
|
127
|
|
|
* @return string The new filename |
|
128
|
|
|
*/ |
|
129
|
3 |
|
public function getNewFileName($targetFilename) |
|
130
|
|
|
{ |
|
131
|
|
|
|
|
132
|
|
|
// load the file information |
|
133
|
3 |
|
$fileInfo = pathinfo($targetFilename); |
|
134
|
|
|
|
|
135
|
|
|
// query whether or not, the file exists |
|
136
|
3 |
|
if ($this->getFilesystemAdapter()->isFile($targetFilename)) { |
|
|
|
|
|
|
137
|
|
|
// initialize the incex and the basename |
|
138
|
1 |
|
$index = 1; |
|
139
|
1 |
|
$baseName = $fileInfo['filename'] . '.' . $fileInfo['extension']; |
|
140
|
|
|
|
|
141
|
|
|
// prepare the new filename by raising the index |
|
142
|
1 |
|
while ($this->getFilesystemAdapter()->isFile($fileInfo['dirname'] . '/' . $baseName)) { |
|
|
|
|
|
|
143
|
1 |
|
$baseName = $fileInfo['filename'] . '_' . $index . '.' . $fileInfo['extension']; |
|
144
|
1 |
|
$index++; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
// set the new filename |
|
148
|
1 |
|
$targetFilename = $baseName; |
|
149
|
|
|
|
|
|
|
|
|
|
150
|
|
|
} else { |
|
151
|
|
|
// if not, simply return the filename |
|
152
|
2 |
|
return $fileInfo['basename']; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
// return the new filename |
|
156
|
1 |
|
return $targetFilename; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Upload's the file with the passed name to the Magento |
|
161
|
|
|
* media directory. If the file already exists, the will |
|
162
|
|
|
* be given a new name that will be returned. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $filename The name of the file to be uploaded |
|
165
|
|
|
* |
|
166
|
|
|
* @return string The name of the uploaded file |
|
167
|
|
|
* @throws \Exception Is thrown, if the file with the passed name is not available |
|
168
|
|
|
*/ |
|
169
|
2 |
|
public function uploadFile($filename) |
|
170
|
|
|
{ |
|
171
|
|
|
|
|
172
|
|
|
// trim the leading /, if available |
|
173
|
2 |
|
$trimmedFilename = ltrim($filename, '/'); |
|
174
|
2 |
|
$mediaDir = ltrim($this->getMediaDir(), '/'); |
|
175
|
2 |
|
$imagesFileDir = ltrim($this->getImagesFileDir(), '/'); |
|
176
|
|
|
|
|
177
|
|
|
// prepare source/target filename |
|
178
|
2 |
|
$sourceFilename = sprintf('%s/%s', $imagesFileDir, $trimmedFilename); |
|
179
|
2 |
|
$targetFilename = sprintf('%s/%s', $mediaDir, $trimmedFilename); |
|
180
|
|
|
|
|
181
|
|
|
// query whether or not the image file to be imported is available |
|
182
|
2 |
|
if (!$this->getFilesystemAdapter()->isFile($sourceFilename)) { |
|
|
|
|
|
|
183
|
1 |
|
throw new \Exception(sprintf('Media file %s not available', $sourceFilename)); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
// prepare the target filename, if necessary |
|
187
|
1 |
|
$newTargetFilename = $this->getNewFileName($targetFilename); |
|
188
|
1 |
|
$targetFilename = str_replace(basename($targetFilename), $newTargetFilename, $targetFilename); |
|
189
|
|
|
|
|
190
|
|
|
// make sure, the target directory exists |
|
191
|
1 |
|
if (!$this->getFilesystemAdapter()->isDir($targetDirectory = dirname($targetFilename))) { |
|
|
|
|
|
|
192
|
1 |
|
$this->getFilesystemAdapter()->mkdir($targetDirectory, 0755); |
|
|
|
|
|
|
193
|
|
|
} |
|
194
|
|
|
|
|
195
|
|
|
// copy the image to the target directory |
|
196
|
1 |
|
$this->getFilesystemAdapter()->copy($sourceFilename, $targetFilename); |
|
|
|
|
|
|
197
|
|
|
|
|
198
|
|
|
// return the new target filename |
|
199
|
1 |
|
return str_replace($mediaDir, '', $targetFilename); |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
/** |
|
203
|
|
|
* Delete the file with the passed name. |
|
204
|
|
|
* |
|
205
|
|
|
* @param string $filename The name of the file to be deleted |
|
206
|
|
|
* |
|
207
|
|
|
* @return boolean TRUE on success, else FALSE |
|
208
|
|
|
*/ |
|
209
|
|
|
public function deleteFile($filename) |
|
210
|
|
|
{ |
|
211
|
|
|
|
|
212
|
|
|
// trim the leading /, if available |
|
213
|
|
|
$trimmedFilename = ltrim($filename, '/'); |
|
214
|
|
|
$mediaDir = ltrim($this->getMediaDir(), '/'); |
|
215
|
|
|
|
|
216
|
|
|
// prepare source/target filename |
|
217
|
|
|
$targetFilename = sprintf('%s/%s', $mediaDir, $trimmedFilename); |
|
218
|
|
|
|
|
219
|
|
|
// query whether or not the image file to be deleted is available |
|
220
|
|
|
if (!$this->getFilesystemAdapter()->isFile($targetFilename)) { |
|
|
|
|
|
|
221
|
|
|
throw new \Exception(sprintf('Media file %s not available', $targetFilename)); |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
// delte the image from the target directory |
|
225
|
|
|
$this->getFilesystemAdapter()->delete($targetFilename); |
|
|
|
|
|
|
226
|
|
|
} |
|
227
|
|
|
} |
|
228
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.