This repository was archived by the owner on Jan 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathConsole.php
204 lines (181 loc) · 6.14 KB
/
Console.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/
namespace Zend\Console;
/**
* A static, utility class for interacting with Console environment.
* Declared abstract to prevent from instantiating.
*/
abstract class Console
{
/**
* @var Adapter\AdapterInterface
*/
protected static $instance;
/**
* Allow overriding whether or not we're in a console env. If set, and
* boolean, returns that value from isConsole().
* @var bool
*/
protected static $isConsole;
/**
* Create and return Adapter\AdapterInterface instance.
*
* @param null|string $forceAdapter Optional adapter class name. Can be absolute namespace or class name
* relative to Zend\Console\Adapter\. If not provided, a best matching
* adapter will be automatically selected.
* @param null|string $forceCharset optional charset name can be absolute namespace or class name relative to
* Zend\Console\Charset\. If not provided, charset will be detected
* automatically.
* @throws Exception\InvalidArgumentException
* @throws Exception\RuntimeException
* @return Adapter\AdapterInterface
*/
public static function getInstance($forceAdapter = null, $forceCharset = null)
{
if (static::$instance instanceof Adapter\AdapterInterface) {
return static::$instance;
}
// Create instance
if ($forceAdapter !== null) {
// Use the supplied adapter class
if (0 === strpos($forceAdapter, '\\')) {
$className = $forceAdapter;
} elseif (stristr($forceAdapter, '\\')) {
$className = __NAMESPACE__ . '\\' . ltrim($forceAdapter, '\\');
} else {
$className = __NAMESPACE__ . '\\Adapter\\' . $forceAdapter;
}
if (! class_exists($className)) {
throw new Exception\InvalidArgumentException(sprintf(
'Cannot find Console adapter class "%s"',
$className
));
}
} else {
// Try to detect best instance for console
$className = static::detectBestAdapter();
// Check if we were able to detect console adapter
if (! $className) {
throw new Exception\RuntimeException('Cannot create Console adapter - am I running in a console?');
}
}
// Create adapter instance
static::$instance = new $className();
// Try to use the supplied charset class
if ($forceCharset !== null) {
if (0 === strpos($forceCharset, '\\')) {
$className = $forceCharset;
} elseif (false !== stripos($forceAdapter, '\\')) {
$className = __NAMESPACE__ . '\\' . ltrim($forceCharset, '\\');
} else {
$className = __NAMESPACE__ . '\\Charset\\' . $forceCharset;
}
if (! class_exists($className)) {
throw new Exception\InvalidArgumentException(sprintf(
'Cannot find Charset class "%s"',
$className
));
}
// Set adapter charset
static::$instance->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([$instance, $funcName], $arguments);
}
}