Upgrade to 3.6.0
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
namespace Kirby\Sane;
|
||||
|
||||
use Kirby\Exception\Exception;
|
||||
use Kirby\Toolkit\F;
|
||||
use Kirby\Filesystem\F;
|
||||
|
||||
/**
|
||||
* Base handler abstract,
|
||||
@@ -19,6 +19,30 @@ use Kirby\Toolkit\F;
|
||||
*/
|
||||
abstract class Handler
|
||||
{
|
||||
/**
|
||||
* Sanitizes the given string
|
||||
*
|
||||
* @param string $string
|
||||
* @return string
|
||||
*/
|
||||
abstract public static function sanitize(string $string): string;
|
||||
|
||||
/**
|
||||
* Sanitizes the contents of a file by overwriting
|
||||
* the file with the sanitized version
|
||||
*
|
||||
* @param string $file
|
||||
* @return void
|
||||
*
|
||||
* @throws \Kirby\Exception\Exception If the file does not exist
|
||||
* @throws \Kirby\Exception\Exception On other errors
|
||||
*/
|
||||
public static function sanitizeFile(string $file): void
|
||||
{
|
||||
$sanitized = static::sanitize(static::readFile($file));
|
||||
F::write($file, $sanitized);
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates file contents
|
||||
*
|
||||
@@ -37,15 +61,31 @@ abstract class Handler
|
||||
* @return void
|
||||
*
|
||||
* @throws \Kirby\Exception\InvalidArgumentException If the file didn't pass validation
|
||||
* @throws \Kirby\Exception\Exception If the file does not exist
|
||||
* @throws \Kirby\Exception\Exception On other errors
|
||||
*/
|
||||
public static function validateFile(string $file): void
|
||||
{
|
||||
static::validate(static::readFile($file));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads the contents of a file
|
||||
* for sanitization or validation
|
||||
*
|
||||
* @param string $file
|
||||
* @return string
|
||||
*
|
||||
* @throws \Kirby\Exception\Exception If the file does not exist
|
||||
*/
|
||||
protected static function readFile(string $file): string
|
||||
{
|
||||
$contents = F::read($file);
|
||||
|
||||
if ($contents === false) {
|
||||
throw new Exception('The file "' . $file . '" does not exist');
|
||||
}
|
||||
|
||||
static::validate($contents);
|
||||
return $contents;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user