/lib/Varien/File/Object.php

https://bitbucket.org/tschrock52/ethodeshare · PHP · 291 lines · 147 code · 10 blank · 134 comment · 30 complexity · 86116483e252e66c516208e43b3bb920 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) 2010 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. $path_parts = pathinfo($fileName);
  188. if(isset($path_parts["extension"])) {
  189. return $path_parts["extension"];
  190. } else {
  191. return '';
  192. }
  193. }
  194. /**
  195. * get name of file
  196. *
  197. * @return string - name of file
  198. */
  199. public function getName()
  200. {
  201. return basename($this->_filename,'.'.$this->getExtension());
  202. }
  203. /**
  204. * render filters
  205. *
  206. * @return none
  207. */
  208. public function renderFilter()
  209. {
  210. #print_r($this->_filter);
  211. if(isset($this->_filter) && count($this->_filter)>0 && $this->filtered==false){
  212. $this->filtered = true;
  213. if(isset($this->_filter['extension'])){
  214. $filter = $this->_filter['extension'];
  215. if($filter!=null){
  216. if(is_array($filter)){
  217. if(!in_array($this->getExtension(),$filter)){
  218. $this->_isCorrect = false;
  219. }
  220. } else {
  221. if($this->getExtension()!=$filter){
  222. $this->_isCorrect = false;
  223. }
  224. }
  225. }
  226. }
  227. if(isset($this->_filter['name'])){
  228. $filter = $this->_filter['name'];
  229. if($filter!=null){
  230. if(is_array($filter)){
  231. if(!in_array($this->getName(),$filter)){
  232. $this->_isCorrect = false;
  233. }
  234. } else {
  235. if($this->getName()!=$filter){
  236. $this->_isCorrect = false;
  237. }
  238. }
  239. }
  240. }
  241. if(isset($this->_filter['regName'])){
  242. $filter = $this->_filter['regName'];
  243. if($filter!=null){
  244. foreach ($filter as $value) {
  245. if(!preg_match($value,$this->getName())){
  246. $this->_isCorrect = false;
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }
  253. /**
  254. * add to array file name
  255. *
  256. * @param array &$arr -export array
  257. * @return none
  258. */
  259. public function toArray(&$arr)
  260. {
  261. if($this->_isCorrect){
  262. $arr['files_in_dirs'][] = $this->_filename;
  263. }
  264. }
  265. /**
  266. * add to xml file name
  267. *
  268. * @param array &$xml -export xml
  269. * @param int $recursionLevel - level of recursion
  270. * @param bool $addOpenTag - nothing
  271. * @param string $rootName - nothing
  272. * @return none
  273. */
  274. public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
  275. {
  276. if($this->_isCorrect){
  277. $xml .=str_repeat("\t",$recursionLevel+2).'<fileName>'.$this->_filename.'</fileName>'."\n";
  278. }
  279. }
  280. }
  281. ?>