Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

The ultimate autoloader #7035

Open
wants to merge 2 commits into
base: v5/develop
Choose a base branch
from

Conversation

plain-solutions-gmbh
Copy link
Contributor

@plain-solutions-gmbh plain-solutions-gmbh commented Feb 26, 2025

Description

I have been working on a adequate solution for an plugin autoloader.

Here is an example:

App::plugin(
    'vendor/plugin', autoloader: [
    	'cache' => true,
    	'classes' => [
    		'namespace' => 'Vendor\\Namespace'
    	],
    	'config' => [
    		'path' => './extends'
    	],
    	'blueprints', 'snippets', 'fields', 'translations',
    	'custom' => function($autoloader) {
	        $autoloader->merge([
	            "options" => [
	                "my_option" => 'foo'
	            ]
	        ]);
	    }
    ]
);

If true is passed to the autoloader, all tasks defined in the autoloader are executed.

Summary of changes

  • Add Autoloader class
  • implement to AppPlugin (not plugin class - cause info and root needs to be defined)
  • Add Method to AppPlugin to get allowed extensions keys

Reasoning

Plugins (if the developer use the autoloader) can be loaded from cache.
Extensions could be easly integrating into the plugin.

Additional context

I create it because of this rejected pull request
It would be nice if it's goes into V5. Cause Plugin already takes a new parameter license

Changelog

  • The autoloader executed if the parameter 'autoload' is either 'true' or an array. (Disabled by default)
  • The array contains tasks that are processed in sequence
  • Tasks can be modify, enable or extend. (Cache is enabled by default)
  • The result is cached and stored by default under ./site/cache/autoloder/{vendor}/{plugin_name}\{timestamp}.php. (Can be deactivated)
  • The plugin folder is modified. The cache is renewed.
  • Loads the classes (by default from classes folder) automatically
  • Fields, sections, blueprints, snippets, templates and translations are loaded from the root by default.
  • For other extensions is the subfolder ./config in charge.

Docs

I could write a cookbook if the feature will be applied

Ready?

  • In-code documentation (wherever needed)
  • Unit tests for fixed bug/feature
  • Tests and CI checks all pass

For review team

  • Add lab and/or sandbox examples (wherever helpful)
  • Add changes & docs to release notes draft in Notion

$cache_folder = pathinfo($this->cache_file, PATHINFO_DIRNAME);
try {
Dir::remove($cache_folder);
} catch (\Throwable $th) {};

Check warning

Code scanning / PHPMD

Design Rules: EmptyCatchBlock Warning

Avoid using empty try-catch blocks in saveCache.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in commit Fix empty catch.

@bastianallgeier
Copy link
Member

This looks very interesting. Give us a bit of time to review this, while we still struggle with the last v5 steps.

@plain-solutions-gmbh
Copy link
Contributor Author

Todo:

  • use F::loadClasses to load classes.
  • remove cache task and move them to tasks parameter

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants