-
Notifications
You must be signed in to change notification settings - Fork 0
Plugin System
Razy uses a shared PluginTrait to add plugin loading capabilities to core classes. Plugins extend the functionality of Template, Collection, Statement, and other components through lazy-loaded PHP closures.
Classes that use PluginTrait gain the loadPlugin() method, which searches a structured plugin directory to load closures on demand.
use Razy\PluginTrait;
class MyComponent {
use PluginTrait;
public function doSomething() {
$plugin = $this->loadPlugin('validator', 'email');
if ($plugin) {
return $plugin($data);
}
}
}| Class | Plugin Types | Location |
| --- | --- | --- |
| Template | Modifiers, Functions, Custom Functions | src/plugins/template/ |
| Statement | Builder plugins | src/plugins/statement/ |
| Collection | Filter plugins | src/plugins/collection/ |
Template modifiers transform output in the rendering pipeline. They are invoked with the pipe | operator in template tags.
src/plugins/template/modifier/
→?→ upper.php
→?→ lower.php
→?→ escape.php
→?→ trim.php
→?→ nl2br.php
→?→ json.php
→?→ base64.php
→?→ md5.php
→?→ number_format.php
→?→ date.php
→?→ default.php
→?→ truncate.php
→?→ url_encode.php
A modifier plugin file returns a closure:
// modifier/upper.php
return function($value): string {
return strtoupper((string) $value);
};Function plugins provide control flow and utility within templates. Called using {@functionName ...} syntax.
src/plugins/template/function/
→?→ if.php
→?→ include.php
→?→ url.php
→?→ asset.php
Builder plugins extend the Statement query builder with named sub-interfaces:
$builder = $statement->builder('search', $searchTerm);
// Adds search-specific WHERE conditionsFilter plugins are invoked through the selector syntax:
// filter/greaterThan.php
return function($value, $threshold): bool {
return $value > (int) $threshold;
};
// Usage: $collection('items.*:greaterThan(price, 100)')Shared plugins are loaded from plugins/ directories in the workspace. Module-specific plugins go in the module's own plugins/ folder:
playground/plugins/
→?→ template/
→ →?→ modifier/
→ →?→ function/
→?→ statement/
→ →?→ builder/
→?→ collection/
→?→ filter/
-
Identify the plugin type (modifier, function, builder, filter)
-
Create a PHP file in the appropriate directory
-
The file must
returnaClosure -
The filename becomes the plugin name (without
.php)
💡 Tip: Plugin files are lazy-loaded — they are only included when first referenced, keeping memory usage minimal.