Skip to content

Commit

Permalink
Merge pull request #4 from luenemam/simplify-locateRoot-withSplFileInfo
Browse files Browse the repository at this point in the history
Use SplFileInfo to simplify DrupalFinder::locateRoot
  • Loading branch information
webflo committed Nov 15, 2016
2 parents 4dcd328 + 7ef709c commit 4272462
Showing 1 changed file with 14 additions and 18 deletions.
32 changes: 14 additions & 18 deletions src/DrupalFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
*/

namespace DrupalFinder;
use SplFileInfo;

class DrupalFinder {

Expand All @@ -24,29 +25,22 @@ class DrupalFinder {
private $composerRoot;

public function locateRoot($start_path) {
$start_path = new SplFileInfo($start_path);
$this->drupalRoot = FALSE;
$this->composerRoot = FALSE;

foreach (array(TRUE, FALSE) as $follow_symlinks) {
$path = $start_path;
if ($follow_symlinks && is_link($path)) {
$path = realpath($path);
}
// Check the start path.
if ($checked_path = $this->isValidRoot($path)) {
return $checked_path;
}
else {
// Move up dir by dir and check each.
while ($path = $this->shiftPathUp($path)) {
if ($follow_symlinks && is_link($path)) {
$path = realpath($path);
}
if ($checked_path = $this->isValidRoot($path)) {
return $checked_path;
}
for ($path = $start_path;
$path->getFilename() != '.';
$path = $path->getPathInfo()) {
if ($follow_symlinks && $path->isLink()) {
$path = $path->getRealPath();
}
}
// Check the start path.
if ($checked_path = $this->isValidRoot($path->getPathname())) {
return $checked_path;
}
};
}

return FALSE;
Expand All @@ -55,6 +49,8 @@ public function locateRoot($start_path) {
/**
* Returns parent directory.
*
* @deprecated does not work with streamWrappers on Windows
*
* @param string
* Path to start from.
*
Expand Down

0 comments on commit 4272462

Please sign in to comment.