Skip to content

Commit

Permalink
feat(resources): Add route collector for menus
Browse files Browse the repository at this point in the history
  • Loading branch information
zooley committed Aug 8, 2023
1 parent 65fe1cf commit 3c14283
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 2 deletions.
102 changes: 102 additions & 0 deletions app/Modules/Resources/Listeners/RouteCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

namespace App\Modules\Resources\Listeners;

use Illuminate\Events\Dispatcher;
use App\Modules\Resources\Models\Type;
use App\Modules\Resources\Events\AssetDisplaying;
use App\Modules\Menus\Events\CollectingRoutes;

/**
* Resources listener for menu routes
*/
class RouteCollector
{
/**
* Register the listeners for the subscriber.
*
* @param Dispatcher $events
* @return void
*/
public function subscribe(Dispatcher $events): void
{
$events->listen(CollectingRoutes::class, self::class . '@handleCollectingRoutes');
}

/**
* Add module-specific routes
*
* @param CollectingRoutes $event
* @return void
*/
public function handleCollectingRoutes(CollectingRoutes $event): void
{
$module = trans('resources::resources.resources');
$types = Type::all();

foreach ($types as $type)
{
$route = route('site.resources.type.' . $type->alias);
$route = str_replace(request()->root(), '', $route);

$event->addRoute(
$module,
$type->name,
'resources::type.' . $type->alias,
$route
);

$route = route('site.resources.' . $type->alias . '.retired');
$route = str_replace(request()->root(), '', $route);

$event->addRoute(
$module,
$type->name . ': ' . trans('resources::resources.retired'),
'resources::type.' . $type->alias . '.retired',
$route
);

$rows = $type->resources()
->with('facets')
->where('display', '>', 0)
->where(function ($where)
{
$where->whereNotNull('listname')
->where('listname', '!=', '');
})
->whereNotNull('description')
->orderBy('display', 'desc')
->get();

foreach ($rows as $row)
{
$route = route('site.resources.' . $type->alias . '.show', ['name' => $row->listname]);
$route = str_replace(request()->root(), '', $route);

$event->addRoute(
$module,
$row->name,
'resources::asset.' . $row->listname,
$route
);

$section = null;
event($e = new AssetDisplaying($row, $section));
$sections = collect($e->getSections());

foreach ($sections as $sec)
{
$route = $sec['route'];
$route = str_replace(request()->root(), '', $route);

$event->addRoute(
$module,
$sec['name'],
'resources::asset.' . $row->listname . '.' . $sec['name'],
$route
);
}
}
}
}
}
13 changes: 11 additions & 2 deletions app/Modules/Resources/Providers/ResourcesServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
use App\Modules\Resources\Listeners\Queues;
use App\Modules\Resources\Listeners\Subresources;
use App\Modules\Resources\Listeners\Users;
use App\Modules\Resources\Listeners\PageCollector;
use App\Modules\Resources\Listeners\RouteCollector;
use Nwidart\Modules\Facades\Module;

class ResourcesServiceProvider extends ServiceProvider
{
Expand Down Expand Up @@ -44,16 +47,22 @@ public function boot()

$this->app['events']->subscribe(new Subresources);
$this->app['events']->subscribe(new Users);
$this->app['events']->subscribe(new PageCollector);

if (is_dir(dirname(dirname(__DIR__))) . '/Queues')
if (Module::isEnabled('queues'))
{
$this->app['events']->subscribe(new Queues);
}

if (is_dir(dirname(dirname(__DIR__))) . '/Groups')
if (Module::isEnabled('groups'))
{
$this->app['events']->subscribe(new Groups);
}

if (Module::isEnabled('menus'))
{
$this->app['events']->subscribe(new RouteCollector);
}
}

/**
Expand Down

0 comments on commit 3c14283

Please sign in to comment.