-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(resources): Add route collector for menus
- Loading branch information
Showing
2 changed files
with
113 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters