Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Sep 1, 2024
1 parent abd930e commit 4dd0df5
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 71 deletions.
128 changes: 64 additions & 64 deletions composer.lock

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use Cone\Root\Http\Controllers\DashboardController;
use Cone\Root\Http\Controllers\DownloadController;
use Cone\Root\Http\Controllers\OptionController;
use Cone\Root\Http\Controllers\ResourceController;
use Illuminate\Support\Facades\Route;

Expand All @@ -11,6 +12,9 @@
// Download
Route::get('/download/{medium:uuid}', DownloadController::class)->name('download');

// Options
Route::get('/options/{group}', [OptionController::class, 'index'])->name('options.index');

// Resource
Route::get('/{resource}', [ResourceController::class, 'index'])->name('resource.index');
Route::get('/{resource}/create', [ResourceController::class, 'create'])->name('resource.create');
Expand Down
25 changes: 20 additions & 5 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Cone\Root\Exceptions\QueryResolutionException;
use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Fields\Field;
use Cone\Root\Fields\Relation;
use Cone\Root\Http\Controllers\ActionController;
Expand All @@ -23,11 +24,13 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Http\Request;
use Illuminate\Routing\Router;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\MessageBag;
use Illuminate\Support\Str;
use JsonSerializable;
use Symfony\Component\HttpFoundation\Response;
use Throwable;

abstract class Action implements Arrayable, Form, JsonSerializable
{
Expand Down Expand Up @@ -226,12 +229,24 @@ public function handleFormRequest(Request $request, Model $model): void
*/
public function perform(Request $request): Response
{
$this->handleFormRequest($request, $this->resolveQuery($request)->getModel());
try {
DB::beginTransaction();

return Redirect::back()->with(
sprintf('alerts.action-%s', $this->getKey()),
Alert::info(__(':action was successful!', ['action' => $this->getName()]))
);
$this->handleFormRequest($request, $this->resolveQuery($request)->getModel());

return Redirect::back()->with(
sprintf('alerts.action-%s', $this->getKey()),
Alert::info(__(':action was successful!', ['action' => $this->getName()]))
);

DB::commit();

Check failure on line 242 in src/Actions/Action.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Unreachable statement - code above always terminates.
} catch (Throwable $exception) {
report($exception);

DB::rollBack();

throw new SaveFormDataException($exception->getMessage());
}
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Http/Controllers/OptionController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cone\Root\Http\Controllers;

class OptionController extends Controller
{
/**
* Create a new controller instance.
*/
public function __construct()
{
//
}

public function index()
{
//
}
}
18 changes: 18 additions & 0 deletions src/Interfaces/Options/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace Cone\Root\Interfaces\Options;

use Cone\Root\Options\Group;

interface Registry
{
/**
* Get or create a new group.
*/
public function group(string $name): Group;

/**
* Get the option groups.
*/
public function groups(): array;
}
2 changes: 1 addition & 1 deletion src/Models/Option.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected function casts(): array
return [
'value' => match (true) {
is_null($this->key) => 'string',
default => Root::instance()->options->resolveCast($this->key),
default => Root::instance()->options->repository->resolveCast($this->key),
},
];
}
Expand Down
73 changes: 73 additions & 0 deletions src/Options/Group.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Cone\Root\Options;

use Cone\Root\Exceptions\SaveFormDataException;
use Cone\Root\Traits\AsForm;
use Cone\Root\Traits\Authorizable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Throwable;

class Group
{
use AsForm;
use Authorizable;

/**
* Get the key.
*/
public function getKey(): string
{
return Str::of($this->getModel())->classBasename()->plural()->kebab()->value();
}

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

/**
* Get the name.
*/
public function getName(): string
{
return __(Str::of($this->getModel())->classBasename()->headline()->plural()->value());
}

/**
* Make a new Eloquent query instance.
*/
public function query(): Builder
{
return $this->getModelInstance()->newQuery()->with($this->with)->withCount($this->withCount);
}

/**
* Handle the request.
*/
public function handleFormRequest(Request $request): void
{
$this->validateFormRequest($request);

try {
DB::beginTransaction();

$this->resolveFields($request)
->authorized($request)
->visible($request->isMethod('POST') ? 'create' : 'update')
->persist($request, $model);

DB::commit();
} catch (Throwable $exception) {
report($exception);

DB::rollBack();

throw new SaveFormDataException($exception->getMessage());
}
}
}
46 changes: 46 additions & 0 deletions src/Options/Registry.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

namespace Cone\Root\Options;

use Cone\Root\Interfaces\Options\Registry as Contract;
use Cone\Root\Interfaces\Options\Repository;
use Illuminate\Support\Str;

class Registry implements Contract
{
/**
* The repository instance.
*/
public readonly Repository $repository;

/**
* The option groups.
*/
protected array $groups = [];

/**
* Create a new registry instance.
*/
public function __construct(Repository $repository)
{
$this->repository = $repository;
}

/**
* Get or create a new group.
*/
public function group(string $key): Group
{
$this->groups[$key] ??= new Group(Str::headline($key), $key);

return $this->groups[$key];
}

/**
* Get the option groups.
*/
public function groups(): array
{
return $this->groups;
}
}
10 changes: 9 additions & 1 deletion src/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Closure;
use Cone\Root\Interfaces\Breadcrumbs\Registry as Breadcrumbs;
use Cone\Root\Interfaces\Navigation\Registry as Navigation;
use Cone\Root\Interfaces\Options\Repository as Options;
use Cone\Root\Interfaces\Options\Registry as Options;
use Cone\Root\Models\User;
use Cone\Root\Resources\Resources;
use Cone\Root\Widgets\Widgets;
Expand Down Expand Up @@ -115,6 +115,14 @@ public function boot(): void
},
sprintf('%s/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'),
]);

foreach ($this->options->groups() as $group) {
$this->navigation->location('sidebar')->new(
$group->getUri(),
$group->getName(),
['icon' => $this->getIcon(), 'group' => __('Options')],
);
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions src/RootServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class RootServiceProvider extends ServiceProvider
Interfaces\Models\Option::class => Models\Option::class,
Interfaces\Models\User::class => Models\User::class,
Interfaces\Navigation\Registry::class => Navigation\Registry::class,
Interfaces\Options\Registry::class => Options\Registry::class,
Interfaces\Options\Repository::class => Options\Repository::class,
];

Expand Down

0 comments on commit 4dd0df5

Please sign in to comment.