generated from glhd/laravel-package-template
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for resource routes (#3)
* Initial resource route implmentation * Remove some dead code
- Loading branch information
Showing
10 changed files
with
588 additions
and
48 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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,116 @@ | ||
<?php | ||
|
||
namespace Glhd\Gretel\Routing; | ||
|
||
use Closure; | ||
use Glhd\Gretel\Registry; | ||
use Glhd\Gretel\Resolvers\ParentResolver; | ||
use Glhd\Gretel\Resolvers\TitleResolver; | ||
use Glhd\Gretel\Resolvers\UrlResolver; | ||
use Illuminate\Support\Facades\Route; | ||
use Illuminate\Support\Str; | ||
use stdClass; | ||
|
||
class ResourceBreadcrumbs | ||
{ | ||
protected static array $parents = [ | ||
'create' => '.index', | ||
'show' => '.index', | ||
'edit' => '.show', | ||
]; | ||
|
||
protected string $name; | ||
|
||
protected array $options; | ||
|
||
protected array $actions = []; | ||
|
||
public function __construct(string $name, array $options = []) | ||
{ | ||
$this->name = $name; | ||
$this->options = $options; | ||
} | ||
|
||
public function configure(array $config): self | ||
{ | ||
foreach ($config as $action => $title) { | ||
$this->configureAction($action, $title); | ||
} | ||
|
||
return $this; | ||
} | ||
|
||
public function index($title, $parent = null, Closure $relation = null): self | ||
{ | ||
return $this->configureAction('index', $title, $parent, $relation); | ||
} | ||
|
||
public function create($title, $parent = null, Closure $relation = null): self | ||
{ | ||
return $this->configureAction('create', $title, $parent, $relation); | ||
} | ||
|
||
public function show($title, $parent = null, Closure $relation = null): self | ||
{ | ||
return $this->configureAction('show', $title, $parent, $relation); | ||
} | ||
|
||
public function edit($title, $parent = null, Closure $relation = null): self | ||
{ | ||
return $this->configureAction('edit', $title, $parent, $relation); | ||
} | ||
|
||
public function register(Registry $registry): void | ||
{ | ||
$registry->withExceptionHandling(function(Registry $registry) { | ||
foreach ($this->actions as $action => $config) { | ||
$registry->register($this->makeBreadcrumbForAction($action, $config)); | ||
} | ||
}); | ||
} | ||
|
||
protected function configureAction(string $action, $title, $parent = null, ?Closure $relation = null): self | ||
{ | ||
if (null === $parent && isset(static::$parents[$action])) { | ||
$parent = static::$parents[$action]; | ||
} | ||
|
||
$this->actions[$action] = (object) compact('title', 'parent', 'relation'); | ||
|
||
return $this; | ||
} | ||
|
||
protected function makeBreadcrumbForAction(string $action, stdClass $config): RouteBreadcrumb | ||
{ | ||
if (is_string($config->parent)) { | ||
$config->parent = preg_replace_callback('/^\.([a-z_-]+)$/', fn($matches) => $this->getRouteNameForAction($matches[1]), $config->parent); | ||
} | ||
|
||
$name = $this->getRouteNameForAction($action); | ||
$title = TitleResolver::make($config->title); | ||
$parent = ParentResolver::make($config->parent, $name, $config->relation); | ||
$url = UrlResolver::make($name, $this->getParameterNamesForAction($action)); | ||
|
||
return new RouteBreadcrumb($name, $title, $parent, $url); | ||
} | ||
|
||
/** @see \Illuminate\Routing\ResourceRegistrar::getResourceRouteName() */ | ||
protected function getRouteNameForAction(string $action): string | ||
{ | ||
$names = $this->options['names'] ?? []; | ||
$action = $names[$action] ?? "{$this->name}.{$action}"; | ||
|
||
return trim(Route::mergeWithLastGroup(['as' => $action])['as'], '.'); | ||
} | ||
|
||
protected function getParameterNamesForAction(string $action): array | ||
{ | ||
$parameters = []; | ||
|
||
if (in_array($action, ['show', 'edit'])) { | ||
$parameters[] = $this->options['parameters'][$this->name] ?? Str::singular($this->name); | ||
} | ||
|
||
return $parameters; | ||
} | ||
} |
Oops, something went wrong.