/lib/Varien/File/Object.php

https://bitbucket.org/acidel/buykoala · PHP · 294 lines · 150 code · 10 blank · 134 comment · 31 complexity · b568f32915b66280176ce87a856de2b3 MD5 · raw file

  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  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@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Varien
  22. * @package Varien_File
  23. * @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * File Object
  28. * *
  29. * @category Varien
  30. * @package Varien_File
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. require_once("Varien/Object.php");
  34. require_once('Varien/Directory/IFactory.php');
  35. class Varien_File_Object extends SplFileObject implements IFactory {
  36. protected $_filename;
  37. protected $_path;
  38. protected $_filter;
  39. protected $_isCorrect=true; # - pass or not filter checking
  40. protected $filtered;
  41. /**
  42. * Constructor
  43. *
  44. * @param string $path - path to directory
  45. * @return none
  46. */
  47. public function __construct($path)
  48. {
  49. parent::__construct($path);
  50. $this->_path=$path;
  51. $this->_filename=basename($path);
  52. }
  53. /**
  54. * add file name to array
  55. *
  56. * @param array &$files - array of files
  57. * @return none
  58. */
  59. public function getFilesName(&$files)
  60. {
  61. $this->getFileName(&$files);
  62. }
  63. /**
  64. * add file name to array
  65. *
  66. * @param array &$files - array of files
  67. * @return none
  68. */
  69. public function getFileName(&$files=null)
  70. {
  71. if($this->_isCorrect){
  72. if($files===null)
  73. return $this->_filename;
  74. $files[] = $this->_filename;
  75. }
  76. }
  77. /**
  78. * add file path to array
  79. *
  80. * @param array &$paths - array of paths
  81. * @return none
  82. */
  83. public function getFilesPaths(&$paths)
  84. {
  85. if($this->_isCorrect){
  86. $paths[] = (string)$this->_path;
  87. }
  88. }
  89. /**
  90. * add file path to array
  91. *
  92. * @param array &$paths - array of paths
  93. * @return none
  94. */
  95. public function getFilePath(&$path=null)
  96. {
  97. if($this->_isCorrect){
  98. if($path===null)
  99. return $this->_path;
  100. $paths[] = $this->_path;
  101. }
  102. }
  103. /**
  104. * use filter
  105. *
  106. * @param bool $useFilter - use or not filter
  107. * @return none
  108. */
  109. public function useFilter($useFilter)
  110. {
  111. if($useFilter){
  112. $this->renderFilter();
  113. } else {
  114. $this->_isCorrect = true;
  115. $this->filtered = false;
  116. }
  117. }
  118. /**
  119. * add file object to array
  120. *
  121. * @param array &$objs - array of gile objects
  122. * @return none
  123. */
  124. public function getFilesObj(&$objs)
  125. {
  126. if($this->_isCorrect){
  127. $objs[] = $this;
  128. }
  129. }
  130. /**
  131. * nothing
  132. *
  133. * @param array &$dirs - array of dirs
  134. * @return none
  135. */
  136. public function getDirsName(&$dirs)
  137. {
  138. return Varien_Directory_Collection::getLastDir($this->_path);
  139. }
  140. /**
  141. * nothing
  142. *
  143. * @param array &$dirs - array of dirs
  144. * @return none
  145. */
  146. public function getDirName()
  147. {
  148. return Varien_Directory_Collection::lastDir($this->_path);
  149. }
  150. /**
  151. * set file filter
  152. *
  153. * @param array $filter - array of filter
  154. * @return none
  155. */
  156. public function setFilesFilter($filter)
  157. {
  158. $this->addFilter($filter);
  159. }
  160. /**
  161. * set file filter
  162. *
  163. * @param array $filter - array of filter
  164. * @return none
  165. */
  166. public function addFilter($filter)
  167. {
  168. $this->_filter = $filter;
  169. }
  170. /**
  171. * get extension of file
  172. *
  173. * @return string - extension of file
  174. */
  175. public function getExtension()
  176. {
  177. return self::getExt($this->_filename);
  178. }
  179. /**
  180. * get extension of file
  181. *
  182. * @param string $fileName - name of file
  183. * @return string - extension of file
  184. */
  185. static public function getExt($fileName)
  186. {
  187. if($fileName === ''){
  188. $path_parts = pathinfo($this->_filename);
  189. } else {
  190. $path_parts = pathinfo($fileName);
  191. }
  192. if(isset($path_parts["extension"]))
  193. return $path_parts["extension"];
  194. else
  195. return '';
  196. }
  197. /**
  198. * get name of file
  199. *
  200. * @return string - name of file
  201. */
  202. public function getName()
  203. {
  204. return basename($this->_filename,'.'.$this->getExtension());
  205. }
  206. /**
  207. * render filters
  208. *
  209. * @return none
  210. */
  211. public function renderFilter()
  212. {
  213. #print_r($this->_filter);
  214. if(isset($this->_filter) && count($this->_filter)>0 && $this->filtered==false){
  215. $this->filtered = true;
  216. if(isset($this->_filter['extension'])){
  217. $filter = $this->_filter['extension'];
  218. if($filter!=null){
  219. if(is_array($filter)){
  220. if(!in_array($this->getExtension(),$filter)){
  221. $this->_isCorrect = false;
  222. }
  223. } else {
  224. if($this->getExtension()!=$filter){
  225. $this->_isCorrect = false;
  226. }
  227. }
  228. }
  229. }
  230. if(isset($this->_filter['name'])){
  231. $filter = $this->_filter['name'];
  232. if($filter!=null){
  233. if(is_array($filter)){
  234. if(!in_array($this->getName(),$filter)){
  235. $this->_isCorrect = false;
  236. }
  237. } else {
  238. if($this->getName()!=$filter){
  239. $this->_isCorrect = false;
  240. }
  241. }
  242. }
  243. }
  244. if(isset($this->_filter['regName'])){
  245. $filter = $this->_filter['regName'];
  246. if($filter!=null){
  247. foreach ($filter as $value) {
  248. if(!preg_match($value,$this->getName())){
  249. $this->_isCorrect = false;
  250. }
  251. }
  252. }
  253. }
  254. }
  255. }
  256. /**
  257. * add to array file name
  258. *
  259. * @param array &$arr -export array
  260. * @return none
  261. */
  262. public function toArray(&$arr)
  263. {
  264. if($this->_isCorrect){
  265. $arr['files_in_dirs'][] = $this->_filename;
  266. }
  267. }
  268. /**
  269. * add to xml file name
  270. *
  271. * @param array &$xml -export xml
  272. * @param int $recursionLevel - level of recursion
  273. * @param bool $addOpenTag - nothing
  274. * @param string $rootName - nothing
  275. * @return none
  276. */
  277. public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
  278. {
  279. if($this->_isCorrect){
  280. $xml .=str_repeat("\t",$recursionLevel+2).'<fileName>'.$this->_filename.'</fileName>'."\n";
  281. }
  282. }
  283. }
  284. ?>