Skip to content

Commit

Permalink
✨ added option for symfony yaml parser (default: spyc)
Browse files Browse the repository at this point in the history
⬆️ upgraded dependencies

Signed-off-by: bnomei <[email protected]>
  • Loading branch information
bnomei committed Feb 7, 2024
1 parent fd85012 commit 6eaac0c
Show file tree
Hide file tree
Showing 15 changed files with 1,000 additions and 795 deletions.
19 changes: 15 additions & 4 deletions classes/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@
namespace Bnomei;

use Closure;
use Exception;
use Kirby\Toolkit\A;
use Spyc;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Yaml\Yaml;

final class Autoloader
{
Expand Down Expand Up @@ -48,6 +50,11 @@ final class Autoloader
public function __construct(array $options = [])
{
$this->options = array_merge_recursive([
// we can not read the kirby options since we are loading
// while kirby is booting, but once spyc is removed we can
// default to symfony yaml
// TODO: maybe with a Kirby::version() check
'yaml.handler' => 'spyc', // spyc or symfony
'blueprints' => [
'folder' => 'blueprints',
'name' => self::ANY_PHP_OR_YML,
Expand Down Expand Up @@ -146,7 +153,7 @@ public function __construct(array $options = [])
], $options);

if (! array_key_exists('dir', $this->options)) {
throw new \Exception('Autoloader needs a directory to start scanning at.');
throw new Exception('Autoloader needs a directory to start scanning at.');
}

$this->registry = [];
Expand Down Expand Up @@ -230,11 +237,11 @@ private function registry(string $type): array
$route = require $file->getRealPath();

// check if return is actually an array (if additional stuff is specified, e.g. method or language) or returns a function
if (is_array($route) || $route instanceof \Closure) {
if (is_array($route) || $route instanceof Closure) {
$this->registry[$type][] = array_merge(
[
'pattern' => /*'/' . */ $pattern,
'action' => $route instanceof \Closure ? $route : null,
'action' => $route instanceof Closure ? $route : null,
],
is_array($route) ? $route : []
);
Expand All @@ -252,7 +259,11 @@ private function registry(string $type): array
$path = $file->getPathname();
// remove BOM
$yaml = str_replace("\xEF\xBB\xBF", '', file_get_contents($path));
$this->registry[$type][$key] = Spyc::YAMLLoadString($yaml);
if ($this->options['yaml.handler'] === 'symfony') {
$this->registry[$type][$key] = Yaml::parse($yaml);
} else {
$this->registry[$type][$key] = Spyc::YAMLLoadString($yaml);
}
} else {
$this->registry[$type][$key] = $file->getRealPath();
}
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "bnomei/autoloader-for-kirby",
"type": "project",
"version": "4.0.0",
"version": "4.1.0",
"license": "MIT",
"description": "Helper to automatically load various Kirby extensions in a plugin",
"authors": [
Expand Down Expand Up @@ -55,7 +55,8 @@
"require": {
"php": ">=8.2.0",
"mustangostang/spyc": "^0.6.3",
"symfony/finder": "^6.0"
"symfony/finder": "^6.0",
"symfony/yaml": "^5.4"
},
"require-dev": {
"getkirby/cms": "^4.0.0-beta.2",
Expand Down
Loading

0 comments on commit 6eaac0c

Please sign in to comment.