setCharset(new $className()); } return static::$instance; } /** * Reset the console instance */ public static function resetInstance() { static::$instance = null; } /** * Check if currently running under MS Windows * * @see http://stackoverflow.com/questions/738823/possible-values-for-php-os * @return bool */ public static function isWindows() { return (defined('PHP_OS') && (substr_compare(PHP_OS, 'win', 0, 3, true) === 0)) || (getenv('OS') != false && substr_compare(getenv('OS'), 'windows', 0, 7, true)) ; } /** * Check if running under MS Windows Ansicon * * @return bool */ public static function isAnsicon() { return getenv('ANSICON') !== false; } /** * Check if running in a console environment (CLI) * * By default, returns value of PHP_SAPI global constant. If $isConsole is * set, and a boolean value, that value will be returned. * * @return bool */ public static function isConsole() { if (null === static::$isConsole) { static::$isConsole = (PHP_SAPI == 'cli'); } return static::$isConsole; } /** * Override the "is console environment" flag * * @param null|bool $flag */ public static function overrideIsConsole($flag) { if (null != $flag) { $flag = (bool) $flag; } static::$isConsole = $flag; } /** * Try to detect best matching adapter * @return string|null */ public static function detectBestAdapter() { // Check if we are in a console environment if (!static::isConsole()) { return; } // Check if we're on windows if (static::isWindows()) { if (static::isAnsicon()) { $className = __NAMESPACE__ . '\Adapter\WindowsAnsicon'; } else { $className = __NAMESPACE__ . '\Adapter\Windows'; } return $className; } // Default is a Posix console $className = __NAMESPACE__ . '\Adapter\Posix'; return $className; } /** * Pass-thru static call to current AdapterInterface instance. * * @param $funcName * @param $arguments * @return mixed */ public static function __callStatic($funcName, $arguments) { $instance = static::getInstance(); return call_user_func_array(array($instance, $funcName), $arguments); } }