Skip to content

Commit 6f2b176

Browse files
authored
Merge pull request #2 from KLisica/directory_versioning
Directory versioning
2 parents 00db3d0 + ffe34d7 commit 6f2b176

File tree

12 files changed

+174
-21
lines changed

12 files changed

+174
-21
lines changed

config/config.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,24 @@
77
'create_requests' => true,
88
'create_repository' => true,
99
'create_controller' => true,
10-
'create_test' => true
10+
'create_test' => true,
11+
12+
// API versioning.
13+
'version' => 'v1',
14+
15+
/**
16+
* List of instances that should ignore versioning. Available:
17+
*
18+
* - controllers
19+
* - models
20+
* - repositories
21+
* - resources
22+
* - requests
23+
* - services
24+
*
25+
*/
26+
'versioning_disabled_for' => ['models'],
27+
28+
// The api route file path to autoimport resource routes.
29+
'api_file_path' => '/routes/api.php'
1130
];

src/Commands/CreateController.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ protected function getStub()
1919

2020
protected function getDefaultNamespace($rootNamespace)
2121
{
22-
return "$rootNamespace\\Http\\Controllers";
22+
$versioning_enabled
23+
= (bool) config('api._version') &&
24+
!in_array('controllers', config('api-formula.versioning_disabled_for'));
25+
26+
return $versioning_enabled
27+
? "$rootNamespace\\Http\\Controllers\\" . config('api._version')
28+
: "$rootNamespace\\Http\\Controllers";
2329
}
2430

