Upgrade to 4.0.0
This commit is contained in:
@@ -27,18 +27,19 @@ class Asset
|
||||
/**
|
||||
* Relative file path
|
||||
*/
|
||||
protected string|null $path = null;
|
||||
protected string|null $path;
|
||||
|
||||
|
||||
/**
|
||||
* Creates a new Asset object for the given path.
|
||||
*/
|
||||
public function __construct(string $path)
|
||||
{
|
||||
$this->setProperties([
|
||||
'path' => dirname($path),
|
||||
'root' => $this->kirby()->root('index') . '/' . $path,
|
||||
'url' => $this->kirby()->url('base') . '/' . $path
|
||||
]);
|
||||
$this->root = $this->kirby()->root('index') . '/' . $path;
|
||||
$this->url = $this->kirby()->url('base') . '/' . $path;
|
||||
|
||||
$path = dirname($path);
|
||||
$this->path = $path === '.' ? '' : $path;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -46,7 +47,7 @@ class Asset
|
||||
*
|
||||
* @throws \Kirby\Exception\BadMethodCallException
|
||||
*/
|
||||
public function __call(string $method, array $arguments = [])
|
||||
public function __call(string $method, array $arguments = []): mixed
|
||||
{
|
||||
// public property access
|
||||
if (isset($this->$method) === true) {
|
||||
@@ -114,15 +115,4 @@ class Asset
|
||||
{
|
||||
return $this->path;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the path
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setPath(string $path): static
|
||||
{
|
||||
$this->path = $path === '.' ? '' : $path;
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
@@ -426,8 +426,10 @@ class Dir
|
||||
* subfolders have been modified for the last time.
|
||||
*
|
||||
* @param string $dir The path of the directory
|
||||
* @param 'date'|'intl'|'strftime'|null $handler Custom date handler or `null`
|
||||
* for the globally configured one
|
||||
*/
|
||||
public static function modified(string $dir, string $format = null, string $handler = 'date'): int|string
|
||||
public static function modified(string $dir, string $format = null, string|null $handler = null): int|string
|
||||
{
|
||||
$modified = filemtime($dir);
|
||||
$items = static::read($dir);
|
||||
|
@@ -475,12 +475,13 @@ class F
|
||||
/**
|
||||
* Get the file's last modification time.
|
||||
*
|
||||
* @param string $handler date, intl or strftime
|
||||
* @param 'date'|'intl'|'strftime'|null $handler Custom date handler or `null`
|
||||
* for the globally configured one
|
||||
*/
|
||||
public static function modified(
|
||||
string $file,
|
||||
string|IntlDateFormatter|null $format = null,
|
||||
string $handler = 'date'
|
||||
string|null $handler = null
|
||||
): string|int|false {
|
||||
if (file_exists($file) !== true) {
|
||||
return false;
|
||||
@@ -727,7 +728,8 @@ class F
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a filename to strip unwanted special characters
|
||||
* Sanitize a file's full name (filename and extension)
|
||||
* to strip unwanted special characters
|
||||
*
|
||||
* <code>
|
||||
*
|
||||
@@ -740,12 +742,34 @@ class F
|
||||
*/
|
||||
public static function safeName(string $string): string
|
||||
{
|
||||
$name = static::name($string);
|
||||
$extension = static::extension($string);
|
||||
$safeName = Str::slug($name, '-', 'a-z0-9@._-');
|
||||
$safeExtension = empty($extension) === false ? '.' . Str::slug($extension) : '';
|
||||
$basename = static::safeBasename($string);
|
||||
$extension = static::safeExtension($string);
|
||||
|
||||
return $safeName . $safeExtension;
|
||||
if (empty($extension) === false) {
|
||||
$extension = '.' . $extension;
|
||||
}
|
||||
|
||||
return $basename . $extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a file's name (without extension)
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public static function safeBasename(string $string): string
|
||||
{
|
||||
$name = static::name($string);
|
||||
return Str::slug($name, '-', 'a-z0-9@._-');
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize a file's extension
|
||||
* @since 4.0.0
|
||||
*/
|
||||
public static function safeExtension(string $string): string
|
||||
{
|
||||
$extension = static::extension($string);
|
||||
return Str::slug($extension);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -10,7 +10,6 @@ use Kirby\Http\Response;
|
||||
use Kirby\Sane\Sane;
|
||||
use Kirby\Toolkit\Escape;
|
||||
use Kirby\Toolkit\Html;
|
||||
use Kirby\Toolkit\Properties;
|
||||
use Kirby\Toolkit\V;
|
||||
|
||||
/**
|
||||
@@ -27,23 +26,21 @@ use Kirby\Toolkit\V;
|
||||
*/
|
||||
class File
|
||||
{
|
||||
use Properties;
|
||||
|
||||
/**
|
||||
* Parent file model
|
||||
* The model object must use the `\Kirby\Filesystem\IsFile` trait
|
||||
*/
|
||||
protected object|null $model = null;
|
||||
protected object|null $model;
|
||||
|
||||
/**
|
||||
* Absolute file path
|
||||
*/
|
||||
protected string|null $root = null;
|
||||
protected string|null $root;
|
||||
|
||||
/**
|
||||
* Absolute file URL
|
||||
*/
|
||||
protected string|null $url = null;
|
||||
protected string|null $url;
|
||||
|
||||
/**
|
||||
* Validation rules to be used for `::match()`
|
||||
@@ -58,14 +55,15 @@ class File
|
||||
*
|
||||
* @param array|string|null $props Properties or deprecated `$root` string
|
||||
* @param string|null $url Deprecated argument, use `$props['url']` instead
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException When the model does not use the `Kirby\Filesystem\IsFile` trait
|
||||
*/
|
||||
public function __construct(
|
||||
array|string|null $props = null,
|
||||
string|null $url = null
|
||||
array|string $props = null,
|
||||
string $url = null
|
||||
) {
|
||||
// Legacy support for old constructor of
|
||||
// the `Kirby\Image\Image` class
|
||||
// @todo 4.0.0 remove
|
||||
if (is_array($props) === false) {
|
||||
$props = [
|
||||
'root' => $props,
|
||||
@@ -73,11 +71,21 @@ class File
|
||||
];
|
||||
}
|
||||
|
||||
$this->setProperties($props);
|
||||
$this->root = $props['root'] ?? null;
|
||||
$this->url = $props['url'] ?? null;
|
||||
$this->model = $props['model'] ?? null;
|
||||
|
||||
if (
|
||||
$this->model !== null &&
|
||||
method_exists($this->model, 'hasIsFileTrait') !== true
|
||||
) {
|
||||
throw new InvalidArgumentException('The model object must use the "Kirby\Filesystem\IsFile" trait');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Improved `var_dump` output
|
||||
* @codeCoverageIgnore
|
||||
*/
|
||||
public function __debugInfo(): array
|
||||
{
|
||||
@@ -343,19 +351,14 @@ class File
|
||||
/**
|
||||
* Returns the file's last modification time
|
||||
*
|
||||
* @param string|null $handler date, intl or strftime
|
||||
* @param 'date'|'intl'|'strftime'|null $handler Custom date handler or `null`
|
||||
* for the globally configured one
|
||||
*/
|
||||
public function modified(
|
||||
string|IntlDateFormatter|null $format = null,
|
||||
string|null $handler = null
|
||||
): string|int|false {
|
||||
$kirby = $this->kirby();
|
||||
|
||||
return F::modified(
|
||||
$this->root(),
|
||||
$format,
|
||||
$handler ?? $kirby?->option('date.handler', 'date') ?? 'date'
|
||||
);
|
||||
return F::modified($this->root(), $format, $handler);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -435,45 +438,6 @@ class File
|
||||
return $this->root ??= $this->model?->root();
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the parent file model, which uses this instance as proxied file asset
|
||||
*
|
||||
* @return $this
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException When the model does not use the `Kirby\Filesystem\IsFile` trait
|
||||
*/
|
||||
protected function setModel(object|null $model = null): static
|
||||
{
|
||||
if ($model !== null && method_exists($model, 'hasIsFileTrait') !== true) {
|
||||
throw new InvalidArgumentException('The model object must use the "Kirby\Filesystem\IsFile" trait');
|
||||
}
|
||||
|
||||
$this->model = $model;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the root
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setRoot(string|null $root = null): static
|
||||
{
|
||||
$this->root = $root;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the file url
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setUrl(string|null $url = null): static
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the absolute url for the file
|
||||
*/
|
||||
|
@@ -65,7 +65,7 @@ class Filename
|
||||
$attributes['format'] ??
|
||||
pathinfo($filename, PATHINFO_EXTENSION)
|
||||
);
|
||||
$this->name = $this->sanitizeName(pathinfo($filename, PATHINFO_FILENAME));
|
||||
$this->name = $this->sanitizeName($filename);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -227,24 +227,21 @@ class Filename
|
||||
|
||||
/**
|
||||
* Sanitizes the file extension.
|
||||
* The extension will be converted
|
||||
* to lowercase and `jpeg` will be
|
||||
* replaced with `jpg`
|
||||
* It also replaces `jpeg` with `jpg`.
|
||||
*/
|
||||
protected function sanitizeExtension(string $extension): string
|
||||
{
|
||||
$extension = strtolower($extension);
|
||||
$extension = F::safeExtension('test.' . $extension);
|
||||
$extension = str_replace('jpeg', 'jpg', $extension);
|
||||
return $extension;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitizes the name with Kirby's
|
||||
* Str::slug function
|
||||
* Sanitizes the file name
|
||||
*/
|
||||
protected function sanitizeName(string $name): string
|
||||
{
|
||||
return Str::slug($name);
|
||||
return F::safeBasename($name);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -5,7 +5,6 @@ namespace Kirby\Filesystem;
|
||||
use Kirby\Cms\App;
|
||||
use Kirby\Exception\BadMethodCallException;
|
||||
use Kirby\Image\Image;
|
||||
use Kirby\Toolkit\Properties;
|
||||
|
||||
/**
|
||||
* Trait for all objects that represent an asset file.
|
||||
@@ -22,8 +21,6 @@ use Kirby\Toolkit\Properties;
|
||||
*/
|
||||
trait IsFile
|
||||
{
|
||||
use Properties;
|
||||
|
||||
/**
|
||||
* File asset object
|
||||
*/
|
||||
@@ -32,19 +29,20 @@ trait IsFile
|
||||
/**
|
||||
* Absolute file path
|
||||
*/
|
||||
protected string|null $root = null;
|
||||
protected string|null $root;
|
||||
|
||||
/**
|
||||
* Absolute file URL
|
||||
*/
|
||||
protected string|null $url = null;
|
||||
protected string|null $url;
|
||||
|
||||
/**
|
||||
* Constructor sets all file properties
|
||||
*/
|
||||
public function __construct(array $props)
|
||||
{
|
||||
$this->setProperties($props);
|
||||
$this->root = $props['root'] ?? null;
|
||||
$this->url = $props['url'] ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +50,7 @@ trait IsFile
|
||||
*
|
||||
* @throws \Kirby\Exception\BadMethodCallException
|
||||
*/
|
||||
public function __call(string $method, array $arguments = [])
|
||||
public function __call(string $method, array $arguments = []): mixed
|
||||
{
|
||||
// public property access
|
||||
if (isset($this->$method) === true) {
|
||||
@@ -136,28 +134,6 @@ trait IsFile
|
||||
return $this->root;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the root
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setRoot(string|null $root = null): static
|
||||
{
|
||||
$this->root = $root;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Setter for the file url
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
protected function setUrl(string|null $url = null): static
|
||||
{
|
||||
$this->url = $url;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the file type
|
||||
*/
|
||||
|
@@ -30,7 +30,7 @@ class Mime
|
||||
'aifc' => 'audio/x-aiff',
|
||||
'aiff' => 'audio/x-aiff',
|
||||
'avi' => 'video/x-msvideo',
|
||||
'avif' => 'image/avif',
|
||||
'avif' => 'image/avif',
|
||||
'bmp' => 'image/bmp',
|
||||
'css' => 'text/css',
|
||||
'csv' => ['text/csv', 'text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream'],
|
||||
@@ -119,24 +119,22 @@ class Mime
|
||||
|
||||
/**
|
||||
* Fixes an invalid MIME type guess for the given file
|
||||
*
|
||||
* @param string $file
|
||||
* @param string $mime
|
||||
* @param string $extension
|
||||
* @return string|null
|
||||
*/
|
||||
public static function fix(string $file, string $mime = null, string $extension = null)
|
||||
{
|
||||
public static function fix(
|
||||
string $file,
|
||||
string|null $mime = null,
|
||||
string|null $extension = null
|
||||
): string|null {
|
||||
// fixing map
|
||||
$map = [
|
||||
'text/html' => [
|
||||
'svg' => ['Kirby\Filesystem\Mime', 'fromSvg'],
|
||||
'svg' => [Mime::class, 'fromSvg'],
|
||||
],
|
||||
'text/plain' => [
|
||||
'css' => 'text/css',
|
||||
'json' => 'application/json',
|
||||
'mjs' => 'text/javascript',
|
||||
'svg' => ['Kirby\Filesystem\Mime', 'fromSvg'],
|
||||
'svg' => [Mime::class, 'fromSvg'],
|
||||
],
|
||||
'text/x-asm' => [
|
||||
'css' => 'text/css'
|
||||
@@ -167,9 +165,6 @@ class Mime
|
||||
|
||||
/**
|
||||
* Guesses a MIME type by extension
|
||||
*
|
||||
* @param string $extension
|
||||
* @return string|null
|
||||
*/
|
||||
public static function fromExtension(string $extension): string|null
|
||||
{
|
||||
@@ -179,11 +174,8 @@ class Mime
|
||||
|
||||
/**
|
||||
* Returns the MIME type of a file
|
||||
*
|
||||
* @param string $file
|
||||
* @return string|false
|
||||
*/
|
||||
public static function fromFileInfo(string $file)
|
||||
public static function fromFileInfo(string $file): string|false
|
||||
{
|
||||
if (function_exists('finfo_file') === true && file_exists($file) === true) {
|
||||
$finfo = finfo_open(FILEINFO_MIME_TYPE);
|
||||
@@ -197,13 +189,13 @@ class Mime
|
||||
|
||||
/**
|
||||
* Returns the MIME type of a file
|
||||
*
|
||||
* @param string $file
|
||||
* @return string|false
|
||||
*/
|
||||
public static function fromMimeContentType(string $file)
|
||||
public static function fromMimeContentType(string $file): string|false
|
||||
{
|
||||
if (function_exists('mime_content_type') === true && file_exists($file) === true) {
|
||||
if (
|
||||
function_exists('mime_content_type') === true &&
|
||||
file_exists($file) === true
|
||||
) {
|
||||
return mime_content_type($file);
|
||||
}
|
||||
|
||||
@@ -212,11 +204,8 @@ class Mime
|
||||
|
||||
/**
|
||||
* Tries to detect a valid SVG and returns the MIME type accordingly
|
||||
*
|
||||
* @param string $file
|
||||
* @return string|false
|
||||
*/
|
||||
public static function fromSvg(string $file)
|
||||
public static function fromSvg(string $file): string|false
|
||||
{
|
||||
if (file_exists($file) === true) {
|
||||
libxml_use_internal_errors(true);
|
||||
@@ -234,10 +223,6 @@ class Mime
|
||||
/**
|
||||
* Tests if a given MIME type is matched by an `Accept` header
|
||||
* pattern; returns true if the MIME type is contained at all
|
||||
*
|
||||
* @param string $mime
|
||||
* @param string $pattern
|
||||
* @return bool
|
||||
*/
|
||||
public static function isAccepted(string $mime, string $pattern): bool
|
||||
{
|
||||
@@ -256,10 +241,6 @@ class Mime
|
||||
* Tests if a MIME wildcard pattern from an `Accept` header
|
||||
* matches a given type
|
||||
* @since 3.3.0
|
||||
*
|
||||
* @param string $test
|
||||
* @param string $wildcard
|
||||
* @return bool
|
||||
*/
|
||||
public static function matches(string $test, string $wildcard): bool
|
||||
{
|
||||
@@ -268,11 +249,8 @@ class Mime
|
||||
|
||||
/**
|
||||
* Returns the extension for a given MIME type
|
||||
*
|
||||
* @param string|null $mime
|
||||
* @return string|false
|
||||
*/
|
||||
public static function toExtension(string $mime = null)
|
||||
public static function toExtension(string $mime = null): string|false
|
||||
{
|
||||
foreach (static::$types as $key => $value) {
|
||||
if (is_array($value) === true && in_array($mime, $value) === true) {
|
||||
@@ -289,9 +267,6 @@ class Mime
|
||||
|
||||
/**
|
||||
* Returns all available extensions for a given MIME type
|
||||
*
|
||||
* @param string|null $mime
|
||||
* @return array
|
||||
*/
|
||||
public static function toExtensions(string $mime = null): array
|
||||
{
|
||||
@@ -314,8 +289,10 @@ class Mime
|
||||
/**
|
||||
* Returns the MIME type of a file
|
||||
*/
|
||||
public static function type(string $file, string|null $extension = null): string|null
|
||||
{
|
||||
public static function type(
|
||||
string $file,
|
||||
string|null $extension = null
|
||||
): string|null {
|
||||
// use the standard finfo extension
|
||||
$mime = static::fromFileInfo($file);
|
||||
|
||||
@@ -338,8 +315,6 @@ class Mime
|
||||
|
||||
/**
|
||||
* Returns all detectable MIME types
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public static function types(): array
|
||||
{
|
||||
|
Reference in New Issue
Block a user