Path
This commit is contained in:
@ -3,6 +3,7 @@ namespace ProVM\Alias;
|
|||||||
|
|
||||||
use function Safe\{touch,mkdir,unlink};
|
use function Safe\{touch,mkdir,unlink};
|
||||||
use ProVM\Concept\Filesystem as FilesystemInterface;
|
use ProVM\Concept\Filesystem as FilesystemInterface;
|
||||||
|
use ProVM\Implement\Path;
|
||||||
|
|
||||||
abstract class Filesystem implements FilesystemInterface
|
abstract class Filesystem implements FilesystemInterface
|
||||||
{
|
{
|
||||||
@ -19,7 +20,7 @@ abstract class Filesystem implements FilesystemInterface
|
|||||||
|
|
||||||
public function buildPath(string $relative_path): string
|
public function buildPath(string $relative_path): string
|
||||||
{
|
{
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
return Path::fromArray([
|
||||||
$this->getBasePath(),
|
$this->getBasePath(),
|
||||||
$relative_path
|
$relative_path
|
||||||
]);
|
]);
|
||||||
|
15
api/ProVM/Concept/Path.php
Normal file
15
api/ProVM/Concept/Path.php
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Concept;
|
||||||
|
|
||||||
|
use ArrayAccess;
|
||||||
|
|
||||||
|
interface Path extends ArrayAccess
|
||||||
|
{
|
||||||
|
public function compose(string $base_path): Path;
|
||||||
|
public function add(string $path_segment): Path;
|
||||||
|
public function build(): string;
|
||||||
|
public static function fromArray(array $path): string;
|
||||||
|
|
||||||
|
public static function isAbsolute(string $path): bool;
|
||||||
|
public static function isRelative(string $path): bool;
|
||||||
|
}
|
@ -2,6 +2,7 @@
|
|||||||
namespace ProVM\Implement;
|
namespace ProVM\Implement;
|
||||||
|
|
||||||
use Psr\Collection\CollectionInterface;
|
use Psr\Collection\CollectionInterface;
|
||||||
|
use ReflectionFunction;
|
||||||
|
|
||||||
class Collection implements CollectionInterface
|
class Collection implements CollectionInterface
|
||||||
{
|
{
|
||||||
@ -41,7 +42,7 @@ class Collection implements CollectionInterface
|
|||||||
}
|
}
|
||||||
protected function processCallable(Callable $value)
|
protected function processCallable(Callable $value)
|
||||||
{
|
{
|
||||||
$ref = new \ReflectionFunction($value);
|
$ref = new ReflectionFunction($value);
|
||||||
$param_array = [];
|
$param_array = [];
|
||||||
$params = $ref->getParameters();
|
$params = $ref->getParameters();
|
||||||
foreach ($params as $p) {
|
foreach ($params as $p) {
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement;
|
namespace ProVM\Implement;
|
||||||
|
|
||||||
use Common\Alias\File as BaseFile;
|
use ProVM\Alias\File as BaseFile;
|
||||||
|
|
||||||
class File extends BaseFile
|
class File extends BaseFile
|
||||||
{
|
{
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace ProVM\Implement;
|
namespace ProVM\Implement;
|
||||||
|
|
||||||
use Common\Concept\File as FileInterface;
|
use ProVM\Concept\File as FileInterface;
|
||||||
use Common\Alias\Filesystem as BaseFilesystem;
|
use ProVM\Alias\Filesystem as BaseFilesystem;
|
||||||
|
|
||||||
class Filesystem extends BaseFilesystem
|
class Filesystem extends BaseFilesystem
|
||||||
{
|
{
|
||||||
|
58
api/ProVM/Implement/Path.php
Normal file
58
api/ProVM/Implement/Path.php
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
<?php
|
||||||
|
namespace ProVM\Implement;
|
||||||
|
|
||||||
|
use ProVM\Concept\Path as PathInterface;
|
||||||
|
|
||||||
|
class Path implements PathInterface
|
||||||
|
{
|
||||||
|
protected array $path;
|
||||||
|
public function compose(string $base_path): PathInterface
|
||||||
|
{
|
||||||
|
$this->path = [];
|
||||||
|
return $this->add($base_path);
|
||||||
|
}
|
||||||
|
public function add(string $path_segment): PathInterface
|
||||||
|
{
|
||||||
|
$this->path []= $path_segment;
|
||||||
|
return $this;
|
||||||
|
}
|
||||||
|
public function build(): string
|
||||||
|
{
|
||||||
|
return implode(DIRECTORY_SEPARATOR, $this->path);
|
||||||
|
}
|
||||||
|
public static function fromArray(array $path): string
|
||||||
|
{
|
||||||
|
$output = new Path();
|
||||||
|
$output->compose(array_shift($path));
|
||||||
|
foreach ($path as $p) {
|
||||||
|
$output->add($p);
|
||||||
|
}
|
||||||
|
return $output->build();
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function isAbsolute(string $path): bool
|
||||||
|
{
|
||||||
|
return !file_exists(Path::fromArray([$path, '..']));
|
||||||
|
}
|
||||||
|
public static function isRelative(string $path): bool
|
||||||
|
{
|
||||||
|
return !Path::isAbsolute($path);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function offsetExists(mixed $offset): bool
|
||||||
|
{
|
||||||
|
return isset($this->path[$offset]);
|
||||||
|
}
|
||||||
|
public function offsetGet(mixed $offset): mixed
|
||||||
|
{
|
||||||
|
return $this->path[$offset];
|
||||||
|
}
|
||||||
|
public function offsetSet(mixed $offset, mixed $value): void
|
||||||
|
{
|
||||||
|
$this->path[$offset] = $value;
|
||||||
|
}
|
||||||
|
public function offsetUnset(mixed $offset): void
|
||||||
|
{
|
||||||
|
unset($this->path[$offset]);
|
||||||
|
}
|
||||||
|
}
|
@ -6,25 +6,25 @@ return [
|
|||||||
return \ProVM\Implement\Collection::fromArray([
|
return \ProVM\Implement\Collection::fromArray([
|
||||||
'base' => dirname(__DIR__, 2),
|
'base' => dirname(__DIR__, 2),
|
||||||
'resources' => function(\Psr\Collection\CollectionInterface $collection) {
|
'resources' => function(\Psr\Collection\CollectionInterface $collection) {
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
return \ProVM\Implement\Path::fromArray([
|
||||||
$collection['base'],
|
$collection['base'],
|
||||||
'resources'
|
'resources'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
'documentation' => function(\Psr\Collection\CollectionInterface $collection) {
|
'documentation' => function(\Psr\Collection\CollectionInterface $collection) {
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
return \ProVM\Implement\Path::fromArray([
|
||||||
$collection['resources'],
|
$collection['resources'],
|
||||||
'documentation'
|
'documentation'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
'routes' => function(\Psr\Collection\CollectionInterface $collection) {
|
'routes' => function(\Psr\Collection\CollectionInterface $collection) {
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
return \ProVM\Implement\Path::fromArray([
|
||||||
$collection['resources'],
|
$collection['resources'],
|
||||||
'routes'
|
'routes'
|
||||||
]);
|
]);
|
||||||
},
|
},
|
||||||
'public' => function(\Psr\Collection\CollectionInterface $collection) {
|
'public' => function(\Psr\Collection\CollectionInterface $collection) {
|
||||||
return implode(DIRECTORY_SEPARATOR, [
|
return \ProVM\Implement\Path::fromArray([
|
||||||
$collection['base'],
|
$collection['base'],
|
||||||
'public'
|
'public'
|
||||||
]);
|
]);
|
||||||
|
Reference in New Issue
Block a user