Skip to content

Commit 5b297fe

Browse files
Init
0 parents  commit 5b297fe

File tree

9 files changed

+325
-0
lines changed

9 files changed

+325
-0
lines changed

.editorconfig

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# This file is for unifying the coding style for different editors and IDEs
2+
# editorconfig.org
3+
4+
[*]
5+
charset = utf-8
6+
indent_style = space
7+
indent_size = 2
8+
end_of_line = lf
9+
insert_final_newline = true
10+
trim_trailing_whitespace = true
11+
12+
[*.php]
13+
indent_size = 4
14+
15+
[*.md,*.txt]
16+
trim_trailing_whitespace = false
17+
insert_final_newline = false
18+
19+
[composer.json]
20+
indent_size = 4

.gitattributes

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Note: You need to uncomment the lines you want to use; the other lines can be deleted
2+
3+
# Git
4+
# .gitattributes export-ignore
5+
# .gitignore export-ignore
6+
7+
# Tests
8+
# /.coveralls.yml export-ignore
9+
# /.travis.yml export-ignore
10+
# /phpunit.xml.dist export-ignore
11+
# /tests/ export-ignore

.gitignore

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# OS files
2+
.DS_Store
3+
4+
# npm modules
5+
/node_modules
6+
7+
# Composer files
8+
/vendor

LICENSE.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2022 Lukas Kleinschmidt
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# Kirby Snippet Controller
2+
Define snippet controllers in a similar way how page controllers work.
3+
By default the plugin will try to find controllers in your `snippets` directory.
4+
Lets have a look at a example `header` snippet.
5+
6+
## How to use it
7+
```bash
8+
📁 snippets
9+
├─ 📄 header.controller.php
10+
└─ 📄 header.php
11+
```
12+
13+
```php
14+
// header.controller.php
15+
16+
// The return value can be a callback
17+
return function ($site) {
18+
return [
19+
'title' => $site->title(),
20+
];
21+
};
22+
23+
// Or you can simply return an array.
24+
return [
25+
'value' => site()->title(),
26+
];
27+
28+
```
29+
30+
You can also define controllers in a plugin.
31+
```php
32+
Kirby::plugin('superwoman/superplugin', [
33+
'snippets' => [
34+
35+
// Refer to a file
36+
'header.controller' => __DIR__ . '/snippets/header.controller.php',
37+
38+
// Return an array
39+
'header.controller' => [
40+
'title' => site()->title(),
41+
],
42+
43+
// Return a callback
44+
'header.controller' => function ($site) {
45+
return [
46+
'title' => $site->title(),
47+
];
48+
},
49+
50+
],
51+
]);
52+
```
53+
54+
### Available callback arguments in your controllers
55+
Like in regular controllers, you can access the `$site`, `$page`, `$pages` and `$kirby` objects by loading them as arguments to the anonymous function. The plugin will inject the right objects for you. In addition, you also have access to the `$data` argument, which contains the data you passed to the snippet.
56+
57+
### Naming convention
58+
By default, the plugin searches for controllers by appending `.controller` to the snippet name. You can change the name resolver in the options. Changing the name also affects plugin-defined controllers.
59+
```php
60+
// config.php
61+
62+
return [
63+
'lukaskleinschmidt.snippet-controller' => [
64+
65+
// The default resolver
66+
'name' => fn (string $name) => $name . '.controller',
67+
68+
// You might want to store controllers in a separate folder
69+
'name' => fn (string $name) => 'controllers/' . $name,
70+
71+
],
72+
];
73+
74+
```
75+
76+
## Commercial Usage
77+
This plugin is free. Please consider to [make a donation](https://www.paypal.me/lukaskleinschmidt/5EUR) if you use it in a commercial project.
78+
79+
## Installation
80+
81+
### Download
82+
Download and copy this repository to `/site/plugins/snippet-controller`.
83+
84+
### Git submodule
85+
```
86+
git submodule add https://github.com/lukaskleinschmidt/kirby-snippet-controller.git site/plugins/snippet-controller
87+
```
88+
89+
### Composer
90+
```
91+
composer require lukaskleinschmidt/kirby-snippet-controller
92+
```
93+
94+
## License
95+
MIT
96+
97+
## Credits
98+
- [Lukas Kleinschmidt](https://github.com/lukaskleinschmidt)

composer.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"name": "lukaskleinschmidt/kirby-snippet-controller",
3+
"description": "Kirby Snippet Controller",
4+
"license": "MIT",
5+
"type": "kirby-plugin",
6+
"version": "1.0.0",
7+
"authors": [
8+
{
9+
"name": "Lukas Kleinschmidt",
10+
"email": "[email protected]"
11+
}
12+
],
13+
"require": {
14+
"getkirby/composer-installer": "^1.2"
15+
},
16+
"config": {
17+
"allow-plugins": {
18+
"getkirby/composer-installer": true
19+
}
20+
}
21+
}

composer.lock

Lines changed: 66 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

helpers.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
use Kirby\Cms\App;
4+
use Kirby\Filesystem\F;
5+
use Kirby\Toolkit\A;
6+
use Kirby\Toolkit\Controller;
7+
8+
if (! function_exists('snippet_controller_value')) {
9+
function snippet_controller_value(string|array $name): mixed
10+
{
11+
$kirby = App::instance();
12+
$names = A::wrap($name);
13+
$root = $kirby->root('snippets');
14+
15+
foreach ($names as $name) {
16+
$name = $kirby->option('lukaskleinschmidt.snippet-controller.name')($name, $kirby);
17+
$file = $root . '/' . $name . '.php';
18+
19+
if (file_exists($file) === false) {
20+
$file = $kirby->extensions('snippets')[$name] ?? null;
21+
}
22+
23+
if ($file) {
24+
break;
25+
}
26+
}
27+
28+
return $file;
29+
}
30+
}
31+
32+
if (! function_exists('snippet_controller')) {
33+
function snippet_controller(string|array $name, array $data = []): array
34+
{
35+
if (is_null($value = snippet_controller_value($name))) {
36+
return $data;
37+
}
38+
39+
if (is_string($value) && is_file($value)) {
40+
$value = F::load($value);
41+
}
42+
43+
if ($value instanceof Closure) {
44+
$kirby = App::instance();
45+
46+
$value = (new Controller($value))->call($kirby, [
47+
'kirby' => $kirby,
48+
'site' => $site = $kirby->site(),
49+
'pages' => $site->children(),
50+
'page' => $site->page(),
51+
'data' => $data,
52+
]);
53+
}
54+
55+
return is_array($value) ? array_merge($value, $data) : $data;
56+
}
57+
}

index.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
use Kirby\Cms\App;
4+
5+
@include_once __DIR__ . '/helpers.php';
6+
7+
App::plugin('lukaskleinschmidt/snippet-controller', [
8+
'options' => [
9+
'name' => fn (string $name) => $name . '.controller',
10+
],
11+
'components' => [
12+
'snippet' => function (App $kirby, $name, array $data = [], bool $slots = false) {
13+
$data = snippet_controller($name, $data);
14+
15+
return $kirby->core()->components()['snippet'](
16+
$kirby,
17+
$name,
18+
$data,
19+
$slots,
20+
);
21+
}
22+
],
23+
]);

0 commit comments

Comments
 (0)