Console
OXID eShop uses Symfony console component to register or execute command, so it’s possible to use it’s features. More information about Symfony console can be found here.
Executing CLI Commands
To get list of commands, execute:
vendor/bin/oe-console
In case you are using Enterprise Edition subshops feature, to get list of specific subshop commands, execute:
vendor/bin/oe-console --shop-id=<shop-id>
Note
If <shop-id> will not be defined: shop 1, going to be used.
Some other commands examples:
vendor/bin/oe-console oe:module:activate <module-id>
vendor/bin/oe-console oe:module:activate <module-id> --shop-id=<shop-id>
vendor/bin/oe-console oe:module:deactivate <module-id> --shop-id=<shop-id>
Creating custom CLI commands
Commands can be created for OXID eShop modules or for OXID eShop components.
Module commands
OXID eShop allows to create commands for modules. Commands will appear in the list only when a module is going to be activated.
Creating a command class
First of all it’s necessary to create a command class.
Example
<?php declare(strict_types=1);
namespace OxidEsales\DemoModule\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HelloWorldCommand extends Command
{
protected function configure()
{
$this->setName('demo-module:say-hello')
->setDescription('"Hello world" command.')
->setHelp('Command says "Hello world".');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello world!');
return 0;
}
}
Registering a command file
When a command file is created, it’s necessary to register it as a service. If services.yaml file is not present, create it in your module root directory.
Important
Do not add a leading backslash to the classname.
wrong: class: \OxidEsales\DemoModule\Command\HelloWorldCommand
correct: class: OxidEsales\DemoModule\Command\HelloWorldCommand
services:
OxidEsales\DemoModule\Command\HelloWorld:
class: OxidEsales\DemoModule\Command\HelloWorldCommand
tags:
- { name: 'console.command', command: 'demo-module:say-hello' }
Important
Despite specifying command: ‘demo-module:say-hello’ explicitly is not needed, we highly recommend to do so, because you will likely run into this issue otherwise.
Now after module activation, command will be available in commands list and it can be executed via:
vendor/bin/oe-console demo-module:say-hello
In case you need to change command name, it can be done also via services.yaml file by adding command entry:
services:
OxidEsales\DemoModule\Command\HelloWorld:
class: OxidEsales\DemoModule\Command\HelloWorldCommand
tags:
- { name: 'console.command', command: 'demo-module:say-hello-another-command' }
And again after module activation command can be called via:
vendor/bin/oe-console demo-module:say-hello-another-command
Demo module with command example can be found here.
OXID eShop component commands
Component commands works similarly as module commands, just one difference, they become active instantly after installation via composer. For more information refer to OXID eShop component.
Command class
Component command example:
<?php declare(strict_types=1);
namespace OxidEsales\DemoComponent\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HelloWorldCommand extends Command
{
protected function configure()
{
$this->setName('demo-component:say-hello')
->setDescription('Says hello.')
->setHelp('This command welcomes you.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello World!');
return 0;
}
}
Important
Component command must extend \Symfony\Component\Console\Command\Command.
Command file registration
When command class is created, it’s necessary to register it as a service. If the services.yaml is not present, create it in your component root directory.
services:
OxidEsales\DemoComponent\Command\HelloWorld:
class: OxidEsales\DemoComponent\Command\HelloWorldCommand
tags:
- { name: 'console.command', command: 'demo-module:say-hello' }
Command testing
For integration testing commands we recommend to use the symfony CommandTester. Within this CommandTester the input and output can even be overwritten for your needs.
Example for executing your command within command tester:
class TestCommand extends Command
{
protected function configure()
{
$this->setName('oe:tests:test-command');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Command has been executed!');
return 0;
}
}
public function testCommandExecution()
{
$commandTester = new CommandTester(new TestCommand());
$commandTester->execute([]);
$output = $commandTester->getDisplay();
$this->assertSame('Command has been executed!' . PHP_EOL, $output);
}