Skip to content

Commit

Permalink
Merge pull request #125 from conedevelopment/app-manager
Browse files Browse the repository at this point in the history
App manager
  • Loading branch information
iamgergo authored Mar 21, 2023
2 parents 0b49011 + 05910b6 commit 6e9e4c5
Show file tree
Hide file tree
Showing 27 changed files with 281 additions and 247 deletions.
4 changes: 2 additions & 2 deletions resources/views/app.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;700&amp;display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Roboto+Mono:wght@400;700&amp;display=swap" rel="stylesheet">
<link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;700&amp;family=IBM+Plex+Sans:wght@400;700&amp;family=Inter:wght@400;500;600;700;800&amp;display=swap" rel="stylesheet">
@foreach(Cone\Root\Support\Facades\Asset::styles() as $key => $style)
@foreach(App::make('root')->assets->styles() as $key => $style)
<link id="style-{{ $key }}" href="{{ $style->getUrl() }}" rel="stylesheet">
@endforeach

Expand All @@ -35,7 +35,7 @@
->useBuildDirectory('vendor/root/build')
->useHotFile(public_path('vendor/root/hot'))
}}
@foreach(Cone\Root\Support\Facades\Asset::scripts() as $key => $script)
@foreach(App::make('root')->assets->scripts() as $key => $script)
<script id="script-{{ $key }}" src="{{ $script->getUrl() }}"></script>
@endforeach
</body>
Expand Down
2 changes: 1 addition & 1 deletion resources/views/icons.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@
<symbol id="icon-horizontal-rule" viewBox="0 0 24 24">
<path d="M4 13v-2h16v2Z" style="fill:currentColor;"/>
</symbol>
@foreach(Cone\Root\Support\Facades\Asset::icons() as $icon)
@foreach(App::make('root')->assets->icons() as $icon)
<symbol id="icon-{{ $icon->getKey() }}" viewBox="0 0 24 24">
@include($icon->getPath())
</symbol>
Expand Down
11 changes: 11 additions & 0 deletions src/Enums/AlertType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

namespace Cone\Root\Enums;

enum AlertType: string
{
case Info = 'info';
case Success = 'success';
case Error = 'danger';
case Warning = 'warning';
}
10 changes: 10 additions & 0 deletions src/Enums/AssetType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Cone\Root\Enums;

enum AssetType: string
{
case Script = 'script';
case Style = 'style';
case Icon = 'icon';
}
2 changes: 1 addition & 1 deletion src/Fields/Meta.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class Meta extends MorphMany
/**
* Create a new relation field instance.
*/
public function __construct(string $label, string $name = 'metas')
public function __construct(string $label, string $name = 'metaData')
{
parent::__construct($label, $name);
}
Expand Down
7 changes: 3 additions & 4 deletions src/Http/Controllers/DashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

namespace Cone\Root\Http\Controllers;

use Cone\Root\Http\Requests\RootRequest;
use Illuminate\Support\Facades\App;
use Cone\Root\Root;
use Inertia\Inertia;
use Inertia\Response;

Expand All @@ -12,11 +11,11 @@ class DashboardController extends Controller
/**
* Handle the incoming request.
*/
public function __invoke(RootRequest $request): Response
public function __invoke(Root $root): Response
{
return Inertia::render('Dashboard', [
'title' => __('Dashboard'),
'widgets' => App::make('root.widgets')->available($request)->toArray(),
'widgets' => $root->widgets->available($root->request())->toArray(),
]);
}
}
5 changes: 3 additions & 2 deletions src/Http/Requests/ResourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
namespace Cone\Root\Http\Requests;

use Cone\Root\Resources\Resource;
use Cone\Root\Support\Facades\Resource as Registry;
use Cone\Root\Root;
use Illuminate\Support\Facades\App;

