|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Eclipse\Common\Foundation\Plugins; |
| 4 | + |
| 5 | +use Filament\Panel; |
| 6 | +use Illuminate\Support\Str; |
| 7 | +use ReflectionClass; |
| 8 | + |
| 9 | +abstract class Plugin implements \Filament\Contracts\Plugin |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var string Absolute path to the plugin directory |
| 13 | + */ |
| 14 | + protected string $basePath; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var string Namespace of the plugin (e.g. `\Eclipse\Cms`) |
| 18 | + */ |
| 19 | + protected string $pluginNamespace; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var string ID of the plugin (e.g. `eclipse-cms`) |
| 23 | + */ |
| 24 | + protected string $id; |
| 25 | + |
| 26 | + /** |
| 27 | + * Register any plugin services. |
| 28 | + * |
| 29 | + * @throws \Exception |
| 30 | + */ |
| 31 | + public function register(Panel $panel): void |
| 32 | + { |
| 33 | + $reflection = new ReflectionClass(static::class); |
| 34 | + |
| 35 | + // Auto-detect plugin attributes |
| 36 | + $this->basePath = dirname($reflection->getFileName(), 2); |
| 37 | + $this->pluginNamespace = $reflection->getNamespaceName(); |
| 38 | + $this->id = Str::of($this->pluginNamespace)->replace('\\', '-')->lower(); |
| 39 | + |
| 40 | + $panelName = ucfirst($panel->getId()); |
| 41 | + |
| 42 | + // Discover all classes, even if directories do not exists — Filament already checks and skips those |
| 43 | + $panel->discoverResources($this->getPanelPath($panelName, 'Resources'), $this->getPanelNamespace($panelName, 'Resources')); |
| 44 | + $panel->discoverPages($this->getPanelPath($panelName, 'Pages'), $this->getPanelNamespace($panelName, 'Pages')); |
| 45 | + $panel->discoverClusters($this->getPanelPath($panelName, 'Clusters'), $this->getPanelNamespace($panelName, 'Clusters')); |
| 46 | + $panel->discoverWidgets($this->getPanelPath($panelName, 'Widgets'), $this->getPanelNamespace($panelName, 'Widgets')); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Bootstrap any plugin services. |
| 51 | + */ |
| 52 | + public function boot(Panel $panel): void |
| 53 | + { |
| 54 | + // |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the absolute path to the plugin directory. If the `$path` parameter is provided, it is appended to the path. |
| 59 | + */ |
| 60 | + public function getPath(?string $path = null): string |
| 61 | + { |
| 62 | + return $this->basePath . ($path ? "/$path" : ""); |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Get the absolute path to the panel directory for the specified class. |
| 67 | + * |
| 68 | + * @param string $panelName Panel name (e.g. `Admin`) |
| 69 | + * @param string $classDir Class dir (e.g. `Resource`) |
| 70 | + * @return string Absolute path for the specified class (e.g. `/app/vendor/eclipsephp/cms-plugin/src/Admin/Filament/Resource`) |
| 71 | + */ |
| 72 | + protected function getPanelPath(string $panelName, string $classDir): string |
| 73 | + { |
| 74 | + return $this->getPath('src') ."/$panelName/Filament/$classDir"; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get the full panel namespace for the specified class. |
| 79 | + * |
| 80 | + * @param string $panelName Panel name (e.g. `Admin`) |
| 81 | + * @param string $classDir Class dir (e.g. `Resource`) |
| 82 | + * @return string Full namespace for the specified class (e.g. `Eclipse\Cms\Admin\Resource`) |
| 83 | + */ |
| 84 | + protected function getPanelNamespace(string $panelName, string $classDir): string |
| 85 | + { |
| 86 | + return "$this->pluginNamespace\\$panelName\\Filament\\$classDir"; |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get plugin ID (e.g. `eclipse-cms`) |
| 91 | + */ |
| 92 | + public function getId(): string |
| 93 | + { |
| 94 | + return $this->id; |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * Create plugin instance |
| 99 | + */ |
| 100 | + public static function make(): static |
| 101 | + { |
| 102 | + return app(static::class); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Get plugin instance |
| 107 | + */ |
| 108 | + public static function get(): static |
| 109 | + { |
| 110 | + /** @var static $plugin */ |
| 111 | + $plugin = filament(app(static::class)->getId()); |
| 112 | + |
| 113 | + return $plugin; |
| 114 | + } |
| 115 | +} |
0 commit comments