Skip to content

Commit

Permalink
Path::getExtension() ignore URL query #1110
Browse files Browse the repository at this point in the history
  • Loading branch information
asika32764 committed May 9, 2024
1 parent efdf46a commit eeea757
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/FileObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@

use function Windwalker\Promise\async;

use const Windwalker\Stream\READ_WRITE_FROM_BEGIN;
use const Windwalker\Stream\READ_ONLY_FROM_BEGIN;
use const Windwalker\Stream\READ_WRITE_FROM_BEGIN;
use const Windwalker\Stream\WRITE_ONLY_RESET;

/**
Expand Down Expand Up @@ -126,6 +126,16 @@ public function __construct($filename, ?string $root = null)
$this->root = $root;
}

public function getFilename(): string
{
return Path::getFilename($this->getPathname(), true);
}

public function getExtension(): string
{
return Path::getExtension($this->getPathname(), true);
}

/**
* getRelativePathFrom
*
Expand Down
35 changes: 27 additions & 8 deletions src/Path.php
Original file line number Diff line number Diff line change
Expand Up @@ -320,37 +320,51 @@ public static function stripExtension(string $file): string
}

/**
* getExtension
*
* @param string $file The file path to get extension.
*
* @return string The ext of file path.
*
* @since 2.0
*/
public static function getExtension(string $file): string
public static function getExtension(string $file, bool $stripQuerySuffix = true): string
{
return pathinfo($file, PATHINFO_EXTENSION);
return pathinfo(static::getFilename($file, $stripQuerySuffix), PATHINFO_EXTENSION);
}

/**
* Get UTF-8 file name from a path.
*
* @param string $path The file path to get basename.
* @param string $path The file path to get basename.
* @param bool $stripQuerySuffix Strip the query suffix after `?` sign.
*
* @return string The file name.
*
* @since 2.0
*/
public static function getFilename(string $path): string
public static function getFilename(string $path, bool $stripQuerySuffix = true): string
{
$paths = explode(DIRECTORY_SEPARATOR, static::clean($path));

if ($paths === []) {
return '';
}

return array_pop($paths);
$filename = array_pop($paths);

if ($stripQuerySuffix) {
$filename = static::stripQuerySuffix($filename);
}

return $filename;
}

public static function stripQuerySuffix(string $path): string
{
if (str_contains($path, '?')) {
[$path] = explode('?', $path);
}

return $path;
}

/**
Expand All @@ -359,12 +373,13 @@ public static function getFilename(string $path): string
* @see https://www.php.net/manual/en/function.basename.php#121405
*
* @param string $path
* @param bool $noExtension
*
* @return string
*
* @since 3.5.17
*/
public static function basename(string $path, bool $noExtension = false): string
public static function basename(string $path, bool $noExtension = false, bool $stripQuerySuffix = true): string
{
$name = '';

Expand All @@ -374,6 +389,10 @@ public static function basename(string $path, bool $noExtension = false): string
$name = $matches[1];
}

if ($stripQuerySuffix) {
$name = static::stripQuerySuffix($name);
}

if ($noExtension) {
$name = static::stripExtension($name);
}
Expand Down

0 comments on commit eeea757

Please sign in to comment.