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

Added the make:cmd file and ability to create subdirectories and comm… #4

Merged
merged 2 commits into from
Dec 16, 2016

Conversation

grahamsutton
Copy link
Contributor

…and file when providing a new command.

You should be able to perform the following command now:

$ php blacksmith make:cmd my/new/command

And consequentially a file should be generated in the respective subdirectories:

some-project/commands/my/new/Command.php

Note: The name of the command will be the new name of the file but with a capital letter.

Other Misc. Changes

  • Made use of 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.
  • Added a new success message in the Output.php class that prints a green message with transparent background to the console.
  • Running 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 the bootstrap file.

}

// Create the file for the new command
fopen($command_file_full_path, 'w');
Copy link
Owner

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?

Copy link
Owner

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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];
Copy link
Owner

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

Copy link
Contributor Author

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

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 :

Copy link
Contributor Author

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;
Copy link
Contributor Author

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.

@eaperezc eaperezc merged commit 5f1d569 into eaperezc:master Dec 16, 2016
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.

None yet

2 participants