Skip to content

Commit 167372c

Browse files
committed
fix: some changes in PHP8.0 stubs & makers commands
1 parent 9a1338b commit 167372c

14 files changed

+83
-64
lines changed

config/repository.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
],
1515

1616
'stub' => [
17-
'entities' => 'stubs/repository.entity.',
18-
'factories' => 'stubs/repository.factory.',
19-
'resources' => 'stubs/repository.resource.',
17+
'entities' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.entity.',
18+
'factories' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.factory.',
19+
'resources' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.resource.',
2020
'repositories' => [
21-
'base' => 'stubs/repository.base.',
22-
'mysql' => 'stubs/repository.mysql.',
23-
'interface' => 'stubs/repository.interface.',
21+
'base' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.base.',
22+
'mysql' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.mysql.',
23+
'interface' => 'stubs/PHP' . env('REPOSITORY_PHP_VERSION', '8.0') . '/repository.interface.',
2424
]
2525
],
2626

src/Commands/MakeEntity.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,8 @@ public function handle(): int
6666
$entityName = str_singular(ucfirst(camel_case($tableName)));
6767
$entityNamespace = config('repository.path.namespace.entities');
6868
$relativeEntitiesPath = config('repository.path.relative.entities');
69-
$entityStubsPath = config('repository.path.stub.entities');
69+
$entityStubsPath = __DIR__ . '/../../' . config('repository.path.stub.entities');
70+
$phpVersion = config('repository.php_version');
7071
$filenameWithPath = $relativeEntitiesPath.$entityName.'.php';
7172

7273
if ($this->option('delete')) {
@@ -75,7 +76,7 @@ public function handle(): int
7576
return 0;
7677
}
7778

