Skip to content

Commit 4316740

Browse files
committed
Merge branch 'release/0.5.2'
2 parents 8886525 + b79602f commit 4316740

19 files changed

+1028
-1
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ charset = utf-8
77
trim_trailing_whitespace = true
88
insert_final_newline = true
99

10-
[*.php]
10+
[*.{php,stub}]
1111
indent_size = 4
1212

1313
[*.md]

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22
All notable changes to this project will be documented in this file. This project adheres to
33
[Semantic Versioning](http://semver.org/) and [this changelog format](http://keepachangelog.com/).
44

5+
## [0.5.2] - 2016-11-11
6+
7+
### Added
8+
- Generator commands to generate the classes required for a JSON API resource. To view available commands use
9+
`php artisan list make:json-api`. The main command is `make:json-api:resource` which generates multiple classes for
10+
a JSON API resource at once. Credit to @jstoone for contributing this feature.
11+
512
## [0.5.1] - 2016-11-09
613

714
### Changed

UPGRADE.md

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

33
This file provides notes on how to upgrade between versions.
44

5+
## v0.5.0|v0.5.1 to v0.5.2
6+
7+
Version `0.5.2` adds generator commands to your application. We've updated the configuration so you will need to
8+
add the `generator` config array from the bottom of the `config/json-api.php` to the `json-api.php` config file
9+
in your application.
10+
11+
This however is optional as the generators will work without this configuration being added.
12+
513
## v0.4 to v0.5
614

715
Refer to the [UPGRADE-0.5.md](UPGRADE-0.5.md) file for instructions.

config/json-api.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,43 @@
143143
*/
144144
'adapters' => [],
145145

146+
/*
147+
|--------------------------------------------------------------------------
148+
| Generators
149+
|--------------------------------------------------------------------------
150+
|
151+
| This package supplies a set of handy generators. These make it possible
152+
| to easily generate every class needed to implement a JSON API resource.
153+
|
154+
| So that we do not enforce any specific patterns, we have included a few
155+
| handy configuration options. This is so that the generators can follow
156+
| your workflow.
157+
|
158+
| ## namespace (default: 'JsonApi')
159+
|
160+
| The folder in which you will be storing everything related to your JSON
161+
| API implementation.
162+
|
163+
| ## by-resource (default: true)
164+
|
165+
| How you are organising your JSON API classes within your application.
166+
|
167+
| - true: e.g. \App\JsonApi\Tasks\{Schema, Request, Hydrator}
168+
| - false:
169+
| - e.g. \App\JsonApi\Schemas\{User, Post, Comment}
170+
| - e.g. \App\JsonApi\Requests\{User, Post, Comment}
171+
|
172+
| ## use-eloquent (default: true)
173+
|
174+
| Whether your JSON API resources predominantly relate to Eloquent models.
175+
| You can override the setting here when running a generator. If the
176+
| setting here is `true` running a generator with `--no-eloquent` will
177+
| override it; if the setting is `false`, then `--eloquent` is the override.
178+
|
179+
*/
180+
'generator' => [
181+
'namespace' => 'JsonApi',
182+
'by-resource' => true,
183+
'use-eloquent' => true,
184+
],
146185
];
Lines changed: 270 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,270 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2016 Cloud Creativity Limited
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace CloudCreativity\LaravelJsonApi\Console\Commands;
20+
21+
use Illuminate\Console\GeneratorCommand;
22+
use Illuminate\Filesystem\Filesystem;
23+
use Symfony\Component\Console\Input\InputArgument;
24+
use Symfony\Component\Console\Input\InputOption;
25+
26+
/**
27+
* Class AbstractGeneratorCommand
28+
*
29+
* @package CloudCreativity\LaravelJsonApi
30+
*/
31+
abstract class AbstractGeneratorCommand extends GeneratorCommand
32+
{
33+
34+
/**
35+
* The console command name.
36+
*
37+
* @var string
38+
*/
39+
protected $name;
40+
41+
/**
42+
* The console command description.
43+
*
44+
* @var string
45+
*/
46+
protected $description;
47+
48+
/**
49+
* The type of class being generated.
50+
*
51+
* @var string
52+
*/
53+
protected $type;
54+
55+
/**
56+
* Whether the resource type is non-dependent on eloquent.
57+
*
58+
* @var boolean
59+
*/
60+
protected $isIndependent = false;
61+
62+
/**
63+
* Whether the resource should use eloquent implementations.
64+
*
65+
* @var boolean
66+
*/
67+
protected $useEloquent;
68+
69+
/**
70+
* The folder within the root namespace, where files should be generated.
71+
*
72+
* @var string
73+
*/
74+
protected $subNamespace;
75+
76+
/**
77+
* Whether generated files should be grouped by their resource files.
78+
*
79+
* @var mixed
80+
*/
81+
protected $namespaceByResource;
82+
83+
/**
84+
* The location of all generator stubs
85+
*
86+
* @var string
87+
*/
88+
private $stubsDirectory = __DIR__ . '/../../../stubs';
89+
90+
/**
91+
* Create a new config clear command instance.
92+
*
93+
* @param Filesystem $files
94+
*/
95+
public function __construct(Filesystem $files)
96+
{
97+
parent::__construct($files);
98+
99+
$this->useEloquent = config('json-api.generator.use-eloquent', true);
100+
$this->subNamespace = config('json-api.generator.namespace', 'JsonApi');
101+
$this->namespaceByResource = config('json-api.generator.by-resource', true);
102+
}
103+
104+
/**
105+
* Build the class with the given name.
106+
* Remove the base controller import if we are already in base namespace.
107+
*
108+
* @param string $name
109+
* @return string
110+
*/
111+
protected function buildClass($name)
112+
{
113+
$stub = $this->files->get($this->getStub());
114+
115+
$this->replaceNamespace($stub, $name)
116+
->replaceResourceType($stub, $this->getResourceName());
117+
118+
return $stub;
119+
}
120+
121+
/**
122+
* Replace the value of the resource type constant
123+
*
124+
* @param mixed $stub
125+
* @param mixed $resource
126+
* @return $this
127+
*/
128+
protected function replaceResourceType(&$stub, $resource)
129+
{
130+
$stub = str_replace('dummyResourceType', snake_case($resource, '-'), $stub);
131+
132+
return $this;
133+
}
134+
135+
/**
136+
* Get the resource name
137+
*
138+
* @return string
139+
*/
140+
protected function getResourceName()
141+
{
142+
$name = ucwords($this->argument('resource'));
143+
144+
if ($this->namespaceByResource) {
145+
return str_plural($name);
146+
}
147+
148+
return $name;
149+
}
150+
151+
/**
152+
* Get the desired class name from the input.
153+
*
154+
* @return string
155+
*/
156+
protected function getNameInput()
157+
{
158+
if (!$this->namespaceByResource) {
159+
return $this->getResourceName();
160+
}
161+
162+
return $this->type;
163+
}
164+
165+
/**
166+
* Get the stub file for the generator.
167+
*
168+
* @return string
169+
*/
170+
protected function getStub()
171+
{
172+
if ($this->isIndependent) {
173+
return $this->getStubFor('independent');
174+
}
175+
176+
if ($this->isEloquent()) {
177+
return $this->getStubFor('eloquent');
178+
}
179+
180+
return $this->getStubFor('abstract');
181+
}
182+
183+
/**
184+
* Get the stub for specific generator type
185+
*
186+
* @param string $implementationType
187+
* @return string
188+
*/
189+
private function getStubFor($implementationType)
190+
{
191+
return implode('', [
192+
$this->stubsDirectory,
193+
'/',
194+
$implementationType,
195+
'/',
196+
lcfirst($this->type),
197+
'.stub',
198+
]);
199+
}
200+
201+
/**
202+
* Determine whether a resource is eloquent or not
203+
*
204+
* @return boolean
205+
*/
206+
private function isEloquent()
207+
{
208+
if ($this->isIndependent) {
209+
return false;
210+
}
211+
212+
if ($this->option('no-eloquent')) {
213+
return false;
214+
}
215+
216+
return $this->option('eloquent') ?: $this->useEloquent;
217+
}
218+
219+
/**
220+
* Get the default namespace for the class.
221+
*
222+
* @param string $rootNamespace
223+
* @return string
224+
*/
225+
protected function getDefaultNamespace($rootNamespace)
226+
{
227+
$namespace = [
228+
$rootNamespace, // #0
229+
'\\', // #1
230+
$this->subNamespace, // #2
231+
'\\', // #3
232+
$this->getResourceName() // #4
233+
];
234+
235+
if (!$this->namespaceByResource) {
236+
$namespace[4] = str_plural($this->type);
237+
}
238+
239+
return implode('', $namespace);
240+
}
241+
242+
/**
243+
* Get the console command arguments.
244+
*
245+
* @return array
246+
*/
247+
protected function getArguments()
248+
{
249+
return [
250+
['resource', InputArgument::REQUIRED, "The resource for which a {$this->type} class will be generated"],
251+
];
252+
}
253+
254+
/**
255+
* Get the console command options.
256+
*
257+
* @return array
258+
*/
259+
protected function getOptions()
260+
{
261+
if ($this->isIndependent) {
262+
return [];
263+
}
264+
265+
return [
266+
['eloquent', 'e', InputOption::VALUE_NONE, 'Use eloquent as adapter'],
267+
['no-eloquent', 'ne', InputOption::VALUE_NONE, 'Use an abstract adapter'],
268+
];
269+
}
270+
}

0 commit comments

Comments
 (0)