2531
/**

src/Commands/CreateFormula.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,16 @@ public function handle()
8181

8282
if ($resourceChoice == $answers[0]) {
8383
// Create model resource.
84-
$this->call('make:resource', ['name' => $modelName . 'Resource']);
84+
$resourceName = $modelName;
85+
86+
if (
87+
(bool) config('api._version') &&
88+
!in_array('resources', config('api-formula.versioning_disabled_for'))
89+
) {
90+
$resourceName = config('api._version') . "/$modelName";
91+
}
92+
93+
$this->call('make:resource', ['name' => $resourceName . 'Resource']);
8594
}
8695

8796
if ($serviceChoice == $answers[0]) {
@@ -131,7 +140,16 @@ public function buildArchitecture(string $modelName)
131140
{
132141
// Create model resource.
133142
if (config('api-formula.create_resource')) {
134-
$this->call('make:resource', ['name' => $modelName . 'Resource']);
143+
$resourceName = $modelName;
144+
145+
if (
146+
(bool) config('api._version') &&
147+
!in_array('resources', config('api-formula.versioning_disabled_for'))
148+
) {
149+
$resourceName = config('api._version') . "/$modelName";
150+
}
151+
152+
$this->call('make:resource', ['name' => $resourceName . 'Resource']);
135153
}
136154

137155
// Create model service.

src/Commands/CreateModel.php

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

33
namespace KLisica\ApiFormula\Commands;
44

5-
use Illuminate\Console\Command;
65
use KLisica\ApiFormula\Extended\GeneratorCommand;
6+
use Illuminate\Console\Command;
77
use Illuminate\Support\Str;
88

99
class CreateModel extends GeneratorCommand
@@ -19,7 +19,13 @@ protected function getStub()
1919

2020
protected function getDefaultNamespace($rootNamespace)
2121
{
22-
return "$rootNamespace\\Models";
22+
$versioning_enabled
23+
= (bool) config('api._version') &&
24+
!in_array('models', config('api-formula.versioning_disabled_for'));
25+
26+
return $versioning_enabled
27+
? "$rootNamespace\\Models\\" . config('api._version')
28+
: "$rootNamespace\\Models";
2329
}
2430

2531
/**

src/Commands/CreateRepository.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,13 @@ protected function getStub()
1919

2020
protected function getDefaultNamespace($rootNamespace)
2121
{
22-
return "$rootNamespace\\Repositories";
22+
$versioning_enabled
23+
= (bool) config('api._version') &&
24+
!in_array('repositories', config('api-formula.versioning_disabled_for'));
25+
26+
return $versioning_enabled
27+
? "$rootNamespace\\Repositories\\" . config('api._version')
28+
: "$rootNamespace\\Repositories";
2329
}
2430

2531
/**

src/Commands/CreateRepositoryInterface.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ protected function getStub()
1818

1919
protected function getDefaultNamespace($rootNamespace)
2020
{
21-
return "$rootNamespace\\Repositories";
21+
$versioning_enabled
22+
= (bool) config('api._version') &&
23+
!in_array('repositories', config('api-formula.versioning_disabled_for'));
24+
25+
return $versioning_enabled
26+
? "$rootNamespace\\Repositories\\" . config('api._version')
27+
: "$rootNamespace\\Repositories";
2228
}
2329

2430
/**

src/Commands/CreateRequest.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ protected function getStub()
1818

1919
protected function getDefaultNamespace($rootNamespace)
2020
{
21-
return "$rootNamespace\\Http\\Requests";
21+
$versioning_enabled
22+
= (bool) config('api._version') &&
23+
!in_array('requests', config('api-formula.versioning_disabled_for'));
24+
25+
return $versioning_enabled
26+
? "$rootNamespace\\Http\\Requests\\" . config('api._version')
27+
: "$rootNamespace\\Http\\Requests";
2228
}
2329

2430
/**

src/Commands/CreateService.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,13 @@ protected function getStub()
1818

1919
protected function getDefaultNamespace($rootNamespace)
2020
{
21-
return "$rootNamespace\\Http\\Services";
21+
$versioning_enabled
22+
= (bool) config('api._version') &&
23+
!in_array('services', config('api-formula.versioning_disabled_for'));
24+
25+
return $versioning_enabled
26+
? "$rootNamespace\\Http\\Services\\" . config('api._version')
27+
: "$rootNamespace\\Http\\Services";
2228
}
2329

2430
/**

src/Commands/Stubs/Controller.stub

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace {{ namespace }};
44

55
use Illuminate\Http\Request;
6-
use App\Repositories\Interfaces\{{ model }}RepositoryInterface;
7-
use App\Http\Resources\{{ model }}Resource;
8-
use App\Http\Services\{{ model }}Service;
9-
use App\Http\Requests\{{ model }}\Create{{ model }};
10-
use App\Http\Requests\{{ model }}\Update{{ model }};
6+
use App\Http\Controllers\Controller;
7+
use {{ repository_path }};
8+
use {{ resource_path }};
9+
use {{ service_path }};
10+
use {{ create_request_path }};
11+
use {{ update_request_path }};
1112
use {{ namespacedModel }};
1213

1314
class {{ class }} extends Controller

src/Helpers/BuildReplacements.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,64 @@ class BuildReplacements
1414
public function replaceStrings(string $model_name): array
1515
{
1616
return [
17+
'{{ repository_path }}' => $this->getFilePathString('repositories', $model_name),
18+
'{{ resource_path }}' => $this->getFilePathString('resources', $model_name),
19+
'{{ service_path }}' => $this->getFilePathString('services', $model_name),
20+
'{{ create_request_path }}' => $this->getFilePathString('requests', $model_name, 'Create' . $model_name),
21+
'{{ update_request_path }}' => $this->getFilePathString('requests', $model_name, 'Update' . $model_name),
1722
'{{ namespacedModel }}' => "App\\Models\\$model_name",
1823
'{{ model }}' => $model_name,
1924
'{{ model_var }}' => Str::snake($model_name),
2025
'{{ model_lc }}' => lcfirst($model_name)
2126
];
2227
}
28+
29+
/**
30+
* Set up file path string.
31+
*
32+
* @param string $instance
33+
* @param string $model_name
34+
* @param string $fixed_model_name
35+
* @return string $path
36+
*/
37+
public function getFilePathString(string $instance, string $model_name, ?string $fixed_model_name = null): string
38+
{
39+
// Return service path.
40+
if ($instance == 'repositories') {
41+
return $this->versioningEnabled($instance)
42+
? 'App\\Http\\Resources\\' . config('api._version') . '\\' . $model_name . 'Resource'
43+
: 'App\\Http\\Resources\\' . $model_name . 'Resource';
44+
}
45+
46+
// Return service path.
47+
if ($instance == 'services') {
48+
return $this->versioningEnabled($instance)
49+
? 'App\\Http\\Services\\' . config('api._version') . '\\' . $model_name . 'Service'
50+
: 'App\\Http\\Services\\' . $model_name . 'Service';
51+
}
52+
53+
// Return request path.
54+
if ($instance == 'requests') {
55+
return $this->versioningEnabled($instance)
56+
? 'App\\Http\\Requests\\' . config('api._version') . "\\$model_name\\$fixed_model_name"
57+
: "App\\Http\\Requests\\$model_name\\$fixed_model_name";
58+
}
59+
60+
// By default returning repository interface path.
61+
return $this->versioningEnabled($instance)
62+
? 'App\\Repositories\\' . config('api._version') . '\\Interfaces\\' . $model_name . 'RepositoryInterface'
63+
: 'App\\Repositories\\Interfaces\\' . $model_name . 'RepositoryInterface';
64+
}
65+
66+
/**
67+
* Check if versioning is enabled for specific instance.
68+
*
69+
* @param string $instance
70+
* @return bool
71+
*/
72+
public function versioningEnabled(string $instance): bool
73+
{
74+
return (bool) config('api._version') &&
75+
!in_array($instance, config('api-formula.versioning_disabled_for'));
76+
}
2377
}

0 commit comments

Comments
 (0)