Skip to content

Commit

Permalink
💥 reverted multi word block models to kebab case
Browse files Browse the repository at this point in the history
  • Loading branch information
bnomei committed Aug 20, 2024
1 parent 8aa6085 commit a4cc6ed
Show file tree
Hide file tree
Showing 8 changed files with 294 additions and 232 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,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
33 changes: 31 additions & 2 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 @@ -427,6 +427,35 @@ 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.3",
"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 a4cc6ed

Please sign in to comment.