PageRenderTime 25ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/DevApp/library/ServerLibraries/ZendFramework/1.7/library/Zend/Filter/File/Rename.php

http://firephp.googlecode.com/
PHP | 282 lines | 152 code | 35 blank | 95 comment | 33 complexity | cf04e122acb2945e7205746b01529ca0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.0, MIT, Apache-2.0
  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-2008 Zend Technologies USA Inc. (http://www.zend.com)
  18. * @license http://framework.zend.com/license/new-bsd New BSD License
  19. * @version $Id: $
  20. */
  21. /**
  22. * @see Zend_Filter_Interface
  23. */
  24. require_once 'Zend/Filter/Interface.php';
  25. /**
  26. * @category Zend
  27. * @package Zend_Filter
  28. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  29. * @license http://framework.zend.com/license/new-bsd New BSD License
  30. */
  31. class Zend_Filter_File_Rename implements Zend_Filter_Interface
  32. {
  33. /**
  34. * Internal array of array(source, target, overwrite)
  35. */
  36. protected $_files = array();
  37. /**
  38. * Class constructor
  39. *
  40. * Options argument may be either a string, a Zend_Config object, or an array.
  41. * If an array or Zend_Config object, it accepts the following keys:
  42. * 'source' => Source filename or directory which will be renamed
  43. * 'target' => Target filename or directory, the new name of the sourcefile
  44. * 'overwrite' => Shall existing files be overwritten ?
  45. *
  46. * @param string|array $options Target file or directory to be renamed
  47. * @param string $target Source filename or directory (deprecated)
  48. * @param bool $overwrite Should existing files be overwritten (deprecated)
  49. * @return void
  50. */
  51. public function __construct($options)
  52. {
  53. if ($options instanceof Zend_Config) {
  54. $options = $options->toArray();
  55. } elseif (is_string($options)) {
  56. $options = array('target' => $options);
  57. } elseif (!is_array($options)) {
  58. require_once 'Zend/Filter/Exception.php';
  59. throw new Zend_Filter_Exception('Invalid options argument provided to filter');
  60. }
  61. if (1 < func_num_args()) {
  62. trigger_error('Support for multiple arguments is deprecated in favor of a single options array', E_USER_NOTICE);
  63. $argv = func_get_args();
  64. array_shift($argv);
  65. $source = array_shift($argv);
  66. $overwrite = false;
  67. if (!empty($argv)) {
  68. $overwrite = array_shift($argv);
  69. }
  70. $options['source'] = $source;
  71. $options['overwrite'] = $overwrite;
  72. }
  73. $this->setFile($options);
  74. }
  75. /**
  76. * Returns the files to rename and their new name and location
  77. *
  78. * @return array
  79. */
  80. public function getFile()
  81. {
  82. return $this->_files;
  83. }
  84. /**
  85. * Sets a new file or directory as target, deleting existing ones
  86. *
  87. * Array accepts the following keys:
  88. * 'source' => Source filename or directory which will be renamed
  89. * 'target' => Target filename or directory, the new name of the sourcefile
  90. * 'overwrite' => Shall existing files be overwritten ?
  91. *
  92. * @param string|array $options Old file or directory to be rewritten
  93. * @return Zend_Filter_File_Rename
  94. */
  95. public function setFile($options)
  96. {
  97. $this->_files = array();
  98. $this->addFile($options);
  99. return $this;
  100. }
  101. /**
  102. * Adds a new file or directory as target to the existing ones
  103. *
  104. * Array accepts the following keys:
  105. * 'source' => Source filename or directory which will be renamed
  106. * 'target' => Target filename or directory, the new name of the sourcefile
  107. * 'overwrite' => Shall existing files be overwritten ?
  108. *
  109. * @param string|array $options Old file or directory to be rewritten
  110. * @return Zend_Filter_File_Rename
  111. */
  112. public function addFile($options)
  113. {
  114. if (is_string($options)) {
  115. $options = array('target' => $options);
  116. } elseif (!is_array($options)) {
  117. require_once 'Zend/Filter/Exception.php';
  118. throw new Zend_Filter_Exception ('Invalid options to rename filter provided');
  119. }
  120. $this->_convertOptions($options);
  121. return $this;
  122. }
  123. /**
  124. * Defined by Zend_Filter_Interface
  125. *
  126. * Renames the file $value to the new name set before
  127. * Returns the file $value, removing all but digit characters
  128. *
  129. * @param string $value Full path of file to change
  130. * @return string The new filename which has been set, or false when there were errors
  131. */
  132. public function filter($value)
  133. {
  134. $file = $this->_getFileName($value);
  135. if ($file['source'] == $file['target']) {
  136. return $value;
  137. }
  138. if (!file_exists($file['source'])) {
  139. return $value;
  140. }
  141. if (($file['overwrite'] == true) and (file_exists($file['target']))) {
  142. unlink($file['target']);
  143. }
  144. if (file_exists($file['target'])) {
  145. require_once 'Zend/Filter/Exception.php';
  146. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
  147. }
  148. $result = rename($file['source'], $file['target']);
  149. if ($result === true) {
  150. return $file['target'];
  151. }
  152. require_once 'Zend/Filter/Exception.php';
  153. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
  154. }
  155. /**
  156. * Internal method for creating the file array
  157. * Supports single and nested arrays
  158. *
  159. * @param array $options
  160. * @return array
  161. */
  162. protected function _convertOptions($options) {
  163. $files = array();
  164. foreach ($options as $key => $value) {
  165. if (is_array($value)) {
  166. $this->_convertOptions($value);
  167. continue;
  168. }
  169. switch ($key) {
  170. case "source":
  171. $files['source'] = (string) $value;
  172. break;
  173. case 'target' :
  174. $files['target'] = (string) $value;
  175. break;
  176. case 'overwrite' :
  177. $files['overwrite'] = (boolean) $value;
  178. break;
  179. default:
  180. break;
  181. }
  182. }
  183. if (empty($files)) {
  184. return $this;
  185. }
  186. if (empty($files['source'])) {
  187. $files['source'] = '*';
  188. }
  189. if (empty($files['target'])) {
  190. $files['target'] = '*';
  191. }
  192. if (empty($files['overwrite'])) {
  193. $files['overwrite'] = false;
  194. }
  195. $found = false;
  196. foreach ($this->_files as $key => $value) {
  197. if ($value['source'] == $files['source']) {
  198. $this->_files[$key] = $files;
  199. $found = true;
  200. }
  201. }
  202. if (!$found) {
  203. $count = count($this->_files);
  204. $this->_files[$count] = $files;
  205. }
  206. return $this;
  207. }
  208. /**
  209. * Internal method to resolve the requested source
  210. * and return all other related parameters
  211. *
  212. * @param string $file Filename to get the informations for
  213. * @return array
  214. */
  215. protected function _getFileName($file)
  216. {
  217. $rename = array();
  218. foreach ($this->_files as $value) {
  219. if ($value['source'] == '*') {
  220. if (!isset($rename['source'])) {
  221. $rename = $value;
  222. $rename['source'] = $file;
  223. }
  224. }
  225. if ($value['source'] == $file) {
  226. $rename = $value;
  227. }
  228. }
  229. if (!isset($rename['source'])) {
  230. return $file;
  231. }
  232. if (!isset($rename['target']) or ($rename['target'] == '*')) {
  233. $rename['target'] = $rename['source'];
  234. }
  235. if (is_dir($rename['target'])) {
  236. $name = basename($rename['source']);
  237. $last = $rename['target'][strlen($rename['target']) - 1];
  238. if (($last != '/') and ($last != '\\')) {
  239. $rename['target'] .= DIRECTORY_SEPARATOR;
  240. }
  241. $rename['target'] .= $name;
  242. }
  243. return $rename;
  244. }
  245. }