class ResourceRequest extends RootRequest
{
Expand All @@ -12,6 +13,6 @@ class ResourceRequest extends RootRequest
*/
public function resource(): Resource
{
return Registry::resolve($this->route()->action['resource']);
return App::make(Root::class)->resources->resolve($this->route()->action['resource']);
}
}
61 changes: 27 additions & 34 deletions src/Resources/Resource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Cone\Root\Resources;

use Closure;
use Cone\Root\Actions\Action;
use Cone\Root\Extracts\Extract;
use Cone\Root\Fields\Field;
Expand Down Expand Up @@ -104,7 +103,7 @@ public function getRouteKeyName(): string
*/
public function getUri(): string
{
return Str::start(sprintf('%s/%s', Root::getPath(), $this->getKey()), '/');
return Str::start(sprintf('%s/%s', App::make(Root::class)->getPath(), $this->getKey()), '/');
}

/**
Expand Down Expand Up @@ -371,7 +370,7 @@ public function isSoftDeletable(): bool
public function resolveBreadcrumbs(RootRequest $request): array
{
$breadcrumbs = [
Root::getPath() => __('Dashboard'),
App::make(Root::class)->getPath() => __('Dashboard'),
$this->getUri() => $this->getName(),
];

Expand Down Expand Up @@ -499,17 +498,17 @@ public function toEdit(UpdateRequest $request, Model $model): array
/**
* Handle the resource registered event.
*/
public function registered(RootRequest $request): void
public function boot(Root $root): void
{
$this->registerRoutes($request);
$this->registerRoutes($root);

App::make('router')->bind($this->getRouteKeyName(), function (string $id): Model {
$root->app->make('router')->bind($this->getRouteKeyName(), function (string $id) use ($root): Model {
return $id === 'create'
? $this->getModelInstance()
: $this->resolveRouteBinding(App::make(ResourceRequest::class), $id);
: $this->resolveRouteBinding($root->app->make(ResourceRequest::class), $id);
});

App::make('router')->pattern(
$root->app->make('router')->pattern(
$this->getRouteKeyName(),
'[0-9]+|[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}|create'
);
Expand All @@ -518,22 +517,26 @@ public function registered(RootRequest $request): void
/**
* Register the routes for the resource.
*/
protected function registerRoutes(RootRequest $request): void
{
$this->routeGroup(function (Router $router) use ($request): void {
if (! App::routesAreCached()) {
$router->as(sprintf('%s.', $this->getKey()))->group(function (Router $router): void {
$this->routes($router);
});
}

$this->resolveExtracts($request)->registerRoutes($request, $router);
$this->resolveActions($request)->registerRoutes($request, $router);
$this->resolveWidgets($request)->registerRoutes($request, $router);

$router->prefix("{{$this->getRouteKeyName()}}")->group(function ($router) use ($request) {
$this->resolveFields($request)->registerRoutes($request, $router);
});
protected function registerRoutes(Root $root): void
{
$root->routes(function (Router $router) use ($root): void {
$router->group(
['prefix' => $this->getKey(), 'resource' => $this->getKey()],
function (Router $router) use ($root): void {
if (! $root->app->routesAreCached()) {
$router->as(sprintf('%s.', $this->getKey()))->group(function (Router $router): void {
$this->routes($router);
});
}

$this->resolveExtracts($root->request())->registerRoutes($root->request(), $router);
$this->resolveActions($root->request())->registerRoutes($root->request(), $router);
$this->resolveWidgets($root->request())->registerRoutes($root->request(), $router);
$router->prefix("{{$this->getRouteKeyName()}}")->group(function ($router) use ($root) {
$this->resolveFields($root->request())->registerRoutes($root->request(), $router);
});
}
);
});
}

Expand All @@ -554,14 +557,4 @@ public function routes(Router $router): void
$router->post("{{$this->getRouteKeyName()}}/restore", [ResourceController::class, 'restore'])->name('restore');
}
}

/**
* Wrap the given routes into the route group.
*/
public function routeGroup(Closure $callback): void
{
Root::routes(function (Router $router) use ($callback): void {
$router->group(['prefix' => $this->getKey(), 'resource' => $this->getKey()], $callback);
});
}
}
117 changes: 92 additions & 25 deletions src/Root.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,149 @@
namespace Cone\Root;

use Closure;
use Cone\Root\Http\Requests\RootRequest;
use Cone\Root\Support\Collections\Assets;
use Cone\Root\Support\Collections\Resources;
use Cone\Root\Support\Collections\Widgets;
use Illuminate\Contracts\Foundation\Application;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Route;
use Illuminate\Support\Str;

abstract class Root
class Root
{
/**
* The package version.
*
* @var string
*/
public const VERSION = '1.1.3';
public const VERSION = '1.2.0';

/**
* The registered callbacks.
* The registered booting callbacks.
*/
protected static array $callbacks = [];
protected array $booting = [];

/**
* Determine if Root should run on the given request.
* The registered booted callbacks.
*/
public static function shouldRun(Request $request): bool
{
$host = empty(static::getDomain())
? parse_url(Config::get('app.url'), PHP_URL_HOST)
: static::getDomain();
protected array $booted = [];

$segments = explode('/', $request->getRequestUri());
/**
* The Application instance.
*/
public readonly Application $app;

/**
* The resources collection.
*/
public readonly Resources $resources;

/**
* The widgets collection.
*/
public readonly Widgets $widgets;

/**
* The assets collection.
*/
public readonly Assets $assets;

return (empty(static::getDomain()) || $request->getHost() === $host)
&& (static::getPath() === '/' || $segments[1] === trim(static::getPath(), '/'));
/**
* Create a new Root instance.
*/
public function __construct(Application $app)
{
$this->app = $app;
$this->resources = new Resources();
$this->widgets = new Widgets();
$this->assets = new Assets();
}

/**
* Run Root and call the registered callbacks.
* Boot the Root application.
*/
public static function run(Request $request): void
public function boot(): void
{
foreach (static::$callbacks as $callback) {
call_user_func_array($callback, [$request]);
foreach ($this->booting as $callback) {
call_user_func_array($callback, [$this]);
}

$this->resources->each->boot($this);

foreach ($this->booted as $callback) {
call_user_func_array($callback, [$this]);
}
}

/**
* Register a callback when Root is running.
* Register a booting callback.
*/
public static function running(Closure $callback): void
public function booting(Closure $callback): void
{
static::$callbacks[] = $callback;
$this->booting[] = $callback;
}

/**
* Get the Root Request instance.
*/
public function request(): RootRequest
{
static $request;

$request = RootRequest::createFrom($this->app['request']);

return $request;
}

/**
* Register a booted callback.
*/
public function booted(Closure $callback): void
{
$this->booted[] = $callback;
}

/**
* Determine if Root should run on the given request.
*/
public function shouldRun(Request $request): bool
{
$host = empty($this->getDomain())
? parse_url(Config::get('app.url'), PHP_URL_HOST)
: $this->getDomain();

$segments = explode('/', $request->getRequestUri());

return (empty($this->getDomain()) || $request->getHost() === $host)
&& ($this->getPath() === '/' || $segments[1] === trim($this->getPath(), '/'));
}

/**
* Register the root routes.
*/
public static function routes(Closure $callback): void
public function routes(Closure $callback): void
{
Route::as('root.')
->domain(static::getDomain())
->prefix(static::getPath())
->domain($this->getDomain())
->prefix($this->getPath())
->middleware(['root'])
->group($callback);
}

/**
* Get the Root URI path.
*/
public static function getPath(): string
public function getPath(): string
{
return Str::start(Config::get('root.path', 'root'), '/');
}

/**
* Get the Root domain.
*/
public static function getDomain(): string
public function getDomain(): string
{
return (string) Config::get('root.domain', null);
}
Expand Down
Loading

0 comments on commit 6e9e4c5

Please sign in to comment.