Skip to content
This repository was archived by the owner on Jul 1, 2023. It is now read-only.

Commit 045bcf8

Browse files
committed
Merge pull request #11 from elkangaroo/fix-exclude-relative
Fix "--exclude" argument to work correctly with relative paths
2 parents ef73af3 + c2c83ed commit 045bcf8

File tree

1 file changed

+32
-98
lines changed

1 file changed

+32
-98
lines changed

src/Manager.php

Lines changed: 32 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,9 @@ protected function getFilesFromPaths(array $paths, array $extensions, array $exc
235235
if (is_file($path)) {
236236
$files[] = $path;
237237
} else if (is_dir($path)) {
238-
239238
$iterator = new \RecursiveDirectoryIterator($path);
240239
if (!empty($excluded)) {
241-
$iterator = new ExcludeRecursiveDirectoryIterator($iterator, $excluded);
240+
$iterator = new RecursiveDirectoryFilterIterator($iterator, $excluded);
242241
}
243242
$iterator = new \RecursiveIteratorIterator($iterator);
244243

@@ -266,134 +265,69 @@ public function getNext()
266265
}
267266
}
268267

269-
class ExcludeRecursiveDirectoryIterator implements \RecursiveIterator
268+
class RecursiveDirectoryFilterIterator extends \RecursiveFilterIterator
270269
{
271-
/** @var array */
272-
private $excluded = array();
273-
274270
/** @var \RecursiveDirectoryIterator */
275271
private $iterator;
276272

273+
/** @var array */
274+
private $excluded = array();
275+
277276
/**
278-
* @param array $excluded
279277
* @param \RecursiveDirectoryIterator $iterator
278+
* @param array $excluded
280279
*/
281280
public function __construct(\RecursiveDirectoryIterator $iterator, array $excluded)
282281
{
282+
parent::__construct($iterator);
283283
$this->iterator = $iterator;
284-
$this->excluded = array_map(array($this, 'normalizePath'), $excluded);
285-
}
286-
287-
/**
288-
* (PHP 5 &gt;= 5.0.0)<br/>
289-
* Return the current element
290-
* @link http://php.net/manual/en/iterator.current.php
291-
* @return mixed Can return any type.
292-
*/
293-
public function current()
294-
{
295-
return $this->iterator->current();
296-
}
297-
298-
/**
299-
* (PHP 5 &gt;= 5.0.0)<br/>
300-
* Move forward to next element
301-
* @link http://php.net/manual/en/iterator.next.php
302-
* @return void Any returned value is ignored.
303-
*/
304-
public function next()
305-
{
306-
$this->iterator->next();
307-
}
308-
309-
/**
310-
* (PHP 5 &gt;= 5.0.0)<br/>
311-
* Return the key of the current element
312-
* @link http://php.net/manual/en/iterator.key.php
313-
* @return mixed scalar on success, or null on failure.
314-
*/
315-
public function key()
316-
{
317-
return $this->iterator->key();
284+
$this->excluded = array_map(array($this, 'getPathname'), $excluded);
318285
}
319286

320287
/**
321-
* (PHP 5 &gt;= 5.0.0)<br/>
322-
* Checks if current position is valid
323-
* @link http://php.net/manual/en/iterator.valid.php
324-
* @return boolean The return value will be casted to boolean and then evaluated.
325-
* Returns true on success or false on failure.
326-
*/
327-
public function valid()
328-
{
329-
return $this->iterator->valid();
330-
}
331-
332-
/**
333-
* (PHP 5 &gt;= 5.0.0)<br/>
334-
* Rewind the Iterator to the first element
335-
* @link http://php.net/manual/en/iterator.rewind.php
336-
* @return void Any returned value is ignored.
288+
* (PHP 5 &gt;= 5.1.0)<br/>
289+
* Check whether the current element of the iterator is acceptable
290+
*
291+
* @link http://php.net/manual/en/filteriterator.accept.php
292+
* @return bool true if the current element is acceptable, otherwise false.
337293
*/
338-
public function rewind()
294+
public function accept()
339295
{
340-
$this->iterator->rewind();
296+
return !in_array($this->current()->getPathname(), $this->excluded);
341297
}
342298

343299
/**
344300
* (PHP 5 &gt;= 5.1.0)<br/>
345-
* Returns if an iterator can be created for the current entry.
346-
* @link http://php.net/manual/en/recursiveiterator.haschildren.php
347-
* @return bool true if the current entry can be iterated over, otherwise returns false.
301+
* Check whether the inner iterator's current element has children
302+
*
303+
* @link http://php.net/manual/en/recursivefilteriterator.haschildren.php
304+
* @return bool true if the inner iterator has children, otherwise false
348305
*/
349-
public function hasChildren()
350-
{
351-
$path = $this->normalizePath($this->iterator->getPathname());
352-
foreach ($this->excluded as $exc) {
353-
if (strpos($path, $exc) === 0) {
354-
return false;
355-
}
356-
}
357-
306+
public function hasChildren() {
358307
return $this->iterator->hasChildren();
359308
}
360309

361310
/**
362311
* (PHP 5 &gt;= 5.1.0)<br/>
363-
* Returns an iterator for the current entry.
364-
* @link http://php.net/manual/en/recursiveiterator.getchildren.php
365-
* @return \RecursiveIterator An iterator for the current entry.
312+
* Return the inner iterator's children contained in a RecursiveFilterIterator
313+
*
314+
* @link http://php.net/manual/en/recursivefilteriterator.getchildren.php
315+
* @return \RecursiveFilterIterator containing the inner iterator's children.
366316
*/
367-
public function getChildren()
368-
{
369-
return new self($this->iterator->getChildren(), $this->excluded);
317+
public function getChildren() {
318+
return new self($this->iterator->getChildren(), array());
370319
}
371320

372-
373321
/**
374-
* Source: http://stackoverflow.com/questions/4774116/c-realpath-without-resolving-symlinks
375-
* @param string $path
322+
* @param string $excluded
376323
* @return string
377324
*/
378-
private function normalizePath($path)
379-
{
380-
if (!isset($path[0]) || $path[0] !== DIRECTORY_SEPARATOR) {
381-
$result = explode(DIRECTORY_SEPARATOR, getcwd());
382-
} else {
383-
$result = array('');
384-
}
385-
386-
$parts = explode(DIRECTORY_SEPARATOR, $path);
387-
foreach($parts as $part) {
388-
if ($part === '' || $part === '.') {
389-
continue;
390-
} if ($part === '..') {
391-
array_pop($result);
392-
} else {
393-
$result[] = $part;
394-
}
325+
private function getPathname($excluded) {
326+
if (DIRECTORY_SEPARATOR !== $excluded[0]) {
327+
$excluded = $this->iterator->getPath() . DIRECTORY_SEPARATOR . $excluded;
395328
}
396329

397-
return implode(DIRECTORY_SEPARATOR, $result);
330+
$directoryFile = new \SplFileInfo($excluded);
331+
return $directoryFile->getPathname();
398332
}
399333
}

0 commit comments

Comments
 (0)