From d99e1f519efc8d57915c55eaaca4c90a9db98126 Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Mon, 7 Sep 2020 13:09:07 +0300 Subject: [PATCH] Added Filesystem::basename() method --- .../src/Grav/Framework/Filesystem/Filesystem.php | 14 ++++++++++++-- .../Interfaces/FilesystemInterface.php | 16 +++++++++++----- 2 files changed, 23 insertions(+), 7 deletions(-) diff --git a/system/src/Grav/Framework/Filesystem/Filesystem.php b/system/src/Grav/Framework/Filesystem/Filesystem.php index 886fa67a8..66f528056 100644 --- a/system/src/Grav/Framework/Filesystem/Filesystem.php +++ b/system/src/Grav/Framework/Filesystem/Filesystem.php @@ -52,6 +52,7 @@ class Filesystem implements FilesystemInterface * Always use Filesystem::getInstance() instead. * * @param bool|null $normalize + * @internal */ protected function __construct(bool $normalize = null) { @@ -127,6 +128,15 @@ class Filesystem implements FilesystemInterface return $this->toString($scheme, $path); } + /** + * {@inheritdoc} + * @see FilesystemInterface::basename() + */ + public function basename(string $path, ?string $suffix = null): string + { + return basename($path, $suffix); + } + /** * {@inheritdoc} * @see FilesystemInterface::dirname() @@ -162,7 +172,7 @@ class Filesystem implements FilesystemInterface * {@inheritdoc} * @see FilesystemInterface::pathinfo() */ - public function pathinfo(string $path, int $options = null) + public function pathinfo(string $path, ?int $options = null) { [$scheme, $path] = $this->getSchemeAndHierarchy($path); @@ -201,7 +211,7 @@ class Filesystem implements FilesystemInterface * @param int|null $options * @return array|string */ - protected function pathinfoInternal(?string $scheme, string $path, int $options = null) + protected function pathinfoInternal(?string $scheme, string $path, ?int $options = null) { if ($options) { return \pathinfo($path, $options); diff --git a/system/src/Grav/Framework/Filesystem/Interfaces/FilesystemInterface.php b/system/src/Grav/Framework/Filesystem/Interfaces/FilesystemInterface.php index 138a90d7f..dab8c53fd 100644 --- a/system/src/Grav/Framework/Filesystem/Interfaces/FilesystemInterface.php +++ b/system/src/Grav/Framework/Filesystem/Interfaces/FilesystemInterface.php @@ -28,7 +28,6 @@ interface FilesystemInterface * * @param string $path A filename or path, does not need to exist as a file. * @param int $levels The number of parent directories to go up (>= 1). - * * @return string Returns parent path. * @throws \RuntimeException * @api @@ -39,13 +38,22 @@ interface FilesystemInterface * Normalize path by cleaning up `\`, `/./`, `//` and `/../`. * * @param string $path A filename or path, does not need to exist as a file. - * * @return string Returns normalized path. * @throws \RuntimeException * @api */ public function normalize(string $path): string; + /** + * Returns filename component of path. + * + * @param string $path A filename or path, does not need to exist as a file. + * @param string|null $suffix If the filename ends in suffix this will also be cut off. + * @return string + * @api + */ + public function basename(string $path, ?string $suffix = null): string; + /** * Stream-safe `\dirname()` replacement. * @@ -53,7 +61,6 @@ interface FilesystemInterface * * @param string $path A filename or path, does not need to exist as a file. * @param int $levels The number of parent directories to go up (>= 1). - * * @return string Returns path to the directory. * @throws \RuntimeException * @api @@ -67,9 +74,8 @@ interface FilesystemInterface * * @param string $path A filename or path, does not need to exist as a file. * @param int|null $options A PATHINFO_* constant. - * * @return array|string * @api */ - public function pathinfo(string $path, int $options = null); + public function pathinfo(string $path, ?int $options = null); }