78-
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 775, true) && ! is_dir($relativeEntitiesPath)) {
79+
if ( ! file_exists($relativeEntitiesPath) && ! mkdir($relativeEntitiesPath, 0775, true) && ! is_dir($relativeEntitiesPath)) {
7980
$this->alert("Directory \"$relativeEntitiesPath\" was not created");
8081
return 0;
8182
}

src/Commands/MakeFactory.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ public function handle(): int
4747
$entityNamespace = config('repository.path.namespace.entities');
4848
$factoryNamespace = config('repository.path.namespace.factories');
4949
$relativeFactoriesPath = config('repository.path.relative.factories');
50-
$factoryStubsPath = config('repository.path.stub.factories');
50+
$factoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.factories');
51+
$phpVersion = config('repository.php_version');
5152
$filenameWithPath = $relativeFactoriesPath.$factoryName.'.php';
5253

5354
if ($this->option('delete')) {
@@ -56,7 +57,7 @@ public function handle(): int
5657
return 0;
5758
}
5859

59-
if ( ! file_exists($relativeFactoriesPath) && ! mkdir($relativeFactoriesPath, 775, true) && ! is_dir($relativeFactoriesPath)) {
60+
if ( ! file_exists($relativeFactoriesPath) && ! mkdir($relativeFactoriesPath, 0775, true) && ! is_dir($relativeFactoriesPath)) {
6061
$this->alert("Directory \"$relativeFactoriesPath\" was not created");
6162
return 0;
6263
}

src/Commands/MakeInterfaceRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,17 +55,17 @@ public function handle(): int
5555
$interfaceName = "I$entityName"."Repository";
5656
$entityNamespace = config('repository.path.namespace.entities');
5757
$repositoryNamespace = config('repository.path.namespace.repositories');
58-
$relativeInterfacePath = config('repository.path.relative.repositories')."\\$entityName";
59-
$interfaceRepositoryStubsPath = config('repository.path.stub.repositories.interface');
60-
$filenameWithPath = $relativeInterfacePath.'\\'.$interfaceName.'.php';
58+
$relativeInterfacePath = config('repository.path.relative.repositories') . "$entityName";
59+
$interfaceRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.interface');
60+
$filenameWithPath = $relativeInterfacePath . '/' . $interfaceName.'.php';
6161

6262
if ($this->option('delete')) {
6363
unlink("$relativeInterfacePath/$interfaceName.php");
6464
$this->info("Interface \"$interfaceName\" has been deleted.");
6565
return 0;
6666
}
6767

68-
if ( ! file_exists($relativeInterfacePath) && ! mkdir($relativeInterfacePath, 775, true) && ! is_dir($relativeInterfacePath)) {
68+
if ( ! file_exists($relativeInterfacePath) && ! mkdir($relativeInterfacePath, 0775, true) && ! is_dir($relativeInterfacePath)) {
6969
$this->alert("Directory \"$relativeInterfacePath\" was not created");
7070
return 0;
7171
}

src/Commands/MakeMySqlRepository.php

Lines changed: 49 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ private function writeGetterFunction(string $getterStub, string $columnName): st
4848
$getterStub);
4949
}
5050

51+
private function writeSetterFunction(string $setterStub, string $columnName): string
52+
{
53+
return str_replace('{{ SetterName }}',
54+
ucfirst(camel_case($columnName)),
55+
$setterStub);
56+
}
57+
5158
/**
5259
* Execute the console command.
5360
*
@@ -65,17 +72,18 @@ public function handle(): int
6572
$entityNamespace = config('repository.path.namespace.entities');
6673
$factoryNamespace = config('repository.path.namespace.factories');
6774
$repositoryNamespace = config('repository.path.namespace.repositories');
68-
$relativeMysqlRepositoryPath = config('repository.path.relative.repositories')."\\$entityName";
69-
$mysqlRepositoryStubsPath = config('repository.path.stub.repositories.mysql');
70-
$filenameWithPath = $relativeMysqlRepositoryPath.'\\'.$mysqlRepositoryName.'.php';
75+
$relativeMysqlRepositoryPath = config('repository.path.relative.repositories')."$entityName";
76+
$mysqlRepositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.mysql');
77+
$phpVersion = config('repository.php_version');
78+
$filenameWithPath = $relativeMysqlRepositoryPath . '/' . $mysqlRepositoryName.'.php';
7179

7280
if ($this->option('delete')) {
7381
unlink("$relativeMysqlRepositoryPath/$mysqlRepositoryName.php");
7482
$this->info("MySql Repository \"$mysqlRepositoryName\" has been deleted.");
7583
return 0;
7684
}
7785

78-
if ( ! file_exists($relativeMysqlRepositoryPath) && ! mkdir($relativeMysqlRepositoryPath) && ! is_dir($relativeMysqlRepositoryPath)) {
86+
if ( ! file_exists($relativeMysqlRepositoryPath) && ! mkdir($relativeMysqlRepositoryPath, 0775, true) && ! is_dir($relativeMysqlRepositoryPath)) {
7987
$this->alert("Directory \"$relativeMysqlRepositoryPath\" was not created");
8088
return 0;
8189
}
@@ -103,61 +111,66 @@ public function handle(): int
103111
$updateFunctionStub = file_get_contents($mysqlRepositoryStubsPath.'update.stub');
104112
$deleteAndUndeleteStub = file_get_contents($mysqlRepositoryStubsPath.'deleteAndUndelete.stub');
105113
$getterStub = file_get_contents($mysqlRepositoryStubsPath.'getter.stub');
114+
$setterStub = file_get_contents($mysqlRepositoryStubsPath.'setter.stub');
106115
$timeFieldStub = file_get_contents($mysqlRepositoryStubsPath.'timeField.stub');
116+
$functions = '';
107117

108118
// Initialize MySql Repository
109-
$baseContent = substr_replace($baseContent,
110-
$this->writeGetOneFunction($getOneStub, 'id', 'int'),
111-
-1, 0);
112-
$baseContent = substr_replace($baseContent,
113-
$this->writeGetAllFunction($getAllStub, 'id', 'int'),
114-
-1, 0);
119+
$functions .= $this->writeGetOneFunction($getOneStub, 'id', 'int');
120+
$functions .= $this->writeGetAllFunction($getAllStub, 'id', 'int');
115121

116122
if ($detectForeignKeys) {
117123
foreach ($foreignKeys as $_foreignKey) {
118-
$baseContent = substr_replace($baseContent,
119-
$this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $entityName),
120-
-1, 0);
121-
$baseContent = substr_replace($baseContent,
122-
$this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $entityName),
123-
-1, 0);
124+
$functions .= $this->writeGetOneFunction($getOneStub, $_foreignKey->COLUMN_NAME, $entityName);
125+
$functions .= $this->writeGetAllFunction($getAllStub, $_foreignKey->COLUMN_NAME, $entityName);
124126
}
125127
}
126128

129+
$getterFunctions = '';
130+
$setterFunctions = '';
127131
// Create "create" function
128132
foreach ($columns as $_column) {
129-
if ( ! in_array($_column->COLUMN_NAME, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
130-
$createFunctionStub = substr_replace($createFunctionStub,
131-
$this->writeGetterFunction($getterStub, $_column->COLUMN_NAME),
132-
-95, 0);
133-
} elseif (in_array($_column->COLUMN_NAME, ['created_at', 'updated_at'], true)) {
134-
$createFunctionStub = substr_replace($createFunctionStub,
135-
$this->writeGetterFunction($timeFieldStub, $_column->COLUMN_NAME),
136-
-95, 0);
133+
if ( ! in_array($_column->COLUMN_NAME, ['id', 'deleted_at'])) {
134+
$getterFunctions .= $this->writeGetterFunction($getterStub, $_column->COLUMN_NAME);
135+
}
136+
if (in_array($_column->COLUMN_NAME, ['created_at', 'updated_at'], true)) {
137+
$setterFunctions .= $this->writeSetterFunction($setterStub, $_column->COLUMN_NAME);
137138
}
138139
}
139-
$baseContent = substr_replace($baseContent, $createFunctionStub, -1, 0);
140+
$createFunctionStub = str_replace(["{{ GetterFunctions }}", "{{ SetterFunctions }}"],
141+
[substr($getterFunctions, 0, -1), substr($setterFunctions, 0, -1)],
142+
$createFunctionStub
143+
);
140144

145+
$functions .= $createFunctionStub;
146+
147+
$getterFunctions = '';
148+
$setterFunctions = '';
141149
// Create "update" function
142150
foreach ($columns as $_column) {
143-
if ( ! in_array($_column->COLUMN_NAME, ['id', 'created_at', 'updated_at', 'deleted_at'])) {
144-
$updateFunctionStub = substr_replace($updateFunctionStub,
145-
$this->writeGetterFunction($getterStub, $_column->COLUMN_NAME),
146-
-12, 0);
147-
} elseif ($_column->COLUMN_NAME === 'updated_at') {
148-
$updateFunctionStub = substr_replace($updateFunctionStub,
149-
$this->writeGetterFunction($getterStub, $_column->COLUMN_NAME),
150-
-12, 0);
151+
if ( ! in_array($_column->COLUMN_NAME, ['id', 'created_at', 'deleted_at'])) {
152+
$getterFunctions .= $this->writeGetterFunction($getterStub, $_column->COLUMN_NAME);
153+
}
154+
if ($_column->COLUMN_NAME === 'updated_at') {
155+
$setterFunctions .= $this->writeSetterFunction($setterStub, $_column->COLUMN_NAME);
151156
}
152157
}
153-
$baseContent = substr_replace($baseContent, $updateFunctionStub, -1, 0);
158+
$updateFunctionStub = str_replace(["{{ GetterFunctions }}", "{{ UpdateFieldSetter }}"],
159+
[substr($getterFunctions, 0, -1), substr($setterFunctions, 0, -1)],
160+
$updateFunctionStub
161+
);
162+
163+
$functions .= $updateFunctionStub;
154164

155165
// Create "delete" and "undelete" functions if necessary
156166
$hasSoftDelete = in_array('deleted_at', $columns->pluck('COLUMN_NAME')->toArray(), true);
157167
if ($hasSoftDelete) {
158-
$baseContent = substr_replace($baseContent, $deleteAndUndeleteStub, -1, 0);
168+
$functions .= $deleteAndUndeleteStub;
159169
}
160170

171+
$baseContent = str_replace('{{ Functions }}',
172+
$functions, $baseContent);
173+
161174
$baseContent = str_replace(['{{ EntityName }}', '{{ EntityNamespace }}', '{{ FactoryName }}', '{{ FactoryNamespace }}', '{{ EntityVariableName }}', '{{ MySqlRepositoryName }}', '{{ RepositoryNamespace }}', '{{ RepositoryInterfaceName }}', '{{ TableName }}', '{{ HasSoftDelete }}'],
162175
[$entityName, $entityNamespace, $factoryName, $factoryNamespace, $entityVariableName, $mysqlRepositoryName, $repositoryNamespace, $interfaceName, $tableName, $hasSoftDelete ? 'true' : 'false'],
163176
$baseContent);
@@ -172,4 +185,4 @@ public function handle(): int
172185

173186
return 0;
174187
}
175-
}
188+
}

src/Commands/MakeRedisRepository.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ public function handle(): int
4242
$interfaceName = "I$entityName"."Repository";
4343
$redisRepositoryName = "Redis$entityName"."Repository";
4444
$redisRepositoryNamespace = config('repository.path.namespace.repositories');
45-
$relativeRedisRepositoryPath = config('repository.path.relative.repositories')."\\$entityName";
45+
$relativeRedisRepositoryPath = config('repository.path.relative.repositories')."$entityName";
4646

4747
if ($this->option('delete')) {
4848
unlink("$relativeRedisRepositoryPath/$redisRepositoryName.php");
4949
$this->info("Redis Repository \"$redisRepositoryName\" has been deleted.");
5050
return 0;
5151
}
5252

53-
if ( ! file_exists($relativeRedisRepositoryPath) && ! mkdir($relativeRedisRepositoryPath) && ! is_dir($relativeRedisRepositoryPath)) {
53+
if ( ! file_exists($relativeRedisRepositoryPath) && ! mkdir($relativeRedisRepositoryPath, 0775, true) && ! is_dir($relativeRedisRepositoryPath)) {
5454
$this->alert("Directory \"$relativeRedisRepositoryPath\" was not created");
5555
return 0;
5656
}

src/Commands/MakeRepository.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -74,17 +74,17 @@ public function handle(): int
7474
$entityNamespace = config('repository.path.namespace.entities');
7575
$factoryNamespace = config('repository.path.namespace.factories');
7676
$repositoryNamespace = config('repository.path.namespace.repositories');
77-
$relativeRepositoryPath = config('repository.path.relative.repositories')."\\$entityName";
78-
$repositoryStubsPath = config('repository.path.stub.repositories.base');
79-
$filenameWithPath = $relativeRepositoryPath.'\\'.$repositoryName.'.php';
77+
$relativeRepositoryPath = config('repository.path.relative.repositories') . "$entityName";
78+
$repositoryStubsPath = __DIR__ . '/../../' . config('repository.path.stub.repositories.base');
79+
$filenameWithPath = $relativeRepositoryPath . DIRECTORY_SEPARATOR . $repositoryName . '.php';
8080

8181
if ($this->option('delete')) {
8282
unlink("$relativeRepositoryPath/$repositoryName.php");
8383
$this->info("Repository \"$repositoryName\" has been deleted.");
8484
return 0;
8585
}
8686

87-
if ( ! file_exists($relativeRepositoryPath) && ! mkdir($relativeRepositoryPath, 775, true) && ! is_dir($relativeRepositoryPath)) {
87+
if ( ! file_exists($relativeRepositoryPath) && ! mkdir($relativeRepositoryPath, 0775, true) && ! is_dir($relativeRepositoryPath)) {
8888
$this->alert("Directory \"$relativeRepositoryPath\" was not created");
8989
return 0;
9090
}

src/Commands/MakeResource.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public function handle(): int
5656
$resourceName = $entityName."Resource";
5757
$resourceNamespace = config('repository.path.namespace.resources');
5858
$relativeResourcesPath = config('repository.path.relative.resources');
59-
$resourceStubsPath = config('repository.path.stub.resources');
59+
$resourceStubsPath = __DIR__ . '/../../' . config('repository.path.stub.resources');
6060
$filenameWithPath = $relativeResourcesPath.$resourceName.'.php';
6161

6262
if ($this->option('delete')) {
@@ -65,7 +65,7 @@ public function handle(): int
6565
return 0;
6666
}
6767

68-
if ( ! file_exists($relativeResourcesPath) && ! mkdir($relativeResourcesPath, 775, true) && ! is_dir($relativeResourcesPath)) {
68+
if ( ! file_exists($relativeResourcesPath) && ! mkdir($relativeResourcesPath, 0775, true) && ! is_dir($relativeResourcesPath)) {
6969
$this->alert("Directory \"$relativeResourcesPath\" was not created");
7070
return 0;
7171
}

src/DatabaseRepositoryServiceProvider.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct($app)
2525
{
2626
parent::__construct($app);
2727

28-
$this->baseModelStubPath = __DIR__ . '/../stubs/Models/PHP'.env('REPOSITORY_PHP_VERSION', '8.0');
28+
$this->baseModelStubPath = __DIR__ . '/../stubs/Models/PHP' .env('REPOSITORY_PHP_VERSION', '8.0');
2929
}
3030

3131
/**
@@ -48,10 +48,6 @@ public function offerPublishing(): void
4848
__DIR__ . '/../config/repository.php' => $this->app->configPath('repository.php'),
4949
], 'repository-config');
5050

51-
$this->publishes([
52-
__DIR__ . '/../stubs/PHP'.env('REPOSITORY_PHP_VERSION', '8.0') => $this->app->basePath('stubs'),
53-
], 'repository-stubs');
54-
5551
$this->publishEnums();
5652
$this->publishEntities();
5753
$this->publishFactories();

stubs/PHP8.0/repository.mysql.class.stub

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,5 @@ class {{ MySqlRepositoryName }} extends MySqlRepository implements {{ Repository
1818

1919
parent::__construct();
2020
}
21+
{{ Functions }}
2122
}

0 commit comments

Comments
 (0)