From 12557af30c6bf8735d13768eb25f12a26f764086 Mon Sep 17 00:00:00 2001 From: Ben Scholzen Date: Fri, 30 Mar 2012 11:26:23 +0200 Subject: [PATCH 1/3] Complete glob fallback and some primitive test for it --- src/Glob.php | 95 ++++++++++++++++++++++++++--------------------- test/GlobTest.php | 41 ++++++++++++++++++++ test/_files/alpha | 0 test/_files/beta | 0 test/_files/delta | 0 test/_files/gamma | 0 6 files changed, 94 insertions(+), 42 deletions(-) create mode 100644 test/GlobTest.php create mode 100644 test/_files/alpha create mode 100644 test/_files/beta create mode 100644 test/_files/delta create mode 100644 test/_files/gamma diff --git a/src/Glob.php b/src/Glob.php index 7a50dcf2b..16750e47e 100644 --- a/src/Glob.php +++ b/src/Glob.php @@ -48,11 +48,12 @@ abstract class Glob * @see http://docs.php.net/glob * @param string $pattern * @param integer $flags + * @param boolean $forceFallback * @return array|false */ - public static function glob($pattern, $flags) + public static function glob($pattern, $flags, $forceFallback = false) { - if (!defined('GLOB_BRACE')) { + if (!defined('GLOB_BRACE') || $forceFallback) { return self::fallbackGlob($pattern, $flags); } else { return self::systemGlob($pattern, $flags); @@ -107,14 +108,33 @@ protected static function fallbackGlob($pattern, $flags) } $flags &= ~self::GLOB_BRACE; + $length = strlen($pattern); $paths = array(); - $begin = strstr($pattern, '{'); + + if ($flags & self::GLOB_NOESCAPE) { + $begin = strpos($pattern, '{'); + } else { + $begin = 0; + + while (true) { + if ($begin === $length) { + $begin = false; + break; + } else if ($pattern[$begin] === '\\' && ($begin + 1) < $length) { + $begin++; + } else if ($pattern[$begin] === '{') { + break; + } + + $begin++; + } + } if ($begin === false) { return self::systemGlob($pattern, $flags); } - $next = self::nextBraceSub($pattern, $begin); + $next = self::nextBraceSub($pattern, $begin + 1, $flags); if ($next === null) { return self::systemGlob($pattern, $flags); @@ -123,76 +143,67 @@ protected static function fallbackGlob($pattern, $flags) $rest = $next; while ($pattern[$rest] !== '}') { - $rest = self::nextBraceSub($pattern, $rest + 1); + $rest = self::nextBraceSub($pattern, $rest + 1, $flags); if ($rest === null) { return self::systemGlob($pattern, $flags); } } - $p = $being + 1; + $p = $begin + 1; - while (true) { - if ($pattern[$next] === '}') { - break; - } + while (true) { + $subPattern = substr($pattern, 0, $begin) + . substr($pattern, $p, $next - $p) + . substr($pattern, $rest + 1); - $result = self::fallbackGlob($pattern, $flags); + $result = self::fallbackGlob($subPattern, $flags | self::GLOB_BRACE); if ($result) { $paths = array_merge($paths, $result); } + + if ($pattern[$next] === '}') { + break; + } $p = $next + 1; - $next = self::nextBraceSub($pattern, $p); + $next = self::nextBraceSub($pattern, $p, $flags); } - return $paths; + return array_unique($paths); } /** * Find the end of the sub-pattern in a brace expression. * - * @param string $pattern + * @param string $pattern * @param integer $begin + * @param integer $flags * @return integer|null */ - protected static function nextBraceSub($pattern, $begin) + protected static function nextBraceSub($pattern, $begin, $flags) { $length = strlen($pattern); $depth = 0; $current = $begin; - - while (true) { - if ($depth === 0) { - if ($pattern[$current] !== ',' && $current < $length) { - if ($pattern[$current] === '{') { - $depth++; - } - - $current++; - continue; - } - } else { - while ($current < $length && $pattern[$current] !== '}' || $depth > 0) { - if ($pattern['current'] === '}') { - $depth--; - } - - $current++; + + while ($current < $length) { + if (!$flags & self::GLOB_NOESCAPE && $pattern[$current] === '\\') { + if (++$current === $length) { + break; } - if ($current >= $length) { - // An incorrectly terminated brace expression. - return null; + $current++; + } else { + if (($pattern[$current] === '}' && $depth-- === 0) || ($pattern[$current] === ',' && $depth === 0)) { + break; + } else if ($pattern[$current++] === '{') { + $depth++; } - - continue; } - - break; } - - return $current; + + return ($current < $length ? $current : null); } } diff --git a/test/GlobTest.php b/test/GlobTest.php new file mode 100644 index 000000000..0ea85c51d --- /dev/null +++ b/test/GlobTest.php @@ -0,0 +1,41 @@ +markTestSkipped('GLOB_BRACE not available'); + } + + $this->assertEquals( + glob(__DIR__ . '/_files/{alph,bet}a', GLOB_BRACE), + Glob::glob(__DIR__ . '/_files/{alph,bet}a', Glob::GLOB_BRACE, true) + ); + } +} diff --git a/test/_files/alpha b/test/_files/alpha new file mode 100644 index 000000000..e69de29bb diff --git a/test/_files/beta b/test/_files/beta new file mode 100644 index 000000000..e69de29bb diff --git a/test/_files/delta b/test/_files/delta new file mode 100644 index 000000000..e69de29bb diff --git a/test/_files/gamma b/test/_files/gamma new file mode 100644 index 000000000..e69de29bb From ba27fcfeeb04e64e086d69ceefeb615625c3b9ca Mon Sep 17 00:00:00 2001 From: prolic Date: Sun, 22 Apr 2012 13:23:11 +0200 Subject: [PATCH 2/3] [zen-27] Renamed interfaces in Zend\Stdlib, added missing pipelines (|) between params in documentation, fixed exceptions in stdlib --- src/Exception/BadMethodCallException.php | 5 ++--- src/Exception/DomainException.php | 6 +++--- src/Exception/InvalidArgumentException.php | 6 ++---- src/Exception/InvalidCallbackException.php | 6 +----- src/Exception/LogicException.php | 6 +++--- 5 files changed, 11 insertions(+), 18 deletions(-) diff --git a/src/Exception/BadMethodCallException.php b/src/Exception/BadMethodCallException.php index 62c1b912c..724b9c6c5 100644 --- a/src/Exception/BadMethodCallException.php +++ b/src/Exception/BadMethodCallException.php @@ -20,8 +20,6 @@ namespace Zend\Stdlib\Exception; -use Zend\Stdlib\Exception\ExceptionInterface; - /** * Bad method call exception * @@ -30,7 +28,8 @@ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class BadMethodCallException extends \BadMethodCallException +class BadMethodCallException + extends \BadMethodCallException implements ExceptionInterface { } diff --git a/src/Exception/DomainException.php b/src/Exception/DomainException.php index 3090b2c21..7913dbb95 100644 --- a/src/Exception/DomainException.php +++ b/src/Exception/DomainException.php @@ -2,8 +2,8 @@ namespace Zend\Stdlib\Exception; -use Zend\Stdlib\Exception\ExceptionInterface; - -class DomainException extends \DomainException implements ExceptionInterface +class DomainException + extends \DomainException + implements ExceptionInterface { } diff --git a/src/Exception/InvalidArgumentException.php b/src/Exception/InvalidArgumentException.php index f11bafc2b..1dd4777a9 100644 --- a/src/Exception/InvalidArgumentException.php +++ b/src/Exception/InvalidArgumentException.php @@ -2,10 +2,8 @@ namespace Zend\Stdlib\Exception; -use Zend\Stdlib\Exception\ExceptionInterface; - -class InvalidArgumentException - extends \InvalidArgumentException +class InvalidArgumentException + extends \InvalidArgumentException implements ExceptionInterface { } diff --git a/src/Exception/InvalidCallbackException.php b/src/Exception/InvalidCallbackException.php index 3fff0b037..1905144d6 100644 --- a/src/Exception/InvalidCallbackException.php +++ b/src/Exception/InvalidCallbackException.php @@ -20,20 +20,16 @@ namespace Zend\Stdlib\Exception; -use Zend\Stdlib\Exception\ExceptionInterface; - /** * Invalid callback exception * - * @uses Exception - * @uses Zend\Stdlib\Exception * @category Zend * @package Zend_Stdlib * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ class InvalidCallbackException - extends DomainException + extends \DomainException implements ExceptionInterface { } diff --git a/src/Exception/LogicException.php b/src/Exception/LogicException.php index 4b6ecaa26..79369f162 100644 --- a/src/Exception/LogicException.php +++ b/src/Exception/LogicException.php @@ -20,8 +20,6 @@ namespace Zend\Stdlib\Exception; -use Zend\Stdlib\Exception\ExceptionInterface; - /** * logic exception * @@ -30,6 +28,8 @@ * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) * @license http://framework.zend.com/license/new-bsd New BSD License */ -class LogicException extends \LogicException implements ExceptionInterface +class LogicException + extends \LogicException + implements ExceptionInterface { } From 1165865751b7b8367c85913d669d940eec297bdf Mon Sep 17 00:00:00 2001 From: prolic Date: Sun, 22 Apr 2012 14:58:17 +0200 Subject: [PATCH 3/3] [zen-27] Renamed interfaces in Zend\Stdlib, refactored: use Zend\Stdlib\DispatchableInterface => use Zend\Stdlib\DispatchableInterface as Dispatchable, Zend\Stdlib\RequestInterface => Zend\Stdlib\RequestInterface as Request, ... --- src/DispatchableInterface.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/DispatchableInterface.php b/src/DispatchableInterface.php index a1698fb32..794227074 100644 --- a/src/DispatchableInterface.php +++ b/src/DispatchableInterface.php @@ -1,7 +1,10 @@