Skip to content

Phalcon cli

NOTE

All classes are prefixed with Phalcon

Cli\Console

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

    • Phalcon\Application\AbstractApplication
    • Phalcon\Cli\Console\Exception
    • Phalcon\Cli\Router\Route
    • Phalcon\Di\DiInterface
    • Phalcon\Events\ManagerInterface
  • Extends

    AbstractApplication

  • Implements

This component allows to create CLI applications using Phalcon

Properties

/**
 * @var array
 */
protected $arguments;

/**
 * @var array
 */
protected $options;

Methods

public function handle( array $arguments = null );
Handle the whole command-line tasks

public function setArgument( array $arguments = null, bool $str = bool, bool $shift = bool ): Console;
Set an specific argument

Cli\Console\Exception

Source on GitHub

  • Namespace

    • Phalcon\Cli\Console
  • Uses

  • Extends

    \Phalcon\Application\Exception

  • Implements

Exceptions thrown in Phalcon\Cli\Console will use this class

Cli\Dispatcher

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

    • Phalcon\Cli\Dispatcher\Exception
    • Phalcon\Dispatcher\AbstractDispatcher
    • Phalcon\Events\ManagerInterface
    • Phalcon\Filter\FilterInterface
  • Extends

    CliDispatcher

  • Implements

    • DispatcherInterface

Dispatching is the process of taking the command-line arguments, extracting the module name, task name, action name, and optional parameters contained in it, and then instantiating a task and calling an action on it.

use Phalcon\Di;
use Phalcon\Cli\Dispatcher;

$di = new Di();

$dispatcher = new Dispatcher();

$dispatcher->setDi($di);

$dispatcher->setTaskName("posts");
$dispatcher->setActionName("index");
$dispatcher->setParams([]);

$handle = $dispatcher->dispatch();

Properties

/**
 * @var string
 */
protected $defaultHandler = main;

/**
 * @var string
 */
protected $defaultAction = main;

/**
 * @var string
 */
protected $handlerSuffix = Task;

/**
 * @var array
 */
protected $options;

Methods

public function callActionMethod( mixed $handler, string $actionMethod, array $params = [] ): mixed;
Calls the action method.

public function getActiveTask(): TaskInterface;
Returns the active task in the dispatcher

public function getLastTask(): TaskInterface;
Returns the latest dispatched controller

public function getOption( mixed $option, mixed $filters = null, mixed $defaultValue = null ): mixed;
Gets an option by its name or numeric index

public function getOptions(): array;
Get dispatched options

public function getTaskName(): string;
Gets last dispatched task name

public function getTaskSuffix(): string;
Gets the default task suffix

public function hasOption( mixed $option ): bool;
Check if an option exists

public function setDefaultTask( string $taskName ): void;
Sets the default task name

public function setOptions( array $options ): void;
Set the options to be dispatched

public function setTaskName( string $taskName ): void;
Sets the task name to be dispatched

public function setTaskSuffix( string $taskSuffix ): void;
Sets the default task suffix

protected function handleException( \Exception $exception );
Handles a user exception

protected function throwDispatchException( string $message, int $exceptionCode = int );
Throws an internal exception

Cli\Dispatcher\Exception

Source on GitHub

  • Namespace

    • Phalcon\Cli\Dispatcher
  • Uses

  • Extends

    \Phalcon\Dispatcher\Exception

  • Implements

Exceptions thrown in Phalcon\Cli\Dispatcher will use this class

Cli\DispatcherInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

    • Phalcon\Dispatcher\DispatcherInterface
  • Extends

    DispatcherInterfaceBase

  • Implements

Interface for Phalcon\Cli\Dispatcher

Methods

public function getActiveTask(): TaskInterface;
Returns the active task in the dispatcher

public function getLastTask(): TaskInterface;
Returns the latest dispatched controller

public function getOptions(): array;
Get dispatched options

public function getTaskName(): string;
Gets last dispatched task name

public function getTaskSuffix(): string;
Gets default task suffix

public function setDefaultTask( string $taskName ): void;
Sets the default task name

public function setOptions( array $options ): void;
Set the options to be dispatched

public function setTaskName( string $taskName ): void;
Sets the task name to be dispatched

