/Zend/Filter/File/Rename.php

https://github.com/ftaiolivista/Zend-Framework-Namespaced- · PHP · 310 lines · 162 code · 41 blank · 107 comment · 36 complexity · fb358fd5e3dc080631e48328ea452d62 MD5 · raw file

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