php-vfs/src/FileSystem.php

120 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2014-07-06 16:31:39 +00:00
<?php
/*
* This file is part of VFS
*
* Copyright (c) 2015 Andrew Lawson <http://adlawson.com>
2014-07-06 16:31:39 +00:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Vfs;
use Psr\Log\LoggerInterface;
use Vfs\Exception\RegisteredSchemeException;
use Vfs\Exception\UnregisteredSchemeException;
use Vfs\Node\Factory\NodeFactoryInterface;
use Vfs\Node\Walker\NodeWalkerInterface;
class FileSystem implements FileSystemInterface
{
protected $logger;
protected $nodeFactory;
protected $nodeWalker;
protected $registry;
2014-07-06 16:31:39 +00:00
protected $scheme;
protected $wrapperClass;
2016-02-19 22:49:02 +00:00
protected $root;
2014-07-06 16:31:39 +00:00
/**
2014-09-06 14:15:08 +00:00
* @param string $scheme
* @param string $wrapperClass
* @param NodeFactoryInterface $nodeFactory
* @param NodeWalkerInterface $nodeWalker
2014-09-06 14:15:08 +00:00
* @param RegistryInterface $registry
* @param LoggerInterface $logger
2014-07-06 16:31:39 +00:00
*/
public function __construct(
$scheme,
$wrapperClass,
NodeFactoryInterface $nodeFactory,
NodeWalkerInterface $nodeWalker,
2014-07-06 16:31:39 +00:00
RegistryInterface $registry,
LoggerInterface $logger
) {
$this->wrapperClass = $wrapperClass;
$this->scheme = rtrim($scheme, ':/\\');
$this->nodeWalker = $nodeWalker;
2014-07-06 16:31:39 +00:00
$this->logger = $logger;
$this->nodeFactory = $nodeFactory;
2014-07-06 16:31:39 +00:00
$this->registry = $registry;
$this->root = $nodeFactory->buildDirectory();
2014-07-06 16:31:39 +00:00
}
/**
2014-09-06 14:15:08 +00:00
* @param string $scheme
2014-07-06 16:31:39 +00:00
* @return FileSystem
*/
public static function factory($scheme = self::SCHEME)
{
$builder = new FileSystemBuilder($scheme);
return $builder->build();
2014-07-06 16:31:39 +00:00
}
public function get($path)
{
return $this->nodeWalker->findNode($this->root, $path);
2014-07-06 16:31:39 +00:00
}
public function getLogger()
{
return $this->logger;
}
public function getNodeFactory()
{
return $this->nodeFactory;
2014-07-06 16:31:39 +00:00
}
public function getNodeWalker()
{
return $this->nodeWalker;
2014-07-06 16:31:39 +00:00
}
public function getScheme()
{
return $this->scheme;
}
public function mount()
{
if ($this->registry->has($this->scheme) || in_array($this->scheme, stream_get_wrappers())) {
throw new RegisteredSchemeException($this->scheme);
}
if (stream_wrapper_register($this->scheme, $this->wrapperClass)) {
$this->registry->add($this->scheme, $this);
2014-09-06 14:15:08 +00:00
2014-07-06 16:31:39 +00:00
return true;
}
return false;
}
public function unmount()
{
if (!$this->registry->has($this->scheme) && !in_array($this->scheme, stream_get_wrappers())) {
throw new UnregisteredSchemeException($this->scheme);
}
if (stream_wrapper_unregister($this->scheme)) {
$this->registry->remove($this->scheme, $this);
2014-09-06 14:15:08 +00:00
2014-07-06 16:31:39 +00:00
return true;
}
return false;
}
}