This repository has been archived by the owner on Jan 31, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/glob-brace-fallback' of https://github.com/DASP…
…RiD/zf2 into feature/stdlib-glob
- Loading branch information
70 parents
0c21258
+
213395c
+
8e514a8
+
2f30186
+
bb4ed65
+
132d5b6
+
030ff33
+
f2f20f3
+
a50e133
+
4c554ee
+
dbfb1b8
+
ccab83f
+
00b350f
+
78945d0
+
f0e5f4b
+
ceb7d8c
+
9e124d1
+
3de5912
+
b6a974a
+
10a6438
+
cb6c1e7
+
18afd6c
+
3baf1bd
+
c800904
+
f52dcb8
+
126ccb2
+
e7d6206
+
e2d24ab
+
ec1abfc
+
290ea90
+
9f4ca1b
+
edaa760
+
c4c0c95
+
d21f055
+
5b18029
+
e6b97af
+
010fb36
+
64c7b8d
+
636523e
+
4cc2cd6
+
e34098a
+
16367cd
+
943c77f
+
8226e5b
+
0b47726
+
3cd8a03
+
cc4782c
+
9c653a6
+
656dbe5
+
9bce1ba
+
7dc18ca
+
861130d
+
2d2ffbd
+
4f413a5
+
2e1067a
+
1d082e4
+
e8aeb79
+
2e4dc80
+
19d128f
+
1b9e4b2
+
1c46483
+
fdda3f2
+
595fcd1
+
b562091
+
ff2fdc3
+
4aa72c0
+
1bb67ac
+
dd54faf
+
57f9063
+
12557af
commit b88ce2e
Showing
6 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,209 @@ | ||
<?php | ||
/** | ||
* Zend Framework | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://framework.zend.com/license/new-bsd | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to license@zend.com so we can send you a copy immediately. | ||
* | ||
* @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 | ||
*/ | ||
|
||
namespace Zend\Stdlib; | ||
|
||
/** | ||
* Wrapper for glob with fallback if GLOB_BRACE is not available. | ||
* | ||
* @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 | ||
*/ | ||
abstract class Glob | ||
{ | ||
/**#@+ | ||
* Glob constants. | ||
*/ | ||
const GLOB_MARK = 0x01; | ||
const GLOB_NOSORT = 0x02; | ||
const GLOB_NOCHECK = 0x04; | ||
const GLOB_NOESCAPE = 0x08; | ||
const GLOB_BRACE = 0x10; | ||
const GLOB_ONLYDIR = 0x20; | ||
const GLOB_ERR = 0x30; | ||
/**#@-*/ | ||
|
||
/** | ||
* Find pathnames matching a pattern. | ||
* | ||
* @see http://docs.php.net/glob | ||
* @param string $pattern | ||
* @param integer $flags | ||
* @param boolean $forceFallback | ||
* @return array|false | ||
*/ | ||
public static function glob($pattern, $flags, $forceFallback = false) | ||
{ | ||
if (!defined('GLOB_BRACE') || $forceFallback) { | ||
return self::fallbackGlob($pattern, $flags); | ||
} else { | ||
return self::systemGlob($pattern, $flags); | ||
} | ||
} | ||
|
||
/** | ||
* Use the glob function provided by the system. | ||
* | ||
* @param string $pattern | ||
* @param integer $flags | ||
* @return array|false | ||
*/ | ||
protected static function systemGlob($pattern, $flags) | ||
{ | ||
if ($flags) { | ||
$flagMap = array( | ||
self::GLOB_MARK => GLOB_MARK, | ||
self::GLOB_NOSORT => GLOB_NOSORT, | ||
self::GLOB_NOCHECK => GLOB_NOCHECK, | ||
self::GLOB_NOESCAPE => GLOB_NOESCAPE, | ||
self::GLOB_BRACE => GLOB_BRACE, | ||
self::GLOB_ONLYDIR => GLOB_ONLYDIR, | ||
self::GLOB_ERR => GLOB_ERR, | ||
); | ||
|
||
$globFlags = 0; | ||
|
||
foreach ($flagMap as $internalFlag => $globFlag) { | ||
if ($flags & $internalFlag) { | ||
$globFlags |= $globFlag; | ||
} | ||
} | ||
} else { | ||
$globFlags = 0; | ||
} | ||
|
||
return glob($pattern, $globFlags); | ||
} | ||
|
||
/** | ||
* Expand braces manually, then use the system glob. | ||
* | ||
* @param string $pattern | ||
* @param integer $flags | ||
* @return array|false | ||
*/ | ||
protected static function fallbackGlob($pattern, $flags) | ||
{ | ||
if (!$flags & self::GLOB_BRACE) { | ||
return self::systemGlob($pattern, $flags); | ||
} | ||
|
||
$flags &= ~self::GLOB_BRACE; | ||
$length = strlen($pattern); | ||
$paths = array(); | ||
|
||
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 + 1, $flags); | ||
|
||
if ($next === null) { | ||
return self::systemGlob($pattern, $flags); | ||
} | ||
|
||
$rest = $next; | ||
|
||
while ($pattern[$rest] !== '}') { | ||
$rest = self::nextBraceSub($pattern, $rest + 1, $flags); | ||
|
||
if ($rest === null) { | ||
return self::systemGlob($pattern, $flags); | ||
} | ||
} | ||
|
||
$p = $begin + 1; | ||
|
||
while (true) { | ||
$subPattern = substr($pattern, 0, $begin) | ||
. substr($pattern, $p, $next - $p) | ||
. substr($pattern, $rest + 1); | ||
|
||
$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, $flags); | ||
} | ||
|
||
return array_unique($paths); | ||
} | ||
|
||
/** | ||
* Find the end of the sub-pattern in a brace expression. | ||
* | ||
* @param string $pattern | ||
* @param integer $begin | ||
* @param integer $flags | ||
* @return integer|null | ||
*/ | ||
protected static function nextBraceSub($pattern, $begin, $flags) | ||
{ | ||
$length = strlen($pattern); | ||
$depth = 0; | ||
$current = $begin; | ||
|
||
while ($current < $length) { | ||
if (!$flags & self::GLOB_NOESCAPE && $pattern[$current] === '\\') { | ||
if (++$current === $length) { | ||
break; | ||
} | ||
|
||
$current++; | ||
} else { | ||
if (($pattern[$current] === '}' && $depth-- === 0) || ($pattern[$current] === ',' && $depth === 0)) { | ||
break; | ||
} else if ($pattern[$current++] === '{') { | ||
$depth++; | ||
} | ||
} | ||
} | ||
|
||
return ($current < $length ? $current : null); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<?php | ||
/** | ||
* Zend Framework | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://framework.zend.com/license/new-bsd | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to license@zend.com so we can send you a copy immediately. | ||
* | ||
* @category Zend | ||
* @package Zend_Stdlib | ||
* @subpackage UnitTests | ||
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com) | ||
* @license http://framework.zend.com/license/new-bsd New BSD License | ||
* @version $Id:$ | ||
*/ | ||
|
||
namespace ZendTest\Stdlib; | ||
|
||
use PHPUnit_Framework_TestCase as TestCase, | ||
Zend\Stdlib\Glob; | ||
|
||
class GlobTest extends TestCase | ||
{ | ||
public function testFallback() | ||
{ | ||
if (!defined('GLOB_BRACE')) { | ||
$this->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) | ||
); | ||
} | ||
} |
Empty file.
Empty file.
Empty file.
Empty file.