Phalcon cli
NOTE
All classes are prefixed with Phalcon
Cli\Console¶
Class Source on GitHub
This component allows to create CLI applications using Phalcon
stdClassPhalcon\Di\InjectablePhalcon\Application\AbstractApplicationPhalcon\Cli\Console
Uses Closure · Phalcon\Application\AbstractApplication · Phalcon\Cli\Console\Exceptions\ContainerRequired · Phalcon\Cli\Console\Exceptions\InvalidModuleDefinition · Phalcon\Cli\Console\Exceptions\ModuleDefinitionPathNotFound · Phalcon\Cli\Router\Route · Phalcon\Events\ManagerInterface · Phalcon\Mvc\ModuleDefinitionInterface
Method Summary¶
public handle( array $arguments = null ) Handle the whole command-line tasks public static setArgument(array $arguments = null,bool $str = true,bool $shift = true) Set an specific argument Properties¶
protected array|string $arguments = [] protected array $options = [] Methods¶
handle()¶
Handle the whole command-line tasks
setArgument()¶
public function setArgument(
array $arguments = null,
bool $str = true,
bool $shift = true
): static;
Set an specific argument
Cli\Console\Exception¶
Class Source on GitHub
Exceptions thrown in Phalcon\Cli\Console will use this class
Cli\Console\Exceptions\ContainerRequired¶
Class Source on GitHub
\ExceptionPhalcon\Application\ExceptionPhalcon\Cli\Console\ExceptionPhalcon\Cli\Console\Exceptions\ContainerRequired
Uses Phalcon\Cli\Console\Exception
Method Summary¶
Methods¶
__construct()¶
Cli\Console\Exceptions\InvalidModuleDefinition¶
Class Source on GitHub
\ExceptionPhalcon\Application\ExceptionPhalcon\Cli\Console\ExceptionPhalcon\Cli\Console\Exceptions\InvalidModuleDefinition
Uses Phalcon\Cli\Console\Exception
Method Summary¶
Methods¶
__construct()¶
Cli\Console\Exceptions\ModuleDefinitionPathNotFound¶
Class Source on GitHub
\ExceptionPhalcon\Application\ExceptionPhalcon\Cli\Console\ExceptionPhalcon\Cli\Console\Exceptions\ModuleDefinitionPathNotFound
Uses Phalcon\Cli\Console\Exception
Method Summary¶
Methods¶
__construct()¶
Cli\Dispatcher¶
Class Source on GitHub
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\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();
stdClassPhalcon\Di\AbstractInjectionAwarePhalcon\Dispatcher\AbstractDispatcherPhalcon\Cli\Dispatcher— implementsPhalcon\Cli\DispatcherInterface
Uses Phalcon\Cli\Dispatcher\Exception · Phalcon\Dispatcher\AbstractDispatcher · Phalcon\Events\ManagerInterface · Phalcon\Filter\FilterInterface
Method Summary¶
public mixed callActionMethod(mixed $handler,string $actionMethod,array $params = []) Calls the action method. public TaskInterface getActiveTask() Returns the active task in the dispatcher public TaskInterface getLastTask() Returns the latest dispatched controller public mixed getOption(mixed $option,mixed $filters = null,mixed $defaultValue = null) Gets an option by its name or numeric index public array getOptions() Get dispatched options public string getTaskName() Gets last dispatched task name public string getTaskSuffix() Gets the default task suffix public bool hasOption( mixed $option ) Check if an option exists public void setDefaultTask( string $taskName ) Sets the default task name public void setOptions( array $options ) Set the options to be dispatched public void setTaskName( string $taskName ) Sets the task name to be dispatched public void setTaskSuffix( string $taskSuffix ) Sets the default task suffix protected handleException( \Exception $exception ) Handles a user exception protected throwDispatchException(string $message,int $exceptionCode = 0) Throws an internal exception Properties¶
protected string $defaultAction = "main" protected string $defaultHandler = "main" protected string $handlerSuffix = "Task" protected array $options = [] Methods¶
callActionMethod()¶
public function callActionMethod(
mixed $handler,
string $actionMethod,
array $params = []
): mixed;
Calls the action method.
The CLI options collected by the dispatcher are appended to the positional params before the call, so a task action receives any options as trailing arguments after its declared parameters.
getActiveTask()¶
Returns the active task in the dispatcher
getLastTask()¶
Returns the latest dispatched controller
getOption()¶
public function getOption(
mixed $option,
mixed $filters = null,
mixed $defaultValue = null
): mixed;
Gets an option by its name or numeric index
getOptions()¶
Get dispatched options
getTaskName()¶
Gets last dispatched task name
getTaskSuffix()¶
Gets the default task suffix
hasOption()¶
Check if an option exists
setDefaultTask()¶
Sets the default task name
setOptions()¶
Set the options to be dispatched
setTaskName()¶
Sets the task name to be dispatched
setTaskSuffix()¶
Sets the default task suffix
handleException()¶
Handles a user exception
throwDispatchException()¶
Throws an internal exception
Cli\DispatcherInterface¶
Interface Source on GitHub
Interface for Phalcon\Cli\Dispatcher
Phalcon\Contracts\Dispatcher\DispatcherPhalcon\Contracts\Cli\DispatcherPhalcon\Cli\DispatcherInterface
Uses Phalcon\Contracts\Cli\Dispatcher
Cli\Dispatcher\Exception¶
Class Source on GitHub
Exceptions thrown in Phalcon\Cli\Dispatcher will use this class
\ExceptionPhalcon\Dispatcher\ExceptionPhalcon\Cli\Dispatcher\Exception
Cli\Router¶
Class Source on GitHub
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();
stdClassPhalcon\Di\AbstractInjectionAwarePhalcon\Cli\Router— implementsPhalcon\Cli\RouterInterface
Uses Phalcon\Cli\Router\Exception · Phalcon\Cli\Router\Exceptions\BeforeMatchNotCallable · Phalcon\Cli\Router\Exceptions\RouterArgumentsInvalidType · Phalcon\Cli\Router\Route · Phalcon\Cli\Router\RouteInterface · Phalcon\Di\AbstractInjectionAware · Phalcon\Di\DiInterface
Method Summary¶
public __construct( bool $defaultRoutes = true ) Phalcon\Cli\Router constructor public RouteInterface add(string $pattern,mixed $paths = null) Adds a route to the router public string getActionName() Returns processed action name public RouteInterface|null getMatchedRoute() Returns the route that matches the handled URI public array getMatches() Returns the sub expressions in the regular expression matched public string getModuleName() Returns processed module name public array getParameters() Returns processed extra params public array getParams() Returns processed extra params public RouteInterface|bool getRouteById( mixed $id ) Returns a route object by its id public RouteInterface|bool getRouteByName( string $name ) Returns a route object by its name public Route[] getRoutes() Returns all the routes defined in the router public string getTaskName() Returns processed task name public handle( mixed $arguments = null ) Handles routing information received from command-line arguments public static setDefaultAction( string $actionName ) Sets the default action name public static setDefaultModule( string $moduleName ) Sets the name of the default module public static setDefaultTask( string $taskName ) Sets the default controller name public static setDefaults( array $defaults ) Sets an array of default paths. If a route is missing a path the router public bool wasMatched() Checks if the router matches any of the defined routes Properties¶
protected string $action = "" protected string $defaultAction = "" protected string $defaultModule = "" protected array $defaultParams = [] protected string $defaultTask = "" protected RouteInterface|null $matchedRoute = null protected array $matches = [] protected string $module = "" protected array $params = [] protected array $routes = [] protected string $task = "" protected bool $wasMatched = false Methods¶
__construct()¶
Phalcon\Cli\Router constructor
add()¶
Adds a route to the router
getActionName()¶
Returns processed action name
getMatchedRoute()¶
Returns the route that matches the handled URI
getMatches()¶
Returns the sub expressions in the regular expression matched
getModuleName()¶
Returns processed module name
getParameters()¶
Returns processed extra params
getParams()¶
Returns processed extra params
getRouteById()¶
Returns a route object by its id
getRouteByName()¶
Returns a route object by its name
getRoutes()¶
Returns all the routes defined in the router
getTaskName()¶
Returns processed task name
handle()¶
Handles routing information received from command-line arguments
setDefaultAction()¶
Sets the default action name
setDefaultModule()¶
Sets the name of the default module
setDefaultTask()¶
Sets the default controller name
setDefaults()¶
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
wasMatched()¶
Checks if the router matches any of the defined routes
Cli\RouterInterface¶
Interface Source on GitHub
Interface for Phalcon\Cli\Router
Phalcon\Cli\RouterInterface
Uses Phalcon\Cli\Router\RouteInterface
Method Summary¶
public RouteInterface add(string $pattern,mixed $paths = null) Adds a route to the router on any HTTP method public string getActionName() Returns processed action name public RouteInterface|null getMatchedRoute() Returns the route that matches the handled URI public array getMatches() Return the sub expressions in the regular expression matched public string getModuleName() Returns processed module name public array getParameters() Returns processed extra params public array getParams() Returns processed extra params public RouteInterface|bool getRouteById( mixed $id ) Returns a route object by its id public RouteInterface|bool getRouteByName( string $name ) Returns a route object by its name public RouteInterface[] getRoutes() Return all the routes defined in the router public string getTaskName() Returns processed task name public handle( mixed $arguments = null ) Handles routing information received from the rewrite engine. public RouterInterface setDefaultAction( string $actionName ) Sets the default action name public RouterInterface setDefaultModule( string $moduleName ) Sets the name of the default module public RouterInterface setDefaultTask( string $taskName ) Sets the default task name public RouterInterface setDefaults( array $defaults ) Sets an array of default paths public bool wasMatched() Check if the router matches any of the defined routes Methods¶
add()¶
Adds a route to the router on any HTTP method
getActionName()¶
Returns processed action name
getMatchedRoute()¶
Returns the route that matches the handled URI
getMatches()¶
Return the sub expressions in the regular expression matched
getModuleName()¶
Returns processed module name
getParameters()¶
Returns processed extra params
getParams()¶
Returns processed extra params
getRouteById()¶
Returns a route object by its id
@todo change param type to string
getRouteByName()¶
Returns a route object by its name
getRoutes()¶
Return all the routes defined in the router
getTaskName()¶
Returns processed task name
handle()¶
Handles routing information received from the rewrite engine.
When arguments is a string (or null), it is matched against the registered routes. When it is an array, matching is bypassed entirely: the array is treated as the already-resolved module/task/action/params, so wasMatched() stays false and getMatchedRoute() returns null even though routing succeeded.
setDefaultAction()¶
Sets the default action name
setDefaultModule()¶
Sets the name of the default module
setDefaultTask()¶
Sets the default task name
setDefaults()¶
Sets an array of default paths
wasMatched()¶
Check if the router matches any of the defined routes
Cli\Router\Exception¶
Class Source on GitHub
Exceptions thrown in Phalcon\Cli\Router will use this class
\Exception
Cli\Router\Exceptions\BeforeMatchNotCallable¶
Class Source on GitHub
\ExceptionPhalcon\Cli\Router\ExceptionPhalcon\Cli\Router\Exceptions\BeforeMatchNotCallable
Uses Phalcon\Cli\Router\Exception
Method Summary¶
Methods¶
__construct()¶
Cli\Router\Exceptions\InvalidRoutePaths¶
Class Source on GitHub
\ExceptionPhalcon\Cli\Router\ExceptionPhalcon\Cli\Router\Exceptions\InvalidRoutePaths
Uses Phalcon\Cli\Router\Exception
Method Summary¶
Methods¶
__construct()¶
Cli\Router\Exceptions\RouterArgumentsInvalidType¶
Class Source on GitHub
\ExceptionPhalcon\Cli\Router\ExceptionPhalcon\Cli\Router\Exceptions\RouterArgumentsInvalidType
Uses Phalcon\Cli\Router\Exception
Method Summary¶
Methods¶
__construct()¶
Cli\Router\Route¶
Class Source on GitHub
This class represents every route added to the router
Phalcon\Cli\Router\Route— implementsPhalcon\Cli\Router\RouteInterface
Uses Phalcon\Cli\Router\Exceptions\BeforeMatchNotCallable · Phalcon\Cli\Router\Exceptions\InvalidRoutePaths
Method Summary¶
public __construct(string $pattern,mixed $paths = null) public RouteInterface beforeMatch( mixed $callback ) Sets a callback that is called if the route is matched. public string compilePattern( string $pattern ) Replaces placeholders from pattern returning a valid PCRE regular public RouteInterface convert(string $name,mixed $converter) Adds a converter to perform an additional transformation for certain public void delimiter( string $delimiter = null ) Set the routing delimiter. public array|bool extractNamedParams( string $pattern ) Extracts parameters from a string public mixed getBeforeMatch() Returns the 'before match' callback if any public string getCompiledPattern() Returns the route's compiled pattern public array getConverters() Returns the router converter public string getDelimiter() Get routing delimiter public string getDescription() Returns the route's description public string getName() Returns the route's name public array getPaths() Returns the paths public string getPattern() Returns the route's pattern public array getReversedPaths() Returns the paths using positions as keys and names as values public string getRouteId() Returns the route's id public void reConfigure(string $pattern,mixed $paths = null) Reconfigure the route adding a new pattern and a set of paths public void reset() Resets the internal route id generator. public RouteInterface setDescription( string $description ) Sets the route's description public RouteInterface setName( string $name ) Sets the route's name Constants¶
string DEFAULT_DELIMITER = " " Properties¶
protected mixed|null $beforeMatch = null protected string $compiledPattern = "" protected array $converters = [] protected string $delimiter protected string $delimiterPath = self::DEFAULT_DELIMITER protected string $description = "" protected string $name = "" protected array $paths = [] protected string $pattern = "" protected string $routeId protected int $uniqueId = 0 Methods¶
__construct()¶
beforeMatch()¶
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
compilePattern()¶
Replaces placeholders from pattern returning a valid PCRE regular expression
convert()¶
Adds a converter to perform an additional transformation for certain parameter
delimiter()¶
Set the routing delimiter.
This sets a process-global delimiter that each route captures at construction time. Configure it once during bootstrap, before any routes are created: routes built before and after a change keep their own delimiter, and Console::setArgument() reads the current value when it parses arguments.
extractNamedParams()¶
Extracts parameters from a string
getBeforeMatch()¶
Returns the 'before match' callback if any
getCompiledPattern()¶
Returns the route's compiled pattern
getConverters()¶
Returns the router converter
getDelimiter()¶
Get routing delimiter
getDescription()¶
Returns the route's description
getName()¶
Returns the route's name
getPaths()¶
Returns the paths
getPattern()¶
Returns the route's pattern
getReversedPaths()¶
Returns the paths using positions as keys and names as values
getRouteId()¶
Returns the route's id
reConfigure()¶
Reconfigure the route adding a new pattern and a set of paths
reset()¶
Resets the internal route id generator.
Intended for test isolation only. The router keys its route map by the route id, so resetting the sequence while a router still holds routes makes newly created routes overwrite existing entries.
setDescription()¶
Sets the route's description
setName()¶
Sets the route's name
Cli\Router\RouteInterface¶
Interface Source on GitHub
Interface for Phalcon\Cli\Router\Route
Note: Phalcon\Cli\Router always constructs and returns the concrete Phalcon\Cli\Router\Route, and there is no injection point for an externally built route, so this interface is a marker for type hints rather than an implementable contract. The fluent route API used in practice - beforeMatch(), getBeforeMatch(), convert(), and getConverters() - is declared on the concrete Route class, not here.
Phalcon\Cli\Router\RouteInterface
Method Summary¶
public string compilePattern( string $pattern ) Replaces placeholders from pattern returning a valid PCRE regular public delimiter( string $delimiter = null ) Set the routing delimiter public string getCompiledPattern() Returns the route's pattern public string getDelimiter() Get routing delimiter public string getDescription() Returns the route's description public string getName() Returns the route's name public array getPaths() Returns the paths public string getPattern() Returns the route's pattern public array getReversedPaths() Returns the paths using positions as keys and names as values public string getRouteId() Returns the route's id public void reConfigure(string $pattern,mixed $paths = null) Reconfigure the route adding a new pattern and a set of paths public void reset() Resets the internal route id generator public RouteInterface setDescription( string $description ) Sets the route's description public RouteInterface setName( string $name ) Sets the route's name Methods¶
compilePattern()¶
Replaces placeholders from pattern returning a valid PCRE regular expression
delimiter()¶
Set the routing delimiter
getCompiledPattern()¶
Returns the route's pattern
getDelimiter()¶
Get routing delimiter
getDescription()¶
Returns the route's description
getName()¶
Returns the route's name
getPaths()¶
Returns the paths
getPattern()¶
Returns the route's pattern
getReversedPaths()¶
Returns the paths using positions as keys and names as values
getRouteId()¶
Returns the route's id
reConfigure()¶
Reconfigure the route adding a new pattern and a set of paths
reset()¶
Resets the internal route id generator
setDescription()¶
Sets the route's description
setName()¶
Sets the route's name
Cli\Task¶
Class Source on GitHub
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()
{
}
}
Action methods receive the routed parameters as positional arguments, followed by any CLI options the dispatcher collected (appended as trailing arguments). Declare optional trailing parameters to read those options.
stdClassPhalcon\Di\InjectablePhalcon\Cli\Task— implementsPhalcon\Cli\TaskInterface,Phalcon\Events\EventsAwareInterface
Uses Phalcon\Di\Injectable · Phalcon\Events\EventsAwareInterface · Phalcon\Events\ManagerInterface
Method Summary¶
public __construct() Phalcon\Cli\Task constructor public ManagerInterface|null getEventsManager() Returns the internal event manager public void setEventsManager( ManagerInterface $eventsManager ) Sets the events manager Properties¶
protected ManagerInterface $eventsManager Methods¶
__construct()¶
Phalcon\Cli\Task constructor
getEventsManager()¶
Returns the internal event manager
setEventsManager()¶
Sets the events manager
Cli\TaskInterface¶
Interface Source on GitHub
Interface for task handlers
Phalcon\Cli\TaskInterface