Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
iamgergo committed Sep 21, 2023
1 parent 689099a commit cb3f7c6
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 213 deletions.
47 changes: 0 additions & 47 deletions src/Console/Commands/RelationMake.php

This file was deleted.

15 changes: 0 additions & 15 deletions src/Console/Commands/WidgetMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,27 +56,13 @@ protected function buildClass($name): string
{
$class = parent::buildClass($name);

$class = $this->replaceAsync($class);

$class = $this->replaceTemplate($class);

$this->makeView();

return $class;
}

/**
* Replace the async related code.
*/
protected function replaceAsync(string $class): string
{
if ($this->option('async')) {
$class = str_replace([PHP_EOL.'%%async%%', '%%/async%%'], '', $class);
}

return preg_replace('/\n%%async%%.*%%\/async%%/s', '', $class);
}

/**
* Replace the template related code.
*/
Expand Down Expand Up @@ -125,7 +111,6 @@ protected function getView(): string
protected function getOptions(): array
{
return [
['async', null, InputOption::VALUE_NONE, 'Mark the widget as async'],
['force', 'f', InputOption::VALUE_NONE, 'Create the class even if the component already exists'],
['template', null, InputOption::VALUE_OPTIONAL, 'The Blade template'],
];
Expand Down
8 changes: 8 additions & 0 deletions src/Widgets/Metric.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Cone\Root\Widgets;

abstract class Metric extends Widget
{
//
}
106 changes: 9 additions & 97 deletions src/Widgets/Widget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,21 @@

namespace Cone\Root\Widgets;

use Closure;
use Cone\Root\Support\Element;
use Cone\Root\Traits\Authorizable;
use Cone\Root\Traits\Makeable;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Contracts\View\View;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Str;
use Stringable;

abstract class Widget implements Htmlable, Stringable
abstract class Widget extends Element
{
use Authorizable;
use Makeable;

/**
* Indicates if the component is async.
*/
protected bool $async = false;

/**
* The blade component.
* The Blade template.
*/
protected string $template = 'root::widgets.widget';

/**
* The data resolver callback.
*/
protected ?Closure $dataResolver = null;

/**
* Get the key.
*/
Expand All @@ -40,14 +25,6 @@ public function getKey(): string
return Str::of(static::class)->classBasename()->kebab()->value();
}

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

/**
* Get the name.
*/
Expand All @@ -57,78 +34,13 @@ public function getName(): string
}

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

return $this;
}

/**
* Determine if the widget is async.
*/
public function isAsync(): bool
{
return $this->async;
}

/**
* Get the data.
*/
public function data(Request $request): array
{
return [];
}

/**
* Set the data resolver.
*/
public function with(array|Closure $data): static
{
$this->dataResolver = is_array($data)
? fn (): array => $data
: $data;

return $this;
}

/**
* Resolve the data.
*/
public function resolveData(Request $request): array
{
return array_merge(
$this->data($request),
is_null($this->dataResolver) ? [] : call_user_func_array($this->dataResolver, [$request])
);
}

/**
* Render the widget.
*/
public function render(): View
{
return App::make('view')->make(
$this->template,
App::call([$this, 'data'])
);
}

/**
* Render the HTML string.
*/
public function toHtml(): string
{
return $this->render()->render();
}

/**
* Convert the field to a string.
* Convert the widget to an array.
*/
public function __toString(): string
public function toArray(): array
{
return $this->toHtml();
return [
'key' => $this->getKey(),
'name' => $this->getName(),
];
}
}
12 changes: 0 additions & 12 deletions stubs/Relation.stub

This file was deleted.

24 changes: 8 additions & 16 deletions stubs/Resource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ use Illuminate\Http\Request;

class {{ class }} extends Resource
{
/**
* Define the relations for the resource.
*/
public function relations(): array
{
return array_merge(parent::relations(), [
//
]);
}

/**
* Define the extracts for the resource.
*/
Expand All @@ -47,18 +37,20 @@ class {{ class }} extends Resource
*/
public function toTable(Request $request): Table
{
return parent::toTable($request)->withColumns(static function (Columns $columns): void {
$columns->id();
});
return parent::toTable($request)
->withColumns(static function (Request $request, Columns $columns): void {
$columns->id();
});
}

/**
* Get the form instance for the resource.
*/
public function toForm(Request $request): Form
{
return parent::toForm($request)->withFields(static function (Fields $fields): void {
//
});
return parent::toForm($request)
->withFields(static function (Request $request, Fields $fields): void {
//
});
}
}
5 changes: 3 additions & 2 deletions stubs/RootServiceProvider.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Providers;

use App\Models\User;
use App\Root\Resources\UserResource;
use Cone\Root\Conversion\Image;
use Cone\Root\Interfaces\Models\User as UserInterface;
use Cone\Root\RootApplicationServiceProvider;
Expand Down Expand Up @@ -55,7 +56,7 @@ class RootServiceProvider extends RootApplicationServiceProvider
protected function resources(): array
{
return [
User::toResource(),
new UserResource(),
];
}

Expand All @@ -65,7 +66,7 @@ class RootServiceProvider extends RootApplicationServiceProvider
protected function widgets(): array
{
return [
Welcome::make(),
new Welcome(),
];
}
}
32 changes: 17 additions & 15 deletions stubs/UserResource.stub
Original file line number Diff line number Diff line change
Expand Up @@ -23,28 +23,30 @@ class UserResource extends Resource
*/
public function toTable(Request $request): Table
{
return parent::toTable($request)->withColumns(static function (Columns $columns): void {
$columns->id();
$columns->text(__('Name'), 'name');
$columns->text(__('Email'), 'email');
});
return parent::toTable($request)
->withColumns(static function (Request $request, Columns $columns): void {
$columns->id();
$columns->text(__('Name'), 'name');
$columns->text(__('Email'), 'email');
});
}

/**
* {@inheritdoc}
*/
public function toForm(Request $request): Form
{
return parent::toForm($request)->withFields(static function (Fields $fields): void {
$fields->text(__('Name'), 'name')
->rules(['required', 'string', 'max:256']);
return parent::toForm($request)
->withFields(static function (Request $request, Fields $fields): void {
$fields->text(__('Name'), 'name')
->rules(['required', 'string', 'max:256']);

$fields->email(__('Email'), 'email')
->rules(['required', 'string', 'email', 'max:256'])
->createRules(['unique:users'])
->updateRules(static function (Request $request, Model $model): array {
return [Rule::unique('users')->ignoreModel($model)];
});
});
$fields->email(__('Email'), 'email')
->rules(['required', 'string', 'email', 'max:256'])
->createRules(['unique:users'])
->updateRules(static function (Request $request, Model $model): array {
return [Rule::unique('users')->ignoreModel($model)];
});
});
}
}
11 changes: 2 additions & 9 deletions stubs/Widget.stub
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,9 @@
namespace {{ namespace }};

use Cone\Root\Widgets\Widget;
use Illuminate\Http\Request;

class {{ class }} extends Widget
{
%%async%%
/**
* Indicates whether the widget is async.
*/
protected bool $async = true;
%%/async%%
/**
* The Blade template.
*/
Expand All @@ -21,9 +14,9 @@ class {{ class }} extends Widget
/**
* Get the data.
*/
public function data(Request $request): array
public function toArray(): array
{
return array_merge(parent::data($request), [
return array_merge(parent::toArray(), [
//
]);
}
Expand Down

0 comments on commit cb3f7c6

Please sign in to comment.