Helper to automatically load various Kirby extensions in a plugin

Overview

Autoloader for Kirby

Release Downloads Twitter

Helper to automatically load various Kirby extensions in a plugin

Commerical Usage

This package is free but if you use it in a commercial project please consider to

Installation

composer require bnomei/autoloader-for-kirby

This package is NOT a kirby plugin

  • This is a composer package because that actually makes it easier to setup and does not mess with the loading order of extensions.
  • Being a package it also can be used not only for local plugins but also as a composer dependency within plugins published online.

Autoloading of extensions

Add the autoloader for each extension type you want once and it will register all files in subfolders correctly.

Supported Extensions

The following extensions can be autoloaded:

  • blueprints (php or yml)
  • classes (php)
  • collections (php)
  • controllers (php)
  • pageModels (php)
  • userModels (php)
  • snippets (php)
  • templates (php)
  • translations (php or yml or json)

NOTE: Loading translations from yaml or json files is added by this package and not originally part of kirby core.

NOTE: The classes autoloader is very basic. Using a custom array with kirby's load()-helper or composers psr-4 autoloading is recommended.

Usage

After requiring it as a dependency in either your project or plugin composer.json you can use the autoload()-helper to load various extension.

/site/plugins/example/index.php



// optionally change some settings
/*
autoloader(__DIR__, [
    'snippets' => [
        'folder' => 'schnippschnapp',
    ],
]);
*/

autoloader(__DIR__)->classes();
// use a different folder
// autoloader(__DIR__)->classes('src');

Kirby::plugin('bnomei/example', [
    'options' => [
        // options
    ],
    'blueprints' => autoloader(__DIR__)->blueprints(),
    'collections' => autoloader(__DIR__)->collections(),
    'controllers' => autoloader(__DIR__)->controllers(),
    'pageModels' => autoloader(__DIR__)->pageModels(),
    'userModels' => autoloader(__DIR__)->userModels(),
    'snippets' => autoloader(__DIR__)->snippets(),
    'templates' => autoloader(__DIR__)->templates(),
    'translations' => autoloader(__DIR__)->translations(),
    // other extensions
]);

Settings

The package does come with default settings to fit most usecases.

Suggestion

This plugin works great in combination with my Kirby CLI Tool which helps you to create extension files faster.

Disclaimer

This package is provided "as is" with no guarantee. Use it at your own risk and always test it yourself before using it in a production environment. If you find any issues, please create a new issue.

License

MIT

It is discouraged to use this package in any project that promotes racism, sexism, homophobia, animal abuse, violence or any other form of hate speech.

You might also like...
Kirby Janitor job for staging

Simple staging Janitor jobs Plugin for very simple staging setup for https://github.com/bnomei/kirby3-janitor/ (required). Beta quality - use at your

Color-managed thumbnails for Kirby 3

ImageKit for Kirby 3 This is not directly related for ImageKit for Kirby 2, but based on the same idea of improving Kirby’s built-in image processing

Add information about PGP public keys on upload in Kirby v3
Add information about PGP public keys on upload in Kirby v3

Kirby3 GnuPG This plugin adds information about PGP public keys on upload, using gpg binary (which needs to be installed for this to work). Getting st

A Frankenstein's monster: Kirby inside of Laravel.

A Kirby-Laravel Starter Kit This is an experimental starter kit for using Kirby within Laravel (a little like Statamic). In my limited experience it r

Kirby wrapper for automated content accessibility checkers Editoria11y and Sa11y

Kirby3 A11yprompter For a comprehensive overview of Sa11y and Editoria11y, how they can assist maintaining an accessible website by supporting content

🦭 Kirby, but headless only – KQL with bearer token, Express-esque middlewares & more

Kirby Headless Starter ℹ️ Send a Bearer test authorization header with a request to the live playground to test this headless starter. This starter ki

A simple wrapper around vlucas' PHP dotenv library for Kirby CMS.

kirby-phpdotenv A simple wrapper around vlucas' PHP dotenv library for Kirby CMS. Why? I've been using .env in my Kirby projects for a while, but I go

PPM is a process manager, supercharger and load balancer for modern PHP applications.
PPM is a process manager, supercharger and load balancer for modern PHP applications.

PPM - PHP Process Manager PHP-PM is a process manager, supercharger and load balancer for PHP applications. It's based on ReactPHP and works best with

The game is implemented as an example of scalable and high load architecture combined with modern software development practices
The game is implemented as an example of scalable and high load architecture combined with modern software development practices

Crossword game The game is implemented as an example of scalable and high load architecture combined with modern software development practices Exampl

