Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement module references inside path files #5

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ function bootstrap($cwd, $home) {
}

// Always add current directory
$result['local'][]= path($cwd);
$result['local'][]= path($cwd, $cwd, $home);
return $result;
}
25 changes: 14 additions & 11 deletions src/scan-path.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
<?php namespace xp;

function path($in, $bail= true) {
function path($path, $base, $home, $bail= true) {
if ('~' === $path[0]) {
$in= $home.DIRECTORY_SEPARATOR.substr($path, 1);
} else if ('/' === $path[0] || '\\' === $path[0] || strlen($path) > 2 && (':' === $path[1] && '\\' === $path[2])) {
$in= $path;
} else {
$in= $base.DIRECTORY_SEPARATOR.$path;
}

$qn= realpath($in);
if (false === $qn) {
if ($bail) {
Expand Down Expand Up @@ -47,19 +55,14 @@ function scanpath(&$result, $paths, $base, $home) {
} else if ('?' === $path[0]) {
$bail= false;
$path= substr($path, 1);
}

// Expand file path
if ('~' === $path[0]) {
$expanded= $home.DIRECTORY_SEPARATOR.substr($path, 1);
} else if ('/' === $path[0] || '\\' === $path[0] || strlen($path) > 2 && (':' === $path[1] && '\\' === $path[2])) {
$expanded= $path;
} else {
$expanded= $base.DIRECTORY_SEPARATOR.$path;
} else if ('@' === $path[0]) {
$resolved= path(substr($path, 1), $base, $home);
scanpath($result, pathfiles($resolved), $resolved, $home);
continue;
}

// Resolve, check for XP core
if ($resolved= path($expanded, $bail)) {
if ($resolved= path($path, $base, $home, $bail)) {
if (null === $result['base']) {
if (0 === substr_compare($resolved, '.xar', -4)) {
if (is_file($f= 'xar://'.$resolved.'?__xp.php')) {
Expand Down
17 changes: 16 additions & 1 deletion test/shared/scan-path-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,5 +118,20 @@ function() { $result= ['base' => null]; \xp\scanpath($result, ['does-not-exist']
'empty use path does not raise warnings' => function() use($path) {
$result= ['base' => null];
\xp\scanpath($result, [''], $this->classpath, $this->home);
},

// Includes
'module reference to home directory' => function() use($path) {
$result= ['base' => null];
file_put_contents($path->compose($this->devel, 'class.pth'), '.');
\xp\scanpath($result, ['@~/devel'], $this->classpath, $this->home);
$this->assertEquals(['base' => null, 'local' => [$this->devel]], $result);
},

'module reference to relative directory' => function() use($path) {
$result= ['base' => null];
file_put_contents($path->compose($this->home, 'class.pth'), '.');
\xp\scanpath($result, ['@home'], $this->classpath, $this->home);
$this->assertEquals(['base' => null, 'local' => [$this->home]], $result);
}
]));
]));