123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799 |
- <?php
- /*
- * This file is part of the symfony package.
- * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- /**
- *
- * Allow to build rules to find files and directories.
- *
- * All rules may be invoked several times, except for ->in() method.
- * Some rules are cumulative (->name() for example) whereas others are destructive
- * (most recent value is used, ->maxdepth() method for example).
- *
- * All methods return the current sfFinder object to allow easy chaining:
- *
- * $files = sfFinder::type('file')->name('*.php')->in(.);
- *
- * Interface loosely based on perl File::Find::Rule module.
- *
- * @package symfony
- * @subpackage util
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- * @version SVN: $Id: sfFinder.class.php 14265 2008-12-22 20:39:59Z FabianLange $
- */
- class sfFinder
- {
- protected $type = 'file';
- protected $names = array();
- protected $prunes = array();
- protected $discards = array();
- protected $execs = array();
- protected $mindepth = 0;
- protected $sizes = array();
- protected $maxdepth = 1000000;
- protected $relative = false;
- protected $follow_link = false;
- protected $sort = false;
- protected $ignore_version_control = true;
- /**
- * Sets maximum directory depth.
- *
- * Finder will descend at most $level levels of directories below the starting point.
- *
- * @param int $level
- * @return object current sfFinder object
- */
- public function maxdepth($level)
- {
- $this->maxdepth = $level;
- return $this;
- }
- /**
- * Sets minimum directory depth.
- *
- * Finder will start applying tests at level $level.
- *
- * @param int $level
- * @return object current sfFinder object
- */
- public function mindepth($level)
- {
- $this->mindepth = $level;
- return $this;
- }
- public function get_type()
- {
- return $this->type;
- }
- /**
- * Sets the type of elements to returns.
- *
- * @param string $name directory or file or any (for both file and directory)
- * @return object new sfFinder object
- */
- public static function type($name)
- {
- $finder = new sfFinder();
- return $finder->setType($name);
- }
- public function setType($name)
- {
- if (strtolower(substr($name, 0, 3)) == 'dir')
- {
- $this->type = 'directory';
- }
- else if (strtolower($name) == 'any')
- {
- $this->type = 'any';
- }
- else
- {
- $this->type = 'file';
- }
- return $this;
- }
- /*
- * glob, patterns (must be //) or strings
- */
- protected function to_regex($str)
- {
- if ($str{0} == '/' && $str{strlen($str) - 1} == '/')
- {
- return $str;
- }
- else
- {
- return sfGlobToRegex::glob_to_regex($str);
- }
- }
- protected function args_to_array($arg_list, $not = false)
- {
- $list = array();
- for ($i = 0; $i < count($arg_list); $i++)
- {
- if (is_array($arg_list[$i]))
- {
- foreach ($arg_list[$i] as $arg)
- {
- $list[] = array($not, $this->to_regex($arg));
- }
- }
- else
- {
- $list[] = array($not, $this->to_regex($arg_list[$i]));
- }
- }
- return $list;
- }
- /**
- * Adds rules that files must match.
- *
- * You can use patterns (delimited with / sign), globs or simple strings.
- *
- * $finder->name('*.php')
- * $finder->name('/\.php$/') // same as above
- * $finder->name('test.php')
- *
- * @param list a list of patterns, globs or strings
- * @return object current sfFinder object
- */
- public function name()
- {
- $args = func_get_args();
- $this->names = array_merge($this->names, $this->args_to_array($args));
- return $this;
- }
- /**
- * Adds rules that files must not match.
- *
- * @see ->name()
- * @param list a list of patterns, globs or strings
- * @return object current sfFinder object
- */
- public function not_name()
- {
- $args = func_get_args();
- $this->names = array_merge($this->names, $this->args_to_array($args, true));
- return $this;
- }
- /**
- * Adds tests for file sizes.
- *
- * $finder->size('> 10K');
- * $finder->size('<= 1Ki');
- * $finder->size(4);
- *
- * @param list a list of comparison strings
- * @return object current sfFinder object
- */
- public function size()
- {
- $args = func_get_args();
- for ($i = 0; $i < count($args); $i++)
- {
- $this->sizes[] = new sfNumberCompare($args[$i]);
- }
- return $this;
- }
- /**
- * Traverses no further.
- *
- * @param list a list of patterns, globs to match
- * @return object current sfFinder object
- */
- public function prune()
- {
- $args = func_get_args();
- $this->prunes = array_merge($this->prunes, $this->args_to_array($args));
- return $this;
- }
- /**
- * Discards elements that matches.
- *
- * @param list a list of patterns, globs to match
- * @return object current sfFinder object
- */
- public function discard()
- {
- $args = func_get_args();
- $this->discards = array_merge($this->discards, $this->args_to_array($args));
- return $this;
- }
- /**
- * Ignores version control directories.
- *
- * Currently supports Subversion, CVS, DARCS, Gnu Arch, Monotone, Bazaar-NG, GIT, Mercurial
- *
- * @param bool $ignore falase when version control directories shall be included (default is true)
- *
- * @return object current sfFinder object
- */
- public function ignore_version_control($ignore = true)
- {
- $this->ignore_version_control = $ignore;
- return $this;
- }
- /**
- * Returns files and directories ordered by name
- *
- * @return object current sfFinder object
- */
- public function sort_by_name()
- {
- $this->sort = 'name';
- return $this;
- }
- /**
- * Returns files and directories ordered by type (directories before files), then by name
- *
- * @return object current sfFinder object
- */
- public function sort_by_type()
- {
- $this->sort = 'type';
- return $this;
- }
- /**
- * Executes function or method for each element.
- *
- * Element match if functino or method returns true.
- *
- * $finder->exec('myfunction');
- * $finder->exec(array($object, 'mymethod'));
- *
- * @param mixed function or method to call
- * @return object current sfFinder object
- */
- public function exec()
- {
- $args = func_get_args();
- for ($i = 0; $i < count($args); $i++)
- {
- if (is_array($args[$i]) && !method_exists($args[$i][0], $args[$i][1]))
- {
- throw new sfException(sprintf('method "%s" does not exist for object "%s".', $args[$i][1], $args[$i][0]));
- }
- else if (!is_array($args[$i]) && !function_exists($args[$i]))
- {
- throw new sfException(sprintf('function "%s" does not exist.', $args[$i]));
- }
- $this->execs[] = $args[$i];
- }
- return $this;
- }
- /**
- * Returns relative paths for all files and directories.
- *
- * @return object current sfFinder object
- */
- public function relative()
- {
- $this->relative = true;
- return $this;
- }
- /**
- * Symlink following.
- *
- * @return object current sfFinder object
- */
- public function follow_link()
- {
- $this->follow_link = true;
- return $this;
- }
- /**
- * Searches files and directories which match defined rules.
- *
- * @return array list of files and directories
- */
- public function in()
- {
- $files = array();
- $here_dir = getcwd();
- $finder = clone $this;
- if ($this->ignore_version_control)
- {
- $ignores = array('.svn', '_svn', 'CVS', '_darcs', '.arch-params', '.monotone', '.bzr', '.git', '.hg');
- $finder->discard($ignores)->prune($ignores);
- }
- // first argument is an array?
- $numargs = func_num_args();
- $arg_list = func_get_args();
- if ($numargs == 1 && is_array($arg_list[0]))
- {
- $arg_list = $arg_list[0];
- $numargs = count($arg_list);
- }
- for ($i = 0; $i < $numargs; $i++)
- {
- $dir = realpath($arg_list[$i]);
- if (!is_dir($dir))
- {
- continue;
- }
- $dir = str_replace('\\', '/', $dir);
- // absolute path?
- if (!self::isPathAbsolute($dir))
- {
- $dir = $here_dir.'/'.$dir;
- }
- $new_files = str_replace('\\', '/', $finder->search_in($dir));
- if ($this->relative)
- {
- $new_files = str_replace(rtrim($dir, '/').'/', '', $new_files);
- }
- $files = array_merge($files, $new_files);
- }
- if ($this->sort == 'name')
- {
- sort($files);
- }
- return array_unique($files);
- }
- protected function search_in($dir, $depth = 0)
- {
- if ($depth > $this->maxdepth)
- {
- return array();
- }
- $dir = realpath($dir);
- if ((!$this->follow_link) && is_link($dir))
- {
- return array();
- }
- $files = array();
- $temp_files = array();
- $temp_folders = array();
- if (is_dir($dir))
- {
- $current_dir = opendir($dir);
- while (false !== $entryname = readdir($current_dir))
- {
- if ($entryname == '.' || $entryname == '..') continue;
- $current_entry = $dir.DIRECTORY_SEPARATOR.$entryname;
- if ((!$this->follow_link) && is_link($current_entry))
- {
- continue;
- }
- if (is_dir($current_entry))
- {
- if ($this->sort == 'type')
- {
- $temp_folders[$entryname] = $current_entry;
- }
- else
- {
- 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))
- {
- $files[] = $current_entry;
- }
- if (!$this->is_pruned($dir, $entryname))
- {
- $files = array_merge($files, $this->search_in($current_entry, $depth + 1));
- }
- }
- }
- else
- {
- 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))
- {
- if ($this->sort == 'type')
- {
- $temp_files[] = $current_entry;
- }
- else
- {
- $files[] = $current_entry;
- }
- }
- }
- }
- if ($this->sort == 'type')
- {
- ksort($temp_folders);
- foreach($temp_folders as $entryname => $current_entry)
- {
- 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))
- {
- $files[] = $current_entry;
- }
- if (!$this->is_pruned($dir, $entryname))
- {
- $files = array_merge($files, $this->search_in($current_entry, $depth + 1));
- }
- }
- sort($temp_files);
- $files = array_merge($files, $temp_files);
- }
- closedir($current_dir);
- }
- return $files;
- }
- protected function match_names($dir, $entry)
- {
- if (!count($this->names)) return true;
- // we must match one "not_name" rules to be ko
- $one_not_name_rule = false;
- foreach ($this->names as $args)
- {
- list($not, $regex) = $args;
- if ($not)
- {
- $one_not_name_rule = true;
- if (preg_match($regex, $entry))
- {
- return false;
- }
- }
- }
- $one_name_rule = false;
- // we must match one "name" rules to be ok
- foreach ($this->names as $args)
- {
- list($not, $regex) = $args;
- if (!$not)
- {
- $one_name_rule = true;
- if (preg_match($regex, $entry))
- {
- return true;
- }
- }
- }
- if ($one_not_name_rule && $one_name_rule)
- {
- return false;
- }
- else if ($one_not_name_rule)
- {
- return true;
- }
- else if ($one_name_rule)
- {
- return false;
- }
- else
- {
- return true;
- }
- }
- protected function size_ok($dir, $entry)
- {
- if (!count($this->sizes)) return true;
- if (!is_file($dir.DIRECTORY_SEPARATOR.$entry)) return true;
- $filesize = filesize($dir.DIRECTORY_SEPARATOR.$entry);
- foreach ($this->sizes as $number_compare)
- {
- if (!$number_compare->test($filesize)) return false;
- }
- return true;
- }
- protected function is_pruned($dir, $entry)
- {
- if (!count($this->prunes)) return false;
- foreach ($this->prunes as $args)
- {
- $regex = $args[1];
- if (preg_match($regex, $entry)) return true;
- }
- return false;
- }
- protected function is_discarded($dir, $entry)
- {
- if (!count($this->discards)) return false;
- foreach ($this->discards as $args)
- {
- $regex = $args[1];
- if (preg_match($regex, $entry)) return true;
- }
- return false;
- }
- protected function exec_ok($dir, $entry)
- {
- if (!count($this->execs)) return true;
- foreach ($this->execs as $exec)
- {
- if (!call_user_func_array($exec, array($dir, $entry))) return false;
- }
- return true;
- }
- public static function isPathAbsolute($path)
- {
- if ($path{0} == '/' || $path{0} == '\\' ||
- (strlen($path) > 3 && ctype_alpha($path{0}) &&
- $path{1} == ':' &&
- ($path{2} == '\\' || $path{2} == '/')
- )
- )
- {
- return true;
- }
- return false;
- }
- }
- /**
- * Match globbing patterns against text.
- *
- * if match_glob("foo.*", "foo.bar") echo "matched\n";
- *
- * // prints foo.bar and foo.baz
- * $regex = glob_to_regex("foo.*");
- * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t)
- * {
- * if (/$regex/) echo "matched: $car\n";
- * }
- *
- * sfGlobToRegex implements glob(3) style matching that can be used to match
- * against text, rather than fetching names from a filesystem.
- *
- * based on perl Text::Glob module.
- *
- * @package symfony
- * @subpackage util
- * @author Fabien Potencier <fabien.potencier@gmail.com> php port
- * @author Richard Clamp <richardc@unixbeard.net> perl version
- * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com>
- * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
- * @version SVN: $Id: sfFinder.class.php 14265 2008-12-22 20:39:59Z FabianLange $
- */
- class sfGlobToRegex
- {
- protected static $strict_leading_dot = true;
- protected static $strict_wildcard_slash = true;
- public static function setStrictLeadingDot($boolean)
- {
- self::$strict_leading_dot = $boolean;
- }
- public static function setStrictWildcardSlash($boolean)
- {
- self::$strict_wildcard_slash = $boolean;
- }
- /**
- * Returns a compiled regex which is the equiavlent of the globbing pattern.
- *
- * @param string $glob pattern
- * @return string regex
- */
- public static function glob_to_regex($glob)
- {
- $first_byte = true;
- $escaping = false;
- $in_curlies = 0;
- $regex = '';
- for ($i = 0; $i < strlen($glob); $i++)
- {
- $car = $glob[$i];
- if ($first_byte)
- {
- if (self::$strict_leading_dot && $car != '.')
- {
- $regex .= '(?=[^\.])';
- }
- $first_byte = false;
- }
- if ($car == '/')
- {
- $first_byte = true;
- }
- if ($car == '.' || $car == '(' || $car == ')' || $car == '|' || $car == '+' || $car == '^' || $car == '$')
- {
- $regex .= "\\$car";
- }
- else if ($car == '*')
- {
- $regex .= ($escaping ? "\\*" : (self::$strict_wildcard_slash ? "[^/]*" : ".*"));
- }
- else if ($car == '?')
- {
- $regex .= ($escaping ? "\\?" : (self::$strict_wildcard_slash ? "[^/]" : "."));
- }
- else if ($car == '{')
- {
- $regex .= ($escaping ? "\\{" : "(");
- if (!$escaping) ++$in_curlies;
- }
- else if ($car == '}' && $in_curlies)
- {
- $regex .= ($escaping ? "}" : ")");
- if (!$escaping) --$in_curlies;
- }
- else if ($car == ',' && $in_curlies)
- {
- $regex .= ($escaping ? "," : "|");
- }
- else if ($car == "\\")
- {
- if ($escaping)
- {
- $regex .= "\\\\";
- $escaping = false;
- }
- else
- {
- $escaping = true;
- }
- continue;
- }
- else
- {
- $regex .= $car;
- $escaping = false;
- }
- $escaping = false;
- }
- return "#^$regex$#";
- }
- }
- /**
- * Numeric comparisons.
- *
- * sfNumberCompare compiles a simple comparison to an anonymous
- * subroutine, which you can call with a value to be tested again.
- * Now this would be very pointless, if sfNumberCompare didn't understand
- * magnitudes.
- * The target value may use magnitudes of kilobytes (k, ki),
- * megabytes (m, mi), or gigabytes (g, gi). Those suffixed
- * with an i use the appropriate 2**n version in accordance with the
- * IEC standard: http://physics.nist.gov/cuu/Units/binary.html
- *
- * based on perl Number::Compare module.
- *
- * @package symfony
- * @subpackage util
- * @author Fabien Potencier <fabien.potencier@gmail.com> php port
- * @author Richard Clamp <richardc@unixbeard.net> perl version
- * @copyright 2004-2005 Fabien Potencier <fabien.potencier@gmail.com>
- * @copyright 2002 Richard Clamp <richardc@unixbeard.net>
- * @see http://physics.nist.gov/cuu/Units/binary.html
- * @version SVN: $Id: sfFinder.class.php 14265 2008-12-22 20:39:59Z FabianLange $
- */
- class sfNumberCompare
- {
- protected $test = '';
- public function __construct($test)
- {
- $this->test = $test;
- }
- public function test($number)
- {
- if (!preg_match('{^([<>]=?)?(.*?)([kmg]i?)?$}i', $this->test, $matches))
- {
- throw new sfException(sprintf('don\'t understand "%s" as a test.', $this->test));
- }
- $target = array_key_exists(2, $matches) ? $matches[2] : '';
- $magnitude = array_key_exists(3, $matches) ? $matches[3] : '';
- if (strtolower($magnitude) == 'k') $target *= 1000;
- if (strtolower($magnitude) == 'ki') $target *= 1024;
- if (strtolower($magnitude) == 'm') $target *= 1000000;
- if (strtolower($magnitude) == 'mi') $target *= 1024*1024;
- if (strtolower($magnitude) == 'g') $target *= 1000000000;
- if (strtolower($magnitude) == 'gi') $target *= 1024*1024*1024;
- $comparison = array_key_exists(1, $matches) ? $matches[1] : '==';
- if ($comparison == '==' || $comparison == '')
- {
- return ($number == $target);
- }
- else if ($comparison == '>')
- {
- return ($number > $target);
- }
- else if ($comparison == '>=')
- {
- return ($number >= $target);
- }
- else if ($comparison == '<')
- {
- return ($number < $target);
- }
- else if ($comparison == '<=')
- {
- return ($number <= $target);
- }
- return false;
- }
- }
|