Skip to content

Commit

Permalink
fix hydration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Oct 24, 2023
1 parent 1b5dbaf commit 9d9ccbc
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public function hydrate(Closure $callback): static
public function resolveHydrate(Request $request, Model $model, mixed $value): void
{
if (is_null($this->hydrateResolver)) {
$this->hydrateResolver = function () use ($model, $value): void {
$this->hydrateResolver = function (Request $request, Model $model, $value): void {
$model->setAttribute($this->getModelAttribute(), $value);
};
}
Expand Down
31 changes: 26 additions & 5 deletions src/Fields/Slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ class Slug extends Text
*/
protected bool $unique = false;

/**
* Indicates if the slug field is nullable.
*/
protected bool $nullable = false;

/**
* The slug resolver.
*/
Expand All @@ -48,15 +53,23 @@ public function __construct(string $label, string $modelAttribute = null)
$this->unique();
}

/**
* Set the "nullable" property.
*/
public function nullable(bool $value = true): static
{
$this->nullable = $value;

return $this;
}

/**
* {@inheritdoc}
*/
public function persist(Request $request, Model $model, mixed $value): void
{
if ($model->exists || ! empty($value)) {
parent::persist($request, $model, $value);
} else {
$model->saved(function (Model $model) use ($request, $value): void {
if (! $model->exists) {
$model->saved(function (Model $model) use ($request): void {
$value = $this->generate($request, $model);

$this->resolveHydrate($request, $model, $value);
Expand All @@ -66,14 +79,22 @@ public function persist(Request $request, Model $model, mixed $value): void
});
});
}

parent::persist($request, $model, $value);
}

/**
* {@inheritdoc}
*/
public function getValueForHydrate(Request $request): mixed
{
return Str::slug(parent::getValueForHydrate($request), $this->separator);
$value = parent::getValueForHydrate($request);

if (! $this->nullable && empty($value)) {
$value = Str::random();
}

return Str::slug($value, $this->separator);
}

/**
Expand Down
29 changes: 8 additions & 21 deletions src/Http/Controllers/ResourceController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Cone\Root\Http\Controllers;

use Cone\Root\Http\Middleware\AuthorizeResource;
use Cone\Root\Resources\Resource;
use Cone\Root\Support\Alert;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
Expand All @@ -25,10 +26,8 @@ public function __construct()
/**
* Display a listing of the resource.
*/
public function index(Request $request): Response
public function index(Request $request, Resource $resource): Response
{
$resource = $request->route('resource');

if ($resource->getPolicy()) {
$this->authorize('viewAny', $resource->getModel());
}
Expand All @@ -42,10 +41,8 @@ public function index(Request $request): Response
/**
* Show the form for creating a new resource.
*/
public function create(Request $request): Response
public function create(Request $request, Resource $resource): Response
{
$resource = $request->route('resource');

if ($resource->getPolicy()) {
$this->authorize('create', $resource->getModel());
}
Expand All @@ -59,10 +56,8 @@ public function create(Request $request): Response
/**
* Store a newly created resource in storage.
*/
public function store(Request $request): RedirectResponse
public function store(Request $request, Resource $resource): RedirectResponse
{
$resource = $request->route('resource');

if ($resource->getPolicy()) {
$this->authorize('create', $resource->getModel());
}
Expand All @@ -78,10 +73,8 @@ public function store(Request $request): RedirectResponse
/**
* Show the form for editing the specified resource.
*/
public function edit(Request $request, Model $model): Response
public function edit(Request $request, Resource $resource, Model $model): Response
{
$resource = $request->route('resource');

if ($resource->getPolicy()) {
$this->authorize('update', $model);
}
Expand All @@ -95,10 +88,8 @@ public function edit(Request $request, Model $model): Response
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Model $model): RedirectResponse
public function update(Request $request, Resource $resource, Model $model): RedirectResponse
{
$resource = $request->route('resource');

if ($resource->getPolicy()) {
$this->authorize('update', $model);
}
Expand All @@ -112,10 +103,8 @@ public function update(Request $request, Model $model): RedirectResponse
/**
* Remove the specified resource from storage.
*/
public function destroy(Request $request, Model $model): RedirectResponse
public function destroy(Request $request, Resource $resource, Model $model): RedirectResponse
{
$resource = $request->route('resource');

$trashed = in_array(SoftDeletes::class, class_uses_recursive($model)) && $model->trashed();

if ($resource->getPolicy()) {
Expand All @@ -131,10 +120,8 @@ public function destroy(Request $request, Model $model): RedirectResponse
/**
* Restore the specified resource in storage.
*/
public function restore(Request $request, Model $model): RedirectResponse
public function restore(Request $request, Resource $resource, Model $model): RedirectResponse
{
$resource = $request->route('resource');

if ($resource->getPolicy()) {
$this->authorize('restore', $model);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Http\Request;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Router;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Gate;
Expand Down Expand Up @@ -284,6 +285,14 @@ public function registerRoutes(Request $request, Router $router): void
});
}

/**
* Handle the route matched event.
*/
public function routeMatched(RouteMatched $event): void
{
//
}

/**
* Get the instance as an array.
*/
Expand Down
4 changes: 4 additions & 0 deletions src/RootServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ protected function registerRoutes(): void

$root = $this->app->make(Root::class);

$this->app['router']->bind('resource', function (string $key) use ($root): Resource {
return $root->resources->resolve($key);
});

$this->app['router']->bind('resourceModel', function (string $id, Route $route): Model {
return $route->parameter('resource')->resolveRouteBinding($this->app['request'], $id);
});
Expand Down

0 comments on commit 9d9ccbc

Please sign in to comment.