8000 Implemented rest of the generator featureset · m-bymike/laravel-json-api@40a0c2a · GitHub
[go: up one dir, main page]

Skip to content

Commit 40a0c2a

Browse files
committed
Implemented rest of the generator featureset
1 parent 004eae9 commit 40a0c2a

File tree

5 files changed

+239
-51
lines changed

5 files changed

+239
-51
lines changed

config/json-api.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,6 @@
174174
'generator' => [
175175
'namespace' => 'JsonApi',
176176
'by-resource' => true,
177-
'use_eloquent' => true,
177+
'use-eloquent' => true,
178178
]
179179
];

src/Console/Commands/JsonApiGeneratorCommand.php

Lines changed: 45 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,33 @@ abstract class JsonApiGeneratorCommand extends LaravelGeneratorCommand
3131
protected $type;
3232

3333
/**
34-
* Whether the resource type is non-dependent on eloquent
34+
* Whether the resource type is non-dependent on eloquent.
3535
*
3636
* @var boolean
3737
*/
3838
protected $isIndependent = false;
3939

40+
/**
41+
* Whether the resource should use eloquent implementations.
42+
*
43+
* @var boolean
44+
*/
45+
protected $useEloquent;
46+
47+
/**
48+
* The folder within the root namespace, where files should be generated.
49+
*
50+
* @var string
51+
*/
52+
protected $subNamespace;
53+
54+
/**
55+
* Whether generated files should be grouped by their resource files.
56+
*
57+
* @var mixed
58+
*/
59+
protected $namespaceByResource;
60+
4061
/**
4162
* The location of all generator stubs
4263
*
@@ -53,13 +74,13 @@ public function __construct(Filesystem $files)
5374
{
5475
parent::__construct($files);
5576

77+
$this->useEloquent = config('json-api.generator.use-eloquent', true);
5678
$this->subNamespace = config('json-api.generator.namespace', 'JsonApi');
57-
$this->useEloquent = config('json-api.generator.use_eloquent', true);
79+
$this->namespaceByResource = config('json-api.generator.by-namespace', true);
5880
}
5981

6082
/**
6183
* Build the class with the given name.
62-
*
6384
* Remove the base controller import if we are already in base namespace.
6485
*
6586
* @param string $name
@@ -72,7 +93,7 @@ protected function buildClass($name)
7293
$stub = $this->files->get($this->getStub());
7394

7495
$this->replaceNamespace($stub, $name)
75-
->replaceResourceType($stub, $this->getPluralizedResource());
96+
->replaceResourceType($stub, $this->getResourceName());
7697

7798
return $stub;
7899
}
@@ -92,24 +113,20 @@ protected function replaceResourceType(&$stub, $resource)
92113
return $this;
93114
}
94115

95-
/**
96-
* Get the plural version of the resource name
97-
*
98-
* @return string
99-
*/
100-
protected function getPluralizedResource()
101-
{
102-
return str_plural($this->getResourceName());
103-
}
104-
105116
/**
106117
* Get the resource name
107118
*
108119
* @return string
109120
*/
110121
protected function getResourceName()
111122
{
112-
return ucwords($this->argument('resource'));
123+
$name = ucwords($this->argument('resource'));
124+
125+
if($this->namespaceByResource) {
126+
return str_plural($name);
127+
}
128+
129+
return $name;
113130
}
114131

115132
/**
@@ -119,7 +136,11 @@ protected function getResourceName()
119136 F438
*/
120137
protected function getNameInput()
121138
{
122-
return trim($this->type);
139+
if( ! $this->namespaceByResource) {
140+
return str_plural($this->type);
141+
}
142+
143+
return $this->type;
123144
}
124145

125146
/**
@@ -164,7 +185,11 @@ private function getStubFor($implementationType)
164185
*/
165186
private function isEloquent()
166187
{
167-
if($this->isIndependent){
188+
if($this->isIndependent) {
189+
return false;
190+
}
191+
192+
if($this->option('no-eloquent')) {
168193
return false;
169194
}
170195

@@ -182,10 +207,7 @@ protected function getDefaultNamespace($rootNamespace)
182207
return implode('', [
183208
$rootNamespace,
184209
'\\',
185-
$this->subNamespace,
186-
'\\',
187-
$this->getPluralizedResource()
188-
]);
210+
$this->subNamespace, '\\', $this->getResourceName() ]);
189211
}
190212

