PageRenderTime 38ms CodeModel.GetById 10ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Zend/Filter/File/Rename.php

https://github.com/gryzz/crystal_magento
PHP | 305 lines | 158 code | 38 blank | 109 comment | 36 complexity | 0a6a0ee4692b4df73bff36a976368822 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-2009 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 16971 2009-07-22 18:05:45Z mikaelkael $
  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-2009 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. * Returns only the new filename without moving it
  125. * But existing files will be erased when the overwrite option is true
  126. *
  127. * @param string $value Full path of file to change
  128. * @param boolean $source Return internal informations
  129. * @return string The new filename which has been set
  130. */
  131. public function getNewName($value, $source = false)
  132. {
  133. $file = $this->_getFileName($value);
  134. if ($file['source'] == $file['target']) {
  135. return $value;
  136. }
  137. if (!file_exists($file['source'])) {
  138. return $value;
  139. }
  140. if (($file['overwrite'] == true) && (file_exists($file['target']))) {
  141. unlink($file['target']);
  142. }
  143. if (file_exists($file['target'])) {
  144. #require_once 'Zend/Filter/Exception.php';
  145. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
  146. }
  147. if ($source) {
  148. return $file;
  149. }
  150. return $file['target'];
  151. }
  152. /**
  153. * Defined by Zend_Filter_Interface
  154. *
  155. * Renames the file $value to the new name set before
  156. * Returns the file $value, removing all but digit characters
  157. *
  158. * @param string $value Full path of file to change
  159. * @throws Zend_Filter_Exception
  160. * @return string The new filename which has been set, or false when there were errors
  161. */
  162. public function filter($value)
  163. {
  164. $file = $this->getNewName($value, true);
  165. if (is_string($file)) {
  166. return $file;
  167. }
  168. $result = rename($file['source'], $file['target']);
  169. if ($result === true) {
  170. return $file['target'];
  171. }
  172. #require_once 'Zend/Filter/Exception.php';
  173. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
  174. }
  175. /**
  176. * Internal method for creating the file array
  177. * Supports single and nested arrays
  178. *
  179. * @param array $options
  180. * @return array
  181. */
  182. protected function _convertOptions($options) {
  183. $files = array();
  184. foreach ($options as $key => $value) {
  185. if (is_array($value)) {
  186. $this->_convertOptions($value);
  187. continue;
  188. }
  189. switch ($key) {
  190. case "source":
  191. $files['source'] = (string) $value;
  192. break;
  193. case 'target' :
  194. $files['target'] = (string) $value;
  195. break;
  196. case 'overwrite' :
  197. $files['overwrite'] = (boolean) $value;
  198. break;
  199. default:
  200. break;
  201. }
  202. }
  203. if (empty($files)) {
  204. return $this;
  205. }
  206. if (empty($files['source'])) {
  207. $files['source'] = '*';
  208. }
  209. if (empty($files['target'])) {
  210. $files['target'] = '*';
  211. }
  212. if (empty($files['overwrite'])) {
  213. $files['overwrite'] = false;
  214. }
  215. $found = false;
  216. foreach ($this->_files as $key => $value) {
  217. if ($value['source'] == $files['source']) {
  218. $this->_files[$key] = $files;
  219. $found = true;
  220. }
  221. }
  222. if (!$found) {
  223. $count = count($this->_files);
  224. $this->_files[$count] = $files;
  225. }
  226. return $this;
  227. }
  228. /**
  229. * Internal method to resolve the requested source
  230. * and return all other related parameters
  231. *
  232. * @param string $file Filename to get the informations for
  233. * @return array
  234. */
  235. protected function _getFileName($file)
  236. {
  237. $rename = array();
  238. foreach ($this->_files as $value) {
  239. if ($value['source'] == '*') {
  240. if (!isset($rename['source'])) {
  241. $rename = $value;
  242. $rename['source'] = $file;
  243. }
  244. }
  245. if ($value['source'] == $file) {
  246. $rename = $value;
  247. }
  248. }
  249. if (!isset($rename['source'])) {
  250. return $file;
  251. }
  252. if (!isset($rename['target']) or ($rename['target'] == '*')) {
  253. $rename['target'] = $rename['source'];
  254. }
  255. if (is_dir($rename['target'])) {
  256. $name = basename($rename['source']);
  257. $last = $rename['target'][strlen($rename['target']) - 1];
  258. if (($last != '/') and ($last != '\\')) {
  259. $rename['target'] .= DIRECTORY_SEPARATOR;
  260. }
  261. $rename['target'] .= $name;
  262. }
  263. return $rename;
  264. }
  265. }