PHP Class ZF\Console\Application

Show file Open project: zfcampus/zf-console Class Usage Examples

Protected Properties

Property Type Description
$banner null | string | callable
$bannerDisabledForUserCommands boolean
$console Zend\Console\Adapter\AdapterInterface
$debug boolean Flag to specify if the application is in debug mode
$dispatcher ZF\Console\DispatcherInterface
$exceptionHandler callable
$footer null | string | callable
$name string
$routeCollection ZF\Console\RouteCollection
$version string

Public Methods

Method Description
__construct ( string $name, string $version, array | Traversabl\Traversable $routes, Zend\Console\Adapter\AdapterInterface $console = null, ZF\Console\DispatcherInterface $dispatcher = null ) Initialize the application
getDispatcher ( ) : ZF\Console\DispatcherInterface
getExceptionHandler ( ) : callable Gets the registered exception handler
isBannerDisabledForUserCommands ( ) : boolean Whether or not to disable the banner in user commands. False by default.
removeRoute ( String $name ) : self Remove a route by name
run ( array $args = null ) : integer Run the application
setBanner ( null | string | callable $bannerOrCallable ) : self Set the banner to display.
setBannerDisabledForUserCommands ( boolean $flag = true ) : self Disables the banner for user commands. Still shows it before usage messages.
setDebug ( boolean $flag ) Sets the debug flag of the application
setExceptionHandler ( callable $handler ) : self Sets exception handler to use the expection Message
setFooter ( null | string | callable $footerOrCallable ) : self Set the footer to display.
showMessage ( string | callable $messageOrCallable ) Display a message (banner or footer)
showUsageMessage ( null | string $name = null ) Displays a usage message for the router
showVersion ( Zend\Console\Adapter\AdapterInterface $console ) : integer Display the application version

Protected Methods

Method Description
calcMaxString ( array $data = [] ) : integer Calculate the maximum string length for an array
initializeExceptionHandler ( ) Initialize the exception handler (if not in debug mode)
mapRouteHandler ( array $route ) Map a route handler
processRun ( array $args ) : integer Process run If the argument list is empty, displays a usage message.
setProcessTitle ( ) Set CLI process title (PHP versions >= 5.5)
setRoutes ( array | Traversabl\Traversable $routes ) : self Set routes to use
setupAutocompleteCommand ( ZF\Console\RouteCollection $routeCollection, ZF\Console\DispatcherInterface $dispatcher ) Sets up the default autocomplete command
setupHelpCommand ( ZF\Console\RouteCollection $routeCollection, ZF\Console\DispatcherInterface $dispatcher ) Sets up the default help command
setupVersionCommand ( ZF\Console\RouteCollection $routeCollection, ZF\Console\DispatcherInterface $dispatcher ) Sets up the default version command
showUnmatchedRouteMessage ( array $args ) Show message indicating inability to match a route.
showUnrecognizedRouteMessage ( string $name ) Display an error message indicating a route name was not recognized
showUsageMessageForRoute ( Route $route, $log = false ) Display the usage message for an individual route
validateMessage ( mixed $stringOrCallable )

Method Details

__construct() public method

Creates a RouteCollection and populates it with the $routes provided. Sets the banner to call showVersion(). If no help command is defined, defines one. If no version command is defined, defines one.
public __construct ( string $name, string $version, array | Traversabl\Traversable $routes, Zend\Console\Adapter\AdapterInterface $console = null, ZF\Console\DispatcherInterface $dispatcher = null )
$name string Application name
$version string Application version
$routes array | Traversabl\Traversable Routes/route specifications to use for the application
$console Zend\Console\Adapter\AdapterInterface Console adapter to use within the application
$dispatcher ZF\Console\DispatcherInterface Configured dispatcher mapping routes to callables

calcMaxString() protected method

Calculate the maximum string length for an array
protected calcMaxString ( array $data = [] ) : integer
$data array
return integer

getDispatcher() public method

public getDispatcher ( ) : ZF\Console\DispatcherInterface
return ZF\Console\DispatcherInterface

getExceptionHandler() public method

Lazy-instantiates an ExceptionHandler instance with the current console instance if no handler has been specified.
public getExceptionHandler ( ) : callable
return callable

initializeExceptionHandler() protected method

Initialize the exception handler (if not in debug mode)

isBannerDisabledForUserCommands() public method

Whether or not to disable the banner in user commands. False by default.

mapRouteHandler() protected method

If a given route specification has a "handler" entry, and the dispatcher does not currently have a handler for that command, map it.
protected mapRouteHandler ( array $route )
$route array

processRun() protected method

If arguments are provided, but no routes match, displays a usage message and returns a status of 1. Otherwise, attempts to dispatch the matched command, returning the execution status.
protected processRun ( array $args ) : integer
$args array
return integer

removeRoute() public method

Remove a route by name
public removeRoute ( String $name ) : self
$name String
return self

run() public method

If no arguments are provided, pulls them from $argv, stripping the script argument first. If the argument list is empty, displays a usage message. If arguments are provided, but no routes match, displays a usage message and returns a status of 1. Otherwise, attempts to dispatch the matched command, returning the execution status.
public run ( array $args = null ) : integer
$args array
return integer

setBanner() public method

Used whenever the application is called without arguments. If the default help implementation is used, also displayed with help messages.
public setBanner ( null | string | callable $bannerOrCallable ) : self
$bannerOrCallable null | string | callable
return self

setBannerDisabledForUserCommands() public method

Disables the banner for user commands. Still shows it before usage messages.
public setBannerDisabledForUserCommands ( boolean $flag = true ) : self
$flag boolean
return self

setDebug() public method

Sets the debug flag of the application
public setDebug ( boolean $flag )
$flag boolean

setExceptionHandler() public method

Sets exception handler to use the expection Message
public setExceptionHandler ( callable $handler ) : self
$handler callable
return self

setFooter() public method

Used whenever the application is called without arguments. If the default help implementation is used, also displayed with help messages.
public setFooter ( null | string | callable $footerOrCallable ) : self
$footerOrCallable null | string | callable
return self

setProcessTitle() protected method

Set CLI process title (PHP versions >= 5.5)
protected setProcessTitle ( )

setRoutes() protected method

Allows specifying an array of routes, which may be mixed Route instances or array specifications for creating routes.
protected setRoutes ( array | Traversabl\Traversable $routes ) : self
$routes array | Traversabl\Traversable
return self

setupAutocompleteCommand() protected method

Creates the route, and maps the command.
protected setupAutocompleteCommand ( ZF\Console\RouteCollection $routeCollection, ZF\Console\DispatcherInterface $dispatcher )
$routeCollection ZF\Console\RouteCollection
$dispatcher ZF\Console\DispatcherInterface

setupHelpCommand() protected method

Creates the route, and maps the command.
protected setupHelpCommand ( ZF\Console\RouteCollection $routeCollection, ZF\Console\DispatcherInterface $dispatcher )
$routeCollection ZF\Console\RouteCollection
$dispatcher ZF\Console\DispatcherInterface

setupVersionCommand() protected method

Creates the route, and maps the command.
protected setupVersionCommand ( ZF\Console\RouteCollection $routeCollection, ZF\Console\DispatcherInterface $dispatcher )
$routeCollection ZF\Console\RouteCollection
$dispatcher ZF\Console\DispatcherInterface

showMessage() public method

If the message is a string and not callable, uses the composed console instance to render it. If the message is a callable, calls it with the composed console instance as an argument.
public showMessage ( string | callable $messageOrCallable )
$messageOrCallable string | callable

showUnmatchedRouteMessage() protected method

Show message indicating inability to match a route.
protected showUnmatchedRouteMessage ( array $args )
$args array

showUnrecognizedRouteMessage() protected method

Display an error message indicating a route name was not recognized
protected showUnrecognizedRouteMessage ( string $name )
$name string

showUsageMessage() public method

If a route name is provided, usage for that route only will be displayed; otherwise, the name/short description for each will be present.
public showUsageMessage ( null | string $name = null )
$name null | string

showUsageMessageForRoute() protected method

Display the usage message for an individual route
protected showUsageMessageForRoute ( Route $route, $log = false )
$route Route

showVersion() public method

Display the application version
public showVersion ( Zend\Console\Adapter\AdapterInterface $console ) : integer
$console Zend\Console\Adapter\AdapterInterface
return integer

validateMessage() protected method

protected validateMessage ( mixed $stringOrCallable )
$stringOrCallable mixed

Property Details

$banner protected property

protected null|string|callable $banner
return null | string | callable

$bannerDisabledForUserCommands protected property

protected bool $bannerDisabledForUserCommands
return boolean

$console protected property

protected AdapterInterface,Zend\Console\Adapter $console
return Zend\Console\Adapter\AdapterInterface

$debug protected property

Flag to specify if the application is in debug mode
protected bool $debug
return boolean

$dispatcher protected property

protected DispatcherInterface,ZF\Console $dispatcher
return ZF\Console\DispatcherInterface

$exceptionHandler protected property

protected callable $exceptionHandler
return callable

$name protected property

protected string $name
return string

$routeCollection protected property

protected RouteCollection,ZF\Console $routeCollection
return ZF\Console\RouteCollection

$version protected property

protected string $version
return string