Quick links: Content - sections - sub sections
EN FR

Jelix has a mechanism to provide your own commands from your modules, and which can be launched with the console.php script. Commands are developped using the Symfony Console component.

Jelix 1.7 supports also its own old script component, jControllerCmdLine, with some specific entry point. But this system is deprecated. To know how to use it, read the Jelix 1.6 manual.

This chapter will learn you how to declare and create commands developed with Symphony Console, in a Jelix application.

Principles

Commands provided by modules, are executed throw the script console.php of the application. This script know all commands of the application by executing a specific script stored into each modules: commands.php.

This commands.php script shoudl declare all commands provided by the module, and each command is implemented with a class inheriting from \Jelix\Scripts\ModuleCommandAbstract (which inherits from Symfony\Component\Console\Command\Command).

Creating a command

Create a class in your module, for example in a directory Commands. Let's say you declared a namespace for the classes of the module, \SoftCie\MyModule\, into module.xml:


 <psr4 name="\SoftCie\MyModule" dir="./"/>

So you could named your command class @\SoftCie\MyModule\Commands\Foo@@, into the file Commands/Foo.php:


namespace \SoftCie\MyModule\Commands;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class Foo extends \Jelix\Scripts\ModuleCommandAbstract {

  protected function configure()
  {
      $this
          ->setName('mymodule:foo')
          ->setDescription('this is a script')
          ->setHelp('')
          ->addArgument(...)
          ->addOption(...)
      ;
  }

  protected function execute(InputInterface $input, OutputInterface $output)
  {

  }
}

In the module configure(), you declare the command name, arguments and options of your command.

The execute() method contains the code of your command.

To know more about methods and properties of a Symphony command, read the manueal of Symfony Console.

You should know that when methods configure() and execute() are executed, you are in the Jelix environment of the main entry point. The application.init.php has been launched, and jApp contains all properties, like from a controller, and you can call any Jelix component or other modules classes.

Declaring the command

When the class of your command is ready, you should declare it so console.php knows it. You then must have script commands.php at the root of the module (alongside module.xml). This script has access to a variable $application containing an object \Symfony\Component\Console\Application. So you can declare the command like this:


use \SoftCie\MyModule\Commands;

$application->add(new Commands\Foo());

Launching the command

Use the console.php script by giving the command name to it, and the command parameters and options if needed.


php console.php mymodule:foo

To know the list of available commands, use the command list or give no parameters:


php console.php list
# or
php console.php

To get help of the command, use the help command followed by the command name, or use the option -h:


php console.php help mymodule:foo
# or
php console.php mymodule:foo -h