Skip to content

Commit bfbe08d

Browse files
author
Admin
committed
Added --composed flag for composed PK
1 parent 3896ca7 commit bfbe08d

File tree

8 files changed

+115
-18
lines changed

8 files changed

+115
-18
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,17 @@ Library for [laravel-crud-wizard-free](https://github.com/macropay-solutions/lar
2020

2121
## Usage
2222

23-
``` php artisan make:api-resource {resourceName} {--decorated} {--table=} {--connection=}```
23+
``` php artisan make:api-resource {resourceName} {--decorated} {--table=} {--connection=} {--composed}```
2424

2525
This will create a template for **controller**, **service** and **model (with prepopulated columns from DB)** (optionally, with the **--decorated** flag it will create also a **decorator** and **middleware**) and will print instructions on what is left to be done manually.
2626

2727
If connection is not the default one, it can be specified in the command option.
2828

29-
If table is not the plural snake cased resourceName, then it can be specified in the table option.
29+
If table is not the snake cased resourceName, then it can be specified in the table option.
3030

31-
Run just **php artisan make:api-resource** for interactive mode that will try to guess the tableName and use default connectionName (except --decorated).
31+
Run just **php artisan make:api-resource** or **php artisan make:api-resource --decorated** for interactive mode that will try to guess the tableName and use default connectionName.
32+
33+
**--composed** flag will generate model and service for composed primary key
3234

3335
Example:
3436
```
@@ -41,8 +43,6 @@ TODO:
4143
- Fill the model's properties and relations,
4244
- Define validations in controller,
4345
- Expose resource in DbCrudMap::MODEL_FQN_TO_CONTROLLER_MAP.
44-
Created Decorator: /var/www/html/project/app/Decorators/OperationDecorator.php
45-
Created Middleware: /var/www/html/project/app/Http/Midleware/Decorators/OperationsMiddleware.php
4646
```
4747

4848
```

src/Console/MakeLaravelCrudWizard.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ public function handle(int $try = 0): void
5050
$this->resourceName ??= $this->argument('resourceName');
5151
$this->connection ??= $this->option('connection');
5252
$this->table ??= $this->option('table');
53+
$composedPk = $this->option('composed');
5354
$columns = [];
5455

5556
try {
@@ -58,7 +59,8 @@ public function handle(int $try = 0): void
5859
$ns = $this->laravel->getNamespace(),
5960
$this->connection,
6061
$this->table,
61-
$this->ignoreError
62+
$this->ignoreError,
63+
$composedPk,
6264
);
6365
} catch (\Throwable $e) {
6466
$this->resourceName ??= $this->ask('resourceName ?');
@@ -88,7 +90,7 @@ public function handle(int $try = 0): void
8890
}
8991
}
9092

91-
$this->makeServiceService->makeCompleteServiceFile($this->resourceName, $ns);
93+
$this->makeServiceService->makeCompleteServiceFile($this->resourceName, $ns, $composedPk);
9294
$this->makeControllerService->makeCompleteControllerFile(
9395
$this->resourceName,
9496
$ns,

src/Services/MakeModelService.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,18 @@ public function makeCompleteModelFile(
2929
?string $connection,
3030
?string $table,
3131
bool $ignoreError,
32+
bool $composedPk,
3233
): array {
3334
$this->createModelFile(
3435
$resourceName,
35-
$this->replaceContentModelStub($laravelNamespace, $resourceName, $connection, $table, $ignoreError)
36+
$this->replaceContentModelStub(
37+
$laravelNamespace,
38+
$resourceName,
39+
$connection,
40+
$table,
41+
$ignoreError,
42+
$composedPk
43+
),
3644
);
3745

3846
return $this->columns;
@@ -47,6 +55,7 @@ protected function replaceContentModelStub(
4755
?string $connection,
4856
?string $table,
4957
bool $ignoreError,
58+
bool $composedPk,
5059
): string {
5160
$table ??= Str::snake(Str::camel($resourceName));
5261

@@ -80,7 +89,7 @@ protected function replaceContentModelStub(
8089
$this->pathsAndNamespacesService->singularUcFirstCamelCase($resourceName),
8190
$fillable,
8291
(string)$connection !== '' ? ' protected $connection = \'' . $connection . '\';' : '',
83-
], File::get($this->pathsAndNamespacesService->getModelStubPath()));
92+
], File::get($this->pathsAndNamespacesService->getModelStubPath($composedPk)));
8493
}
8594

8695
protected function createModelFile(string $resourceName, string $modelStub) : void

src/Services/MakeServiceService.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,19 @@ public function __construct(
1717
$this->output = $consoleOutput;
1818
}
1919

20-
public function makeCompleteServiceFile(string $resourceName, string $laravelNamespace): void
20+
public function makeCompleteServiceFile(string $resourceName, string $laravelNamespace, bool $composedPk): void
2121
{
22-
$this->createServiceFile($this->replaceContentServiceStub($resourceName, $laravelNamespace), $resourceName);
22+
$this->createServiceFile(
23+
$this->replaceContentServiceStub($resourceName, $laravelNamespace, $composedPk),
24+
$resourceName
25+
);
2326
}
2427

25-
protected function replaceContentServiceStub(string $resourceName, string $laravelNamespace): string
26-
{
28+
protected function replaceContentServiceStub(
29+
string $resourceName,
30+
string $laravelNamespace,
31+
bool $composedPk
32+
): string {
2733
return \str_replace([
2834
'DummyNamespace',
2935
'DummyModel',
@@ -32,7 +38,7 @@ protected function replaceContentServiceStub(string $resourceName, string $larav
3238
\trim($laravelNamespace, '\\'),
3339
$this->pathsAndNamespacesService->singularUcFirstCamelCase($resourceName),
3440
$this->pathsAndNamespacesService->pluralUcFirstCamelCase($resourceName) . 'Service',
35-
], File::get($this->pathsAndNamespacesService->getServiceStubPath()));
41+
], File::get($this->pathsAndNamespacesService->getServiceStubPath($composedPk)));
3642
}
3743

3844
protected function createServiceFile(string $serviceStub, string $resourceName): void

src/Services/PathsAndNamespacesService.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,14 @@ public function getRealpathBase(string $directory): string
1616
return \realpath(\base_path($directory));
1717
}
1818

19-
public function getModelStubPath(): string
19+
public function getModelStubPath(bool $composedPk): string
2020
{
21-
return $this->getStubPath() . DIRECTORY_SEPARATOR . 'Model.stub';
21+
return $this->getStubPath() . DIRECTORY_SEPARATOR . ($composedPk ? 'ModelComposed.stub' : 'Model.stub');
2222
}
2323

24-
public function getServiceStubPath(): string
24+
public function getServiceStubPath(bool $composedPk): string
2525
{
26-
return $this->getStubPath() . DIRECTORY_SEPARATOR . 'Service.stub';
26+
return $this->getStubPath() . DIRECTORY_SEPARATOR . ($composedPk ? 'ServiceComposed.stub' : 'Service.stub');
2727
}
2828

2929
public function getControllerStubPath(): string

src/stubs/Model.stub

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,20 @@ class DummyClass extends BaseModel
88
{
99
public const RESOURCE_NAME = 'DummyResourceName';
1010
DummyConnection
11+
protected $primaryKey = 'id';
12+
protected $keyType = 'int';
1113
protected $table = 'DummyTableName';
14+
protected array $ignoreExternalCreateFor = [
15+
'id',
16+
'updated_at',
17+
];
18+
protected array $ignoreUpdateFor = [
19+
'id',
20+
'created_at',
21+
];
22+
protected array $allowNonExternalUpdatesFor = [
23+
'updated_at',
24+
];
1225

1326
protected $fillable = [
1427
DummyFillable

src/stubs/ModelComposed.stub

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
namespace DummyNamespace\Models;
4+
5+
use MacropaySolutions\LaravelCrudWizard\Models\BaseModel;
6+
7+
class DummyClass extends BaseModel
8+
{
9+
public const RESOURCE_NAME = 'DummyResourceName';
10+
DummyConnection
11+
protected $primaryKey = '';
12+
protected $keyType = 'string';
13+
protected $table = 'DummyTableName';
14+
protected array $ignoreExternalCreateFor = [
15+
'updated_at',
16+
];
17+
protected array $ignoreUpdateFor = [
18+
'created_at',
19+
];
20+
protected array $allowNonExternalUpdatesFor = [
21+
'updated_at',
22+
];
23+
24+
protected $fillable = [
25+
DummyFillable
26+
];
27+
28+
public function getPrimaryKeyFilter(): array
29+
{
30+
return [
31+
'id_1' => $this->getAttribute('id_1'), // todo
32+
'id_2' => $this->getAttribute('id_2'), // todo
33+
// ... todo
34+
];
35+
}
36+
}

src/stubs/ServiceComposed.stub

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
namespace DummyNamespace\Services;
4+
5+
use DummyNamespace\Models\DummyModel;
6+
use MacropaySolutions\LaravelCrudWizard\Services\BaseResourceService;
7+
8+
class DummyClass extends BaseResourceService
9+
{
10+
/**
11+
* @inheritDoc
12+
*/
13+
protected function setBaseModel(): void
14+
{
15+
$this->model = new DummyModel();
16+
}
17+
18+
/**
19+
* @inheritDoc
20+
*/
21+
protected function extractIdentifierConditions(string $identifier): array
22+
{
23+
$exploded = \explode($this->model::COMPOSITE_PK_SEPARATOR, $identifier);
24+
25+
return [
26+
['id_1', \reset($exploded)], // todo
27+
['id_2', \next($exploded)], // todo
28+
// ... todo
29+
];
30+
}
31+
}

0 commit comments

Comments
 (0)