public function setTaskSuffix( string $taskSuffix ): void;
Sets the default task suffix

Cli\Router

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

    • Phalcon\Cli\Router\Exception
    • Phalcon\Cli\Router\Route
    • Phalcon\Cli\Router\RouteInterface
    • Phalcon\Di\AbstractInjectionAware
    • Phalcon\Di\DiInterface
  • Extends

    AbstractInjectionAware

  • Implements

Phalcon\Cli\Router is the standard framework router. Routing is the process of taking a command-line arguments and decomposing it into parameters to determine which module, task, and action of that task should receive the request.

$router = new \Phalcon\Cli\Router();

$router->handle(
    [
        "module" => "main",
        "task"   => "videos",
        "action" => "process",
    ]
);

echo $router->getTaskName();

Properties

//
protected $action;

//
protected $defaultAction;

//
protected $defaultModule;

/**
 * @var array
 */
protected $defaultParams;

//
protected $defaultTask;

//
protected $matchedRoute;

//
protected $matches;

//
protected $module;

/**
 * @var array
 */
protected $params;

//
protected $routes;

//
protected $task;

//
protected $wasMatched = false;

Methods

public function __construct( bool $defaultRoutes = bool );
Phalcon\Cli\Router constructor

public function add( string $pattern, mixed $paths = null ): RouteInterface;
Adds a route to the router

$router->add("/about", "About::main");

public function getActionName(): string;
Returns processed action name

public function getMatchedRoute(): RouteInterface;
Returns the route that matches the handled URI

public function getMatches(): array;
Returns the sub expressions in the regular expression matched

public function getModuleName(): string;
Returns processed module name

public function getParams(): array;
Returns processed extra params

public function getRouteById( mixed $id ): RouteInterface | bool;
Returns a route object by its id

public function getRouteByName( string $name ): RouteInterface | bool;
Returns a route object by its name

public function getRoutes(): Route[];
Returns all the routes defined in the router

public function getTaskName(): string;
Returns processed task name

public function handle( mixed $arguments = null );
Handles routing information received from command-line arguments

public function setDefaultAction( string $actionName );
Sets the default action name

public function setDefaultModule( string $moduleName );
Sets the name of the default module

public function setDefaultTask( string $taskName ): void;
Sets the default controller name

public function setDefaults( array $defaults ): Router;
Sets an array of default paths. If a route is missing a path the router will use the defined here. This method must not be used to set a 404 route

$router->setDefaults(
    [
        "module" => "common",
        "action" => "index",
    ]
);

public function wasMatched(): bool;
Checks if the router matches any of the defined routes

Cli\Router\Exception

Source on GitHub

  • Namespace

    • Phalcon\Cli\Router
  • Uses

  • Extends

    \Exception

  • Implements

Exceptions thrown in Phalcon\Cli\Router will use this class

Cli\Router\Route

Source on GitHub

  • Namespace

    • Phalcon\Cli\Router
  • Uses

  • Extends

  • Implements

    • RouteInterface

This class represents every route added to the router

Constants

const DEFAULT_DELIMITER =  ;

Properties

//
protected $beforeMatch;

//
protected $compiledPattern;

//
protected $converters;

//
protected $delimiter;

//
protected $static $delimiterPath;

//
protected $description;

//
protected $id;

//
protected $name;

//
protected $paths;

//
protected $pattern;

//
protected $static $uniqueId = 0;

Methods

public function __construct( string $pattern, mixed $paths = null );

public function beforeMatch( mixed $callback ): RouteInterface;
Sets a callback that is called if the route is matched. The developer can implement any arbitrary conditions here If the callback returns false the route is treated as not matched

public function compilePattern( string $pattern ): string;
Replaces placeholders from pattern returning a valid PCRE regular expression

public function convert( string $name, mixed $converter ): RouteInterface;
Adds a converter to perform an additional transformation for certain parameter

public static function delimiter( string $delimiter = null ): void;
Set the routing delimiter

public function extractNamedParams( string $pattern ): array | bool;
Extracts parameters from a string

public function getBeforeMatch(): mixed;
Returns the 'before match' callback if any

public function getCompiledPattern(): string;
Returns the route's compiled pattern

public function getConverters(): array;
Returns the router converter

