-
Notifications
You must be signed in to change notification settings - Fork 109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Artisan generators #26
Artisan generators #26
Conversation
*/ | ||
'generator' => [ | ||
'namespace' => 'JsonApi', | ||
'by_resource' => true, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can these be dasherized rather than underscored? I.e. by-resource
. Just keeps it consistent with how the rest of the config file is written.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking really good so far. Have added a number of comments but this is definitely heading in the right direction!
protected function getOptions() | ||
{ | ||
return [ | ||
['eloquent', 'e', InputOption::VALUE_OPTIONAL, 'Use eloquent as adapter.'], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So my preference would be to only register the Eloquent option if it is an option for the specific command. Checking $this->isIndependent
would work I think.
The reason being is that if (for example) you do php artisan make:json-api:search -h
it shows an Eloquent option even though the Eloquent option is not used by that command. Which I think is potentially confusing.
I appreciate this means you'll have to call each command individually rather than doing a foreach
loop in the resource generator, but I think it's clearer for users to understand.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the eloquent option, I think have --eloquent
and --no-eloquent
as boolean options. If --eloquent
and --no-eloquent
both return false
(i.e. neither have been provided) then fall back to the value in config. I.e...
protected function isEloquent()
{
if ($this->isIndependent) {
return false;
}
if ($this->option('no-eloquent')) {
return false;
}
return $this->option('eloquent') ?: $this->useEloquent;
}
protected $attributes = [ | ||
// | ||
]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stub should have the following in it:
/**
* @return string
*/
public function getResourceType()
{
return self::RESOURCE_TYPE;
}
protected function bindArtisanCommands() | ||
{ | ||
foreach( $this->generatorCommands as $key => $command ) { | ||
$this->app->singleton($key, function($app) use ($command) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I don't ever register commands as singletons. Is this recommended Laravel usage?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is, if singleton makes sense, but I found out that it was redundant to register the commands as singleton instances, since the binding will happen in a more optimized way behind the scenes, when they get registered with artisan.
* | ||
* @var mixed | ||
*/ | ||
protected $isIndependant = false; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typo in variable name - should be isIndependent
protected function getStub() | ||
{ | ||
if($this->isIndependant) { | ||
return __DIR__.'/stubs/'.strtolower($this->type).'.stub'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we move the stubs
directory to the root project folder. I have a strong belief that the src
folder should just be for classes!
protected $hasMany = [ | ||
// | ||
]; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the Eloquent request stub, can we add the following:
/**
* @var array
*/
protected $allowedFilteringParameters = [
'id',
];
Because by default the search supports that as a filtering parameter.
PS: haven't used this review feature before! I do appreciate you're sharing unfinished work!! |
Thanks a bunch for giving good feedback on my code. I think you did very well on using the feature. :) I'll keep you posted as I do progress. |
Changes from the latest commit fixes all of your comments from the review:
Extra changes
|
Alright! The generators are now feature complete, and ready for you to lay your eyes upon them. 🎃 Change log since last commit-burst:
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is looking really great! Can't wait to use it in my applications.
A number of comments to address, then it'll be ready to merge. I've picked up on a few code-style things but I'm not overly worried about it - as I reformat the package code using PHP Storm when I'm preparing a release.
| JSON API Namespaces | ||
|-------------------------------------------------------------------------- | ||
| | ||
|-------------------------------------------------------------------------- | JSON API Namespaces |-------------------------------------------------------------------------- | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this heading be put back to the format that it was before?
|-------------------------------------------------------------------------- | ||
| | ||
| With this package comes a set of handy generators. Which makes it possible | ||
| to easily generate every class needed to implement an api resource. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can this sentence be changed to:
This package supplies a set of handy generators. These make it possible
to easily generate every class needed to implement a JSON API resource.
| we have included a few handy configuration options, so that the generators | ||
| can follow your workflow. | ||
| | ||
| `namespace` = The folder in which you will be storing everyting |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
typo: everything
| e.g. \App\JsonApi\Schemas\{User, Post, Comment} | ||
| e.g. \App\JsonApi\Requests\{User, Post, Comment} | ||
| | ||
| `use_eloquent` = Whether you are using Eloquent ORM in this app. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggested re-wording for the explanation here:
Whether your JSON API resources relate to Eloquent models or not.
You can override the setting here when running a generator. If the
setting here is `true` running a generator with `--no-eloquent` will
override it; if the setting is `false`, then `--eloquent` is the override.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't know why this has not been marked as complete, but I've changed the text as mentioned. I hope I didn't misunderstand you. :)
'namespace' => 'JsonApi', | ||
'by-resource' => true, | ||
'use-eloquent' => true, | ||
] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requires a trailing comma for consistency with coding style.
{ | ||
return self::RESOURCE_TYPE; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The following methods can be added to this stub:
/**
* @param object $resource
* @return mixed
*/
public function getId($resource)
{
// TODO
}
/**
* @param object $resource
* @return mixed
*/
public function getAttributes($resource)
{
// TODO
}
@@ -0,0 +1,28 @@ | |||
<?php | |||
|
|||
namespace DummyNamespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing semi-colon
@@ -0,0 +1,43 @@ | |||
<?php | |||
|
|||
namespace DummyNamespace |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing semi-colon
* | ||
* @var string | ||
*/ | ||
protected $description = 'Create a new json-api resource hydrator'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
On all of the descriptions, can we change json-api
to JSON API
@@ -9,8 +9,7 @@ | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you put this back to what it was?
Awesome review, awesome job!I've now run through every review you've made and change-log is below. You are a very sweet reviewer, and have a very sharp eye! Lucky me that you've almost written down, to the word, what changes I should make. Easy peasy. Such a pleasure working like this. 👍 Changelog
|
@jstoone awesome work - thanks so much for doing this, it's a fantastic addition to the package and something that I wouldn't have got to for a while because of work deadlines. If you have any other proposal for additions to the package do let me know via issues! (Not saying you'd have to do the pull requests but would be great to hear your ideas!) |
@lindyhopchris you are very welcome! As I said before, it was a pleasure doing this and working with you. Let's see what we'll figure out next. :) |
This is the initial work for what's been discussed in #25.
The good ol' todo
stubs/eloquent/hydrator.stub
stubs/eloquent/schema.stub
stubs/eloquent/search.stub
(Eloquent only)stubs/abstract/hydrator.stub
stubs/abstract/schema.stub
stubs/independent/validators.stub
stubs/independent/equest.stub
ResourceMakeCommand
--only=foo,bar
--except=foo,bar
SearchMakeCommand
ifeloquent == true