Skip to content

Commit

Permalink
bring back (and simplify) routing
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Oct 24, 2023
1 parent 8ea4350 commit 1b5dbaf
Show file tree
Hide file tree
Showing 22 changed files with 405 additions and 264 deletions.
7 changes: 0 additions & 7 deletions routes/api.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
<?php

use Cone\Root\Http\Controllers\NotificationsController;
use Cone\Root\Http\Controllers\ResourceFieldController;
use Illuminate\Support\Facades\Route;

// Notifications
Route::apiResource('notifications', NotificationsController::class)->except(['store']);

// Resource Fields
Route::any('/{resource}/fields/{field}', ResourceFieldController::class)->where('field', '.*');

// Actions
// Action Fields
52 changes: 27 additions & 25 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
namespace Cone\Root\Actions;

use Cone\Root\Fields\Field;
use Cone\Root\Http\Controllers\ActionController;
use Cone\Root\Interfaces\Form;
use Cone\Root\Support\Alert;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\Makeable;
use Cone\Root\Traits\RegistersRoutes;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Concerns\HasAttributes;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;
Expand All @@ -25,6 +28,9 @@ abstract class Action implements Arrayable, Form, JsonSerializable
use Authorizable;
use HasAttributes;
use Makeable;
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}

/**
* The Blade template.
Expand All @@ -46,11 +52,6 @@ abstract class Action implements Arrayable, Form, JsonSerializable
*/
protected ?Builder $query = null;

/**
* The API URI.
*/
protected ?string $apiUri = null;

/**
* Handle the action.
*/
Expand Down Expand Up @@ -96,24 +97,6 @@ public function getTemplate(): string
return $this->template;
}

/**
* Set the API URI.
*/
public function setApiUri(string $apiUri): static
{
$this->apiUri = $apiUri;

return $this;
}

/**
* Get the API URI.
*/
public function getApiUri(): ?string
{
return $this->apiUri;
}

/**
* Set the Eloquent query.
*/
Expand All @@ -129,7 +112,6 @@ public function setQuery(Builder $query): static
*/
protected function resolveField(Request $request, Field $field): void
{
$field->setApiUri(sprintf('/%s/fields/%s', $this->getApiUri(), $field->getUriKey()));
$field->setAttribute('form', $this->getKey());
$field->resolveErrorsUsing(function (Request $request): MessageBag {
return $this->errors($request);
Expand Down Expand Up @@ -190,6 +172,26 @@ public function perform(Request $request): Response
);
}

/**
* Register the routes using the given router.
*/
public function registerRoutes(Request $request, Router $router): void
{
$this->__registerRoutes($request, $router);

$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->resolveFields($request)->registerRoutes($request, $router);
});
}

/**
* The routes that should be registered.
*/
public function routes(Router $router): void
{
$router->post('/', ActionController::class);
}

/**
* Convert the element to a JSON serializable format.
*/
Expand All @@ -210,7 +212,7 @@ public function toArray(): array
'modalKey' => $this->getModalKey(),
'name' => $this->getName(),
'template' => $this->getTemplate(),
'url' => $this->getApiUri(),
'url' => $this->getUri(),
];
}

Expand Down
16 changes: 16 additions & 0 deletions src/Actions/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace Cone\Root\Actions;

use Cone\Root\Traits\RegistersRoutes;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

Expand All @@ -27,4 +29,18 @@ public function mapToTableComponents(Request $request): array
{
return $this->map->toTableComponent($request)->all();
}

