Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Protected Member Functions | Protected Attributes
Zend_Filter_File_Rename Class Reference
Inheritance diagram for Zend_Filter_File_Rename:
Zend_Filter_Interface

Public Member Functions

 __construct ($options)
 
 getFile ()
 
 setFile ($options)
 
 addFile ($options)
 
 getNewName ($value, $source=false)
 
 filter ($value)
 

Protected Member Functions

 _convertOptions ($options)
 
 _getFileName ($file)
 

Protected Attributes

 $_files = array()
 

Detailed Description

Definition at line 33 of file Rename.php.

Constructor & Destructor Documentation

◆ __construct()

__construct (   $options)

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 ?

Parameters
string | array$optionsTarget file or directory to be renamed
string$targetSource filename or directory (deprecated)
bool$overwriteShould existing files be overwritten (deprecated)
Returns
void

Definition at line 54 of file Rename.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
$source
Definition: source.php:23

Member Function Documentation

◆ _convertOptions()

_convertOptions (   $options)
protected

Internal method for creating the file array Supports single and nested arrays

Parameters
array$options
Returns
array

Definition at line 208 of file Rename.php.

208  {
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  }
$count
Definition: recent.phtml:13
$value
Definition: gender.phtml:16
_convertOptions($options)
Definition: Rename.php:208
foreach($appDirs as $dir) $files

◆ _getFileName()

_getFileName (   $file)
protected

Internal method to resolve the requested source and return all other related parameters

Parameters
string$fileFilename to get the informations for
Returns
array

Definition at line 273 of file Rename.php.

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  }
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14

◆ addFile()

addFile (   $options)

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 ?

Parameters
string | array$optionsOld file or directory to be rewritten
Returns
Zend_Filter_File_Rename

Definition at line 120 of file Rename.php.

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  }
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
_convertOptions($options)
Definition: Rename.php:208

◆ filter()

filter (   $value)

Defined by Zend_Filter_Interface

Renames the file $value to the new name set before Returns the file $value, removing all but digit characters

Parameters
string$valueFull path of file to change
Exceptions
Zend_Filter_Exception
Returns
string The new filename which has been set, or false when there were errors

Implements Zend_Filter_Interface.

Definition at line 184 of file Rename.php.

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  }
getNewName($value, $source=false)
Definition: Rename.php:142
$value
Definition: gender.phtml:16

◆ getFile()

getFile ( )

Returns the files to rename and their new name and location

Returns
array

Definition at line 85 of file Rename.php.

86  {
87  return $this->_files;
88  }

◆ getNewName()

getNewName (   $value,
  $source = false 
)

Returns only the new filename without moving it But existing files will be erased when the overwrite option is true

Parameters
string$valueFull path of file to change
boolean$sourceReturn internal informations
Returns
string The new filename which has been set

Definition at line 142 of file Rename.php.

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  }
$source
Definition: source.php:23
$value
Definition: gender.phtml:16

◆ setFile()

setFile (   $options)

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 ?

Parameters
string | array$optionsOld file or directory to be rewritten
Returns
Zend_Filter_File_Rename

Definition at line 101 of file Rename.php.

102  {
103  $this->_files = array();
104  $this->addFile($options);
105 
106  return $this;
107  }

Field Documentation

◆ $_files

$_files = array()
protected

Internal array of array(source, target, overwrite)

Definition at line 38 of file Rename.php.


The documentation for this class was generated from the following file: