22Creating Spark Commands
33#######################
44
5- While the ability to use Controllers via CLI like any other route is convenient, you might find times where you
6- need a little something different. That's where Spark commands come in. They are simple classes that do not
7- need to have routes defined, making them perfect for building tools that developers can use to make
8- their jobs simpler, whether by handling migrations or database seeding, checking cronjob status, or even
9- building out custom code generators for your company.
5+ While the ability to use Controllers via CLI like any other route is convenient, you might find times where you need a
6+ little something different. That's where Spark commands come in. They are simple classes that do not need to have routes
7+ defined, making them perfect for building tools that developers can use to make their jobs simpler, whether by handling
8+ migrations or database seeding, checking cronjob status, or even building out custom code generators for your company.
109
1110.. contents ::
1211 :local:
@@ -16,56 +15,60 @@ building out custom code generators for your company.
1615Creating New Commands
1716*********************
1817
19- You can very easily create new commands to use in your own development. Each class must be in its own file,
20- must extend ``CodeIgniter\CLI\BaseCommand ``, and must implement the ``run() `` method.
18+ You can very easily create new commands to use in your own development. Each class must be in its own file, must extend
19+ ``CodeIgniter\CLI\BaseCommand ``, and must implement the ``run() `` method.
2120
22- The following properties should be used in order to get listed in CLI commands and to add help functionality to your command:
21+ The following properties should be used in order to get listed in CLI commands and to add help functionality to your
22+ command:
2323
24- * ``$group ``: a string to describe the group the command is lumped under when listing commands. For example: ``Database ``
24+ * ``$group ``: a string to describe the group the command is lumped under when listing commands. For example:
25+ ``Database ``
2526* ``$name ``: a string to describe the command's name. For example: ``make:controller ``
2627* ``$description ``: a string to describe the command. For example: ``Generates a new controller file. ``
2728* ``$usage ``: a string to describe the command usage. For example: ``make:controller <name> [options] ``
28- * ``$arguments ``: an array of strings to describe each command argument. For example: ``'name' => 'The controller class name.' ``
29- * ``$options ``: an array of strings to describe each command option. For example: ``'--force' => 'Force overwrite existing file.' ``
29+ * ``$arguments ``: an array of strings to describe each command argument. For example:
30+ ``'name' => 'The controller class name.' ``
31+ * ``$options ``: an array of strings to describe each command option. For example:
32+ ``'--force' => 'Force overwrite existing file.' ``
3033
3134**Help description will be automatically generated according to the above parameters. **
3235
3336File Location
3437=============
3538
36- Commands must be stored within a directory named **Commands **. However, that directory has to be located in the PSR-4 namespaces
37- so that the :doc: `Autoloader </concepts/autoloader >` can locate it. This could be in **app/Commands **, or
38- a directory that you keep commands in to use in all of your project development, like **Acme/Commands **.
39+ Commands must be stored within a directory named **Commands **. However, that directory has to be located in the PSR-4
40+ namespaces so that the :doc: `Autoloader </concepts/autoloader >` can locate it. This could be in **app/Commands **, or a
41+ directory that you keep commands in to use in all of your project development, like **Acme/Commands **.
3942
40- .. note :: When the commands are executed, the full CodeIgniter CLI environment has been loaded, making it
41- possible to get environment information, path information, and to use any of the tools you would use when making a Controller.
43+ .. note :: When the commands are executed, the full CodeIgniter CLI environment has been loaded, making it possible to
44+ get environment information, path information, and to use any of the tools you would use when making a Controller.
4245
4346An Example Command
4447==================
4548
46- Let's step through an example command whose only function is to report basic information about the application
47- itself, for demonstration purposes. Start by creating a new file at **app/Commands/AppInfo.php **. It
48- should contain the following code:
49+ Let's step through an example command whose only function is to report basic information about the application itself,
50+ for demonstration purposes. Start by creating a new file at **app/Commands/AppInfo.php **. It should contain the
51+ following code:
4952
5053.. literalinclude :: cli_commands/002.php
5154
52- If you run the **list ** command, you will see the new command listed under its own ``Demo `` group. If you take
53- a close look, you should see how this works fairly easily. The ``$group `` property simply tells it how to organize
54- this command with all of the other commands that exist, telling it what heading to list it under.
55+ If you run the **list ** command, you will see the new command listed under its own ``Demo `` group. If you take a close
56+ look, you should see how this works fairly easily. The ``$group `` property simply tells it how to organize this command
57+ with all of the other commands that exist, telling it what heading to list it under.
5558
56- The ``$name `` property is the name this command can be called by. The only requirement is that it must not contain
57- a space, and all characters must be valid on the command line itself. By convention, though, commands are lowercase,
58- with further grouping of commands being done by using a colon with the command name itself. This helps keep
59- multiple commands from having naming collisions.
59+ The ``$name `` property is the name this command can be called by. The only requirement is that it must not contain a
60+ space, and all characters must be valid on the command line itself. By convention, though, commands are lowercase, with
61+ further grouping of commands being done by using a colon with the command name itself. This helps keep multiple commands
62+ from having naming collisions.
6063
6164The final property, ``$description ``, is a short string that is displayed in the **list ** command and should describe
6265what the command does.
6366
6467run()
6568-----
6669
67- The ``run() `` method is the method that is called when the command is being run. The ``$params `` array is a list of
68- any CLI arguments after the command name for your use. If the CLI string was:
70+ The ``run() `` method is the method that is called when the command is being run. The ``$params `` array is a list of any
71+ CLI arguments after the command name for your use. If the CLI string was:
6972
7073.. code-block :: console
7174
@@ -87,8 +90,8 @@ See the :doc:`CLI Library </cli/cli_library>` page for detailed information.
8790Command Termination
8891-------------------
8992
90- By default, the command exits with a success code of ``0 ``. If an error is encountered while executing a command,
91- you can terminate the command by using the ``return `` language construct with an exit code in the ``run() `` method.
93+ By default, the command exits with a success code of ``0 ``. If an error is encountered while executing a command, you
94+ can terminate the command by using the ``return `` language construct with an exit code in the ``run() `` method.
9295
9396For example, ``return EXIT_ERROR; ``
9497
@@ -100,8 +103,8 @@ You can use the ``EXIT_*`` exit code constants defined in the **app/Config/Const
100103BaseCommand
101104***********
102105
103- The ``BaseCommand `` class that all commands must extend has a couple of helpful utility methods that you should
104- be familiar with when creating your own commands. It also has a :doc: `Logger </general/logging >` available at
106+ The ``BaseCommand `` class that all commands must extend has a couple of helpful utility methods that you should be
107+ familiar with when creating your own commands. It also has a :doc: `Logger </general/logging >` available at
105108``$this->logger ``.
106109
107110.. php :namespace :: CodeIgniter\CLI
@@ -136,8 +139,7 @@ be familiar with when creating your own commands. It also has a :doc:`Logger </g
136139 :param integer $extra: How many extra spaces to add at the end.
137140 :param integer $indent: The indent spaces.
138141
139- Pads our string out so that all titles are the same length to nicely line
140- up descriptions:
142+ Pads our string out so that all titles are the same length to nicely line up descriptions:
141143
142144 .. literalinclude :: cli_commands/007.php
143145 :lines: 2-
@@ -150,4 +152,5 @@ be familiar with when creating your own commands. It also has a :doc:`Logger </g
150152 :param array $array: The ``$key `` => ``$value `` array.
151153 :param integer $pad: The pad spaces.
152154
153- A method to calculate padding for ``$key => $value `` array output. The padding can be used to output a well-formatted table in CLI.
155+ A method to calculate padding for ``$key => $value `` array output. The padding can be used to output a
156+ well-formatted table in CLI.
0 commit comments