Skip to content

Commit

Permalink
glob method added
Browse files Browse the repository at this point in the history
  • Loading branch information
SmetDenis committed Mar 5, 2016
1 parent 0d12310 commit 74b75cc
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
40 changes: 26 additions & 14 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,18 @@ public function get($source)
return $this->_find($paths, $path);
}

/**
* Get absolute path to a file or a directory.
*
* @param $source (example: "default:file.txt")
* @return null|string
*/
public function glob($source)
{
list(, $paths, $path) = $this->_parse($source);
return $this->_find($paths, $path, true);
}

/**
* Get all absolute path to a file or a directory.
*
Expand Down Expand Up @@ -356,18 +368,26 @@ protected function _checkRoot()
*
* @param string|array $paths
* @param string $file
* @return null|string
* @param bool $isGlob
* @return null|string|array
*/
protected function _find($paths, $file)
protected function _find($paths, $file, $isGlob = false)
{
$paths = (array)$paths;
$file = ltrim($file, "\\/");

foreach ($paths as $path) {

$fullPath = $this->clean($path . '/' . $file);
if (file_exists($fullPath) || is_dir($fullPath)) {

if ($isGlob) {
$paths = glob($fullPath) ?: array();
return $paths;

} elseif (file_exists($fullPath) || is_dir($fullPath)) {
return $fullPath;
}

}

return null;
Expand Down Expand Up @@ -450,20 +470,12 @@ protected function _isDeleted($alias, $keys)
* Parse source string.
*
* @param string $source (example: "default:file.txt")
* @param string $alias
* @return array
*/
protected function _parse($source, $alias = '')
protected function _parse($source)
{
$path = null;
$parts = explode(':', $source, 2);
$count = count($parts);

if ($count == 1) {
list($path) = $parts;
} elseif ($count == 2) {
list($alias, $path) = $parts;
}
$path = null;
list($alias, $path) = explode(':', $source, 2);

$path = ltrim($path, "\\/");
$paths = isset($this->_paths[$alias]) ? $this->_paths[$alias] : array();
Expand Down
41 changes: 28 additions & 13 deletions tests/PathTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,26 @@
*/
class PathTest extends PHPUnit
{

protected $_root;

protected $_paths = array();

/**
* @param $paths
* @return array
*/
protected function _clearPaths($paths)
{
$return = array();
$paths = (array)$paths;

foreach ($paths as $key => $path) {
$return[$key] = FS::clean($path, '/');
}

return $return;
}

public function setUp()
{
$this->_root = FS::clean(__DIR__ . '/test', '/');
Expand Down Expand Up @@ -584,7 +600,7 @@ public function testHasCDBack()
isSame($expected, $path->getPaths('default:'));
}

public function testAddDeprecated()
public function testDeprecated_add()
{
$path = Path::getInstance(__METHOD__);

Expand Down Expand Up @@ -615,19 +631,18 @@ public function testAddDeprecated()
isSame($this->_clearPaths($expected), $defaultPaths);
}

/**
* @param $paths
* @return array
*/
protected function _clearPaths($paths)
public function testGlob()
{
$return = array();
$paths = (array)$paths;
$path = new Path();

foreach ($paths as $key => $path) {
$return[$key] = FS::clean($path, '/');
}
$path->set('root', __DIR__ . '/..');
$path->set('src', 'root:src');

return $return;
$paths = $path->glob('root:src/*.php');

isSame($this->_clearPaths(array(
PROJECT_ROOT . '/src/Exception.php',
PROJECT_ROOT . '/src/Path.php',
)), $paths);
}
}

0 comments on commit 74b75cc

Please sign in to comment.