3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Console
;
13 * A static, utility class for interacting with Console environment.
14 * Declared abstract to prevent from instantiating.
16 abstract class Console
19 * @var Adapter\AdapterInterface
21 protected static $instance;
24 * Allow overriding whether or not we're in a console env. If set, and
25 * boolean, returns that value from isConsole().
28 protected static $isConsole;
31 * Create and return Adapter\AdapterInterface instance.
33 * @param null|string $forceAdapter Optional adapter class name. Can be absolute namespace or class name
34 * relative to Zend\Console\Adapter\. If not provided, a best matching
35 * adapter will be automatically selected.
36 * @param null|string $forceCharset optional charset name can be absolute namespace or class name relative to
37 * Zend\Console\Charset\. If not provided, charset will be detected
39 * @throws Exception\InvalidArgumentException
40 * @throws Exception\RuntimeException
41 * @return Adapter\AdapterInterface
43 public static function getInstance($forceAdapter = null, $forceCharset = null)
45 if (static::$instance instanceof Adapter\AdapterInterface
) {
46 return static::$instance;
51 if ($forceAdapter !== null) {
52 // Use the supplied adapter class
53 if (substr($forceAdapter, 0, 1) == '\\') {
54 $className = $forceAdapter;
55 } elseif (stristr($forceAdapter, '\\')) {
56 $className = __NAMESPACE__
. '\\' . ltrim($forceAdapter, '\\');
58 $className = __NAMESPACE__
. '\\Adapter\\' . $forceAdapter;
61 if (!class_exists($className)) {
62 throw new Exception\
InvalidArgumentException(sprintf(
63 'Cannot find Console adapter class "%s"',
68 // Try to detect best instance for console
69 $className = static::detectBestAdapter();
71 // Check if we were able to detect console adapter
73 throw new Exception\
RuntimeException('Cannot create Console adapter - am I running in a console?');
77 // Create adapter instance
78 static::$instance = new $className();
80 // Try to use the supplied charset class
81 if ($forceCharset !== null) {
82 if (substr($forceCharset, 0, 1) == '\\') {
83 $className = $forceCharset;
84 } elseif (stristr($forceAdapter, '\\')) {
85 $className = __NAMESPACE__
. '\\' . ltrim($forceCharset, '\\');
87 $className = __NAMESPACE__
. '\\Charset\\' . $forceCharset;
90 if (!class_exists($className)) {
91 throw new Exception\
InvalidArgumentException(sprintf(
92 'Cannot find Charset class "%s"',
97 // Set adapter charset
98 static::$instance->setCharset(new $className());
101 return static::$instance;
105 * Reset the console instance
107 public static function resetInstance()
109 static::$instance = null;
113 * Check if currently running under MS Windows
115 * @see http://stackoverflow.com/questions/738823/possible-values-for-php-os
118 public static function isWindows()
121 (defined('PHP_OS') && (substr_compare(PHP_OS
, 'win', 0, 3, true) === 0)) ||
122 (getenv('OS') != false && substr_compare(getenv('OS'), 'windows', 0, 7, true))
127 * Check if running under MS Windows Ansicon
131 public static function isAnsicon()
133 return getenv('ANSICON') !== false;
137 * Check if running in a console environment (CLI)
139 * By default, returns value of PHP_SAPI global constant. If $isConsole is
140 * set, and a boolean value, that value will be returned.
144 public static function isConsole()
146 if (null === static::$isConsole) {
147 static::$isConsole = (PHP_SAPI
== 'cli');
149 return static::$isConsole;
153 * Override the "is console environment" flag
155 * @param null|bool $flag
157 public static function overrideIsConsole($flag)
160 $flag = (bool) $flag;
162 static::$isConsole = $flag;
166 * Try to detect best matching adapter
167 * @return string|null
169 public static function detectBestAdapter()
171 // Check if we are in a console environment
172 if (!static::isConsole()) {
176 // Check if we're on windows
177 if (static::isWindows()) {
178 if (static::isAnsicon()) {
179 $className = __NAMESPACE__
. '\Adapter\WindowsAnsicon';
181 $className = __NAMESPACE__
. '\Adapter\Windows';
187 // Default is a Posix console
188 $className = __NAMESPACE__
. '\Adapter\Posix';
193 * Pass-thru static call to current AdapterInterface instance.
199 public static function __callStatic($funcName, $arguments)
201 $instance = static::getInstance();
202 return call_user_func_array(array($instance, $funcName), $arguments);