Skip to content

Commit

Permalink
Use common base directory of given directories as basedir in phar mode
Browse files Browse the repository at this point in the history
This fixes #63
  • Loading branch information
theseer committed Oct 3, 2015
1 parent 0d787e2 commit 63813ec
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 1 deletion.
4 changes: 4 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,10 @@ public function getBaseDirectory() {
if ($this->baseDirectory !== NULL) {
return realpath($this->baseDirectory);
}
if ($this->isPharMode()) {
$comparator = new PathComparator($this->directories);
return $comparator->getCommondBase();
}
if ($this->outputFile != 'STDOUT') {
return realpath(dirname($this->outputFile) ?: '.');
}
Expand Down
41 changes: 41 additions & 0 deletions src/PathComparator.php
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;
}
}

}
5 changes: 4 additions & 1 deletion src/autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ function($class) {
'theseer\\autoload\\parseresult' => '/ParseResult.php',
'theseer\\autoload\\parserexception' => '/Parser.php',
'theseer\\autoload\\parserinterface' => '/ParserInterface.php',
'theseer\\autoload\\pathcomparator' => '/PathComparator.php',
'theseer\\autoload\\pharbuilder' => '/PharBuilder.php',
'theseer\\autoload\\sourcefile' => '/SourceFile.php',
'theseer\\autoload\\staticrenderer' => '/StaticRenderer.php',
Expand All @@ -142,6 +143,8 @@ function($class) {
if (isset($classes[$cn])) {
require __DIR__ . $classes[$cn];
}
}
},
true,
false
);
// @codeCoverageIgnoreEnd
35 changes: 35 additions & 0 deletions tests/PathComparator.test.php
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__)
]
];
}
}

}

0 comments on commit 63813ec

Please sign in to comment.