|
| 1 | +# Pablo Technical Documentation |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Pablo is a modular PHP web application framework that uses a plugin-based architecture. It requires PHP 8.4 or higher and follows modern PHP practices including strict types and namespacing. |
| 6 | + |
| 7 | +## Core Architecture |
| 8 | + |
| 9 | +### Entry Point (public/index.php) |
| 10 | + |
| 11 | +The application bootstraps through `public/index.php`, which: |
| 12 | + |
| 13 | +1. Defines the ROOT constant |
| 14 | +2. Loads the Composer autoloader |
| 15 | +3. Initializes configuration |
| 16 | +4. Creates and runs the main application instance |
| 17 | + |
| 18 | +### Initialization Process |
| 19 | + |
| 20 | +The `Init` class (`src/Core/Init.php`) handles the application bootstrap sequence: |
| 21 | + |
| 22 | +1. Environment validation |
| 23 | +2. Session initialization |
| 24 | +3. Configuration setup |
| 25 | +4. Plugin navigation initialization |
| 26 | +5. Theme initialization |
| 27 | +6. Plugin execution |
| 28 | + |
| 29 | +## Plugin System |
| 30 | + |
| 31 | +### Plugin Structure |
| 32 | + |
| 33 | +Each plugin resides in its own directory under `src/Plugins/` and consists of: |
| 34 | + |
| 35 | +- `Plugin.php`: The main plugin class |
| 36 | +- `meta.json`: Plugin configuration and metadata |
| 37 | + |
| 38 | +### Plugin Base Class |
| 39 | + |
| 40 | +All plugins extend the abstract `Plugin` class (`src/Core/Plugin.php`) which: |
| 41 | + |
| 42 | +- Implements the `PluginInterface` |
| 43 | +- Provides access to the theme instance |
| 44 | +- Requires implementation of the `execute()` method |
| 45 | +- Provides string conversion through `__toString()` |
| 46 | + |
| 47 | +### Plugin Interface |
| 48 | + |
| 49 | +The `PluginInterface` (`src/Interfaces/PluginInterface.php`) defines two methods: |
| 50 | + |
| 51 | +- `execute(): mixed` - Main plugin logic |
| 52 | +- `__toString(): string` - String representation |
| 53 | + |
| 54 | +### Plugin Discovery |
| 55 | + |
| 56 | +The `PluginScanner` class handles plugin discovery: |
| 57 | + |
| 58 | +1. Scans the plugins directory |
| 59 | +2. Reads each plugin's meta.json |
| 60 | +3. Creates a navigation structure with: |
| 61 | + - Plugin name |
| 62 | + - URL (based on plugin directory name) |
| 63 | + - Icon |
| 64 | + - Display order |
| 65 | + |
| 66 | +### Plugin Metadata (meta.json) |
| 67 | + |
| 68 | +Each plugin includes a meta.json file with: |
| 69 | + |
| 70 | +```json |
| 71 | +{ |
| 72 | + "name": "Plugin Name", |
| 73 | + "description": "Plugin description", |
| 74 | + "icon": "Bootstrap icon class", |
| 75 | + "order": "Navigation display order" |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +### Plugin Invocation Process |
| 80 | + |
| 81 | +1. **URL Routing** |
| 82 | + |
| 83 | + - Plugins are invoked via the `plugin` URL parameter |
| 84 | + - Default plugin is "Home" if no plugin parameter is specified |
| 85 | + |
| 86 | +2. **Plugin Loading** |
| 87 | + |
| 88 | + ```php |
| 89 | + $pluginName = ucfirst(strtolower($this->config->in['plugin'] ?? 'Home')); |
| 90 | + $pluginClass = "Markc\\Pablo\\Plugins\\{$pluginName}\\Plugin"; |
| 91 | + ``` |
| 92 | + |
| 93 | +3. **Instantiation** |
| 94 | + |
| 95 | + - Plugin class is instantiated with Theme instance |
| 96 | + - Validates plugin implements PluginInterface |
| 97 | + |
| 98 | + ```php |
| 99 | + $plugin = new $pluginClass($this->theme); |
| 100 | + ``` |
| 101 | + |
| 102 | +4. **Execution** |
| 103 | + - Plugin's execute() method is called via \_\_toString() |
| 104 | + - Output is stored in config->out['main'] |
| 105 | + ```php |
| 106 | + $this->config->out['main'] = (string)$plugin; |
| 107 | + ``` |
| 108 | + |
| 109 | +### Creating a New Plugin |
| 110 | + |
| 111 | +1. Create directory: `src/Plugins/YourPlugin/` |
| 112 | + |
| 113 | +2. Create meta.json: |
| 114 | + |
| 115 | +```json |
| 116 | +{ |
| 117 | + "name": "Your Plugin", |
| 118 | + "description": "Plugin description", |
| 119 | + "icon": "bi bi-your-icon fw", |
| 120 | + "order": 1 |
| 121 | +} |
| 122 | +``` |
| 123 | + |
| 124 | +3. Create Plugin.php: |
| 125 | + |
| 126 | +```php |
| 127 | +<?php |
| 128 | +declare(strict_types=1); |
| 129 | + |
| 130 | +namespace Markc\Pablo\Plugins\YourPlugin; |
| 131 | + |
| 132 | +use Markc\Pablo\Core\Plugin as BasePlugin; |
| 133 | + |
| 134 | +class Plugin extends BasePlugin |
| 135 | +{ |
| 136 | + public function execute(): mixed |
| 137 | + { |
| 138 | + // Your plugin logic here |
| 139 | + return "Plugin output"; |
| 140 | + } |
| 141 | +} |
| 142 | +``` |
| 143 | + |
| 144 | +### Plugin Features |
| 145 | + |
| 146 | +1. **Theme Access** |
| 147 | + |
| 148 | + - Plugins have access to the theme instance via `$this->theme` |
| 149 | + - Enables consistent styling and layout |
| 150 | + |
| 151 | +2. **Output Formats** |
| 152 | + |
| 153 | + - HTML (default) |
| 154 | + - Text (strips HTML tags) |
| 155 | + - JSON |
| 156 | + - Partial (specific sections) |
| 157 | + |
| 158 | +3. **API Support** |
| 159 | + |
| 160 | + - Plugins can handle API requests |
| 161 | + - CSRF protection for API endpoints |
| 162 | + - JSON response formatting |
| 163 | + |
| 164 | +4. **Debug Mode** |
| 165 | + - Detailed error reporting when APP_DEBUG is enabled |
| 166 | + - Request logging |
| 167 | + - Performance metrics |
| 168 | + |
| 169 | +## Security Features |
| 170 | + |
| 171 | +1. **Session Security** |
| 172 | + |
| 173 | + - HTTP-only cookies |
| 174 | + - Strict same-site policy |
| 175 | + - CSRF token generation and validation |
| 176 | + |
| 177 | +2. **Input Sanitization** |
| 178 | + |
| 179 | + - Configuration sanitizes input |
| 180 | + - Type declarations enforce data types |
| 181 | + |
| 182 | +3. **Error Handling** |
| 183 | + - Production-safe error messages |
| 184 | + - Detailed debugging in development |
| 185 | + |
| 186 | +## Best Practices |
| 187 | + |
| 188 | +1. **Plugin Development** |
| 189 | + |
| 190 | + - Use strict types |
| 191 | + - Implement proper error handling |
| 192 | + - Follow PSR standards |
| 193 | + - Document your code |
| 194 | + - Include meaningful meta.json |
| 195 | + |
| 196 | +2. **Security** |
| 197 | + |
| 198 | + - Validate all input |
| 199 | + - Use CSRF tokens for forms/API |
| 200 | + - Sanitize output |
| 201 | + - Follow least privilege principle |
| 202 | + |
| 203 | +3. **Performance** |
| 204 | + - Keep plugins focused and lightweight |
| 205 | + - Cache when appropriate |
| 206 | + - Optimize database queries |
| 207 | + - Minimize dependencies |
0 commit comments