PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/Group-I/jobeet/lib/vendor/symfony/lib/util/sfFinder.class.php

https://bitbucket.org/hosseinzolfi/db-lab-spring-2011/
PHP | 793 lines | 488 code | 99 blank | 206 comment | 106 complexity | 7b4032c4a81cf2b43ff9a8d2198faa92 MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. *
  11. * Allow to build rules to find files and directories.
  12. *
  13. * All rules may be invoked several times, except for ->in() method.
  14. * Some rules are cumulative (->name() for example) whereas others are destructive
  15. * (most recent value is used, ->maxdepth() method for example).
  16. *
  17. * All methods return the current sfFinder object to allow easy chaining:
  18. *
  19. * $files = sfFinder::type('file')->name('*.php')->in(.);
  20. *
  21. * Interface loosely based on perl File::Find::Rule module.
  22. *
  23. * @package symfony
  24. * @subpackage util
  25. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  26. * @version SVN: $Id: sfFinder.class.php 30528 2010-08-04 16:25:14Z fabien $
  27. */
  28. class sfFinder
  29. {
  30. protected $type = 'file';
  31. protected $names = array();
  32. protected $prunes = array();
  33. protected $discards = array();
  34. protected $execs = array();
  35. protected $mindepth = 0;
  36. protected $sizes = array();
  37. protected $maxdepth = 1000000;
  38. protected $relative = false;
  39. protected $follow_link = false;
  40. protected $sort = false;
  41. protected $ignore_version_control = true;
  42. /**
  43. * Sets maximum directory depth.
  44. *
  45. * Finder will descend at most $level levels of directories below the starting point.
  46. *
  47. * @param int $level
  48. * @return sfFinder current sfFinder object
  49. */
  50. public function maxdepth($level)
  51. {
  52. $this->maxdepth = $level;
  53. return $this;
  54. }
  55. /**
  56. * Sets minimum directory depth.
  57. *
  58. * Finder will start applying tests at level $level.
  59. *
  60. * @param int $level
  61. * @return sfFinder current sfFinder object
  62. */
  63. public function mindepth($level)
  64. {
  65. $this->mindepth = $level;
  66. return $this;
  67. }
  68. public function get_type()
  69. {
  70. return $this->type;
  71. }
  72. /**
  73. * Sets the type of elements to returns.
  74. *
  75. * @param string $name directory or file or any (for both file and directory)
  76. * @return sfFinder new sfFinder object
  77. */
  78. public static function type($name)
  79. {
  80. $finder = new self();
  81. return $finder->setType($name);
  82. }
  83. /**
  84. * Sets the type of elements to returns.
  85. *
  86. * @param string $name directory or file or any (for both file and directory)
  87. * @return sfFinder Current object
  88. */
  89. public function setType($name)
  90. {
  91. $name = strtolower($name);
  92. if (substr($name, 0, 3) === 'dir')
  93. {
  94. $this->type = 'directory';
  95. return $this;
  96. }
  97. if ($name === 'any')
  98. {
  99. $this->type = 'any';
  100. return $this;
  101. }
  102. $this->type = 'file';
  103. return $this;
  104. }
  105. /*
  106. * glob, patterns (must be //) or strings
  107. */
  108. protected function to_regex($str)
  109. {
  110. if (preg_match('/^(!)?([^a-zA-Z0-9\\\\]).+?\\2[ims]?$/', $str))
  111. {
  112. return $str;
  113. }
  114. return sfGlobToRegex::glob_to_regex($str);
  115. }
  116. protected function args_to_array($arg_list, $not = false)
  117. {
  118. $list = array();
  119. $nbArgList = count($arg_list);
  120. for ($i = 0; $i < $nbArgList; $i++)
  121. {
  122. if (is_array($arg_list[$i]))
  123. {
  124. foreach ($arg_list[$i] as $arg)
  125. {
  126. $list[] = array($not, $this->to_regex($arg));
  127. }
  128. }
  129. else
  130. {
  131. $list[] = array($not, $this->to_regex($arg_list[$i]));
  132. }
  133. }
  134. return $list;
  135. }
  136. /**
  137. * Adds rules that files must match.
  138. *
  139. * You can use patterns (delimited with / sign), globs or simple strings.
  140. *
  141. * $finder->name('*.php')
  142. * $finder->name('/\.php$/') // same as above
  143. * $finder->name('test.php')
  144. *
  145. * @param list a list of patterns, globs or strings
  146. * @return sfFinder Current object
  147. */
  148. public function name()
  149. {
  150. $args = func_get_args();
  151. $this->names = array_merge($this->names, $this->args_to_array($args));
  152. return $this;
  153. }
  154. /**
  155. * Adds rules that files must not match.
  156. *
  157. * @see ->name()
  158. * @param list a list of patterns, globs or strings
  159. * @return sfFinder Current object
  160. */
  161. public function not_name()
  162. {
  163. $args = func_get_args();
  164. $this->names = array_merge($this->names, $this->args_to_array($args, true));
  165. return $this;
  166. }
  167. /**
  168. * Adds tests for file sizes.
  169. *
  170. * $finder->size('> 10K');
  171. * $finder->size('<= 1Ki');
  172. * $finder->size(4);
  173. *
  174. * @param list a list of comparison strings
  175. * @return sfFinder Current object
  176. */
  177. public function size()
  178. {
  179. $args = func_get_args();
  180. $numargs = count($args);
  181. for ($i = 0; $i < $numargs; $i++)
  182. {
  183. $this->sizes[] = new sfNumberCompare($args[$i]);
  184. }
  185. return $this;
  186. }
  187. /**
  188. * Traverses no further.
  189. *
  190. * @param list a list of patterns, globs to match
  191. * @return sfFinder Current object
  192. */
  193. public function prune()
  194. {
  195. $args = func_get_args();
  196. $this->prunes = array_merge($this->prunes, $this->args_to_array($args));
  197. return $this;
  198. }
  199. /**
  200. * Discards elements that matches.
  201. *
  202. * @param list a list of patterns, globs to match
  203. * @return sfFinder Current object
  204. */
  205. public function discard()
  206. {
  207. $args = func_get_args();
  208. $this->discards = array_merge($this->discards, $this->args_to_array($args));
  209. return $this;
  210. }
  211. /**
  212. * Ignores version control directories.
  213. *
  214. * Currently supports Subversion, CVS, DARCS, Gnu Arch, Monotone, Bazaar-NG, GIT, Mercurial
  215. *
  216. * @param bool $ignore falase when version control directories shall be included (default is true)
  217. *
  218. * @return sfFinder Current object
  219. */
  220. public function ignore_version_control($ignore = true)
  221. {
  222. $this->ignore_version_control = $ignore;
  223. return $this;
  224. }
  225. /**
  226. * Returns files and directories ordered by name
  227. *
  228. * @return sfFinder Current object
  229. */
  230. public function sort_by_name()
  231. {
  232. $this->sort = 'name';
  233. return $this;
  234. }
  235. /**
  236. * Returns files and directories ordered by type (directories before files), then by name
  237. *
  238. * @return sfFinder Current object
  239. */
  240. public function sort_by_type()
  241. {
  242. $this->sort = 'type';
  243. return $this;
  244. }
  245. /**
  246. * Executes function or method for each element.
  247. *
  248. * Element match if functino or method returns true.
  249. *
  250. * $finder->exec('myfunction');
  251. * $finder->exec(array($object, 'mymethod'));
  252. *
  253. * @param mixed function or method to call
  254. * @return sfFinder Current object
  255. */
  256. public function exec()
  257. {
  258. $args = func_get_args();
  259. $numargs = count($args);
  260. for ($i = 0; $i < $numargs; $i++)
  261. {
  262. if (is_array($args[$i]) && !method_exists($args[$i][0], $args[$i][1]))
  263. {
  264. throw new sfException(sprintf('method "%s" does not exist for object "%s".', $args[$i][1], $args[$i][0]));
  265. }
  266. if (!is_array($args[$i]) && !function_exists($args[$i]))
  267. {
  268. throw new sfException(sprintf('function "%s" does not exist.', $args[$i]));
  269. }
  270. $this->execs[] = $args[$i];
  271. }
  272. return $this;
  273. }
  274. /**
  275. * Returns relative paths for all files and directories.
  276. *
  277. * @return sfFinder Current object
  278. */
  279. public function relative()
  280. {
  281. $this->relative = true;
  282. return $this;
  283. }
  284. /**
  285. * Symlink following.
  286. *
  287. * @return sfFinder Current object
  288. */
  289. public function follow_link()
  290. {
  291. $this->follow_link = true;
  292. return $this;
  293. }
  294. /**
  295. * Searches files and directories which match defined rules.
  296. *
  297. * @return array list of files and directories
  298. */
  299. public function in()
  300. {
  301. $files = array();
  302. $here_dir = getcwd();
  303. $finder = clone $this;
  304. if ($this->ignore_version_control)
  305. {
  306. $ignores = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg');
  307. $finder->discard($ignores)->prune($ignores);
  308. }
  309. // first argument is an array?
  310. $numargs = func_num_args();
  311. $arg_list = func_get_args();
  312. if ($numargs === 1 && is_array($arg_list[0]))
  313. {
  314. $arg_list = $arg_list[0];
  315. $numargs = count($arg_list);
  316. }
  317. for ($i = 0; $i < $numargs; $i++)
  318. {
  319. $dir = realpath($arg_list[$i]);
  320. if (!is_dir($dir))
  321. {
  322. continue;
  323. }
  324. $dir = str_replace('\\', '/', $dir);
  325. // absolute path?
  326. if (!self::isPathAbsolute($dir))
  327. {
  328. $dir = $here_dir.'/'.$dir;
  329. }
  330. $new_files = str_replace('\\', '/', $finder->search_in($dir));
  331. if ($this->relative)
  332. {
  333. $new_files = str_replace(rtrim($dir, '/').'/', '', $new_files);
  334. }
  335. $files = array_merge($files, $new_files);
  336. }
  337. if ($this->sort === 'name')
  338. {
  339. sort($files);
  340. }
  341. return array_unique($files);
  342. }
  343. protected function search_in($dir, $depth = 0)
  344. {
  345. if ($depth > $this->maxdepth)
  346. {
  347. return array();
  348. }
  349. $dir = realpath($dir);
  350. if ((!$this->follow_link) && is_link($dir))
  351. {
  352. return array();
  353. }
  354. $files = array();
  355. $temp_files = array();
  356. $temp_folders = array();
  357. if (is_dir($dir) && is_readable($dir))
  358. {
  359. $current_dir = opendir($dir);
  360. while (false !== $entryname = readdir($current_dir))
  361. {
  362. if ($entryname == '.' || $entryname == '..') continue;
  363. $current_entry = $dir.DIRECTORY_SEPARATOR.$entryname;
  364. if ((!$this->follow_link) && is_link($current_entry))
  365. {
  366. continue;
  367. }
  368. if (is_dir($current_entry))
  369. {
  370. if ($this->sort === 'type')
  371. {
  372. $temp_folders[$entryname] = $current_entry;
  373. }
  374. else
  375. {
  376. if (($this->type === 'directory' || $this->type === 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->exec_ok($dir, $entryname))
  377. {
  378. $files[] = $current_entry;
  379. }
  380. if (!$this->is_pruned($dir, $entryname))
  381. {
  382. $files = array_merge($files, $this->search_in($current_entry, $depth + 1));
  383. }
  384. }
  385. }
  386. else
  387. {
  388. if (($this->type !== 'directory' || $this->type === 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->size_ok($dir, $entryname) && $this->exec_ok($dir, $entryname))
  389. {
  390. if ($this->sort === 'type')
  391. {
  392. $temp_files[] = $current_entry;
  393. }
  394. else
  395. {
  396. $files[] = $current_entry;
  397. }
  398. }
  399. }
  400. }
  401. if ($this->sort === 'type')
  402. {
  403. ksort($temp_folders);
  404. foreach($temp_folders as $entryname => $current_entry)
  405. {
  406. if (($this->type === 'directory' || $this->type === 'any') && ($depth >= $this->mindepth) && !$this->is_discarded($dir, $entryname) && $this->match_names($dir, $entryname) && $this->exec_ok($dir, $entryname))
  407. {
  408. $files[] = $current_entry;
  409. }
  410. if (!$this->is_pruned($dir, $entryname))
  411. {
  412. $files = array_merge($files, $this->search_in($current_entry, $depth + 1));
  413. }
  414. }
  415. sort($temp_files);
  416. $files = array_merge($files, $temp_files);
  417. }
  418. closedir($current_dir);
  419. }
  420. return $files;
  421. }
  422. protected function match_names($dir, $entry)
  423. {
  424. if (!count($this->names)) return true;
  425. // Flags indicating that there was attempts to match
  426. // at least one "not_name" or "name" rule respectively
  427. // to following variables:
  428. $one_not_name_rule = false;
  429. $one_name_rule = false;
  430. foreach ($this->names as $args)
  431. {
  432. list($not, $regex) = $args;
  433. $not ? $one_not_name_rule = true : $one_name_rule = true;
  434. if (preg_match($regex, $entry))
  435. {
  436. // We must match ONLY ONE "not_name" or "name" rule:
  437. // if "not_name" rule matched then we return "false"
  438. // if "name" rule matched then we return "true"
  439. return $not ? false : true;
  440. }
  441. }
  442. if ($one_not_name_rule && $one_name_rule)
  443. {
  444. return false;
  445. }
  446. else if ($one_not_name_rule)
  447. {
  448. return true;
  449. }
  450. else if ($one_name_rule)
  451. {
  452. return false;
  453. }
  454. return true;
  455. }
  456. protected function size_ok($dir, $entry)
  457. {
  458. if (0 === count($this->sizes)) return true;
  459. if (!is_file($dir.DIRECTORY_SEPARATOR.$entry)) return true;
  460. $filesize = filesize($dir.DIRECTORY_SEPARATOR.$entry);
  461. foreach ($this->sizes as $number_compare)
  462. {
  463. if (!$number_compare->test($filesize)) return false;
  464. }
  465. return true;
  466. }
  467. protected function is_pruned($dir, $entry)
  468. {
  469. if (0 === count($this->prunes)) return false;
  470. foreach ($this->prunes as $args)
  471. {
  472. $regex = $args[1];
  473. if (preg_match($regex, $entry)) return true;
  474. }
  475. return false;
  476. }
  477. protected function is_discarded($dir, $entry)
  478. {
  479. if (0 === count($this->discards)) return false;
  480. foreach ($this->discards as $args)
  481. {
  482. $regex = $args[1];
  483. if (preg_match($regex, $entry)) return true;
  484. }
  485. return false;
  486. }
  487. protected function exec_ok($dir, $entry)
  488. {
  489. if (0 === count($this->execs)) return true;
  490. foreach ($this->execs as $exec)
  491. {
  492. if (!call_user_func_array($exec, array($dir, $entry))) return false;
  493. }
  494. return true;
  495. }
  496. public static function isPathAbsolute($path)
  497. {
  498. if ($path{0} === '/' || $path{0} === '\\' ||
  499. (strlen($path) > 3 && ctype_alpha($path{0}) &&
  500. $path{1} === ':' &&
  501. ($path{2} === '\\' || $path{2} === '/')
  502. )
  503. )
  504. {
  505. return true;
  506. }
  507. return false;
  508. }
  509. }
  510. /**
  511. * Match globbing patterns against text.
  512. *
  513. * if match_glob("foo.*", "foo.bar") echo "matched\n";
  514. *
  515. * // prints foo.bar and foo.baz
  516. * $regex = glob_to_regex("foo.*");
  517. * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t)
  518. * {
  519. * if (/$regex/) echo "matched: $car\n";
  520. * }
  521. *
  522. * sfGlobToRegex implements glob(3) style matching that can be used to match
  523. * against text, rather than fetching names from a filesystem.
  524. *
  525. * based on perl Text::Glob module.
  526. *
  527. * @package symfony
  528. * @subpackage util
  529. * @author Fabien Potencier <fabien.potencier@gmail.com> php port
  530. * @author Richard Clamp <richardc@unixbeard.net> perl version
  531. * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com>
  532. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  533. * @version SVN: $Id: sfFinder.class.php 30528 2010-08-04 16:25:14Z fabien $
  534. */
  535. class sfGlobToRegex
  536. {
  537. protected static $strict_leading_dot = true;
  538. protected static $strict_wildcard_slash = true;
  539. public static function setStrictLeadingDot($boolean)
  540. {
  541. self::$strict_leading_dot = $boolean;
  542. }
  543. public static function setStrictWildcardSlash($boolean)
  544. {
  545. self::$strict_wildcard_slash = $boolean;
  546. }
  547. /**
  548. * Returns a compiled regex which is the equiavlent of the globbing pattern.
  549. *
  550. * @param string $glob pattern
  551. * @return string regex
  552. */
  553. public static function glob_to_regex($glob)
  554. {
  555. $first_byte = true;
  556. $escaping = false;
  557. $in_curlies = 0;
  558. $regex = '';
  559. $sizeGlob = strlen($glob);
  560. for ($i = 0; $i < $sizeGlob; $i++)
  561. {
  562. $car = $glob[$i];
  563. if ($first_byte)
  564. {
  565. if (self::$strict_leading_dot && $car !== '.')
  566. {
  567. $regex .= '(?=[^\.])';
  568. }
  569. $first_byte = false;
  570. }
  571. if ($car === '/')
  572. {
  573. $first_byte = true;
  574. }
  575. if ($car === '.' || $car === '(' || $car === ')' || $car === '|' || $car === '+' || $car === '^' || $car === '$')
  576. {
  577. $regex .= "\\$car";
  578. }
  579. elseif ($car === '*')
  580. {
  581. $regex .= ($escaping ? '\\*' : (self::$strict_wildcard_slash ? '[^/]*' : '.*'));
  582. }
  583. elseif ($car === '?')
  584. {
  585. $regex .= ($escaping ? '\\?' : (self::$strict_wildcard_slash ? '[^/]' : '.'));
  586. }
  587. elseif ($car === '{')
  588. {
  589. $regex .= ($escaping ? '\\{' : '(');
  590. if (!$escaping) ++$in_curlies;
  591. }
  592. elseif ($car === '}' && $in_curlies)
  593. {
  594. $regex .= ($escaping ? '}' : ')');
  595. if (!$escaping) --$in_curlies;
  596. }
  597. elseif ($car === ',' && $in_curlies)
  598. {
  599. $regex .= ($escaping ? ',' : '|');
  600. }
  601. elseif ($car === '\\')
  602. {
  603. if ($escaping)
  604. {
  605. $regex .= '\\\\';
  606. $escaping = false;
  607. }
  608. else
  609. {
  610. $escaping = true;
  611. }
  612. continue;
  613. }
  614. else
  615. {
  616. $regex .= $car;
  617. }
  618. $escaping = false;
  619. }
  620. return '#^'.$regex.'$#';
  621. }
  622. }
  623. /**
  624. * Numeric comparisons.
  625. *
  626. * sfNumberCompare compiles a simple comparison to an anonymous
  627. * subroutine, which you can call with a value to be tested again.
  628. * Now this would be very pointless, if sfNumberCompare didn't understand
  629. * magnitudes.
  630. * The target value may use magnitudes of kilobytes (k, ki),
  631. * megabytes (m, mi), or gigabytes (g, gi). Those suffixed
  632. * with an i use the appropriate 2**n version in accordance with the
  633. * IEC standard: http://physics.nist.gov/cuu/Units/binary.html
  634. *
  635. * based on perl Number::Compare module.
  636. *
  637. * @package symfony
  638. * @subpackage util
  639. * @author Fabien Potencier <fabien.potencier@gmail.com> php port
  640. * @author Richard Clamp <richardc@unixbeard.net> perl version
  641. * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com>
  642. * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
  643. * @see http://physics.nist.gov/cuu/Units/binary.html
  644. * @version SVN: $Id: sfFinder.class.php 30528 2010-08-04 16:25:14Z fabien $
  645. */
  646. class sfNumberCompare
  647. {
  648. protected $test = '';
  649. public function __construct($test)
  650. {
  651. $this->test = $test;
  652. }
  653. public function test($number)
  654. {
  655. if (!preg_match('{^([<>]=?)?(.*?)([kmg]i?)?$}i', $this->test, $matches))
  656. {
  657. throw new sfException(sprintf('don\'t understand "%s" as a test.', $this->test));
  658. }
  659. $target = array_key_exists(2, $matches) ? $matches[2] : '';
  660. $magnitude = array_key_exists(3, $matches) ? $matches[3] : '';
  661. if (strtolower($magnitude) === 'k') $target *= 1000;
  662. if (strtolower($magnitude) === 'ki') $target *= 1024;
  663. if (strtolower($magnitude) === 'm') $target *= 1000000;
  664. if (strtolower($magnitude) === 'mi') $target *= 1024*1024;
  665. if (strtolower($magnitude) === 'g') $target *= 1000000000;
  666. if (strtolower($magnitude) === 'gi') $target *= 1024*1024*1024;
  667. $comparison = array_key_exists(1, $matches) ? $matches[1] : '==';
  668. if ($comparison === '==' || $comparison == '')
  669. {
  670. return ($number == $target);
  671. }
  672. if ($comparison === '>')
  673. {
  674. return ($number > $target);
  675. }
  676. if ($comparison === '>=')
  677. {
  678. return ($number >= $target);
  679. }
  680. if ($comparison === '<')
  681. {
  682. return ($number < $target);
  683. }
  684. if ($comparison === '<=')
  685. {
  686. return ($number <= $target);
  687. }
  688. return false;
  689. }
  690. }