Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Rename.php
Go to the documentation of this file.
1 <?php
25 #require_once 'Zend/Filter/Interface.php';
26 
34 {
38  protected $_files = array();
39 
54  public function __construct($options)
55  {
56  if ($options instanceof Zend_Config) {
57  $options = $options->toArray();
58  } elseif (is_string($options)) {
59  $options = array('target' => $options);
60  } elseif (!is_array($options)) {
61  #require_once 'Zend/Filter/Exception.php';
62  throw new Zend_Filter_Exception('Invalid options argument provided to filter');
63  }
64 
65  if (1 < func_num_args()) {
66  $argv = func_get_args();
67  array_shift($argv);
68  $source = array_shift($argv);
69  $overwrite = false;
70  if (!empty($argv)) {
71  $overwrite = array_shift($argv);
72  }
73  $options['source'] = $source;
74  $options['overwrite'] = $overwrite;
75  }
76 
77  $this->setFile($options);
78  }
79 
85  public function getFile()
86  {
87  return $this->_files;
88  }
89 
101  public function setFile($options)
102  {
103  $this->_files = array();
104  $this->addFile($options);
105 
106  return $this;
107  }
108 
120  public function addFile($options)
121  {
122  if (is_string($options)) {
123  $options = array('target' => $options);
124  } elseif (!is_array($options)) {
125  #require_once 'Zend/Filter/Exception.php';
126  throw new Zend_Filter_Exception ('Invalid options to rename filter provided');
127  }
128 
129  $this->_convertOptions($options);
130 
131  return $this;
132  }
133 
142  public function getNewName($value, $source = false)
143  {
144  $file = $this->_getFileName($value);
145 
146  if (!is_array($file) || !array_key_exists('source', $file) || !array_key_exists('target', $file)) {
147  return $value;
148  }
149 
150  if ($file['source'] == $file['target']) {
151  return $value;
152  }
153 
154  if (!file_exists($file['source'])) {
155  return $value;
156  }
157 
158  if (($file['overwrite'] == true) && (file_exists($file['target']))) {
159  unlink($file['target']);
160  }
161 
162  if (file_exists($file['target'])) {
163  #require_once 'Zend/Filter/Exception.php';
164  throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
165  }
166 
167  if ($source) {
168  return $file;
169  }
170 
171  return $file['target'];
172  }
173 
184  public function filter($value)
185  {
186  $file = $this->getNewName($value, true);
187  if (is_string($file)) {
188  return $file;
189  }
190 
191  $result = rename($file['source'], $file['target']);
192 
193  if ($result === true) {
194  return $file['target'];
195  }
196 
197  #require_once 'Zend/Filter/Exception.php';
198  throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
199  }
200 
208  protected function _convertOptions($options) {
209  $files = array();
210  foreach ($options as $key => $value) {
211  if (is_array($value)) {
212  $this->_convertOptions($value);
213  continue;
214  }
215 
216  switch ($key) {
217  case "source":
218  $files['source'] = (string) $value;
219  break;
220 
221  case 'target' :
222  $files['target'] = (string) $value;
223  break;
224 
225  case 'overwrite' :
226  $files['overwrite'] = (boolean) $value;
227  break;
228 
229  default:
230  break;
231  }
232  }
233 
234  if (empty($files)) {
235  return $this;
236  }
237 
238  if (empty($files['source'])) {
239  $files['source'] = '*';
240  }
241 
242  if (empty($files['target'])) {
243  $files['target'] = '*';
244  }
245 
246  if (empty($files['overwrite'])) {
247  $files['overwrite'] = false;
248  }
249 
250  $found = false;
251  foreach ($this->_files as $key => $value) {
252  if ($value['source'] == $files['source']) {
253  $this->_files[$key] = $files;
254  $found = true;
255  }
256  }
257 
258  if (!$found) {
259  $count = count($this->_files);
260  $this->_files[$count] = $files;
261  }
262 
263  return $this;
264  }
265 
273  protected function _getFileName($file)
274  {
275  $rename = array();
276  foreach ($this->_files as $value) {
277  if ($value['source'] == '*') {
278  if (!isset($rename['source'])) {
279  $rename = $value;
280  $rename['source'] = $file;
281  }
282  }
283 
284  if ($value['source'] == $file) {
285  $rename = $value;
286  }
287  }
288 
289  if (!isset($rename['source'])) {
290  return $file;
291  }
292 
293  if (!isset($rename['target']) or ($rename['target'] == '*')) {
294  $rename['target'] = $rename['source'];
295  }
296 
297  if (is_dir($rename['target'])) {
298  $name = basename($rename['source']);
299  $last = $rename['target'][strlen($rename['target']) - 1];
300  if (($last != '/') and ($last != '\\')) {
301  $rename['target'] .= DIRECTORY_SEPARATOR;
302  }
303 
304  $rename['target'] .= $name;
305  }
306 
307  return $rename;
308  }
309 }
getNewName($value, $source=false)
Definition: Rename.php:142
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23
$count
Definition: recent.phtml:13
__construct($options)
Definition: Rename.php:54
$value
Definition: gender.phtml:16
_convertOptions($options)
Definition: Rename.php:208
foreach($appDirs as $dir) $files
if(!isset($_GET['name'])) $name
Definition: log.php:14