Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main'
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Nov 23, 2024
2 parents 7cd29f1 + 14b4eaa commit a98c566
Show file tree
Hide file tree
Showing 9 changed files with 298 additions and 235 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,21 @@ Kirby::plugin('bnomei/example', [

## Settings

The package does come with [default settings](https://github.com/bnomei/autoloader-for-kirby/blob/main/classes/Autoloader.php#L27) to fit most usecases.
The package does come with [default settings](https://github.com/bnomei/autoloader-for-kirby/blob/main/classes/Autoloader.php#L27) to fit most usecases. But you can change them every time you call the `autoloader()`-helper for a different directory (aka in each plugin `index.php`-file).

**/site/plugins/example/index.php**
```php
<?php

Kirby::plugin('bnomei/example', autoloader(__DIR__, [
'blockModels' => [
// mapping BlockModel class names to file names, like
// MyCustomBlock::class => 'my.custom' (site/blueprints/blocks/my.custom.yml)
'transform' => fn ($key) => \Bnomei\Autoloader::pascalToDotCase($key),
],
])->toArray()
);
```

## Suggestion

Expand Down
38 changes: 34 additions & 4 deletions classes/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ final class Autoloader

public function __construct(array $options = [])
{
$this->options = array_merge_recursive([
$this->options = self::array_merge_recursive_distinct([
// 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
Expand Down Expand Up @@ -95,7 +95,7 @@ public function __construct(array $options = [])
'name' => self::BLOCK_PHP,
'key' => 'classname',
'require' => false,
'transform' => fn ($key) => lcfirst($key),
'transform' => fn ($key) => self::pascalToKebabCase($key),
'map' => [],
],
'pageModels' => [
Expand Down Expand Up @@ -177,7 +177,7 @@ private function registry(string $type): array
}

$this->registry[$type] = [];
$finder = (new Finder())->files()
$finder = (new Finder)->files()
->name($options['name'])
->in($dir);

Expand Down Expand Up @@ -422,11 +422,41 @@ public function toArray(array $merge = []): array
return array_merge_recursive($this->registry, $merge);
}

public function pascalToKebabCase(string $string): string
public static function pascalToKebabCase(string $string): string
{
return ltrim(strtolower((string) preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $string)), '-');
}

public static function pascalToCamelCase(string $string): string
{
return lcfirst($string);
}

public static function pascalToDotCase(string $string): string
{
return ltrim(strtolower((string) preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '.$0', $string)), '.');
}

public static function array_merge_recursive_distinct(array $array1, array $array2)
{
$merged = $array1;

foreach ($array2 as $key => $value) {
if (is_array($value) && isset($merged[$key]) && is_array($merged[$key])) {
$merged[$key] = self::array_merge_recursive_distinct($merged[$key], $value);
} else {
$merged[$key] = $value;
}
}

return $merged;
}

public static function singletonClear(): void
{
self::$singleton = null;
}

public static function singleton(array $options = []): self
{
if (self::$singleton && self::$singleton->dir() === $options['dir']) {
Expand Down
2 changes: 1 addition & 1 deletion 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.3.2",
"version": "4.4.0",
"license": "MIT",
"description": "Helper to automatically load various Kirby extensions in a plugin",
"authors": [
Expand Down
Loading

0 comments on commit a98c566

Please sign in to comment.