Skip to content
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

Merged
merged 7 commits into from
Nov 8, 2016
Merged

Artisan generators #26

merged 7 commits into from
Nov 8, 2016

Conversation

jstoone
Copy link
Contributor

@jstoone jstoone commented Oct 31, 2016

This is the initial work for what's been discussed in #25.


The good ol' todo

  • Create Generators
    • Hydrator
    • Resource
    • Schema
    • Search (Eloquent only)
    • Validators
    • Request
    • Resource (Creates all of the above)
  • Create eloquent implementation stubs
    • stubs/eloquent/hydrator.stub
    • stubs/eloquent/schema.stub
    • stubs/eloquent/search.stub (Eloquent only)
  • Create non-eloquent implementation stubs
    • stubs/abstract/hydrator.stub
    • stubs/abstract/schema.stub
  • Create implementation agnostic stubs
    • stubs/independent/validators.stub
    • stubs/independent/equest.stub
  • Make it possible to not use eloquent
  • Make it possible to change namespace format as mentioned in [Proposal] Add artisan command class generators #25
  • Add extra flags to ResourceMakeCommand
    • Implement --only=foo,bar
    • Implement --except=foo,bar
  • Only run SearchMakeCommand if eloquent == true
  • Write documentation

@lindyhopchris lindyhopchris changed the base branch from master to develop November 1, 2016 09:30
*/
'generator' => [
'namespace' => 'JsonApi',
'by_resource' => true,
Copy link
Member

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.

Copy link
Member

@lindyhopchris lindyhopchris left a 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.'],
Copy link
Member

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.

Copy link
Member

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 = [
//
];
}
Copy link
Member

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) {
Copy link
Member

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?

Copy link
Contributor Author

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;
Copy link
Member

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';
Copy link
Member

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 = [
//
];
}
Copy link
Member

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.

@lindyhopchris
Copy link
Member

PS: haven't used this review feature before! I do appreciate you're sharing unfinished work!!

@jstoone
Copy link
Contributor Author

jstoone commented Nov 1, 2016

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.

@jstoone
Copy link
Contributor Author

jstoone commented Nov 2, 2016

Changes from the latest commit fixes all of your comments from the review:

  • Use dashes in config
  • Fix typo in $isIndependent variable
  • Move stubs to root folder
  • Remove --eloquent option from independent generators
  • Clean up isEloquent() logic
  • Add getResourceType()-method to schema stub
  • Add allowedFilterParameters() to eloquent stub

Extra changes

  • Cleaned up getStub()-method
  • Request is now independent
  • Simplified command registration in ServiceProvider
  • independent is now a type, and has its own folder.

@jstoone
Copy link
Contributor Author

jstoone commented Nov 3, 2016

Alright! The generators are now feature complete, and ready for you to lay your eyes upon them. 🎃

Change log since last commit-burst:

  • --no-eloquent-flag now works
  • You can now how choose between two namespace models:
    • By resource name e.g: JsonApi\Tasks\Schema
    • By class type e.g: JsonApi\Schemas\Task
  • The Search class now only gets generated if configured for eloquent
  • A bit of documentation added
  • Found a config item that still was using an underscore
    • 'use_eloquent => 'use-eloquent'`

Copy link
Member

@lindyhopchris lindyhopchris left a 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 |-------------------------------------------------------------------------- |
Copy link
Member

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.
Copy link
Member

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
Copy link
Member

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.
Copy link
Member

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.

Copy link
Contributor Author

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,
]
Copy link
Member

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;
}
}
Copy link
Member

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
Copy link
Member

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
Copy link
Member

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';
Copy link
Member

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,
Copy link
Member

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?

@jstoone
Copy link
Contributor Author

jstoone commented Nov 7, 2016

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

  • Corrected config/json-api.php
    • Sentences/re-wording
    • Spelling
    • Formatting
  • Renamed JsonApiGeneratorCommand to AbstractGeneratorCommand
  • Change resource type from using camel_case to snake_case
  • General typos
  • Lowercase the generator type when creating stub path
  • Correct abstract hydrator stub
  • Correct abstract schema stub
  • Add missing semi-colon to namespace in:
    • Independent validators stub
    • Eloquent Search stub
  • Change generator descriptions to "JSON API"
  • Reformat licence in in ServiceProvider

@lindyhopchris lindyhopchris merged commit 1804aa5 into cloudcreativity:develop Nov 8, 2016
@lindyhopchris
Copy link
Member

@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!)

@jstoone
Copy link
Contributor Author

jstoone commented Nov 8, 2016

@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. :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants