Command Line Interface

Create task

Command Line Interface allows to simply create scripts, which can be executed from system shell or via cron.
Task class must extend Vegas\Cli\TaskAbstract class and implement method setupOptions that defines all available task actions.
Name of task class must contain word Task, eg. "CustomTask".

We can create a Task in two locations:

  • global application task - located in the "app/tasks/" directory
  • application module task - located in the application module directory, eg. "app/modules/ModuleName/tasks"

Global application task

touch app/tasks/CustomTask.php

class CustomTask extends \Vegas\Cli\TaskAbstract
{
    /**
     * Available options
     *
     * @return mixed
     */
    public function setupOptions()
    {
        $action = new \Vegas\Cli\Task\Action('test', 'Test action');

        //foo option
        $foo = new \Vegas\Cli\Task\Option('foo', 'f', 'Foo option. Usage app:custom test -f numberOfSth');
        $foo->setValidator(function($value) {
            if (!is_numeric($value)) return false;
            return true;
        });
        $foo->setRequired(true);
        $action->addOption($foo);
        $this->addTaskAction($action);
    }

    /**
     * Test action that shows number passed to "f" option and in the first argument
     */
    public function testAction()
    {
        $this->putText($this->getArg(0));
        $this->putText($this->getOption('f'));
    }
}

The global application task must be located inside "app/tasks" directory, and cannot have a namespace.

Running

php cli/cli.php app:custom test 6789 -f 1234

Output
6789
1234

app:custom means that task is a global application task, and it's located in the "app/tasks/CustomTask.php" file.
test name of task action to execute.
6789 the first execution argument
-f 1234 the value of option "f" ("foo")

Application module task

touch app/modules/Example/tasks/CustomTask.php

namespace Example\Tasks;

class CustomTask extends \Vegas\Cli\TaskAbstract
{
    /**
     * Available options
     *
     * @return mixed
     */
    public function setupOptions()
    {
        $action = new \Vegas\Cli\Task\Action('test', 'Test action');

        //foo option
        $foo = new \Vegas\Cli\Task\Option('foo', 'f', 'Foo option. Usage app:custom test -f numberOfSth');
        $foo->setValidator(function($value) {
            if (!is_numeric($value)) return false;
            return true;
        });
        $foo->setRequired(true);
        $action->addOption($foo);
        $this->addTaskAction($action);
    }

    /**
     * Test action that shows number passed to "f" option and in the first argument
     */
    public function testAction()
    {
        $this->putText($this->getArg(0));
        $this->putText($this->getOption('f'));
    }
}

The application module task must be located inside "tasks" directory inside module directory, and must have a namespace.

Running

php cli/cli.php app:example:custom test 6789 -f 1234

Output
6789
1234
app:example:custom means that task is an application module task, and it's located in the "app/modules/Example/tasks/CustomTask.php" file.
test name of task action to execute.
6789 the first execution argument
-f 1234 the value of option "f" ("foo")

Help

To display all actions available in task use the following command:

php cli/cli.php app:example:custom -h

To display help for specific action (eg. test) use the following command:

php cli/cli.php app:example:custom test -h