Skip to content

Commit

Permalink
refactor: New ServiceProvider approach
Browse files Browse the repository at this point in the history
BREAKING-CHANGE: MoonShineApplicationServiceProvider.php removed, new MoonShineServiceProvider.stub
  • Loading branch information
lee-to committed Nov 5, 2024
1 parent 6c8e8cc commit 4f03074
Show file tree
Hide file tree
Showing 18 changed files with 131 additions and 123 deletions.
7 changes: 6 additions & 1 deletion rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@

use Rector\CodeQuality\Rector\If_\ExplicitBoolCompareRector;
use Rector\Config\RectorConfig;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessParamTagRector;
use Rector\DeadCode\Rector\ClassMethod\RemoveUselessReturnTagRector;
use Rector\DeadCode\Rector\Property\RemoveUselessVarTagRector;
use Rector\Php81\Rector\Array_\FirstClassCallableRector;
use Rector\Set\ValueObject\LevelSetList;
use Rector\Set\ValueObject\SetList;
Expand All @@ -24,7 +27,9 @@
__DIR__ . '/src/Laravel/stubs',
ExplicitBoolCompareRector::class,
FirstClassCallableRector::class,

RemoveUselessReturnTagRector::class,
RemoveUselessVarTagRector::class,
RemoveUselessParamTagRector::class,
]);

$rectorConfig->importNames();
Expand Down
2 changes: 2 additions & 0 deletions src/Contracts/src/Core/CrudResourceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,14 @@ public function massDelete(array $ids): void;

/**
* @param TData $item
* @param ?TFields $fields
*
*/
public function delete(mixed $item, ?FieldsContract $fields = null): bool;

/**
* @param TData $item
* @param ?TFields $fields
*
* @return TData
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,48 @@ interface AppliesRegisterContract
{
public function type(string $type): static;

/**
* @param class-string $for
*/
public function for(string $for): static;

/**
* @return class-string
*/
public function getFor(): string;

/**
* @param class-string $for
*/
public function defaultFor(string $for): static;

/**
* @return class-string
*/
public function getDefaultFor(): string;

/**
* @param ?class-string $for
*/
public function findByField(
FormElementContract $field,
string $type = 'fields',
?string $for = null
): ?ApplyContract;

/**
* @param class-string<FormElementContract> $fieldClass
* @param class-string<ApplyContract> $applyClass
*/
public function add(string $fieldClass, string $applyClass): static;

/**
* @param array<class-string<FormElementContract>, class-string<ApplyContract>> $data
*/
public function push(array $data): static;

/**
* @param class-string<FormElementContract> $fieldClass
*/
public function get(string $fieldClass, ?ApplyContract $default = null): ?ApplyContract;
}
15 changes: 12 additions & 3 deletions src/Contracts/src/Core/DependencyInjection/EndpointsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,20 @@ public function toPage(
array $extra = [],
): mixed;


/**
* @param ?TResource $resource
*
*/
public function updateField(
?ResourceContract $resource = null,
?PageContract $page = null,
array $extra = []
): string;


