[ Index ]
 

Code source de Symfony 1.0.0

Accédez au Source d'autres logiciels libresSoutenez Angelica Josefina !

title

Body

[fermer]

/lib/vendor/pake/ -> pakeGlobToRegex.class.php (source)

   1  <?php
   2  
   3  /**
   4   * @package    pake
   5   * @author     Fabien Potencier <fabien.potencier@symfony-project.com> php port
   6   * @author     Richard Clamp <richardc@unixbeard.net> perl version
   7   * @copyright  2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
   8   * @copyright  2002 Richard Clamp <richardc@unixbeard.net>
   9   * @license    see the LICENSE file included in the distribution
  10   * @version    SVN: $Id: pakeGlobToRegex.class.php 1791 2006-08-23 21:17:06Z fabien $
  11   */
  12  
  13  if (class_exists('pakeGlobToRegex'))
  14  {
  15    return;
  16  }
  17  
  18  /**
  19   *
  20   * Match globbing patterns against text.
  21   *
  22   *   if match_glob("foo.*", "foo.bar") echo "matched\n";
  23   *
  24   * // prints foo.bar and foo.baz
  25   * $regex = glob_to_regex("foo.*");
  26   * for (array('foo.bar', 'foo.baz', 'foo', 'bar') as $t)
  27   * {
  28   *   if (/$regex/) echo "matched: $car\n";
  29   * }
  30   *
  31   * pakeGlobToRegex implements glob(3) style matching that can be used to match
  32   * against text, rather than fetching names from a filesystem.
  33   *
  34   * based on perl Text::Glob module.
  35   *
  36   * @package    pake
  37   * @author     Fabien Potencier <fabien.potencier@symfony-project.com> php port
  38   * @author     Richard Clamp <richardc@unixbeard.net> perl version
  39   * @copyright  2004-2005 Fabien Potencier <fabien.potencier@symfony-project.com>
  40   * @copyright  2002 Richard Clamp <richardc@unixbeard.net>
  41   * @license    see the LICENSE file included in the distribution
  42   * @version    SVN: $Id: pakeGlobToRegex.class.php 1791 2006-08-23 21:17:06Z fabien $
  43   */
  44  class pakeGlobToRegex
  45  {
  46    private static $strict_leading_dot = true;
  47    private static $strict_wildcard_slash = true;
  48  
  49    public static function setStrictLeadingDot($boolean)
  50    {
  51      self::$strict_leading_dot = $boolean;
  52    }
  53  
  54    public static function setStrictWildcardSlash($boolean)
  55    {
  56      self::$strict_wildcard_slash = $boolean;
  57    }
  58  
  59    /**
  60     * Returns a compiled regex which is the equiavlent of the globbing pattern.
  61     *
  62     * @param  string glob pattern
  63     * @return string regex
  64     */
  65    public static function glob_to_regex($glob)
  66    {
  67      $first_byte = true;
  68      $escaping = false;
  69      $in_curlies = 0;
  70      $regex = '';
  71      for ($i = 0; $i < strlen($glob); $i++)
  72      {
  73        $car = $glob[$i];
  74        if ($first_byte)
  75        {
  76          if (self::$strict_leading_dot && $car != '.')
  77          {
  78            $regex .= '(?=[^\.])';
  79          }
  80  
  81          $first_byte = false;
  82        }
  83  
  84        if ($car == '/')
  85        {
  86          $first_byte = true;
  87        }
  88  
  89        if ($car == '.' || $car == '(' || $car == ')' || $car == '|' || $car == '+' || $car == '^' || $car == '$')
  90        {
  91          $regex .= "\\$car";
  92        }
  93        else if ($car == '*')
  94        {
  95          $regex .= ($escaping ? "\\*" : (self::$strict_wildcard_slash ? "[^/]*" : ".*"));
  96        }
  97        else if ($car == '?')
  98        {
  99          $regex .= ($escaping ? "\\?" : (self::$strict_wildcard_slash ? "[^/]" : "."));
 100        }
 101        else if ($car == '{')
 102        {
 103          $regex .= ($escaping ? "\\{" : "(");
 104          if (!$escaping) ++$in_curlies;
 105        }
 106        else if ($car == '}' && $in_curlies)
 107        {
 108          $regex .= ($escaping ? "}" : ")");
 109          if (!$escaping) --$in_curlies;
 110        }
 111        else if ($car == ',' && $in_curlies)
 112        {
 113          $regex .= ($escaping ? "," : "|");
 114        }
 115        else if ($car == "\\")
 116        {
 117          if ($escaping)
 118          {
 119            $regex .= "\\\\";
 120            $escaping = false;
 121          }
 122          else
 123          {
 124            $escaping = true;
 125          }
 126  
 127          continue;
 128        }
 129        else
 130        {
 131          $regex .= $car;
 132          $escaping = false;
 133        }
 134        $escaping = false;
 135      }
 136  
 137      return "#^$regex$#";
 138    }
 139  }


Généré le : Fri Mar 16 22:42:14 2007 par Balluche grâce à PHPXref 0.7