Skip to content

Commit da3886e

Browse files
author
Admin
committed
Make RESOURCE_NAME optional
1 parent c9017b0 commit da3886e

File tree

7 files changed

+26
-12
lines changed

7 files changed

+26
-12
lines changed

src/Exports/ListResourceExcel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public function sheets(): array
4343
{
4444
$sheets = [];
4545

46-
if ($this->baseResourceModel instanceof BaseModel && (string)$this->baseResourceModel::RESOURCE_NAME !== '') {
46+
if ($this->baseResourceModel instanceof BaseModel) {
4747
$sheets [] = GeneralHelper::app(
4848
Resource::class,
4949
[
@@ -78,7 +78,7 @@ public function sheets(): array
7878

7979
public function failed(\Throwable $e): void
8080
{
81-
Log::error('Download ListResourceExcel error for ' . $this->baseModel::RESOURCE_NAME . ', error: ' .
81+
Log::error('Download ListResourceExcel error for ' . $this->baseModel::resourceName() . ', error: ' .
8282
$e->getMessage());
8383
}
8484

src/Exports/Sheets/Resource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public function collection(): Collection
4747
*/
4848
public function title(): string
4949
{
50-
return $this->baseModel::RESOURCE_NAME;
50+
return $this->baseModel::resourceName();
5151
}
5252

5353
public function headings(): array
@@ -66,7 +66,7 @@ public function styles(Worksheet $sheet): array
6666

6767
public function failed(\Throwable $e): void
6868
{
69-
Log::error('Download Resource sheet error for ' . $this->baseModel::RESOURCE_NAME . ', error: ' .
69+
Log::error('Download Resource sheet error for ' . $this->baseModel::resourceName() . ', error: ' .
7070
$e->getMessage());
7171
}
7272
}

src/Helpers/GeneralHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function getSafeErrorMessage(\Throwable $e, string $messagePrefix
3434
}
3535

3636
if ($e instanceof ModelNotFoundException) {
37-
return Str::singular($e->getModel()::RESOURCE_NAME ?? 'Resource') . ' not found.';
37+
return Str::singular($e->getModel()::resourceName()) . ' not found.';
3838
}
3939

4040
return $e->getMessage();

src/Helpers/ResourceHelper.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,17 @@ public static function getResourceNameToControllerFQNMap(array $modelFqnToContro
2323

2424
foreach ($modelFqnToControllerMap as $resourceFQN => $controllerFQN) {
2525
/** @var BaseModel $resourceFQN */
26-
if (\strlen($resourceFQN::RESOURCE_NAME) > 0 && \strlen($controllerFQN) > 0) {
27-
$map[$resourceFQN::RESOURCE_NAME] = $controllerFQN;
26+
$resource = $resourceFQN::resourceName();
27+
28+
if (isset($map[$resource])) {
29+
throw new \Exception('Duplicate resource name: ' . $resource);
30+
}
31+
32+
if (!\class_exists($controllerFQN)) {
33+
throw new \Exception('Controller class does not exist for resource: ' . $resource);
2834
}
35+
36+
$map[$resource] = $controllerFQN;
2937
}
3038

3139
return $resourceNameToControllerFQNMap = $map;

src/Http/Controllers/ResourceControllerTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,9 +324,9 @@ public function deleteRelated(string $identifier, string $relation, string $rela
324324
protected function getResourceAsModelFQN(): string
325325
{
326326
$resource = ResourceHelper::getResourceControllerToModelFQNMap($this->modelFqnToControllerMap)[$this::class] ??
327-
'';
327+
null;
328328

329-
if (\strlen($resource) > 0) {
329+
if (\is_string($resource)) {
330330
return $resource;
331331
}
332332

src/Models/BaseModel.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Illuminate\Support\Facades\Cache;
99
use Illuminate\Support\Facades\DB;
1010
use Illuminate\Support\Facades\Log;
11+
use Illuminate\Support\Str;
1112
use MacropaySolutions\LaravelCrudWizard\Eloquent\CustomRelations\Builders\CleverEloquentBuilder;
1213
use MacropaySolutions\LaravelCrudWizard\Eloquent\CustomRelations\HasCleverRelationships;
1314
use MacropaySolutions\LaravelCrudWizard\Helpers\GeneralHelper;
@@ -55,6 +56,11 @@ public function __construct(array $attributes = [])
5556
$this->append('primary_key_identifier');
5657
}
5758

59+
public static function resourceName(): string
60+
{
61+
return static::RESOURCE_NAME ?? Str::snake(Str::pluralStudly(\class_basename(static::class)), '-');
62+
}
63+
5864
/**
5965
* @inheritDoc
6066
*/
@@ -78,7 +84,7 @@ public function getColumns(bool $includingPrimary = true): array
7884
$columns
7985
)
8086
) {
81-
Log::warning('LaravelCrudWizard warning: the resource ' . $this::RESOURCE_NAME .
87+
Log::warning('LaravelCrudWizard warning: the resource ' . $this::resourceName() .
8288
' uses reserved words as columns: ' . \implode(',', $reservedUsed));
8389
}
8490

@@ -227,7 +233,7 @@ function () use ($driver): array {
227233

228234
try {
229235
return $result[$this->getConnectionName() . $this->getTable()] ??= \collect(Cache::remember(
230-
$this::RESOURCE_NAME . 'IndexesForFiltering',
236+
$this::resourceName() . 'IndexesForFiltering',
231237
Carbon::now()->addDay(),
232238
$callback
233239
));

src/Services/BaseResourceService.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public function __construct()
2222

2323
public function getResourceName(): string
2424
{
25-
return $this->model::RESOURCE_NAME;
25+
return $this->model::resourceName();
2626
}
2727

2828
/**

0 commit comments

Comments
 (0)