public static function getDelimiter(): string;
Get routing delimiter

public function getDescription(): string;
Returns the route's description

public function getName(): string;
Returns the route's name

public function getPaths(): array;
Returns the paths

public function getPattern(): string;
Returns the route's pattern

public function getReversedPaths(): array;
Returns the paths using positions as keys and names as values

public function getRouteId(): string;
Returns the route's id

public function reConfigure( string $pattern, mixed $paths = null ): void;
Reconfigure the route adding a new pattern and a set of paths

public static function reset(): void;
Resets the internal route id generator

public function setDescription( string $description ): RouteInterface;
Sets the route's description

public function setName( string $name ): RouteInterface;
Sets the route's name

$router->add(
    "/about",
    [
        "controller" => "about",
    ]
)->setName("about");

Cli\Router\RouteInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Cli\Router
  • Uses

  • Extends

  • Implements

Interface for Phalcon\Cli\Router\Route

Methods

public function compilePattern( string $pattern ): string;
Replaces placeholders from pattern returning a valid PCRE regular expression

public static function delimiter( string $delimiter = null );
Set the routing delimiter

public function getCompiledPattern(): string;
Returns the route's pattern

public static function getDelimiter(): string;
Get routing delimiter

public function getDescription(): string;
Returns the route's description

public function getName(): string;
Returns the route's name

public function getPaths(): array;
Returns the paths

public function getPattern(): string;
Returns the route's pattern

public function getReversedPaths(): array;
Returns the paths using positions as keys and names as values

public function getRouteId(): string;
Returns the route's id

public function reConfigure( string $pattern, mixed $paths = null ): void;
Reconfigure the route adding a new pattern and a set of paths

public static function reset(): void;
Resets the internal route id generator

public function setDescription( string $description ): RouteInterface;
Sets the route's description

public function setName( string $name ): RouteInterface;
Sets the route's name

Cli\RouterInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

    • Phalcon\Cli\Router\RouteInterface
  • Extends

  • Implements

Interface for Phalcon\Cli\Router

Methods

public function add( string $pattern, mixed $paths = null ): RouteInterface;
Adds a route to the router on any HTTP method

public function getActionName(): string;
Returns processed action name

public function getMatchedRoute(): RouteInterface;
Returns the route that matches the handled URI

public function getMatches(): array;
Return the sub expressions in the regular expression matched

public function getModuleName(): string;
Returns processed module name

public function getParams(): array;
Returns processed extra params

public function getRouteById( mixed $id ): RouteInterface;
Returns a route object by its id

public function getRouteByName( string $name ): RouteInterface;
Returns a route object by its name

public function getRoutes(): RouteInterface[];
Return all the routes defined in the router

public function getTaskName(): string;
Returns processed task name

public function handle( mixed $arguments = null );
Handles routing information received from the rewrite engine

public function setDefaultAction( string $actionName ): void;
Sets the default action name

public function setDefaultModule( string $moduleName ): void;
Sets the name of the default module

public function setDefaultTask( string $taskName ): void;
Sets the default task name

public function setDefaults( array $defaults ): void;
Sets an array of default paths

public function wasMatched(): bool;
Check if the router matches any of the defined routes

Cli\Task

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

    • Phalcon\Di\Injectable
    • Phalcon\Events\EventsAwareInterface
    • Phalcon\Events\ManagerInterface
  • Extends

    Injectable

  • Implements

    • EventsAwareInterface
    • TaskInterface

Every command-line task should extend this class that encapsulates all the task functionality

A task can be used to run "tasks" such as migrations, cronjobs, unit-tests, or anything that you want. The Task class should at least have a "mainAction" method.

class HelloTask extends \Phalcon\Cli\Task
{
    // This action will be executed by default
    public function mainAction()
    {

    }

    public function findAction()
    {

    }
}

Properties

//
protected $eventsManager;

Methods

final public function __construct();
Phalcon\Cli\Task constructor

public function getEventsManager(): ManagerInterface | null;
Returns the internal event manager

public function setEventsManager( ManagerInterface $eventsManager ): void;
Sets the events manager

Cli\TaskInterface Interface

Source on GitHub

  • Namespace

    • Phalcon\Cli
  • Uses

  • Extends

  • Implements

Interface for task handlers