/**
* @param ?TResource $resource
*
*/
public function method(
string $method,
?string $message = null,
Expand All @@ -46,7 +52,10 @@ public function component(
array $additionally = []
): string;


/**
* @param ?TResource $resource
*
*/
public function reactive(
?PageContract $page = null,
?ResourceContract $resource = null,
Expand Down
4 changes: 4 additions & 0 deletions src/Contracts/src/Core/DependencyInjection/FieldsContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ public function whenFieldsConditions(): static;

/**
* @param T $default
*
* @return ?T
*/
public function findByColumn(
string $column,
Expand All @@ -62,6 +64,8 @@ public function findByColumn(
/**
* @param class-string<T> $class
* @param ?T $default
*
* @return ?T
*/
public function findByClass(
string $class,
Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/src/Core/HasResourceContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,8 @@ public function setResource(ResourceContract $resource): static;

public function hasResource(): bool;

/**
* @return ?T
*/
public function getResource(): ?ResourceContract;
}
3 changes: 3 additions & 0 deletions src/Contracts/src/UI/HasModalContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ interface HasModalContract
{
public function isInModal(): bool;

/**
* @return ?T
*/
public function getModal(): ?ComponentContract;

public function toggleModal(string $name = 'default'): static;
Expand Down
3 changes: 3 additions & 0 deletions src/Contracts/src/UI/HasOffCanvasContract.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
interface HasOffCanvasContract
{
/**
* @return ?T
*/
public function getOffCanvas(): ?ComponentContract;

public function isInOffCanvas(): bool;
Expand Down
12 changes: 12 additions & 0 deletions src/Core/src/Traits/HasResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,19 @@
*/
trait HasResource
{
/**
* @var ?T
*/
protected ?ResourceContract $resource = null;

/**
* @var ?PT
*/
protected ?ResourceContract $parentResource = null;

/**
* @return ?PT
*/
public function getParentResource(): ?ResourceContract
{
return $this->parentResource;
Expand Down Expand Up @@ -45,6 +54,9 @@ public function hasResource(): bool
return ! \is_null($this->resource);
}

/**
* @return ?T
*/
public function getResource(): ?ResourceContract
{
return $this->resource;
Expand Down
4 changes: 1 addition & 3 deletions src/Laravel/src/Commands/InstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,9 +255,7 @@ protected function initServiceProvider(): void
$replace,
);

if (! moonshine()->runningUnitTests()) {
$this->registerServiceProvider();
}
$this->registerServiceProvider();

$this->components->task('Service Provider created');
}
Expand Down
36 changes: 22 additions & 14 deletions src/Laravel/src/Commands/MoonShineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace MoonShine\Laravel\Commands;

use Closure;
use Illuminate\Support\Stringable;
use Leeto\PackageCommand\Command;
use MoonShine\MenuManager\MenuItem;

Expand All @@ -18,34 +20,40 @@ protected function getDirectory(): string

public static function addResourceOrPageToProviderFile(string $class, bool $page = false, string $prefix = ''): void
{
$method = $page ? 'pages' : 'resources';

self::addResourceOrPageTo(
$prefix . $class,
append: "$class::class",
class: $prefix . $class,
to: app_path('Providers/MoonShineServiceProvider.php'),
method: $page ? 'pages' : 'resources',
page: $page
isPage: $page,
between: static fn(Stringable $content): Stringable => $content->betweenFirst("->$method([", ']'),
replace: static fn(Stringable $content, Closure $tab): Stringable => $content->append("{$tab()}$class::class,\n{$tab(3)}"),
);
}

public static function addResourceOrPageToMenu(string $class, string $title, bool $page = false, string $prefix = ''): void
{
self::addResourceOrPageTo(
$prefix . $class,
append: "MenuItem::make('{$title}', $class::class)",
class: $prefix . $class,
to: app_path('MoonShine/Layouts/MoonShineLayout.php'),
method: 'menu',
page: $page,
isPage: $page,
between: static fn(Stringable $content): Stringable => $content->betweenFirst("protected function menu(): array", '}'),
replace: static fn(Stringable $content, Closure $tab): Stringable => $content->replace("];", "{$tab()}MenuItem::make('{$title}', $class::class),\n{$tab(2)}];"),
use: MenuItem::class,
);
}

private static function addResourceOrPageTo(string $class, string $append, string $to, string $method, bool $page, string $use = ''): void
/**
* @param Closure(Stringable $content): Stringable $between
* @param Closure(Stringable $content, Closure $tab): Stringable $replace
*/
private static function addResourceOrPageTo(string $class, string $to, bool $isPage, Closure $between, Closure $replace, string $use = ''): void
{
if (! file_exists($to)) {
return;
}

$dir = $page ? 'Pages' : 'Resources';
$dir = $isPage ? 'Pages' : 'Resources';
$namespace = moonshineConfig()->getNamespace("\\$dir\\") . $class;

$content = str(file_get_contents($to));
Expand All @@ -57,7 +65,7 @@ private static function addResourceOrPageTo(string $class, string $append, strin
$tab = static fn (int $times = 1): string => str_repeat(' ', $times * 4);

$headSection = $content->before('class ');
$resourcesSection = $content->betweenFirst("protected function $method(): array", '}');
$replaceContent = $between($content);

if ($content->contains($use)) {
$use = '';
Expand All @@ -66,13 +74,13 @@ private static function addResourceOrPageTo(string $class, string $append, strin
$content = str_replace(
[
$headSection->value(),
$resourcesSection->value(),
$replaceContent->value(),
],
[
$headSection->replaceLast(';', (";\nuse $namespace;" . ($use ? "\nuse $use;" : ''))),
$resourcesSection->replace("];", "{$tab()}$append,\n{$tab(2)}];")->value(),
$replace($replaceContent, $tab)->value(),
],
$content->value()
$content->value(),
);

file_put_contents($to, $content);
Expand Down
72 changes: 0 additions & 72 deletions src/Laravel/src/Providers/MoonShineApplicationServiceProvider.php

This file was deleted.

3 changes: 3 additions & 0 deletions src/Laravel/src/Traits/Request/HasPageRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
trait HasPageRequest
{
/**
* @return ?TPage
*/
public function findPage(): ?PageContract
{
return memoize(function (): ?PageContract {
Expand Down
3 changes: 3 additions & 0 deletions src/Laravel/src/Traits/Request/HasResourceRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
trait HasResourceRequest
{
/**
* @return ?T
*/
public function getResource(): ?ResourceContract
{
if (\is_null($this->getResourceUri())) {
Expand Down
2 changes: 1 addition & 1 deletion src/Laravel/stubs/Apply.stub
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use MoonShine\Contracts\UI\ApplyContract;
use MoonShine\Contracts\UI\FieldContract;

/**
* Сhange the FieldContract in the DocBlocks to the desired Field
* Change the FieldContract in the DocBlocks to the desired Field
* @implements ApplyContract<FieldContract>
*/
class DummyClass implements ApplyContract
Expand Down
Loading

0 comments on commit 4f03074

Please sign in to comment.