Skip to content

Commit

Permalink
Improve Orchid service provider for separate package
Browse files Browse the repository at this point in the history
  • Loading branch information
tabuna committed Oct 22, 2023
1 parent 2851617 commit 9466324
Showing 1 changed file with 132 additions and 7 deletions.
139 changes: 132 additions & 7 deletions src/Platform/OrchidServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,147 @@

namespace Orchid\Platform;

use Illuminate\Contracts\Foundation\CachesRoutes;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Route;
use Orchid\Icons\IconFinder;

/*
* This class represents the Orchid Service Provider.
* It is used to register the menus, permissions and search models to the dashboard.
*/
abstract class OrchidServiceProvider extends ServiceProvider
{
/**
* The Orchid Dashboard instance.
*
* @var \Orchid\Platform\Dashboard|null
*/
protected Dashboard|null $orchid;

/**
* Boot the application events.
*/
public function boot(Dashboard $dashboard): void
{
// Need for backward compatibility
$this->orchid = $dashboard;

$this
->definePermissions()
->defineRoutes()
->defineSearch()
->defineIcons()
->defineMenu();
}

/**
* Get the Orchid Dashboard instance.
*
* @return \Orchid\Platform\Dashboard
*/
private function orchidSingleton(): Dashboard
{
if ($this->orchid === null) {
$this->orchid = $this->app->make(Dashboard::class);
}

return $this->orchid;
}

/**
* Define search functionality for the dashboard.
*
* @return $this
*/
private function defineSearch(): static
{
$this->orchidSingleton()->registerSearch($this->registerSearchModels());

return $this;
}

/**
* Define menu items for the dashboard.
*
* @return $this
*/
private function defineMenu(): static
{
// Register the menu items
View::composer('platform::dashboard', function () use ($dashboard) {
foreach ([...$this->menu(), ...$this->registerMenu(), ...$this->registerMainMenu(), ...$this->registerProfileMenu()] as $element) {
$dashboard->registerMenuElement($element);
View::composer('platform::dashboard', function () {
$elements = [...$this->menu(), ...$this->registerMenu(), ...$this->registerMainMenu(), ...$this->registerProfileMenu()];

foreach ($elements as $element) {
$this->orchidSingleton()->registerMenuElement($element);
}
});

return $this;
}

/**
* Define permissions for the dashboard.
*
* @return $this
*/
private function definePermissions(): static
{
$permissions = [...$this->permissions(), ...$this->registerPermissions()];

// Register the permissions
foreach ([...$this->permissions(), ...$this->registerPermissions()] as $permission) {
$dashboard->registerPermissions($permission);
foreach ($permissions as $permission) {
$this->orchidSingleton()->registerPermissions($permission);
}

return $this;
}

/**
* Define routes for the dashboard.
*
* @return $this
*/
private function defineRoutes(): static
{
if ($this->app instanceof CachesRoutes && $this->app->routesAreCached()) {
return $this;
}

// Register the search models
$dashboard->registerSearch($this->registerSearchModels());
Route::domain((string) config('platform.domain'))
->prefix(Dashboard::prefix('/'))
->middleware(config('platform.middleware.private'))
->group(function (Router $route) {
$this->routes($route);
});

return $this;
}

/**
* Define icon registration for the dashboard.
*
* @return $this
*/
private function defineIcons(): static
{
$iconFinder = $this->app->make(IconFinder::class);

collect($this->icons())->each(fn($path, $prefix) => $iconFinder->registerIconDirectory($prefix, $path));

return $this;
}

/**
* Get the icon paths and prefixes.
*
* @return array
*/
public function icons(): array
{
return [];
}

/**
Expand Down Expand Up @@ -113,4 +226,16 @@ public function registerSearchModels(): array
{
return [];
}

/**
* Define routes setup.
*
* @param \Illuminate\Routing\Router $router
*
* @return void
*/
public function routes(Router $router): void
{
// Define routes.
}
}

0 comments on commit 9466324

Please sign in to comment.