Sections

Class Phalcon\Cli\Console

Source on GitHub

Namespace Phalcon\Cli
Uses Phalcon\Application\AbstractApplication, Phalcon\Cli\Router\Route, Phalcon\Cli\Console\Exception, Phalcon\DiInterface, Phalcon\Events\ManagerInterface
Extends AbstractApplication

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

Class Phalcon\Cli\Console\Exception

Source on GitHub

Namespace Phalcon\Cli\Console
Extends \Phalcon\Application\Exception

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

Class Phalcon\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

@param mixed $option @param string|array $filters @param mixed $defaultValue

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

Class Phalcon\Cli\Dispatcher\Exception

Source on GitHub

Namespace Phalcon\Cli\Dispatcher
Extends \Phalcon\Dispatcher\Exception

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

Interface Phalcon\Cli\DispatcherInterface

Source on GitHub

Namespace Phalcon\Cli
Uses Phalcon\Dispatcher\DispatcherInterface
Extends DispatcherInterfaceBase

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

Class Phalcon\Cli\Router

Source on GitHub

Namespace Phalcon\Cli
Uses Phalcon\DiInterface, Phalcon\Di\InjectionAwareInterface, Phalcon\Cli\Router\Route, Phalcon\Cli\Router\Exception
Implements InjectionAwareInterface

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 container;

//
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 ): void;

Phalcon\Cli\Router constructor

public function add( string $pattern, mixed $paths = null ): RouteInterface;

Adds a route to the router

$router->add("/about", "About::main");
@param string array paths
public function getActionName(): string;

Returns processed action name

public function getDI(): DiInterface;

Returns the internal dependency injector

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

@param int 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

@param array arguments

public function setDI( DiInterface $container ): void;

Sets the dependency injector

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

Class Phalcon\Cli\Router\Exception

Source on GitHub

Namespace Phalcon\Cli\Router
Extends \Phalcon\Exception

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

Class Phalcon\Cli\Router\Route

Source on GitHub

Namespace Phalcon\Cli\Router

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 ): void;

@param array|string paths

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

@param callback callback

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

@param callable converter

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 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

@param array string paths
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");

Interface Phalcon\Cli\Router\RouteInterface

Source on GitHub

Namespace Phalcon\Cli\Router

Interface for Phalcon\Cli\Router\Route

Methods

public function compilePattern( string $pattern ): string;

Replaces placeholders from pattern returning a valid PCRE regular expression

public function getCompiledPattern(): string;

Returns the route’s pattern

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 function setDescription( string $description ): RouteInterface;

Sets the route’s description

public function setName( string $name ): RouteInterface;

Sets the route’s name

Interface Phalcon\Cli\RouterInterface

Source on GitHub

Namespace Phalcon\Cli
Uses Phalcon\Cli\Router\RouteInterface

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

@param array arguments

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

Class Phalcon\Cli\Task

Source on GitHub

Namespace Phalcon\Cli
Uses Phalcon\Di\Injectable
Extends Injectable
Implements 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()
    {

    }
}

Methods

final public function __construct(): void;

Phalcon\Cli\Task constructor

Interface Phalcon\Cli\TaskInterface

Source on GitHub

Namespace Phalcon\Cli

Interface for task handlers