PharInvocationCollection.php in Drupal 7
File
misc/typo3/phar-stream-wrapper/src/Resolver/PharInvocationCollection.php
View source
<?php
namespace TYPO3\PharStreamWrapper\Resolver;
use TYPO3\PharStreamWrapper\Collectable;
class PharInvocationCollection implements Collectable {
const UNIQUE_INVOCATION = 1;
const UNIQUE_BASE_NAME = 2;
const DUPLICATE_ALIAS_WARNING = 32;
private $invocations = array();
public function has(PharInvocation $invocation) {
return in_array($invocation, $this->invocations, true);
}
public function collect(PharInvocation $invocation, $flags = null) {
if ($flags === null) {
$flags = static::UNIQUE_INVOCATION | static::DUPLICATE_ALIAS_WARNING;
}
if ($invocation
->getBaseName() === '' || $invocation
->getAlias() === '' || !$this
->assertUniqueBaseName($invocation, $flags) || !$this
->assertUniqueInvocation($invocation, $flags)) {
return false;
}
if ($flags & static::DUPLICATE_ALIAS_WARNING) {
$this
->triggerDuplicateAliasWarning($invocation);
}
$this->invocations[] = $invocation;
return true;
}
public function findByCallback($callback, $reverse = false) {
foreach ($this
->getInvocations($reverse) as $invocation) {
if (call_user_func($callback, $invocation) === true) {
return $invocation;
}
}
return null;
}
private function assertUniqueBaseName(PharInvocation $invocation, $flags) {
if (!($flags & static::UNIQUE_BASE_NAME)) {
return true;
}
return $this
->findByCallback(function (PharInvocation $candidate) use ($invocation) {
return $candidate
->getBaseName() === $invocation
->getBaseName();
}) === null;
}
private function assertUniqueInvocation(PharInvocation $invocation, $flags) {
if (!($flags & static::UNIQUE_INVOCATION)) {
return true;
}
return $this
->findByCallback(function (PharInvocation $candidate) use ($invocation) {
return $candidate
->equals($invocation);
}) === null;
}
private function triggerDuplicateAliasWarning(PharInvocation $invocation) {
$sameAliasInvocation = $this
->findByCallback(function (PharInvocation $candidate) use ($invocation) {
return $candidate
->isConfirmed() === $invocation
->isConfirmed() && $candidate
->getAlias() === $invocation
->getAlias();
}, true);
if ($sameAliasInvocation === null) {
return;
}
trigger_error(sprintf('Alias %s cannot be used by %s, already used by %s', $invocation
->getAlias(), $invocation
->getBaseName(), $sameAliasInvocation
->getBaseName()), E_USER_WARNING);
}
private function getInvocations($reverse = false) {
if ($reverse) {
return array_reverse($this->invocations);
}
return $this->invocations;
}
}