Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions formwork/config/routes/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
'filters' => [
'request.validateSize' => [
'action' => static function (Config $config, Request $request, Router $router, ErrorsControllerInterface $errorsController) {
if ($config->get('system.panel.enabled') && $router->requestHasPrefix($config->get('system.panel.root'))) {
if ($config->getBool('system.panel.enabled') && $router->requestHasPrefix($config->getString('system.panel.root'))) {
return;
}

Expand All @@ -86,7 +86,7 @@

'request.validateCsrf' => [
'action' => static function (Config $config, Request $request, Router $router, CsrfToken $csrfToken, ErrorsControllerInterface $errorsController) {
if ($config->get('system.panel.enabled') && $router->requestHasPrefix($config->get('system.panel.root'))) {
if ($config->getBool('system.panel.enabled') && $router->requestHasPrefix($config->getString('system.panel.root'))) {
// CSRF validation is handled by a separate filter in the panel routes
return;
}
Expand All @@ -113,7 +113,7 @@
$router->setRequest(Str::removeStart($router->request(), '/' . $requested));
} elseif (($preferred = $site->languages()->preferred()) !== null) {
// Don't redirect if we are in Panel
if ($config->get('system.panel.enabled') && $router->requestHasPrefix($config->get('system.panel.root'))) {
if ($config->getBool('system.panel.enabled') && $router->requestHasPrefix($config->getString('system.panel.root'))) {
return;
}
return new RedirectResponse($request->root() . $preferred . $router->request());
Expand Down
10 changes: 5 additions & 5 deletions formwork/config/views/methods.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@
[
'site' => $app->site(),
'baseRoute' => $currentPage !== null ? $currentPage->route() : '/',
'allowHtml' => $app->config()->get('system.pages.content.allowHtml'),
'addHeadingIds' => $app->config()->get('system.pages.content.addHeadingIds'),
'commonmarkExtensions' => $app->config()->get('system.pages.content.commonmarkExtensions', []),
'allowHtml' => $app->config()->getBool('system.pages.content.allowHtml'),
'addHeadingIds' => $app->config()->getBool('system.pages.content.addHeadingIds'),
'commonmarkExtensions' => $app->config()->getArray('system.pages.content.commonmarkExtensions', []),
]
);
},
Expand All @@ -70,7 +70,7 @@
'date' => static function (int $timestamp, ?string $format = null) use ($app): string {
return Date::formatTimestamp(
$timestamp,
$format ?? $app->config()->get('system.date.dateFormat'),
$format ?? $app->config()->getString('system.date.dateFormat'),
$app->translations()->getCurrent()
);
},
Expand All @@ -79,7 +79,7 @@
* Formats a timestamp as a datetime string
*/
'datetime' => static function (int $timestamp) use ($app): string {
return Date::formatTimestamp($timestamp, $app->config()->get('system.date.datetimeFormat'), $app->translations()->getCurrent());
return Date::formatTimestamp($timestamp, $app->config()->getString('system.date.datetimeFormat'), $app->translations()->getCurrent());
},

/**
Expand Down
9 changes: 6 additions & 3 deletions formwork/fields/date.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
* Return the field value as a timestamp
*/
'toTimestamp' => function (Field $field) use ($app): ?int {
$formats = $app->config()->getMultiple(['system.date.dateFormat', 'system.date.datetimeFormat']);
$formats = [
$app->config()->getString('system.date.dateFormat'),
$app->config()->getString('system.date.datetimeFormat'),
];
return $field->isEmpty() ? null : Date::toTimestamp($field->value(), $formats);
},

Expand Down Expand Up @@ -100,8 +103,8 @@
}

$inputFormats = [
$app->config()->get('system.date.dateFormat'),
$app->config()->get('system.date.datetimeFormat'),
$app->config()->getString('system.date.dateFormat'),
$app->config()->getString('system.date.datetimeFormat'),
];

$format = $field->hasTime()
Expand Down
6 changes: 3 additions & 3 deletions formwork/fields/markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
[
'site' => $site,
'baseRoute' => $currentPage !== null ? $currentPage->route() : '/',
'allowHtml' => $app->config()->get('system.pages.content.allowHtml'),
'addHeadingIds' => $app->config()->get('system.pages.content.addHeadingIds'),
'commonmarkExtensions' => $app->config()->get('system.pages.content.commonmarkExtensions', []),
'allowHtml' => $app->config()->getBool('system.pages.content.allowHtml'),
'addHeadingIds' => $app->config()->getBool('system.pages.content.addHeadingIds'),
'commonmarkExtensions' => $app->config()->getArray('system.pages.content.commonmarkExtensions', []),
]
);
},
Expand Down
4 changes: 2 additions & 2 deletions formwork/fields/upload.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* Return the accepted MIME types for the field
*/
'acceptMimeTypes' => function (Field $field) use ($app) {
$allowedExtensions = $app->config()->get('system.files.allowedExtensions', '');
$allowedExtensions = $app->config()->getArray('system.files.allowedExtensions', []);

$accept = is_string($field->get('accept'))
? preg_split('/\s*,\s*/', $field->get('accept'), flags: PREG_SPLIT_NO_EMPTY)
Expand Down Expand Up @@ -89,7 +89,7 @@
return null;
}

$allowedExtensions = $app->config()->get('system.files.allowedExtensions', '');
$allowedExtensions = $app->config()->getArray('system.files.allowedExtensions', []);
$allowedMimeTypes = Arr::map($allowedExtensions, MimeType::fromExtension(...));
$acceptMimeTypes = $field->acceptMimeTypes();

Expand Down
18 changes: 9 additions & 9 deletions formwork/src/Cms/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,8 +284,8 @@ private function loadServices(Container $container): void
->alias('config');

$container->define(ViewFactory::class)
->parameter('resolutionPaths', fn(Config $config) => ['system' => $config->get('system.views.paths.system')])
->parameter('methods', fn(Container $container, Config $config) => $container->call(require $config->get('system.views.methods.system')));
->parameter('resolutionPaths', fn(Config $config) => ['system' => $config->getString('system.views.paths.system')])
->parameter('methods', fn(Container $container, Config $config) => $container->call(require $config->getString('system.views.methods.system')));

$container->define(ErrorsController::class)
->alias(ErrorsControllerInterface::class);
Expand Down Expand Up @@ -323,13 +323,13 @@ private function loadServices(Container $container): void
->alias('templates');

$container->define(Statistics::class)
->parameter('options', fn(Config $config) => $config->get('site.statistics'))
->parameter('options', fn(Config $config) => $config->getArray('site.statistics'))
->parameter('translation', fn(Translations $translations) => $translations->getCurrent())
->alias('statistics');

$container->define(FilesCache::class)
->parameter('path', fn(Config $config) => $config->get('system.cache.path'))
->parameter('defaultTtl', fn(Config $config) => $config->get('system.cache.time'))
->parameter('path', fn(Config $config) => $config->getString('system.cache.path'))
->parameter('defaultTtl', fn(Config $config) => $config->getInt('system.cache.time'))
->alias(AbstractCache::class)
->alias('cache');

Expand Down Expand Up @@ -376,14 +376,14 @@ private function loadRoutes(): void
{
$this->events()->dispatch(new RoutesBeforeLoadEvent($this->router()));

if ($this->config()->get('system.panel.enabled')) {
if ($this->config()->getBool('system.panel.enabled')) {
$this->router()->loadFromFile(
$this->config()->get('system.routes.files.panel'),
Str::wrap($this->config()->get('system.panel.root'), '/')
$this->config()->getString('system.routes.files.panel'),
Str::wrap($this->config()->getString('system.panel.root'), '/')
);
}

$this->router()->loadFromFile($this->config()->get('system.routes.files.system'));
$this->router()->loadFromFile($this->config()->getString('system.routes.files.system'));

$this->events()->dispatch(new RoutesAfterLoadEvent($this->router()));
}
Expand Down
14 changes: 7 additions & 7 deletions formwork/src/Cms/Site.php
Original file line number Diff line number Diff line change
Expand Up @@ -270,8 +270,8 @@ public function metadata(): MetadataCollection
}

$defaults = [
'charset' => $this->config->get('system.charset'),
'generator' => $this->config->get('system.metadata.setGenerator') ? 'Formwork' : null,
'charset' => $this->config->getString('system.charset'),
'generator' => $this->config->getBool('system.metadata.setGenerator') ? 'Formwork' : null,
];

$data = array_filter([...$defaults, ...$this->data['metadata']]);
Expand Down Expand Up @@ -436,7 +436,7 @@ public function findPage(string $route): ?Page
*/
public function indexPage(): Page
{
return $this->findPage($this->config->get('system.pages.index'))
return $this->findPage($this->config->getString('system.pages.index'))
?? throw new PageNotFoundException('Site index page not found');
}

Expand All @@ -447,7 +447,7 @@ public function indexPage(): Page
*/
public function errorPage(): Page
{
return $this->findPage($this->config->get('system.pages.error'))
return $this->findPage($this->config->getString('system.pages.error'))
?? throw new PageNotFoundException('Site error page not found');
}

Expand Down Expand Up @@ -478,15 +478,15 @@ public function files(): FileCollection

$files = [];

$path = $this->config->get('system.files.paths.site');
$path = $this->config->getString('system.files.paths.site');

if (FileSystem::isDirectory($path, assertExists: false)) {
foreach (FileSystem::listFiles($path) as $file) {
$extension = '.' . FileSystem::extension($file);
if (Str::endsWith($file, $this->config->get('system.files.metadataExtension'))) {
if (Str::endsWith($file, $this->config->getString('system.files.metadataExtension'))) {
continue;
}
if (in_array($extension, $this->config->get('system.files.allowedExtensions'), true)) {
if (in_array($extension, $this->config->getArray('system.files.allowedExtensions', []), true)) {
$files[] = $this->app()->getService(FileFactory::class)->make(FileSystem::joinPaths($path, $file));
}
}
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Commands/BackupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ public function list(array $argv = []): void
*/
private function getBackupper(?string $hostname = null): Backupper
{
return new Backupper([...$this->app->config()->get('system.backup'), 'hostname' => $hostname ?? (gethostname() ?: 'local-cli')]);
return new Backupper([...$this->app->config()->getArray('system.backup'), 'hostname' => $hostname ?? (gethostname() ?: 'local-cli')]);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions formwork/src/Commands/CacheCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ private function configCacheStats(): void
*/
private function imagesCacheStats(): void
{
$path = $this->app->config()->get('system.images.processPath');
$path = $this->app->config()->getString('system.images.processPath');
$items = iterator_to_array(FileSystem::listContents($path));
$size = FileSystem::directorySize($path);

Expand All @@ -233,7 +233,7 @@ private function imagesCacheStats(): void
*/
private function pagesCacheStats(): void
{
$path = $this->app->config()->get('system.cache.path');
$path = $this->app->config()->getString('system.cache.path');
$items = iterator_to_array(FileSystem::listContents($path));
$size = FileSystem::directorySize($path);

Expand Down Expand Up @@ -273,7 +273,7 @@ private function clearCaches(array $types): void
*/
private function clearImagesCache(): void
{
$path = $this->app->config()->get('system.images.processPath');
$path = $this->app->config()->getString('system.images.processPath');
FileSystem::delete($path, recursive: true);
FileSystem::createDirectory($path, recursive: true);
}
Expand Down
6 changes: 3 additions & 3 deletions formwork/src/Commands/UpdatesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public function update(array $argv = []): void
}
$this->climate->br();

if ($this->app->config()->get('system.cache.enabled')) {
if ($this->app->config()->getBool('system.cache.enabled')) {
$this->climate->out('Clearing cache...');
$this->app->getService(AbstractCache::class)->clear();
$this->climate->br();
Expand All @@ -219,15 +219,15 @@ public function update(array $argv = []): void
*/
private function getUpdater(array $config): Updater
{
return new Updater([...$this->app->config()->get('system.updates'), ...$config], App::instance());
return new Updater([...$this->app->config()->getArray('system.updates'), ...$config], App::instance());
}

/**
* Get Backupper instance
*/
private function getBackupper(): Backupper
{
return new Backupper([...$this->app->config()->get('system.backup'), 'hostname' => gethostname() ?: 'local-cli']);
return new Backupper([...$this->app->config()->getArray('system.backup'), 'hostname' => gethostname() ?: 'local-cli']);
}

/**
Expand Down
75 changes: 75 additions & 0 deletions formwork/src/Config/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Formwork\Utils\Arr;
use Formwork\Utils\FileSystem;
use Formwork\Utils\Str;
use UnexpectedValueException;

class Config implements ArraySerializable
{
Expand Down Expand Up @@ -64,6 +65,80 @@ public function get(string $key, mixed $default = null): mixed
return Arr::get($this->config, $key, $default);
}

/**
* Get a string value from the config
*
* @throws UnexpectedValueException If the config value is not a string
*/
public function getString(string $key, ?string $default = null): string
{
$value = $this->get($key, $default);
if (!is_string($value)) {
throw new UnexpectedValueException(sprintf('Config value for key "%s" is not a string, got %s', $key, get_debug_type($value)));
}
return $value;
}

/**
* Get a boolean value from the config
*
* @throws UnexpectedValueException If the config value is not a boolean
*/
public function getBool(string $key, ?bool $default = null): bool
{
$value = $this->get($key, $default);
if (!is_bool($value)) {
throw new UnexpectedValueException(sprintf('Config value for key "%s" is not a boolean, got %s', $key, get_debug_type($value)));
}
return $value;
}

/**
* Get an integer value from the config
*
* @throws UnexpectedValueException If the config value is not an integer
*/
public function getInt(string $key, ?int $default = null): int
{
$value = $this->get($key, $default);
if (!is_int($value)) {
throw new UnexpectedValueException(sprintf('Config value for key "%s" is not an integer, got %s', $key, get_debug_type($value)));
}
return $value;
}

/**
* Get a float value from the config
*
* @throws UnexpectedValueException If the config value is not a float
*/
public function getFloat(string $key, ?float $default = null): float
{
$value = $this->get($key, $default);
if (!is_float($value)) {
throw new UnexpectedValueException(sprintf('Config value for key "%s" is not a float, got %s', $key, get_debug_type($value)));
}
return $value;
}

/**
* Get an array value from the config
*
* @param ?array<mixed> $default
*
* @throws UnexpectedValueException If the config value is not an array
*
* @return array<mixed>
*/
public function getArray(string $key, ?array $default = null): array
{
$value = $this->get($key, $default);
if (!is_array($value)) {
throw new UnexpectedValueException(sprintf('Config value for key "%s" is not an array, got %s', $key, get_debug_type($value)));
}
return $value;
}

/**
* Get multiple values from the config
*
Expand Down
4 changes: 2 additions & 2 deletions formwork/src/Controllers/AssetsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function asset(RouteParams $routeParams): Response
return $this->redirect($this->router->rewrite(['type' => 'images']), ResponseStatus::MovedPermanently);
}

$path = FileSystem::joinPaths($this->config->get('system.images.processPath'), $routeParams->get('id'), $routeParams->get('name'));
$path = FileSystem::joinPaths($this->config->getString('system.images.processPath'), $routeParams->get('id'), $routeParams->get('name'));

if (FileSystem::isFile($path, assertExists: false)) {
return new FileResponse($path, headers: ['Cache-Control' => 'private, max-age=31536000, immutable'], autoEtag: true, autoLastModified: true);
Expand All @@ -35,7 +35,7 @@ public function asset(RouteParams $routeParams): Response
*/
public function template(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.templates.path'), 'assets', Path::resolve($routeParams->get('file'), '/', DIRECTORY_SEPARATOR));
$path = FileSystem::joinPaths($this->config->getString('system.templates.path'), 'assets', Path::resolve($routeParams->get('file'), '/', DIRECTORY_SEPARATOR));

if (FileSystem::isFile($path, assertExists: false)) {
$headers = $this->request->query()->has('v')
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Controllers/ErrorsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function error(ResponseStatus $responseStatus = ResponseStatus::InternalS
{
Response::cleanOutputBuffers();

if ($this->config->get('system.debug.enabled') || $this->request->isLocalhost()) {
if ($this->config->getBool('system.debug.enabled') || $this->request->isLocalhost()) {
$data['throwable'] = $throwable;
$data['stackTrace'] = $throwable !== null ? $this->getTrace($throwable) : [];
}
Expand Down
2 changes: 1 addition & 1 deletion formwork/src/Controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ final class FilesController extends AbstractController
*/
public function file(RouteParams $routeParams): Response
{
$path = FileSystem::joinPaths($this->config->get('system.files.paths.site'), $routeParams->get('name'));
$path = FileSystem::joinPaths($this->config->getString('system.files.paths.site'), $routeParams->get('name'));

if (FileSystem::isFile($path, assertExists: false)) {
return new FileResponse($path);
Expand Down
Loading
Loading