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

Move alias learning so only aliases of valid phar files are learnt #16

Merged
merged 2 commits into from
Feb 27, 2019
Merged
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
10 changes: 10 additions & 0 deletions src/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,4 +103,14 @@ public function resolveBaseName(string $path, int $flags = null)
{
return $this->resolver->resolveBaseName($path, $flags);
}

/**
* @param string $path
* @param null|int $flags
* @return bool
*/
public function learnAlias(string $path, int $flags = null)
{
return $this->resolver->learnAlias($path);
}
}
1 change: 1 addition & 0 deletions src/PharStreamWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,7 @@ public function url_stat(string $path, int $flags)
protected function assert(string $path, string $command)
{
if ($this->resolveAssertable()->assert($path, $command) === true) {
Manager::instance()->learnAlias($path);
return;
}

Expand Down
7 changes: 7 additions & 0 deletions src/Resolvable.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,11 @@ interface Resolvable
* @return null|string
*/
public function resolveBaseName(string $path, int $flags = null);

/**
* @param string $path
* @param int|null $flags
* @return bool
*/
public function learnAlias(string $path, int $flags = null);
}
29 changes: 20 additions & 9 deletions src/Resolver/BaseNameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@ public function resolveBaseName(string $path, int $flags = null)
if ($baseName !== null && $flags & static::RESOLVE_REALPATH) {
$baseName = realpath($baseName);
}
if ($baseName !== null && $hasPharPrefix && $flags & static::RESOLVE_ALIAS) {
$this->learnAlias($baseName);
}

return $baseName;
}
Expand All @@ -65,16 +62,30 @@ private function resolveBaseNameFromAliasMap(string $path)
return $this->getAlias($possibleAlias ?: '');
}


/**
* @param string $basePath
* @param string $path
* @param int|null $flags
* @return bool
*/
private function learnAlias(string $basePath)
public function learnAlias(string $path, int $flags = null): bool
{
$alias = (new Reader($basePath))->resolveContainer()->getAlias();
if ($alias !== '' && $alias !== $basePath) {
$this->setAlias($alias, $basePath);
$flags = $flags ?? static::RESOLVE_REALPATH;
$baseName = Helper::determineBaseFile($path);
if ($baseName === null) {
return false;
}

if ($flags & static::RESOLVE_REALPATH) {
$baseName = realpath($baseName);
}

$alias = (new Reader($baseName))->resolveContainer()->getAlias();
if ($alias === '' || $alias === $baseName) {
return false;
}

$this->setAlias($alias, $baseName);
return true;
}

/**
Expand Down