191213
/**
@@ -212,7 +234,8 @@ protected function getOptions()
212234
}
213235

214236
return [
215-
['eloquent', 'e', InputOption::VALUE_OPTIONAL, 'Use eloquent as adapter.'],
237+
['eloquent', 'e', InputOption::VALUE_NONE, 'Use eloquent as adapter'],
238+
['no-eloquent', 'ne', InputOption::VALUE_NONE, 'Use an abstract adapter'],
216239
];
217240
}
218241
}

src/Console/Commands/ResourceMakeCommand.php

Lines changed: 135 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,22 @@
33
namespace CloudCreativity\LaravelJsonApi\Console\Commands;
44

55
use Illuminate\Console\Command;
6+
use CloudCreativity\LaravelJsonApi\Console\Commands\RequestMakeCommand;
7+
use CloudCreativity\LaravelJsonApi\Console\Commands\ValidatorsMakeCommand;
8+
use CloudCreativity\LaravelJsonApi\Console\Commands\HydratorMakeCommand;
9+
use CloudCreativity\LaravelJsonApi\Console\Commands\SchemaMakeCommand;
10+
use CloudCreativity\LaravelJsonApi\Console\Commands\SearchMakeCommand;
11+
use Illuminate\Support\Collection;
12+
use Symfony\Component\Console\Input\InputOption;
13+
use Symfony\Component\Console\Input\InputArgument;
614

715
class ResourceMakeCommand extends Command
816
{
917
/**
1018
* The name and signature of the console command.
1119
*
12-
* @var string
13-
*/
14-
protected $signature = 'make:json-api:resource
15-
{resource : The resource to create files for}
16-
{--eloquent : Use eloquent as adapter}';
20+
* @var string */
21+
protected $name = 'make:json-api:resource';
1722

1823
/**
1924
* The console command description.
@@ -22,23 +27,19 @@ class ResourceMakeCommand extends Command
2227
*/
2328
protected $description = 'Create a full Json Api resource.';
2429

25-
private $commands = [
26-
'make:json-api:hydrator',
27-
'make:json-api:schema',
28-
'make:json-api:search',
29-
'make:json-api:request',
30-
'make:json-api:validators',
31-
];
32-
3330
/**
34-
* Create a new command instance.
31+
* The available generator commands.
3532
*
36-
* @return void
33+
* @var array
3734
*/
38-
public function __construct()
39-
{
40-
parent::__construct();
41-
}
35+
private $commands = [
36+
'make:json-api:request' => RequestMakeCommand::class,
37+
'make:json-api:validators' => ValidatorsMakeCommand::class,
38+
39+
'make:json-api:hydrator' => HydratorMakeCommand::class,
40+
'make:json-api:schema' => SchemaMakeCommand::class,
41+
'make:json-api:search' => SearchMakeCommand::class,
42+
];
4243

