|
1 | 1 | <?php |
| 2 | + |
2 | 3 | namespace digitaldiff\diffbase; |
3 | 4 |
|
4 | 5 | use Craft; |
|
9 | 10 | use craft\events\RegisterCpNavItemsEvent; |
10 | 11 | use craft\events\RegisterTemplateRootsEvent; |
11 | 12 | use craft\web\View; |
| 13 | +use Twig\Error\LoaderError; |
| 14 | +use Twig\Error\RuntimeError; |
| 15 | +use Twig\Error\SyntaxError; |
12 | 16 | use yii\base\Event; |
13 | 17 | use digitaldiff\diffbase\models\Settings; |
14 | 18 | use digitaldiff\diffbase\services\ApiService; |
| 19 | +use yii\base\Exception; |
15 | 20 |
|
| 21 | +/** |
| 22 | + * Main plugin class for the "diff. base plugin". |
| 23 | + * |
| 24 | + * This class handles the initialization of the plugin, including: |
| 25 | + * - Registering template paths |
| 26 | + * - Defining custom URL rules for the site and control panel |
| 27 | + * - Adding a custom navigation item to the control panel |
| 28 | + * - Providing a settings model and rendering the settings page |
| 29 | + */ |
16 | 30 | class Plugin extends BasePlugin |
17 | 31 | { |
| 32 | + /** |
| 33 | + * @var string The schema version of the plugin. |
| 34 | + */ |
18 | 35 | public string $schemaVersion = '1.0.0'; |
19 | | -// public bool $hasCpSettings = true; |
20 | | -// public bool $hasCpSection = true; |
21 | 36 |
|
| 37 | + /** |
| 38 | + * @var bool Indicates whether the plugin has a control panel settings page. |
| 39 | + */ |
| 40 | + public bool $hasCpSettings = true; |
| 41 | + |
| 42 | + /** |
| 43 | + * Configures the plugin's components. |
| 44 | + * |
| 45 | + * @return array The configuration array for the plugin's components. |
| 46 | + */ |
22 | 47 | public static function config(): array |
23 | 48 | { |
24 | 49 | return [ |
25 | 50 | 'components' => [ |
26 | | - 'apiService' => ApiService::class, |
| 51 | + 'apiService' => ApiService::class, // Registers the ApiService for business logic |
27 | 52 | ], |
28 | 53 | ]; |
29 | 54 | } |
30 | 55 |
|
31 | | - public function init() |
| 56 | + /** |
| 57 | + * Initializes the plugin. |
| 58 | + * |
| 59 | + * This method is called automatically when the plugin is loaded. It sets up |
| 60 | + * aliases, registers template paths, defines URL rules, and adds a control panel |
| 61 | + * navigation item. |
| 62 | + */ |
| 63 | + public function init(): void |
32 | 64 | { |
33 | 65 | parent::init(); |
34 | 66 |
|
| 67 | + // Set an alias for the plugin's base path |
35 | 68 | Craft::setAlias('@digitaldiff/diffbase', $this->getBasePath()); |
36 | 69 |
|
37 | | - // Template-Pfad registrieren |
| 70 | + // Register the template path for the plugin |
38 | 71 | Event::on( |
39 | 72 | View::class, |
40 | | - View::EVENT_REGISTER_CP_TEMPLATE_ROOTS, |
| 73 | + View::EVENT_REGISTER_SITE_TEMPLATE_ROOTS, |
41 | 74 | function(RegisterTemplateRootsEvent $event) { |
42 | | - $event->roots['_diffbase'] = $this->getBasePath() . '/templates'; |
| 75 | + $event->roots['diffbase'] = __DIR__ . 'src/templates'; // Registers the template directory |
43 | 76 | } |
44 | 77 | ); |
45 | 78 |
|
46 | | - // Site URL-Regeln für API |
| 79 | + // Register site URL rules for the plugin's API |
47 | 80 | Event::on( |
48 | 81 | UrlManager::class, |
49 | 82 | UrlManager::EVENT_REGISTER_SITE_URL_RULES, |
50 | 83 | function (RegisterUrlRulesEvent $event) { |
51 | | - $event->rules['api/info'] = '_diffbase/api/info'; |
| 84 | + $event->rules['api/info'] = 'diffbase/api/info'; // Maps 'api/info' to the ApiController |
52 | 85 | } |
53 | 86 | ); |
54 | 87 |
|
55 | | - // CP URL-Regeln |
| 88 | + // Register control panel URL rules for the plugin |
56 | 89 | Event::on( |
57 | 90 | UrlManager::class, |
58 | 91 | UrlManager::EVENT_REGISTER_CP_URL_RULES, |
59 | 92 | function (RegisterUrlRulesEvent $event) { |
60 | | - $event->rules['_diffbase'] = '_diffbase/cp/index'; |
61 | | - $event->rules['_diffbase/settings'] = '_diffbase/cp/settings'; |
| 93 | + $event->rules['diffbase'] = 'diffbase/cp/index'; // Maps 'diffbase' to CpController::actionIndex |
| 94 | + $event->rules['diffbase/test'] = 'diffbase/cp/test'; // Maps 'diffbase/test' to CpController::actionTest |
| 95 | + $event->rules['diffbase/<action:\w+>'] = 'diffbase/cp/<action>'; // Maps dynamic actions |
62 | 96 | } |
63 | 97 | ); |
64 | 98 |
|
| 99 | + // Add a custom navigation item to the control panel |
65 | 100 | Event::on( |
66 | 101 | Cp::class, |
67 | 102 | Cp::EVENT_REGISTER_CP_NAV_ITEMS, |
68 | | - function (RegisterCpNavItemsEvent $event) { |
| 103 | + function(RegisterCpNavItemsEvent $event) { |
69 | 104 | $event->navItems[] = [ |
70 | | - 'url' => '_diffbase', |
71 | | - 'label' => 'diff. base plugin', |
72 | | - 'icon' => '@digitaldiff/diffbase/icon-mask.svg', |
73 | | - 'weight' => 2, // Position im Hauptmenü |
| 105 | + 'label' => 'diff. base plugin', // Label for the navigation item |
| 106 | + 'url' => 'diffbase', // URL for the navigation item |
| 107 | + 'icon' => '@digitaldiff/diffbase/icon-mask.svg', // Icon for the navigation item |
| 108 | + 'weight' => 3, // Position in the navigation menu |
74 | 109 | ]; |
75 | 110 | } |
76 | 111 | ); |
77 | | - |
78 | | -/* // CP Navigation |
79 | | - Event::on( |
80 | | - Cp::class, |
81 | | - Cp::EVENT_REGISTER_CP_NAV_ITEMS, |
82 | | - function (RegisterCpNavItemsEvent $event) { |
83 | | - foreach ($event->navItems as &$item) { |
84 | | - if ($item['url'] === 'settings') { |
85 | | - $item['subnav']['_diffbase'] = [ |
86 | | - 'label' => 'DiffBase', |
87 | | - 'url' => '_diffbase', |
88 | | - 'selected' => Craft::$app->getRequest()->getSegment(1) === '_diffbase' |
89 | | - ]; |
90 | | - break; |
91 | | - } |
92 | | - } |
93 | | - } |
94 | | - );*/ |
95 | 112 | } |
96 | 113 |
|
| 114 | + /** |
| 115 | + * Creates the settings model for the plugin. |
| 116 | + * |
| 117 | + * @return Settings The settings model instance. |
| 118 | + */ |
97 | 119 | protected function createSettingsModel(): Settings |
98 | 120 | { |
99 | 121 | return new Settings(); |
100 | 122 | } |
101 | 123 |
|
| 124 | + /** |
| 125 | + * Renders the settings page for the plugin. |
| 126 | + * |
| 127 | + * @return string|null The rendered HTML for the settings page. |
| 128 | + * |
| 129 | + * @throws SyntaxError If there is a syntax error in the Twig template. |
| 130 | + * @throws Exception If an error occurs during rendering. |
| 131 | + * @throws RuntimeError If a runtime error occurs in the Twig template. |
| 132 | + * @throws LoaderError If the Twig template cannot be loaded. |
| 133 | + */ |
102 | 134 | protected function settingsHtml(): ?string |
103 | 135 | { |
104 | | - return Craft::$app->getView()->renderTemplate('_diffbase/settings', [ |
105 | | - 'plugin' => $this, |
106 | | - 'settings' => $this->getSettings(), |
| 136 | + return Craft::$app->getView()->renderTemplate('diffbase/settings', [ |
| 137 | + 'plugin' => $this, // Passes the plugin instance to the template |
| 138 | + 'settings' => $this->getSettings(), // Passes the settings model to the template |
107 | 139 | ]); |
108 | 140 | } |
109 | 141 | } |
0 commit comments