Comments
  • Add 'transform' option for custom key transform function (e.g. PascalCase to kebab-case) (closes #5)

    Add 'transform' option for custom key transform function (e.g. PascalCase to kebab-case) (closes #5)

    This PR replaces the 'lowercase' option with a more generic 'transform' option that allows to supply a custom function that does key transforms. This is useful for models that respond to an item whose name is written in kebab-case (e.g. 'blog-article' or 'secondary-hero').

    A PascalCase (Class name standard) to kebab-case (usually keys for Kirby items) transformer is included and added as default for models. Items previously using the 'lowercase' option have been replaced by the function.

    This PR also updates a test (AmazeBlock -> VeryAmazeBlock) and adds a new page test (JustAnotherPage) to ensure the feature works.

    opened by tobimori 2
  • Filenames including additional dots

    Filenames including additional dots

    related https://github.com/bnomei/autoloader-for-kirby/pull/2

    i though again why excluded the file with additional dots by default. it had to do with a project where i replicated the k2 patterns plugin. that used a folder called patterns with snippets and snippetname.config.php file (like a controller for that pattern snippet). and i still like the idea that autoloader sticks to kirby defaults (like in their docs).

    but since autoloader can be configured on creation you can easily make it load files with additions dots yourself using the ANY_ constants.

    in you plugin index.php

    autoloader(__DIR__, [
        'blueprints' => [
            'name' => \Bnomei\Autoloader::ANY_YML, // <-- this one is new and i will add it asap
        ],
        'snippets' => [
            'name' => \Bnomei\Autoloader::ANY_PHP,
        ],
    ]);
    
    documentation 
    opened by bnomei 1
  • Support transforming casing styles

    Support transforming casing styles

    Currently, Autoloader can't load any model which counterpart has e.g. a hyphen in its name, which would be the case for kebab-case named models.

    This might sound confusing, I'll explain it with an example: Lots of my blocks or pages end up with names like 'blog-article' or 'secondary-hero'. When specifying a model for such a type, the class needs to be specified in PascalCase, e.g. 'BlogArticlePage' or 'SecondaryHeroBlock'. Autoloader tries to resolve those cases to 'blogarticle' and 'secondaryhero', as it currently does not support kebab-case and only has an option for lowercase transforms.

    A working code snippet for transforming would be $key = ltrim(strtolower(preg_replace('/[A-Z]([A-Z](?![a-z]))*/', '-$0', $key)), '-') (Adapted from StackOverflow).

    Possible solutions for this is either adding support for specific transform cases yourself, which might be a lot of work or allowing a custom function to be set in the config that can be used to create such transforms yourself.

    enhancement 
    opened by tobimori 0
  • route loader

    route loader

    from @tobimori via discord

    this is basically everything needed (i use the same finder component), you can optimise it for your code

        $finder = (new Finder())->files()
          ->name(static::PHP)
          ->in($dir);
    
        foreach ($finder as $file) {
          $pattern = strtolower($file->getRelativePathname());
          $pattern = preg_replace('~(.*)' . preg_quote('.php', '~') . '~', '$1' . '', $pattern, 1); // replace extension at end
          $pattern = preg_replace('~(.*)' . preg_quote('index', '~') . '~', '$1' . '', $pattern, 1); // replace index at end, for root of folder, but not in paths etc.
    
          $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) {
            $this->registry[] = array_merge(
              [
                'pattern' => '/' . $pattern,
                'action' => $route instanceof \Closure ? $route : null
              ],
              is_array($route) ? $route : []
            );
          }
        }
    
    opened by bnomei 0
Releases(v1.9.0)
Owner
Bruno Meilick
hm..
Bruno Meilick
This Kirby V3 Plugin brings snippets and blueprints together in one place. It includes useful tools that completely changing the way you work with Kirby: Fast and well organized.

Kirby Components Overview Do you love to make awesome projects with Kirby CMS? Do you also find it difficult to switch between snippets and blueprints

Roman Gsponer 6 May 31, 2023
Plugin for Kirby that allows you to load assets generated by Vite.

Kirby Vite Plugin Plugin for Kirby that allows you to load assets generated by Vite. In development mode, assets are loaded from Vite's development se

Oblik Studio 10 Nov 20, 2022
Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files

toKirby Perch Dashboard app for exporting content to (Kirby) text files and Kirby Blueprint files. You can easily install and test it in a few steps.

R. Banus 4 Jan 15, 2022
Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Automatically load the next page of products in Magento. Easy to install and configure, this module works 100% out of the box with vanilla Magento 1.9.x and earlier.

Strategery 123 Nov 20, 2021
GitHub action to set up PHP with extensions, php.ini configuration, coverage drivers, and various tools.

GitHub action to set up PHP with extensions, php.ini configuration, coverage drivers, and various tools.

Shivam Mathur 2.4k Jan 6, 2023
This plugin allows you to create many-to-many relationships between pages in Kirby and synchronizes them on both sides.

Kirby 3 Many To Many Field This plugin allows you to create many-to-many relationships between pages in Kirby.

Jonas Holfeld 41 Nov 19, 2022
Kirby plugin to visually show hidden characters in all kind of input fields and their previews.

Kirby Hidden Characters Kirby plugin to visually show hidden characters in all kind of input fields and their previews. This includes white spaces and

Jakob Grommas 21 Oct 17, 2022
Kirby 3 Plugin for running jobs like cleaning the cache from within the Panel, PHP code, CLI or a cronjob

Kirby 3 Janitor Kirby 3 Plugin for running jobs. It is a Panel Button! It has jobs build-in for cleaning the cache, sessions, create zip-backup, pre-g

Bruno Meilick 68 Dec 21, 2022
A horrendous PM plugin to manually load all the chunks in your world without logging on. Only for the sole purpose of aiding in PM4 -> DF world conversion.

ChunkLoader A horrendous PM plugin to manually load all the chunks in your world without logging on. Only for the sole purpose of aiding in PM4 -> DF

null 2 Aug 10, 2022
Composer Plugin for automatically including files for easing function usage in php.

Php Inc Php inc is a composer plugin for automatically including certain files into composer's autoload and autoload-dev files config. Given a set of

Krak 5 Jan 11, 2022