DummyFilesystem.php in X Autoload 7.3
File
tests/Drupal/xautoload/Tests/DummyFilesystem.php
View source
<?php
namespace Drupal\xautoload\Tests;
class DummyFilesystem {
protected $knownPaths = array();
const NOTHING = FALSE;
const DIR = '(dir)';
const FILE = '(file)';
function addClass($file, $class) {
$this
->addKnownFile($file);
if (self::FILE !== ($existing = $this->knownPaths[$file])) {
throw new \Exception("A class '{$existing}' already exists at '{$file}'. Cannot overwrite with class '{$class}'.");
}
$this->knownPaths[$file] = $class;
}
function addKnownFiles($files) {
foreach ($files as $file) {
$this
->addKnownFile($file);
}
}
function addKnownFile($file) {
if (!isset($this->knownPaths[$file])) {
$this->knownPaths[$file] = self::FILE;
$this
->addKnownDir(dirname($file));
}
elseif (self::DIR === $this->knownPaths[$file]) {
throw new \Exception("A directory already exists at '{$file}', cannot overwrite with a file.");
}
}
function addKnownDir($dir) {
if (FALSE === strpos($dir, '://')) {
return;
}
if (!isset($this->knownPaths[$dir])) {
$this
->addKnownDir(dirname($dir));
}
$this->knownPaths[$dir] = self::DIR;
}
function resolvePath($path) {
if (isset($this->knownPaths[$path])) {
return $this->knownPaths[$path];
}
else {
return self::NOTHING;
}
}
function getStat($path) {
if (!isset($this->knownPaths[$path])) {
return FALSE;
}
elseif (self::DIR === $this->knownPaths[$path]) {
return stat(__DIR__);
}
else {
$contents = $this
->getFileContents($path);
$resource = tmpfile();
fwrite($resource, $contents);
$stat = fstat($resource);
fclose($resource);
return $stat;
}
}
function getFileContents($path) {
if (!isset($this->knownPaths[$path])) {
throw new \Exception("Assumed file '{$path}' does not exist.");
}
elseif (self::DIR === $this->knownPaths[$path]) {
throw new \Exception("Assumed file '{$path}' is a directory.");
}
if (self::FILE === $this->knownPaths[$path]) {
return '<?php';
}
$class = $this->knownPaths[$path];
if (FALSE === ($pos = strrpos($class, '\\'))) {
return <<<EOT
<?php
class {<span class="php-variable">$class</span>} {}
EOT;
}
$namespace = substr($class, 0, $pos);
$classname = substr($class, $pos + 1);
return <<<EOT
<?php
namespace {<span class="php-variable">$namespace</span>};
class {<span class="php-variable">$classname</span>} {}
EOT;
}
}