PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/andrewjleavitt/magestudy
PHP | 304 lines | 157 code | 38 blank | 109 comment | 36 complexity | 4eb59a724f0d243c85409a78452a3229 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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. * @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-2010 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. $argv = func_get_args();
  63. array_shift($argv);
  64. $source = array_shift($argv);
  65. $overwrite = false;
  66. if (!empty($argv)) {
  67. $overwrite = array_shift($argv);
  68. }
  69. $options['source'] = $source;
  70. $options['overwrite'] = $overwrite;
  71. }
  72. $this->setFile($options);
  73. }
  74. /**
  75. * Returns the files to rename and their new name and location
  76. *
  77. * @return array
  78. */
  79. public function getFile()
  80. {
  81. return $this->_files;
  82. }
  83. /**
  84. * Sets a new file or directory as target, deleting existing ones
  85. *
  86. * Array accepts the following keys:
  87. * 'source' => Source filename or directory which will be renamed
  88. * 'target' => Target filename or directory, the new name of the sourcefile
  89. * 'overwrite' => Shall existing files be overwritten ?
  90. *
  91. * @param string|array $options Old file or directory to be rewritten
  92. * @return Zend_Filter_File_Rename
  93. */
  94. public function setFile($options)
  95. {
  96. $this->_files = array();
  97. $this->addFile($options);
  98. return $this;
  99. }
  100. /**
  101. * Adds a new file or directory as target to the existing ones
  102. *
  103. * Array accepts the following keys:
  104. * 'source' => Source filename or directory which will be renamed
  105. * 'target' => Target filename or directory, the new name of the sourcefile
  106. * 'overwrite' => Shall existing files be overwritten ?
  107. *
  108. * @param string|array $options Old file or directory to be rewritten
  109. * @return Zend_Filter_File_Rename
  110. */
  111. public function addFile($options)
  112. {
  113. if (is_string($options)) {
  114. $options = array('target' => $options);
  115. } elseif (!is_array($options)) {
  116. #require_once 'Zend/Filter/Exception.php';
  117. throw new Zend_Filter_Exception ('Invalid options to rename filter provided');
  118. }
  119. $this->_convertOptions($options);
  120. return $this;
  121. }
  122. /**
  123. * Returns only the new filename without moving it
  124. * But existing files will be erased when the overwrite option is true
  125. *
  126. * @param string $value Full path of file to change
  127. * @param boolean $source Return internal informations
  128. * @return string The new filename which has been set
  129. */
  130. public function getNewName($value, $source = false)
  131. {
  132. $file = $this->_getFileName($value);
  133. if ($file['source'] == $file['target']) {
  134. return $value;
  135. }
  136. if (!file_exists($file['source'])) {
  137. return $value;
  138. }
  139. if (($file['overwrite'] == true) && (file_exists($file['target']))) {
  140. unlink($file['target']);
  141. }
  142. if (file_exists($file['target'])) {
  143. #require_once 'Zend/Filter/Exception.php';
  144. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. It already exists.", $value));
  145. }
  146. if ($source) {
  147. return $file;
  148. }
  149. return $file['target'];
  150. }
  151. /**
  152. * Defined by Zend_Filter_Interface
  153. *
  154. * Renames the file $value to the new name set before
  155. * Returns the file $value, removing all but digit characters
  156. *
  157. * @param string $value Full path of file to change
  158. * @throws Zend_Filter_Exception
  159. * @return string The new filename which has been set, or false when there were errors
  160. */
  161. public function filter($value)
  162. {
  163. $file = $this->getNewName($value, true);
  164. if (is_string($file)) {
  165. return $file;
  166. }
  167. $result = rename($file['source'], $file['target']);
  168. if ($result === true) {
  169. return $file['target'];
  170. }
  171. #require_once 'Zend/Filter/Exception.php';
  172. throw new Zend_Filter_Exception(sprintf("File '%s' could not be renamed. An error occured while processing the file.", $value));
  173. }
  174. /**
  175. * Internal method for creating the file array
  176. * Supports single and nested arrays
  177. *
  178. * @param array $options
  179. * @return array
  180. */
  181. protected function _convertOptions($options) {
  182. $files = array();
  183. foreach ($options as $key => $value) {
  184. if (is_array($value)) {
  185. $this->_convertOptions($value);
  186. continue;
  187. }
  188. switch ($key) {
  189. case "source":
  190. $files['source'] = (string) $value;
  191. break;
  192. case 'target' :
  193. $files['target'] = (string) $value;
  194. break;
  195. case 'overwrite' :
  196. $files['overwrite'] = (boolean) $value;
  197. break;
  198. default:
  199. break;
  200. }
  201. }
  202. if (empty($files)) {
  203. return $this;
  204. }
  205. if (empty($files['source'])) {
  206. $files['source'] = '*';
  207. }
  208. if (empty($files['target'])) {
  209. $files['target'] = '*';
  210. }
  211. if (empty($files['overwrite'])) {
  212. $files['overwrite'] = false;
  213. }
  214. $found = false;
  215. foreach ($this->_files as $key => $value) {
  216. if ($value['source'] == $files['source']) {
  217. $this->_files[$key] = $files;
  218. $found = true;
  219. }
  220. }
  221. if (!$found) {
  222. $count = count($this->_files);
  223. $this->_files[$count] = $files;
  224. }
  225. return $this;
  226. }
  227. /**
  228. * Internal method to resolve the requested source
  229. * and return all other related parameters
  230. *
  231. * @param string $file Filename to get the informations for
  232. * @return array
  233. */
  234. protected function _getFileName($file)
  235. {
  236. $rename = array();
  237. foreach ($this->_files as $value) {
  238. if ($value['source'] == '*') {
  239. if (!isset($rename['source'])) {
  240. $rename = $value;
  241. $rename['source'] = $file;
  242. }
  243. }
  244. if ($value['source'] == $file) {
  245. $rename = $value;
  246. }
  247. }
  248. if (!isset($rename['source'])) {
  249. return $file;
  250. }
  251. if (!isset($rename['target']) or ($rename['target'] == '*')) {
  252. $rename['target'] = $rename['source'];
  253. }
  254. if (is_dir($rename['target'])) {
  255. $name = basename($rename['source']);
  256. $last = $rename['target'][strlen($rename['target']) - 1];
  257. if (($last != '/') and ($last != '\\')) {
  258. $rename['target'] .= DIRECTORY_SEPARATOR;
  259. }
  260. $rename['target'] .= $name;
  261. }
  262. return $rename;
  263. }
  264. }