This component provides functionality that helps writing CLI oriented code that has runtime-specific execution params.
Environment
provides some useful methods:
Environment::isWindows
- Checks if currently running under MS WindowsEnvironment::isAnsicon
- When currently running under MS Windows checks if ANSI x3.64 is supported and enabledEnvironment::isConsole
- Checks if running in a console environment (CLI)Environment::isInteractive
- Checks if the file descriptor is an interactive terminalEnvironment::hasColorSupport
- Checks the supports of colorizationEnvironment::getDimensions
- Gets the terminal dimensions based on the current environmentEnvironment::setDimensions
- Sets terminal dimensionsEnvironment::getModeCon
- Runs and parses Microsoft DOSMODE CON
command if it's available (suppressing any error output)Environment::getSttySize
- Runs and parsesstty size
command if it's available (suppressing any error output)Environment::getNumberOfColumns
- Gets the number of columns of the terminalEnvironment::getNumberOfRows
- Gets the number of rows of the terminal
Firstly you need to create an environment aware Console Application and implement
Phalcon\Cli\Environment\EnvironmentAwareInterface
:
namespace MyAwesomeApplication;
use Phalcon\Cli\Environment\EnvironmentInterface;
use Phalcon\Cli\Environment\EnvironmentAwareInterface;
use Phalcon\Di\FactoryDefault\Cli as CliDi;
use Phalcon\Cli\Console as PhConsole;
use Phalcon\DiInterface;
class Console extends PhConsole implements EnvironmentAwareInterface
{
/**
* @var EnvironmentInterface|null
*/
protected $environment;
public function __construct(DiInterface $di = null)
{
$di = $di ?: new CliDi;
parent::__construct($di);
}
public function setEnvironment(EnvironmentInterface $environment)
{
$this->environment = $environment;
return $this;
}
public function getEnvironment()
{
return $this->environment;
}
}
Then you need to add Environment
component to your Console Application.
use Phalcon\Cli\Environment\Environment;
use MyAwesomeApplication\Console;
$application = new Console;
$application->setEnvironment(
new Environment
);
The Phalcon\Cli\Environment\EnvironmentInterface
interface must be implemented to create your own Environment
replacing the one provided by Phalcon\Cli\Environment
component or extend the current one.