-
Notifications
You must be signed in to change notification settings - Fork 2
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
Added the make:cmd file and ability to create subdirectories and comm… #4
Conversation
…and file when providing a new command.
} | ||
|
||
// Create the file for the new command | ||
fopen($command_file_full_path, 'w'); |
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.
where are you closing the file? fclose?
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.
and what happens if this fopen fails and thows and error?
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.
I have to look more into how fopen
works, but seems logical we would need to close it.
And yes, we should be handling that exception. I'll print an alert message to the console if it fails. We should probably also prevent the subdirectories from being created, too, if it fails.
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 I looked into fopen
a little bit more and learned that fopen
only generates a warning if it fails to write and that an opened file will get closed at script termination. With that in mind, it still is best practice to use fclose
to close it manually, so I'll be adding that fix.
I also wrapped that in a try/catch and will be throwing an error if the command being created already exists, which will be caught so an alert message can be printed to the console.
*/ | ||
protected function createCommand() | ||
{ | ||
$arg = $this->getArguments()[0]; |
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.
you could save this argument in a property of the class so you can reuse it later as $this->filepath or something like that
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.
Sure thing. I'll update that to be a property. I was wondering too if we wanted to include the ability to create multiple commands in the same command?
Something like:
php blacksmith make:cmd my/first/command my/second/command
Which would create two commands in the following directories:
my-project/commands/my/first/Command.php
my-project/commands/my/second/Command.php
Currently, we are not supporting that. Didn't think it was completely necessary for now.
* | ||
* For example: | ||
* | ||
* php blacksmith make:cmd this:is:my: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.
this example is not accurate because you're exploding by DIRECTORY_SEPARATOR and not by :
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.
My mistake! Good catch. Thanks.
@@ -3,6 +3,7 @@ | |||
namespace Blacksmith\Commands\Make; | |||
|
|||
use Blacksmith\Command; | |||
use Exception; |
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 temporary. We will need to start creating custom Exceptions. I wanted to know where you wanted to organize custom exceptions first.
…and file when providing a new command.
You should be able to perform the following command now:
And consequentially a file should be generated in the respective subdirectories:
Note: The name of the command will be the new name of the file but with a capital letter.
Other Misc. Changes
DIRECTORY_SEPARATOR
constant so that file paths can be OS agnostic. We should probably update some of the other places in the app to use it, too.success
message in theOutput.php
class that prints a green message with transparent background to the console.make:cmd
command will actually create the/commands
folder recursively if it doesn't yet exist, so we may actually be able to remove it from thebootstrap
file.