-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use common base directory of given directories as basedir in phar mode
This fixes #63
- Loading branch information
Showing
4 changed files
with
84 additions
and
1 deletion.
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
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 | ||
namespace TheSeer\Autoload { | ||
|
||
class PathComparator { | ||
|
||
/** | ||
* @var string[] | ||
*/ | ||
private $directories = array(); | ||
|
||
/** | ||
* PathComparator constructor. | ||
* | ||
* @param array $directories | ||
*/ | ||
public function __construct(array $directories) { | ||
foreach($directories as $dir) { | ||
$this->directories[] = realpath($dir); | ||
} | ||
} | ||
|
||
public function getCommondBase() { | ||
if (count($this->directories) == 0) { | ||
return '/'; | ||
} | ||
$result = $this->directories[0]; | ||
foreach($this->directories as $dir) { | ||
$result = substr($dir, 0, $this->commonPrefix($result, $dir)); | ||
} | ||
return rtrim($result,'/'); | ||
} | ||
|
||
|
||
private function commonPrefix( $s1, $s2, $i=0 ) { | ||
return ( | ||
!empty($s1[$i]) && !empty($s2[$i]) && $s1[$i] == $s2[$i] | ||
) ? $this->commonPrefix( $s1, $s2, ++$i ) : $i; | ||
} | ||
} | ||
|
||
} |
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
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,35 @@ | ||
<?php | ||
|
||
namespace TheSeer\Autoload\Tests { | ||
|
||
use TheSeer\Autoload\PathComparator; | ||
|
||
class PathComparatorTest extends \PHPUnit_Framework_TestCase { | ||
|
||
/** | ||
* @dataProvider directoriesProvider | ||
*/ | ||
public function testComparatorYieldsCorrectCommonBase(array $directories, $common) { | ||
$comparator = new PathComparator($directories); | ||
$this->assertEquals($common, $comparator->getCommondBase()); | ||
} | ||
|
||
public function directoriesProvider() { | ||
return [ | ||
'empty' => [ | ||
[], '/' | ||
], | ||
'single' => [ | ||
array(__DIR__), __DIR__ | ||
], | ||
'two' => [ | ||
array(__DIR__, dirname(__DIR__)), dirname(__DIR__) | ||
], | ||
'partns' => [ | ||
array(__DIR__ . '/../src', __DIR__ . '/../vendor/theseer'), dirname(__DIR__) | ||
] | ||
]; | ||
} | ||
} | ||
|
||
} |