Skip to content

Commit 3561aad

Browse files
committed
Create Resources Using Stubs (PHP 7.4)
1 parent 44c95ba commit 3561aad

File tree

7 files changed

+66
-22
lines changed

7 files changed

+66
-22
lines changed

config/repository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
'relative' => [
2626
'entities' => 'app/Models/Entities/',
2727
'factories' => 'app/Models/Factories/',
28-
'resources' => 'app/Http/Resources/Admin/',
28+
'resources' => 'app/Models/Resources/',
2929
'repositories' => 'app/Models/Repositories/',
3030
],
3131

src/Commands/MakeResource.php

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Nanvaie\DatabaseRepository\Commands;
44

5-
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
65
use Illuminate\Console\Command;
6+
use Nanvaie\DatabaseRepository\CustomMySqlQueries;
77

88
class MakeResource extends Command
99
{
@@ -27,6 +27,20 @@ class MakeResource extends Command
2727

2828
use CustomMySqlQueries;
2929

30+
public function writeGetter(string $getterStub, string $columnName, string $attributeName)
31+
{
32+
return str_replace(['{{ ColumnName }}', '{{ GetterName }}'],
33+
[$columnName, ucfirst($attributeName)],
34+
$getterStub);
35+
}
36+
37+
public function writeForeignGetter(string $foreignGetterStub, string $columnName, string $attributeName)
38+
{
39+
return str_replace(['{{ AttributeName }}', '{{ GetterName }}', '{{ AttributeType }}'],
40+
[$columnName, ucfirst($attributeName), ucfirst($attributeName)],
41+
$foreignGetterStub);
42+
}
43+
3044
/**
3145
* Execute the console command.
3246
*
@@ -38,9 +52,12 @@ public function handle(): int
3852
$detectForeignKeys = $this->option('foreign-keys');
3953
$entityName = str_singular(ucfirst(camel_case($tableName)));
4054
$entityVariableName = camel_case($entityName);
55+
$entityNamespace = config('repository.path.namespace.entities');
4156
$resourceName = $entityName . "Resource";
4257
$resourceNamespace = config('repository.path.namespace.resources');
4358
$relativeResourcesPath = config('repository.path.relative.resources');
59+
$resourceStubsPath = config('repository.path.stub.resources');
60+
$filenameWithPath = $relativeResourcesPath.$resourceName.'.php';
4461

4562
if ($this->option('delete')) {
4663
unlink("$relativeResourcesPath/$resourceName.php");
@@ -69,38 +86,32 @@ public function handle(): int
6986
$foreignKeys = $this->extractForeignKeys($tableName);
7087
}
7188

72-
// Initialize Class
73-
$resourceContent = "<?php\n\nnamespace $resourceNamespace;\n\n";
74-
$resourceContent .= "use App\Http\Resources\Resource;\n";
75-
$resourceContent .= "use App\Models\Entities\\$entityName;\n\n";
76-
$resourceContent .= "class $resourceName extends Resource\n{\n";
89+
$baseContent = file_get_contents($resourceStubsPath.'class.stub');
90+
$getterStub = file_get_contents($resourceStubsPath.'getter.default.stub');
91+
$foreignGetterStub = file_get_contents($resourceStubsPath.'getter.foreign.stub');
7792

78-
// Create "toArray" Function
79-
$resourceContent .= "\t/**\n\t * @param $entityName \$$entityVariableName\n\t * @return array\n\t */\n";
80-
$resourceContent .= "\tpublic function toArray($entityName \$$entityVariableName): array\n\t{\n";
81-
$resourceContent .= " \t\treturn [";
8293
foreach ($columns as $_column) {
83-
$resourceContent .= "\n\t\t\t'$_column->COLUMN_NAME' => \$$entityVariableName" . "->get" . ucfirst(camel_case($_column->COLUMN_NAME)) . "(),";
94+
$baseContent = substr_replace($baseContent,
95+
$this->writeGetter($getterStub, $_column->COLUMN_NAME, camel_case($_column->COLUMN_NAME)),
96+
-293, 0);
8497
}
8598

86-
// Add Additional Resources for Foreign Keys
8799
if ($detectForeignKeys) {
88-
$resourceContent .= "\n";
89100
foreach ($foreignKeys as $_foreignKey) {
90-
$resourceContent .= "\n\t\t\t'" . snake_case($_foreignKey->VARIABLE_NAME) .
91-
"' => \$$entityVariableName" . "->get" . ucfirst($_foreignKey->VARIABLE_NAME) . "()" .
92-
" ? (new " . $_foreignKey->ENTITY_DATA_TYPE . "Resource())->toArray(\$$entityVariableName" .
93-
"->get" . ucfirst($_foreignKey->VARIABLE_NAME) . "()) : null,";
101+
$baseContent = substr_replace($baseContent,
102+
$this->writeForeignGetter($foreignGetterStub, $_foreignKey->VARIABLE_NAME, $_foreignKey->ENTITY_DATA_TYPE),
103+
-19, 0);
94104
}
95105
}
96-
$resourceContent .= "\n\t\t];\n\t}\n";
97106

98-
$resourceContent .= "}";
107+
$baseContent = str_replace(['{{ EntityName }}', '{{ EntityNamespace }}', '{{ EntityVariableName }}', '{{ ResourceName }}', '{{ ResourceNamespace }}'],
108+
[$entityName, $entityNamespace, $entityVariableName, $resourceName, $resourceNamespace],
109+
$baseContent);
99110

100-
file_put_contents("$relativeResourcesPath/$resourceName.php", $resourceContent);
111+
file_put_contents($filenameWithPath, $baseContent);
101112

102113
if ($this->option('add-to-git')) {
103-
shell_exec("git add $relativeResourcesPath/$resourceName.php");
114+
shell_exec("git add $filenameWithPath");
104115
}
105116

106117
$this->info("Resource \"$resourceName\" has been created.");

stubs/Models/PHP7.4/Resource/IResource.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models\Resources;
44

5+
use App\Models\Entities\Entity;
56
use Illuminate\Support\Collection;
67

78
interface IResource

stubs/Models/PHP7.4/Resource/Resource.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Models\Resources;
44

5+
use App\Models\Entities\Entity;
56
use Illuminate\Support\Collection;
67

78
abstract class Resource implements IResource
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace {{ ResourceNamespace }};
4+
5+
use {{ EntityNamespace }}\Entity;
6+
use {{ EntityNamespace }}\{{ EntityName }};
7+
8+
class {{ ResourceName }} extends Resource
9+
{
10+
/**
11+
* @param Entity|{{ EntityName }} ${{ EntityVariableName }}
12+
* @return array
13+
*/
14+
public function toArray(${{ EntityVariableName }}): array
15+
{
16+
return [
17+
];
18+
}
19+
20+
/**
21+
* @param Entity|{{ EntityName }} ${{ EntityVariableName }}
22+
* @return array
23+
*/
24+
public function toArrayWithForeignKeys(${{ EntityVariableName }}): array
25+
{
26+
return $this->toArray(${{ EntityVariableName }}) + [
27+
];
28+
}
29+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'{{ ColumnName }}' => ${{ EntityVariableName }}->get{{ GetterName }}(),
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
'{{ AttributeName }}' => ${{ EntityVariableName }}->get{{ GetterName }}() ? (new {{ AttributeType }}Resource())->toArray(${{ EntityVariableName }}->get{{ GetterName }}()) : null,

0 commit comments

Comments
 (0)