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

/lib/Varien/Directory/Collection.php

https://bitbucket.org/acidel/buykoala
PHP | 461 lines | 238 code | 10 blank | 213 comment | 32 complexity | 9b0a4592fbcdf4798b275f953df01929 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_Directory
  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. * Varien Directory Collection
  28. * *
  29. * @category Varien
  30. * @package Varien_Directory
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. require_once('Varien/Data/Collection.php');
  34. require_once('Varien/Directory/Factory.php');
  35. require_once('Varien/Directory/IFactory.php');
  36. class Varien_Directory_Collection extends Varien_Data_Collection implements IFactory{
  37. protected $_path='';
  38. protected $_dirName='';
  39. protected $_recursionLevel=0;
  40. protected $_isRecursion;
  41. protected $_filters = array();
  42. /**
  43. * Constructor
  44. *
  45. * @param string $path - path to directory
  46. * @param bool $is_recursion - use or not recursion
  47. * @return none
  48. */
  49. public function __construct($path,$isRecursion=true,$recursionLevel = 0)
  50. {
  51. parent::__construct();
  52. $this->setPath($path);
  53. $this->_dirName = $this->lastDir();
  54. $this->setRecursion($isRecursion);
  55. $this->setRecursionLevel($recursionLevel);
  56. if($this->getRecursion() || $this->getRecursionLevel()==0){
  57. $this->parseDir();
  58. }
  59. }
  60. /**
  61. * Get name of this directory
  62. *
  63. * @return string - name of this directory
  64. */
  65. public function getDirName()
  66. {
  67. return $this->_dirName;
  68. }
  69. /**
  70. * Get recursion
  71. *
  72. * @return bool - is or not recursion
  73. */
  74. public function getRecursion()
  75. {
  76. return $this->_isRecursion;
  77. }
  78. /**
  79. * Get recursion level
  80. *
  81. * @return int - recursion level
  82. */
  83. public function getRecursionLevel()
  84. {
  85. return $this->_recursionLevel;
  86. }
  87. /**
  88. * Get path
  89. *
  90. * @return string - path to this directory
  91. */
  92. public function getPath()
  93. {
  94. return $this->_path;
  95. }
  96. /**
  97. * Set path to this directory
  98. * @param string $path - path to this directory
  99. * @param bool $isRecursion - use or not recursion
  100. * @return none
  101. */
  102. public function setPath($path, $isRecursion='')
  103. {
  104. if(is_dir($path)){
  105. if(isset($this->_path) && $this->_path!=$path && $this->_path!=''){
  106. $this->_path = $path;
  107. if($isRecursion!='')$this->_isRecursion = $isRecursion;
  108. $this->parseDir();
  109. } else {
  110. $this->_path = $path;
  111. }
  112. } else {
  113. throw new Exception($path. 'is not dir.');
  114. }
  115. }
  116. /**
  117. * Set recursion
  118. *
  119. * @param bool $isRecursion - use or not recursion
  120. * @return none
  121. */
  122. public function setRecursion($isRecursion)
  123. {
  124. $this->_isRecursion = $isRecursion;
  125. }
  126. /**
  127. * Set level of recursion
  128. *
  129. * @param int $recursionLevel - level of recursion
  130. * @return none
  131. */
  132. public function setRecursionLevel($recursionLevel)
  133. {
  134. $this->_recursionLevel = $recursionLevel;
  135. }
  136. /**
  137. * get latest dir in the path
  138. *
  139. * @param string $path - path to directory
  140. * @return string - latest dir in the path
  141. */
  142. public function lastDir()
  143. {
  144. return self::getLastDir($this->getPath());
  145. }
  146. /**
  147. * get latest dir in the path
  148. *
  149. * @param string $path - path to directory
  150. * @return string - latest dir in the path
  151. */
  152. static public function getLastDir($path){
  153. if($path=='') $path = $this->getPath();
  154. $last = strrpos($path, "/");
  155. return substr($path,$last+1);
  156. }
  157. /**
  158. * add item to collection
  159. *
  160. * @param IFactory $item - item of collection
  161. * @return none
  162. */
  163. public function addItem(IFactory $item)
  164. {
  165. $this->_items[] = $item;
  166. }
  167. /**
  168. * parse this directory
  169. *
  170. * @return none
  171. */
  172. protected function parseDir()
  173. {
  174. $this->clear();
  175. $iter = new RecursiveDirectoryIterator($this->getPath());
  176. while ($iter->valid()) {
  177. $curr = (string)$iter->getSubPathname();
  178. if (!$iter->isDot() && $curr[0]!='.'){
  179. $this->addItem(Varien_Directory_Factory::getFactory($iter->current(),$this->getRecursion(),$this->getRecursionLevel()));
  180. }
  181. $iter->next();
  182. }
  183. }
  184. /**
  185. * set filter using
  186. *
  187. * @param bool $useFilter - filter using
  188. * @return none
  189. */
  190. public function useFilter($useFilter)
  191. {
  192. $this->_renderFilters();
  193. $this->walk('useFilter', array($useFilter));
  194. }
  195. /**
  196. * get files names of current collection
  197. *
  198. * @return array - files names of current collection
  199. */
  200. public function filesName()
  201. {
  202. $files = array();
  203. $this->getFilesName($files);
  204. return $files;
  205. }
  206. /**
  207. * get files names of current collection
  208. *
  209. * @param array $files - array of files names
  210. * @return none
  211. */
  212. public function getFilesName(&$files)
  213. {
  214. $this->walk('getFilesName', array(&$files));
  215. }
  216. /**
  217. * get files paths of current collection
  218. *
  219. * @return array - files paths of current collection
  220. */
  221. public function filesPaths()
  222. {
  223. $paths = array();
  224. $this->getFilesPaths($paths);
  225. return $paths;
  226. }
  227. /**
  228. * get files paths of current collection
  229. *
  230. * @param array $files - array of files paths
  231. * @return none
  232. */
  233. public function getFilesPaths(&$paths)
  234. {
  235. $this->walk('getFilesPaths', array(&$paths));
  236. }
  237. /**
  238. * get SplFileObject objects of files of current collection
  239. *
  240. * @return array - array of SplFileObject objects
  241. */
  242. public function filesObj()
  243. {
  244. $objs = array();
  245. $this->getFilesObj($objs);
  246. return $objs;
  247. }
  248. /**
  249. * get SplFileObject objects of files of current collection
  250. *
  251. * @param array $objs - array of SplFileObject objects
  252. * @return none
  253. */
  254. public function getFilesObj(&$objs)
  255. {
  256. $this->walk('getFilesObj', array(&$objs));
  257. }
  258. /**
  259. * get names of dirs of current collection
  260. *
  261. * @return array - array of names of dirs
  262. */
  263. public function dirsName()
  264. {
  265. $dir = array();
  266. $this->getDirsName($dir);
  267. return $dir;
  268. }
  269. /**
  270. * get names of dirs of current collection
  271. *
  272. * @param array $dirs - array of names of dirs
  273. * @return none
  274. */
  275. public function getDirsName(&$dirs)
  276. {
  277. $this->walk('getDirsName', array(&$dirs));
  278. if($this->getRecursionLevel()>0)
  279. $dirs[] = $this->getDirName();
  280. }
  281. /**
  282. * set filters for files
  283. *
  284. * @param array $filter - array of filters
  285. * @return none
  286. */
  287. protected function setFilesFilter($filter)
  288. {
  289. $this->walk('setFilesFilter', array($filter));
  290. }
  291. /**
  292. * display this collection as array
  293. *
  294. * @return array
  295. */
  296. public function __toArray()
  297. {
  298. $arr = array();
  299. $this->toArray($arr);
  300. return $arr;
  301. }
  302. /**
  303. * display this collection as array
  304. * @param array &$arr - this collection array
  305. * @return none
  306. */
  307. public function toArray(&$arr)
  308. {
  309. if($this->getRecursionLevel()>0){
  310. $arr[$this->getDirName()] = array();
  311. $this->walk('toArray', array(&$arr[$this->getDirName()]));
  312. } else {
  313. $this->walk('toArray', array(&$arr));
  314. }
  315. }
  316. /**
  317. * get this collection as xml
  318. * @param bool $addOpenTag - add or not header of xml
  319. * @param string $rootName - root element name
  320. * @return none
  321. */
  322. public function __toXml($addOpenTag=true,$rootName='Struct')
  323. {
  324. $xml='';
  325. $this->toXml($xml,$addOpenTag,$rootName);
  326. return $xml;
  327. }
  328. /**
  329. * get this collection as xml
  330. * @param string &$xml - xml
  331. * @param bool $addOpenTag - add or not header of xml
  332. * @param string $rootName - root element name
  333. * @return none
  334. */
  335. public function toXml(&$xml,$recursionLevel=0,$addOpenTag=true,$rootName='Struct')
  336. {
  337. if($recursionLevel==0 ){
  338. $xml = '';
  339. if($addOpenTag)
  340. $xml.= '<?xml version="1.0" encoding="UTF-8"?>'."\n";
  341. $xml.= '<'.$rootName.'>'."\n";
  342. }
  343. $recursionLevel = $this->getRecursionLevel();
  344. $xml.= str_repeat("\t",$recursionLevel+1)."<$this->_dirName>\n";
  345. $this->walk('toXml', array(&$xml,$recursionLevel,$addOpenTag,$rootName));
  346. $xml.= str_repeat("\t",$recursionLevel+1)."</$this->_dirName>"."\n";
  347. if($recursionLevel==0 ){
  348. $xml.= '</'.$rootName.'>'."\n";
  349. }
  350. }
  351. /**
  352. * apply filters
  353. * @return none
  354. */
  355. protected function _renderFilters()
  356. {
  357. $exts = array();
  358. $names = array();
  359. $regName = array();
  360. foreach ($this->_filters as $filter){
  361. switch ($filter['field']){
  362. case 'extension':
  363. if(is_array($filter['value'])){
  364. foreach ($filter['value'] as $value){
  365. $exts[] = $value;
  366. }
  367. } else {
  368. $exts[] = $filter['value'];
  369. }
  370. break;
  371. case 'name':
  372. if(is_array($filter['value'])){
  373. foreach ($filter['value'] as $value){
  374. $names[] = $filter['value'];
  375. }
  376. } else {
  377. $names[] = $filter['value'];
  378. }
  379. break;
  380. case 'regName':
  381. if(is_array($filter['value'])){
  382. foreach ($filter['value'] as $value){
  383. $regName[] = $filter['value'];
  384. }
  385. } else {
  386. $regName[] = $filter['value'];
  387. }
  388. break;
  389. }
  390. }
  391. $filter = array();
  392. if(count($exts)>0) {
  393. $filter['extension'] = $exts;
  394. } else {
  395. $filter['extension'] = null;
  396. }
  397. if(count($names)>0) {
  398. $filter['name']=$names;
  399. } else {
  400. $filter['name']=null;
  401. }
  402. if(count($regName)>0) {
  403. $filter['regName']=$regName;
  404. } else {
  405. $filter['regName']=null;
  406. }
  407. $this->setFilesFilter($filter);
  408. }
  409. /**
  410. * add filter
  411. * @return none
  412. */
  413. public function addFilter($field, $value)
  414. {
  415. $filter = array();
  416. $filter['field'] = $field;
  417. $filter['value'] = $value;
  418. $this->_filters[] = $filter;
  419. $this->_isFiltersRendered = false;
  420. $this->walk('addFilter',array($field, $value));
  421. return $this;
  422. }
  423. }
  424. /* Example */
  425. /*
  426. $a = new Varien_Directory_Collection('/usr/home/vasily/dev/magento/lib',false);
  427. $a->addFilter("extension","php");
  428. $a->useFilter(true);
  429. print "-----------------------\n";
  430. print_r($a->filesName());
  431. $a->setPath('/usr/home/vasily/dev/magento/lib/Varien/Image',true);
  432. $a->useFilter(true);
  433. print "-----------------------\n";
  434. print_r($a->filesName());
  435. print "-----------------------\n";
  436. $filesObj = $a->filesObj();
  437. print $filesObj[0]->fgets();
  438. print $filesObj[0]->fgets();
  439. print $filesObj[0]->fgets();
  440. print $filesObj[0]->fgets();
  441. print $filesObj[0]->fgets();
  442. print $filesObj[0]->fgets();
  443. */
  444. ?>