1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
|
<?php
/**
* Zend Framework
*
* LICENSE
*
* This source file is subject to the new BSD license that is bundled
* with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://framework.zend.com/license/new-bsd
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@zend.com so we can send you a copy immediately.
*
* @category Zend
* @package Zend_Filter
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @version $Id$
*/
/**
* @see Zend_Filter_Interface
*/
/**
* @category Zend
* @package Zend_Filter
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
class Zend_Filter_File_Rename implements Zend_Filter_Interface
{
/**
* Internal array of array(source, target, overwrite)
*/
protected $_files = array();
/**
* Class constructor
*
* Options argument may be either a string, a Zend_Config object, or an array.
* If an array or Zend_Config object, it accepts the following keys:
* 'source' => Source filename or directory which will be renamed
* 'target' => Target filename or directory, the new name of the sourcefile
* 'overwrite' => Shall existing files be overwritten ?
*
* @param string|array $options Target file or directory to be renamed
* @param string $target Source filename or directory (deprecated)
* @param bool $overwrite Should existing files be overwritten (deprecated)
* @return void
*/
public function __construct($options)
{
if ($options instanceof Zend_Config) {
$options = $options->toArray();
} elseif (is_string($options)) {
$options = array('target' => $options);
} elseif (!is_array($options)) {
throw new Zend_Filter_Exception('Invalid options argument provided to filter');
}
if (1 < func_num_args()) {
$argv = func_get_args();
array_shift($argv);
$source = array_shift($argv);
$overwrite = false;
if (!empty($argv)) {
$overwrite = array_shift($argv);
}
$options['source'] = $source;
$options['overwrite'] = $overwrite;
}
$this->setFile($options);
}
/**
* Returns the files to rename and their new name and location
*
* @return array
*/
public function getFile()
{
return $this->_files;
}
/**
* Sets a new file or directory as target, deleting existing ones
*
* Array accepts the following keys:
* 'source' => Source filename or directory which will be renamed
* 'target' => Target filename or directory, the new name of the sourcefile
* 'overwrite' => Shall existing files be overwritten ?
*
* @param string|array $options Old file or directory to be rewritten
* @return Zend_Filter_File_Rename
*/
public function setFile($options)
{
$this->_files = array();
$this->addFile($options);
return $this;
}
/**
* Adds a new file or directory as target to the existing ones
*
* Array accepts the following keys:
* 'source' => Source filename or directory which will be renamed
* 'target' => Target filename or directory, the new name of the sourcefile
* 'overwrite' => Shall existing files be overwritten ?
*
* @param string|array $options Old file or directory to be rewritten
* @return Zend_Filter_File_Rename
*/
public function addFile($options)
{
if (is_string($options)) {
$options = array('target' => $options);
} elseif (!is_array($options)) {
throw new Zend_Filter_Exception ('Invalid options to rename filter provided');
}
$this->_convertOptions($options);
return $this;
}
/**
* Returns only the new filename without moving it
* But existing files will be erased when the overwrite option is true
*
* @param string $value Full path of file to change
* @param boolean $source Return internal informations
* @return string The new filename which has been set
*/
public function getNewName($value, $source = false)
{
$file = $this->_getFileName($value);
if (!is_array($file) || !array_key_exists('source', $file) || !array_key_exists('target', $file)) {
return $value;
}
if ($file['source'] == $file['target']) {
return $value;
}
if (!file_exists($file['source'])) {
return $value;
}
if (($file['overwrite'] == true) && (file_exists($file['target']))) {
unlink($file['target']);
}
if (file_exists($file['target'])) {
throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
}
if ($source) {
return $file;
}
return $file['target'];
}
/**
* Defined by Zend_Filter_Interface
*
* Renames the file $value to the new name set before
* Returns the file $value, removing all but digit characters
*
* @param string $value Full path of file to change
* @throws Zend_Filter_Exception
* @return string The new filename which has been set, or false when there were errors
*/
public function filter($value)
{
$file = $this->getNewName($value, true);
if (is_string($file)) {
return $file;
}
$result = rename($file['source'], $file['target']);
if ($result === true) {
return $file['target'];
}
throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
}
/**
* Internal method for creating the file array
* Supports single and nested arrays
*
* @param array $options
* @return array
*/
protected function _convertOptions($options) {
$files = array();
foreach ($options as $key => $value) {
if (is_array($value)) {
$this->_convertOptions($value);
continue;
}
switch ($key) {
case "source":
$files['source'] = (string) $value;
break;
case 'target' :
$files['target'] = (string) $value;
break;
case 'overwrite' :
$files['overwrite'] = (boolean) $value;
break;
default:
break;
}
}
if (empty($files)) {
return $this;
}
if (empty($files['source'])) {
$files['source'] = '*';
}
if (empty($files['target'])) {
$files['target'] = '*';
}
if (empty($files['overwrite'])) {
$files['overwrite'] = false;
}
$found = false;
foreach ($this->_files as $key => $value) {
if ($value['source'] == $files['source']) {
$this->_files[$key] = $files;
$found = true;
}
}
if (!$found) {
$count = count($this->_files);
$this->_files[$count] = $files;
}
return $this;
}
/**
* Internal method to resolve the requested source
* and return all other related parameters
*
* @param string $file Filename to get the informations for
* @return array
*/
protected function _getFileName($file)
{
$rename = array();
foreach ($this->_files as $value) {
if ($value['source'] == '*') {
if (!isset($rename['source'])) {
$rename = $value;
$rename['source'] = $file;
}
}
if ($value['source'] == $file) {
$rename = $value;
}
}
if (!isset($rename['source'])) {
return $file;
}
if (!isset($rename['target']) or ($rename['target'] == '*')) {
$rename['target'] = $rename['source'];
}
if (is_dir($rename['target'])) {
$name = basename($rename['source']);
$last = $rename['target'][strlen($rename['target']) - 1];
if (($last != '/') and ($last != '\\')) {
$rename['target'] .= DIRECTORY_SEPARATOR;
}
$rename['target'] .= $name;
}
return $rename;
}
}
|