PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/www/libs/Zend/Filter/File/Rename.php

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