-
-
Notifications
You must be signed in to change notification settings - Fork 142
feat(console): add make:controller and make:model commands
#647
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
feat(console): add make:controller and make:model commands
#647
Conversation
brendt
left a comment
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.
Looks really nice now! I left a review, but there's one thing I'm unsure about that I wanted to discuss with this PR.
We now also have the concept of "installers": https://tempestphp.com/docs/internals/package-development/
These installers aren't the same as stub generators, but there are some similarities. I wonder if we can make it so that these generators "feel" as close as possible to installers?
In particular, I'm thinking about the PublishesFiles trait, which is actually very similar to what StubFileGenerator does. It already has support for both files and classes, it will also take care of updating imports where necessary, and it will handle the override part automatically.
Here's an example of what that would look like:
final class MakeControllerCommand
{
use PublishesFiles;
use HasGeneratorCommand;
#[ConsoleCommand(
name : 'make:controller',
description: 'Create a new controller class with a basic route',
aliases : ['controller:make', 'controller:create', 'create:controller'],
)]
public function __invoke(
#[ConsoleArgument(
help: 'The name of the controller class to create ( "Controller" will be suffixed )',
)]
string $className,
#[ConsoleArgument(
help: 'The route path inside the controller',
)]
?string $controllerPath = null,
#[ConsoleArgument(
help: 'The view name to return in the controller',
)]
?string $controllerView = null,
): void {
$suggestedPath = $this->getSuggestedPath(
className : $className,
pathPrefix : 'Controllers',
classSuffix: 'Controller',
);
$targetPath = $this->promptTargetPath($suggestedPath);
$this->publish(
source: ControllerStub::class,
destination: $targetPath,
callback: function (string $source, string $destination) use ($controllerView, $controllerPath): void
{
$content = str(file_get_contents($destination));
$replacements = arr([
'dummy-path' => $controllerPath,
'dummy-view' => $controllerView,
]);
file_put_contents(
$destination,
$content->replace(
$replacements->keys()->toArray(),
$replacements->values()->toArray()
)
);
}
);
}
}We could of course improve the PublishesFiles trait to have a shorthand for string replacements as well, something like:
$this->publish(
source: ControllerStub::class,
destination: $targetPath,
replace: [
'dummy-path' => $controllerPath,
'dummy-view' => $controllerView,
],
);I think it would be nice if the PublishesFiles trait could be reused. I realise it goes against my first suggestion of extracting StubFileGenerator, but I only realised the similarities after reading through the simplified implementation just now.
It's an iterative process, I hope you'll indulge me 😅
Anyway, I'm totally open to discuss this, I just want to make sure we go with the absolute easiest, most consistent approach 👍 And, of course, really great job on the PR already, we're getting there! 💪
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
|
I agree with all you said. I have another idea, what about keep the I think of this because the Then the WDYT ? Another question, in general and for this PR, do you think the generators commands (e.g. |
The downside here is that now Apart from that, I think it's a good idea to have the trait simple wrap an underlying service, just like we do with
The third option, actually, it should live in So, I realise it won't be available if people install |
innocenzi
left a comment
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.
Here are a few style nitpicks, as well as a concern regarding conventions suggested by the implementation.
Also, the pull request title should be:
feat(console): add `make:controller` and `make:model` commands
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeControllerCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeModelCommand.php
Outdated
Show resolved
Hide resolved
src/Tempest/Console/src/Commands/Generators/MakeModelCommand.php
Outdated
Show resolved
Hide resolved
@brendt So, we should provide the Then how I must handle the fact that |
|
@innocenzi Huge thanks for your review and requested changes 🙏
Thanks for that aswell 😄 I will try to fix everything you pointed out |
Pull Request Test Coverage Report for Build 11800557345Warning: This coverage report may be inaccurate.This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.
Details
💛 - Coveralls |
|
@gturpin-dev I saw you changed the PR title, but it's still wrong (missing the colon after the commit type, missing backticks around commands and wrong scope) 😅 You can copy paste this one: |
make:controller and make:model commands
|
@innocenzi Nevermind this is not my day 😂 |
Correct
That's the beauty of discovery :) If you install one of the standalone components in a project that doesn't have |
This is what I believed but I was wondering about the fact theoretically anyone can call itself and break things, but nobody have to do that, so I think it's ok 😅 |
Yeah, no reason to worry about it :) Let me know when I need to review again? |
|
@brendt Sure ! I'm still working on things we've discussed. Do you think some tests on those commands ( and the |
Hmm no tests are pretty crucial, but I'm happy to help you with them :) Here's what we can do: you tell me which parts you think should definitely be tested, and I'll write those tests. I think it would be a good opportunity for you to learn to identify testable parts, and then you'll get an example of how those tests can be written, which you can learn from again. |
Absolutely, would love this 🙏 |
Co-authored-by: Enzo Innocenzi <[email protected]>
Co-authored-by: Enzo Innocenzi <[email protected]>
Co-authored-by: Enzo Innocenzi <[email protected]>
Co-authored-by: Enzo Innocenzi <[email protected]>
Co-authored-by: Enzo Innocenzi <[email protected]>
Co-authored-by: Enzo Innocenzi <[email protected]>
|
Let's go!! |
|
Well done! |
…stphp#647) Co-authored-by: Enzo Innocenzi <[email protected]> Co-authored-by: Brent Roose <[email protected]>
This PR replace the PR #568
I started from scratch because lot of work in the other PR is stale.
Here's a clean version of the real changes.
I've updated the overall "generator commands" as discussed in Discord and in the other PR reviews.
So this is a first step to generator commands MVP. It's a starting point for #472
If everything is ok, the #568 could be closed.
@brendt Could you please add @innocenzi as co-author of this one as a lot of work is also done by/with him 🙏