Skip to content

Commit fc6d549

Browse files
committed
How to add CLI commands
1 parent 6fcd9c6 commit fc6d549

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

1. Magento Architecture and Customization Techniques/7. Utilize the CLI.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,68 @@ Production - max speed, no errors, no file generation:
3131
- errors logged, not displayed
3232
- static not created dynamically, must be deployed
3333
- not need for www-data to write, pub/static can be read-only
34+
35+
### How to add CLI commands (off-topic)
36+
37+
Magento 2 CLI is based on the Symfony Console component.
38+
39+
To create new CLI command you need:
40+
- Create command class (the recommended location is {module}/Console/Command)
41+
This class must extends from [\Symfony\Component\Console\Command\Command](https://github.com/symfony/console/blob/master/Command/Command.php) and have 2 methods:
42+
43+
`configure` - to set the name, description, command line arguments etc
44+
45+
`execute` - is the place where you write your code
46+
47+
```php
48+
<?php
49+
namespace Vendor\Module\Console\Command;
50+
51+
class ExampleCommand extends \Symfony\Component\Console\Command\Command
52+
{
53+
protected function configure()
54+
{
55+
$options = [
56+
new \Symfony\Component\Console\Input\InputOption(
57+
'param',
58+
null,
59+
\Symfony\Component\Console\Input\InputOption::VALUE_REQUIRED,
60+
'Param description'
61+
),
62+
];
63+
$this->setName('example:hello')
64+
->setDescription('Hello world command')
65+
->setDefinition($options);
66+
parent::configure();
67+
}
68+
69+
protected function execute(\Symfony\Component\Console\Input\InputInterface $input, \Symfony\Component\Console\Output\OutputInterface $output)
70+
{
71+
$output->writeln('hello world');
72+
}
73+
}
74+
```
75+
76+
- Declare your command in [\Magento\Framework\Console\CommandListInterface](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Console/CommandListInterface.php) using dependency injection ({module}/etc/di.xml).
77+
See also CommandListInterface implementation: [\Magento\Framework\Console\CommandList](https://github.com/magento/magento2/blob/2.2-develop/lib/internal/Magento/Framework/Console/CommandList.php)
78+
79+
{module}/etc/di.xml:
80+
```xml
81+
<type name="Magento\Framework\Console\CommandListInterface">
82+
<arguments>
83+
<argument name="commands" xsi:type="array">
84+
<item name="exampleHello" xsi:type="object">Vendor\Module\Console\Command\ExampleCommand</item>
85+
</argument>
86+
</arguments>
87+
</type>
88+
```
89+
90+
- Clean the cache and compiled code directories
91+
92+
rm -rf cache/* page_cache/* di/* generation/*
93+
94+
###### Links
95+
- [Magento DevDocs - How to add CLI commands](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cli-cmds/cli-howto.html)
96+
- [Magento DevDocs - Command naming guidelines](https://devdocs.magento.com/guides/v2.2/extension-dev-guide/cli-cmds/cli-naming-guidelines.html)
97+
- [Symfony Documentation - The Console Component](https://symfony.com/doc/3.4/components/console.html)
98+
- [Magento - sample-module-command](https://github.com/magento/magento2-samples/tree/master/sample-module-command)

0 commit comments

Comments
 (0)