Skip to content
This repository has been archived by the owner on Jan 8, 2020. It is now read-only.

Commit

Permalink
Glob::glob() should throw an exception on error
Browse files Browse the repository at this point in the history
  • Loading branch information
marc-mabe committed Apr 29, 2013
1 parent 3af0591 commit ef307f3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
22 changes: 17 additions & 5 deletions library/Zend/Stdlib/Glob.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace Zend\Stdlib;

use Zend\Stdlib\Exception;
use Zend\Stdlib\ErrorHandler;

/**
* Wrapper for glob with fallback if GLOB_BRACE is not available.
*/
Expand All @@ -33,9 +36,10 @@ abstract class Glob
* @param string $pattern
* @param integer $flags
* @param bool $forceFallback
* @return array|false
* @return array
* @throws Exception\RuntimeException
*/
public static function glob($pattern, $flags, $forceFallback = false)
public static function glob($pattern, $flags = 0, $forceFallback = false)
{
if (!defined('GLOB_BRACE') || $forceFallback) {
return static::fallbackGlob($pattern, $flags);
Expand All @@ -49,7 +53,8 @@ public static function glob($pattern, $flags, $forceFallback = false)
*
* @param string $pattern
* @param integer $flags
* @return array|false
* @return array
* @throws Exception\RuntimeException
*/
protected static function systemGlob($pattern, $flags)
{
Expand All @@ -75,15 +80,22 @@ protected static function systemGlob($pattern, $flags)
$globFlags = 0;
}

return glob($pattern, $globFlags);
ErrorHandler::start();
$res = glob($pattern, $globFlags);
$err = ErrorHandler::stop();
if ($res === false) {
throw new Exception\RuntimeException("glob('{$pattern}', {$globFlags}) failed", 0, $err);
}
return $res;
}

/**
* Expand braces manually, then use the system glob.
*
* @param string $pattern
* @param integer $flags
* @return array|false
* @return array
* @throws Exception\RuntimeException
*/
protected static function fallbackGlob($pattern, $flags)
{
Expand Down
9 changes: 9 additions & 0 deletions tests/ZendTest/Stdlib/GlobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,13 @@ public function testNonMatchingGlobReturnsArray()
$result = Glob::glob('/some/path/{,*.}{this,orthis}.php', Glob::GLOB_BRACE);
$this->assertInternalType('array', $result);
}

public function testThrowExceptionOnError()
{
$this->setExpectedException('Zend\Stdlib\Exception\RuntimeException');

// run into a max path lengh error
$path = '/' . str_repeat('a', 10000);
Glob::glob($path);
}
}

0 comments on commit ef307f3

Please sign in to comment.