/**
* Register the action routes.
*/
public function registerRoutes(Request $request, Router $router): void
{
$router->prefix('actions')->group(function (Router $router) use ($request): void {
$this->each(static function (Action $action) use ($request, $router): void {
if (in_array(RegistersRoutes::class, class_uses_recursive($action))) {
$action->registerRoutes($request, $router);
}
});
});
}
}
6 changes: 2 additions & 4 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,8 @@ public function getRelation(Model $model): EloquentRelation
*/
protected function resolveField(Request $request, Field $field): void
{
if (! is_null($this->apiUri)) {
$field->setApiUri(sprintf('%s/%s', $this->apiUri, $field->getUriKey()));
}

$field->setAttribute('form', $this->getAttribute('form'));
$field->resolveErrorsUsing($this->errorsResolver);
$field->setModelAttribute(
sprintf('%s.*.%s', $this->getModelAttribute(), $field->getModelAttribute())
);
Expand Down
31 changes: 22 additions & 9 deletions src/Fields/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@

use Closure;
use Cone\Root\Models\Medium;
use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesFields;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphToMany;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\Config;

class Editor extends Field
{
use RegistersRoutes {
RegistersRoutes::registerRoutes as __registerRoutes;
}
use ResolvesFields;

/**
Expand Down Expand Up @@ -41,17 +46,11 @@ public function __construct(string $label, string $modelAttribute = null)
}

/**
* {@inheritdoc}
* Get the URI key.
*/
public function setApiUri(string $apiUri): static
public function getUriKey(): string
{
if (! is_null($this->media)) {
$this->media->setApiUri(
sprintf('%s/%s', $apiUri, $this->media->getUriKey())
);
}

return parent::setApiUri($apiUri);
return str_replace('.', '-', $this->getRequestKey());
}

/**
Expand Down Expand Up @@ -142,6 +141,20 @@ public function __construct(string $modelAttribute)
};
}

/**
* Register the routes using the given router.
*/
public function registerRoutes(Request $request, Router $router): void
{
$this->__registerRoutes($request, $router);

if (! is_null($this->media)) {
$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->media->registerRoutes($request, $router);
});
}
}

/**
* {@inheritdoc}
*/
Expand Down
40 changes: 0 additions & 40 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Contracts\Support\MessageBag;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
Expand Down Expand Up @@ -83,11 +82,6 @@ abstract class Field implements Arrayable, JsonSerializable
*/
protected bool $withOldValue = true;

/**
* The API URI.
*/
protected ?string $apiUri = null;

/**
* Indicates if the field has been hydrated.
*/
Expand Down Expand Up @@ -148,40 +142,6 @@ public function getValidationKey(): string
return $this->getRequestKey();
}

/**
* Get the URI key.
*/
public function getUriKey(): string
{
return str_replace('.', '-', $this->getRequestKey());
}

/**
* Set the API URI.
*/
public function setApiUri(string $apiUri): static
{
$this->apiUri = $apiUri;

return $this;
}

/**
* Get the API URI.
*/
public function getApiUri(): ?string
{
return $this->apiUri;
}

/**
* Handle the incoming API request.
*/
public function handleApiRequest(Request $request, Model $model): JsonResponse
{
return new JsonResponse($this->toArray());
}

/**
* Set the label attribute.
*/
Expand Down
16 changes: 16 additions & 0 deletions src/Fields/Fields.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

namespace Cone\Root\Fields;

use Cone\Root\Traits\RegistersRoutes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

Expand Down Expand Up @@ -50,4 +52,18 @@ public function mapToFormComponents(Request $request, Model $model): array
{
return $this->map->toFormComponent($request, $model)->all();
}

/**
* Register the field routes.
*/
public function registerRoutes(Request $request, Router $router): void
{
$router->prefix('fields')->group(function (Router $router) use ($request): void {
$this->each(static function (Field $field) use ($request, $router): void {
if (in_array(RegistersRoutes::class, class_uses_recursive($field))) {
$field->registerRoutes($request, $router);
}
});
});
}
}
28 changes: 24 additions & 4 deletions src/Fields/Fieldset.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,48 @@

namespace Cone\Root\Fields;

use Cone\Root\Traits\RegistersRoutes;
use Cone\Root\Traits\ResolvesFields;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\App;

class Fieldset extends Field
{
use RegistersRoutes;
use ResolvesFields;

/**
* The Blade template.
*/
protected string $template = 'root::fields.fieldset';

/**
* Get the URI key.
*/
public function getUriKey(): string
{
return str_replace('.', '-', $this->getRequestKey());
}

/**
* Handle the callback for the field resolution.
*/
protected function resolveField(Request $request, Field $field): void
{
if (! is_null($this->apiUri)) {
$field->setApiUri(sprintf('%s/%s', $this->apiUri, $field->getUriKey()));
}
$field->setAttribute('form', $this->getAttribute('form'));
$field->resolveErrorsUsing($this->errorsResolver);
}

/**
* Register the routes using the given router.
*/
public function registerRoutes(Request $request, Router $router): void
{
$router->prefix($this->getUriKey())->group(function (Router $router) use ($request): void {
$this->resolveFields($request)->registerRoutes($request, $router);
});
}

/**
Expand Down Expand Up @@ -77,7 +97,7 @@ public function toValidate(Request $request, Model $model): array
{
return array_merge(
parent::toValidate($request, $model),
$this->resolveFields($request)->mapToValidate($request)
$this->resolveFields($request)->mapToValidate($request, $model)
);
}
}
Loading

0 comments on commit 1b5dbaf

Please sign in to comment.