to your main composer file where you require “getkirby/cms”.
Create a folder with your plugin name example: Less
Filename: index.php
<?php
namespace Kirby\Plugins\Less;
use Kirby\Cms\App as Kirby;
use Kirby\Cms\Page;
use Kirby\Http\Header;
Kirby::plugin('Modufolio/Less', [
'hooks' => [
'page.*:*' => function ($page): void {
$allModules = new AllModules();
$allModules->writeModules();
$allModules->compileLess();
},
.....
.....
I don’t have to import Allmodules because it’s in the same namespace.
Filename: AllModules.php
<?php
namespace Kirby\Plugins\Less;
use Kirby\Toolkit\F;
class AllModules
{
public function getModules(): array
{
$modulesArray = [];
// find all sections templates and remove section- in the names
foreach (site()->index()->filterBy('intendedTemplate', '*=', 'section_') as $page) {
....
....
thanks for sharing. the idea to use the composer.json file of project to load all classes of installed plugins is nice.
what happens if you load classes that already have namespaces? like from a plugin of mine?
the psr4 loading worked well so far in most of my plugins. the plugin has their own composer.json file to do that. but i still had to add a manual load once in a while to fix certain issues with either non composer installs, plugin load order or hooks etc. i sadly i did not document why exactly but all my plugins that create a global helper function like janitor, autoid or monolog end up requiring the class non the less.