PageRenderTime 24ms CodeModel.GetById 9ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Varien/File/Object.php

https://github.com/gryzz/crystal_magento
PHP | 262 lines | 150 code | 9 blank | 103 comment | 31 complexity | bd3b219c09053d68c7fca743fbd08939 MD5 | raw file
  1. <?php
  2. require_once("Varien/Object.php");
  3. require_once('Varien/Directory/IFactory.php');
  4. class Varien_File_Object extends SplFileObject implements IFactory {
  5. protected $_filename;
  6. protected $_path;
  7. protected $_filter;
  8. protected $_isCorrect=true; # - pass or not filter checking
  9. protected $filtered;
  10. /**
  11. * Constructor
  12. *
  13. * @param string $path - path to directory
  14. * @return none
  15. */
  16. public function __construct($path)
  17. {
  18. parent::__construct($path);
  19. $this->_path=$path;
  20. $this->_filename=basename($path);
  21. }
  22. /**
  23. * add file name to array
  24. *
  25. * @param array &$files - array of files
  26. * @return none
  27. */
  28. public function getFilesName(&$files)
  29. {
  30. $this->getFileName(&$files);
  31. }
  32. /**
  33. * add file name to array
  34. *
  35. * @param array &$files - array of files
  36. * @return none
  37. */
  38. public function getFileName(&$files=null)
  39. {
  40. if($this->_isCorrect){
  41. if($files===null)
  42. return $this->_filename;
  43. $files[] = $this->_filename;
  44. }
  45. }
  46. /**
  47. * add file path to array
  48. *
  49. * @param array &$paths - array of paths
  50. * @return none
  51. */
  52. public function getFilesPaths(&$paths)
  53. {
  54. if($this->_isCorrect){
  55. $paths[] = (string)$this->_path;
  56. }
  57. }
  58. /**
  59. * add file path to array
  60. *
  61. * @param array &$paths - array of paths
  62. * @return none
  63. */
  64. public function getFilePath(&$path=null)
  65. {
  66. if($this->_isCorrect){
  67. if($path===null)
  68. return $this->_path;
  69. $paths[] = $this->_path;
  70. }
  71. }
  72. /**
  73. * use filter
  74. *
  75. * @param bool $useFilter - use or not filter
  76. * @return none
  77. */
  78. public function useFilter($useFilter)
  79. {
  80. if($useFilter){
  81. $this->renderFilter();
  82. } else {
  83. $this->_isCorrect = true;
  84. $this->filtered = false;
  85. }
  86. }
  87. /**
  88. * add file object to array
  89. *
  90. * @param array &$objs - array of gile objects
  91. * @return none
  92. */
  93. public function getFilesObj(&$objs)
  94. {
  95. if($this->_isCorrect){
  96. $objs[] = $this;
  97. }
  98. }
  99. /**
  100. * nothing
  101. *
  102. * @param array &$dirs - array of dirs
  103. * @return none
  104. */
  105. public function getDirsName(&$dirs)
  106. {
  107. return Varien_Directory_Collection::getLastDir($this->_path);
  108. }
  109. /**
  110. * nothing
  111. *
  112. * @param array &$dirs - array of dirs
  113. * @return none
  114. */
  115. public function getDirName()
  116. {
  117. return Varien_Directory_Collection::lastDir($this->_path);
  118. }
  119. /**
  120. * set file filter
  121. *
  122. * @param array $filter - array of filter
  123. * @return none
  124. */
  125. public function setFilesFilter($filter)
  126. {
  127. $this->addFilter($filter);
  128. }
  129. /**
  130. * set file filter
  131. *
  132. * @param array $filter - array of filter
  133. * @return none
  134. */
  135. public function addFilter($filter)
  136. {
  137. $this->_filter = $filter;
  138. }
  139. /**
  140. * get extension of file
  141. *
  142. * @return string - extension of file
  143. */
  144. public function getExtension()
  145. {
  146. return self::getExt($this->_filename);
  147. }
  148. /**
  149. * get extension of file
  150. *
  151. * @param string $fileName - name of file
  152. * @return string - extension of file
  153. */
  154. static public function getExt($fileName)
  155. {
  156. if($fileName === ''){
  157. $path_parts = pathinfo($this->_filename);
  158. } else {
  159. $path_parts = pathinfo($fileName);
  160. }
  161. if(isset($path_parts["extension"]))
  162. return $path_parts["extension"];
  163. else
  164. return '';
  165. }
  166. /**
  167. * get name of file
  168. *
  169. * @return string - name of file
  170. */
  171. public function getName()
  172. {
  173. return basename($this->_filename,'.'.$this->getExtension());
  174. }
  175. /**
  176. * render filters
  177. *
  178. * @return none
  179. */
  180. public function renderFilter()
  181. {
  182. #print_r($this->_filter);
  183. if(isset($this->_filter) && count($this->_filter)>0 && $this->filtered==false){
  184. $this->filtered = true;
  185. if(isset($this->_filter['extension'])){
  186. $filter = $this->_filter['extension'];
  187. if($filter!=null){
  188. if(is_array($filter)){
  189. if(!in_array($this->getExtension(),$filter)){
  190. $this->_isCorrect = false;
  191. }
  192. } else {
  193. if($this->getExtension()!=$filter){
  194. $this->_isCorrect = false;
  195. }
  196. }
  197. }
  198. }
  199. if(isset($this->_filter['name'])){
  200. $filter = $this->_filter['name'];
  201. if($filter!=null){
  202. if(is_array($filter)){
  203. if(!in_array($this->getName(),$filter)){
  204. $this->_isCorrect = false;
  205. }
  206. } else {
  207. if($this->getName()!=$filter){
  208. $this->_isCorrect = false;
  209. }
  210. }
  211. }
  212. }
  213. if(isset($this->_filter['regName'])){
  214. $filter = $this->_filter['regName'];
  215. if($filter!=null){
  216. foreach ($filter as $value) {
  217. if(!preg_match($value,$this->getName())){
  218. $this->_isCorrect = false;
  219. }
  220. }
  221. }
  222. }
  223. }
  224. }
  225. /**
  226. * add to array file name
  227. *
  228. * @param array &$arr -export array
  229. * @return none
  230. */
  231. public function toArray(&$arr)
  232. {
  233. if($this->_isCorrect){
  234. $arr['files_in_dirs'][] = $this->_filename;
  235. }
  236. }
  237. /**
  238. * add to xml file name
  239. *
  240. * @param array &$xml -export xml
  241. * @param int $recursionLevel - level of recursion
  242. * @param bool $addOpenTag - nothing
  243. * @param string $rootName - nothing
  244. * @return none
  245. */
  246. public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
  247. {
  248. if($this->_isCorrect){
  249. $xml .=str_repeat("\t",$recursionLevel+2).'<fileName>'.$this->_filename.'</fileName>'."\n";
  250. }
  251. }
  252. }
  253. ?>