4344
/**
4445
* Execute the console command.
@@ -50,17 +51,123 @@ public function handle()
5051
$resourceParameter = [
5152
'resource' => $this->argument('resource'),
5253
];
53-
$eloquentParameter = [
54+
$adapterParameters = array_merge($resourceParameter, [
5455
'--eloquent' => $this->option('eloquent'),
55-
];
56+
'--no-eloquent' => $this->option('no-eloquent'),
57+
]);
58+
59+
$commands = collect($this->commands);
60+
61+
// Filter out any commands the user asked os to.
62+
if($this->option('only') || $this->option('except')) {
63+
$type = $this->option('only') ? 'only' : 'except';
64+
65+
$commands = $this->filterCommands($commands, $type);
66+
}
5667

57-
// Call independent generators
58-
$this->call('make:json-api:validators', $resourceParameter);
59-
$this->call('make:json-api:request', $resourceParameter);
68+
// The search unit is only for eloquent.
69+
if( ! $this->isEloquent()) {
70+
$commands->forget('make:json-api:search');
71+
}
72+
73+
// Run independent commands.
74+
$this->runCommandsWithParameters($commands->only([
75+
'make:json-api:request',
76+
'make:json-api:validators',
77+
]), $resourceParameter);
78+
79+
// Run adapter commands.
80+
$this->runCommandsWithParameters($commands->only([
81+
'make:json-api:hydrator',
82+
'make:json-api:schema',
83+
'make:json-api:search',
84+
]), $adapterParameters);
85+
86+
// Just tell the user, if no files are created
87+
if($commands->isEmpty()) {
88+
$this->info('No files created.');
89+
}
90+
91+
// Give the user a digial high-five.
92+
$this->comment('All done, keep doing what you do.');
93+
}
94+
95+
/**
96+
* Filters out commands using either 'except' or 'only' filter.
97+
*
98+
* @param Collection $commands
99+
* @param string $type
100+
*
101+
* @return Collection
102+
*/
103+
private function filterCommands(Collection $commands, $type)
104+
{
105+
$baseCommandName = 'make:json-api:';
106+
$filterValues = explode(',', $this->option($type));
107+
108+
$targetCommands = collect($filterValues)
109+
->map(function($target) use ($baseCommandName) {
110+
return $baseCommandName . strtolower(trim($target));
111+
});
112+
113+
return $commands->{$type}($targetCommands->toArray());
114+
}
60115

61-
// Call configurable commands
62-
$this->call('make:json-api:hydrator', array_merge($resourceParameter, $eloquentParameter));
63-
$this->call('make:json-api:schema', array_merge($resourceParameter, $eloquentParameter));
64-
$this->call('make:json-api:search', array_merge($resourceParameter, $eloquentParameter));
116+
/**
117+
* Runs the given commands and paases them all the given parameters.
118+
*
119+
* @param Collection $commands
120+
* @param array $parameters
121+
*
122+
* @return void
123+
*/
124+
private function runCommandsWithParameters(Collection $commands, array $parameters)
125+
{
126+
$commands->keys()->each(function($command) use ($parameters) {
127+
$this->call($command, $parameters);
128+
});
129+
}
130+
131+
/**
132+
* Determine whether the generator should use eloquent or not.
133+
*
134+
* @return boolean
135+
*/
136+
private function isEloquent()
137+
{
138+
$useEloquent = config('json-api.generator.use-eloquent', true);
139+
140+
if($this->option('no-eloquent')) {
141+
return false;
142+
}
143+
144+
return $this->option('eloquent') ?: $useEloquent;
145+
}
146+
147+
/**
148+
* Get the console command arguments.
149+
*
150+
* @return array
151+
*/
152+
protected function getArguments()
153+
{
154+
return [
155+
['resource', InputArgument::REQUIRED, 'The resource to create files for'],
156+
];
157+
}
158+
159+
/**
160+
* Get the console command options.
161+
*
162+
* @return array
163+
*/
164+
protected function getOptions()
165+
{
166+
return [
167+
['eloquent', 'e`', InputOption::VALUE_NONE, 'Use eloquent as adapter'],
168+
['no-eloquent', 'ne', InputOption::VALUE_NONE, 'Use an abstract adapter'],
169+
['only', 'o', InputOption::VALUE_OPTIONAL, 'Specifiy the exact resources you\'d like.'],
170+
['except', 'ex', InputOption::VALUE_OPTIONAL, 'Specifiy the resources you\'d like to skip.'],
171+
];
65172
}
66173
}

stubs/abstract/hydrator.stub

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace DummyNamespace;
4+
5+
use CloudCreativity\JsonApi\Hydrator\AbstractHydrator;
6+
use CloudCreativity\JsonApi\Hydrator\RelatedHydratorTrait;
7+
use CloudCreativity\JsonApi\Contracts\Hydrator\HydratesRelatedInterface;
8+
9+
class Hydrator extends AbstractHydrator implements HydratesRelatedInterface
10+
{
11+
use RelatedHydratorTrait;
12+
13+
/**
14+
* @var array
15+
*/
16+
protected $attributes = [
17+
//
18+
];
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $relationships = [
24+
//
25+
];
26+
27+
}
28+

0 commit comments

Comments
 (0)
0