PageRenderTime 39ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Zend/Console/Console.php

http://github.com/zendframework/zf2
PHP | 204 lines | 109 code | 19 blank | 76 comment | 17 complexity | 138636c70deca9b5ef3b48ba40fd93a4 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  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
  8. */
  9. namespace Zend\Console;
  10. /**
  11. * A static, utility class for interacting with Console environment.
  12. * Declared abstract to prevent from instantiating.
  13. */
  14. abstract class Console
  15. {
  16. /**
  17. * @var Adapter\AdapterInterface
  18. */
  19. protected static $instance;
  20. /**
  21. * Allow overriding whether or not we're in a console env. If set, and
  22. * boolean, returns that value from isConsole().
  23. * @var bool
  24. */
  25. protected static $isConsole;
  26. /**
  27. * Create and return Adapter\AdapterInterface instance.
  28. *
  29. * @param null|string $forceAdapter Optional adapter class name. Can be absolute namespace or class name
  30. * relative to Zend\Console\Adapter\. If not provided, a best matching
  31. * adapter will be automatically selected.
  32. * @param null|string $forceCharset optional charset name can be absolute namespace or class name relative to
  33. * Zend\Console\Charset\. If not provided, charset will be detected
  34. * automatically.
  35. * @throws Exception\InvalidArgumentException
  36. * @throws Exception\RuntimeException
  37. * @return Adapter\AdapterInterface
  38. */
  39. public static function getInstance($forceAdapter = null, $forceCharset = null)
  40. {
  41. if (static::$instance instanceof Adapter\AdapterInterface) {
  42. return static::$instance;
  43. }
  44. // Create instance
  45. if ($forceAdapter !== null) {
  46. // Use the supplied adapter class
  47. if (substr($forceAdapter, 0, 1) == '\\') {
  48. $className = $forceAdapter;
  49. } elseif (stristr($forceAdapter, '\\')) {
  50. $className = __NAMESPACE__ . '\\' . ltrim($forceAdapter, '\\');
  51. } else {
  52. $className = __NAMESPACE__ . '\\Adapter\\' . $forceAdapter;
  53. }
  54. if (!class_exists($className)) {
  55. throw new Exception\InvalidArgumentException(sprintf(
  56. 'Cannot find Console adapter class "%s"',
  57. $className
  58. ));
  59. }
  60. } else {
  61. // Try to detect best instance for console
  62. $className = static::detectBestAdapter();
  63. // Check if we were able to detect console adapter
  64. if (!$className) {
  65. throw new Exception\RuntimeException('Cannot create Console adapter - am I running in a console?');
  66. }
  67. }
  68. // Create adapter instance
  69. static::$instance = new $className();
  70. // Try to use the supplied charset class
  71. if ($forceCharset !== null) {
  72. if (substr($forceCharset, 0, 1) == '\\') {
  73. $className = $forceCharset;
  74. } elseif (stristr($forceAdapter, '\\')) {
  75. $className = __NAMESPACE__ . '\\' . ltrim($forceCharset, '\\');
  76. } else {
  77. $className = __NAMESPACE__ . '\\Charset\\' . $forceCharset;
  78. }
  79. if (!class_exists($className)) {
  80. throw new Exception\InvalidArgumentException(sprintf(
  81. 'Cannot find Charset class "%s"',
  82. $className
  83. ));
  84. }
  85. // Set adapter charset
  86. static::$instance->setCharset(new $className());
  87. }
  88. return static::$instance;
  89. }
  90. /**
  91. * Reset the console instance
  92. */
  93. public static function resetInstance()
  94. {
  95. static::$instance = null;
  96. }
  97. /**
  98. * Check if currently running under MS Windows
  99. *
  100. * @see http://stackoverflow.com/questions/738823/possible-values-for-php-os
  101. * @return bool
  102. */
  103. public static function isWindows()
  104. {
  105. return
  106. (defined('PHP_OS') && (substr_compare(PHP_OS, 'win', 0, 3, true) === 0)) ||
  107. (getenv('OS') != false && substr_compare(getenv('OS'), 'windows', 0, 7, true))
  108. ;
  109. }
  110. /**
  111. * Check if running under MS Windows Ansicon
  112. *
  113. * @return bool
  114. */
  115. public static function isAnsicon()
  116. {
  117. return getenv('ANSICON') !== false;
  118. }
  119. /**
  120. * Check if running in a console environment (CLI)
  121. *
  122. * By default, returns value of PHP_SAPI global constant. If $isConsole is
  123. * set, and a boolean value, that value will be returned.
  124. *
  125. * @return bool
  126. */
  127. public static function isConsole()
  128. {
  129. if (null === static::$isConsole) {
  130. static::$isConsole = (PHP_SAPI == 'cli');
  131. }
  132. return static::$isConsole;
  133. }
  134. /**
  135. * Override the "is console environment" flag
  136. *
  137. * @param null|bool $flag
  138. */
  139. public static function overrideIsConsole($flag)
  140. {
  141. if (null != $flag) {
  142. $flag = (bool) $flag;
  143. }
  144. static::$isConsole = $flag;
  145. }
  146. /**
  147. * Try to detect best matching adapter
  148. * @return string|null
  149. */
  150. public static function detectBestAdapter()
  151. {
  152. // Check if we are in a console environment
  153. if (!static::isConsole()) {
  154. return;
  155. }
  156. // Check if we're on windows
  157. if (static::isWindows()) {
  158. if (static::isAnsicon()) {
  159. $className = __NAMESPACE__ . '\Adapter\WindowsAnsicon';
  160. } else {
  161. $className = __NAMESPACE__ . '\Adapter\Windows';
  162. }
  163. return $className;
  164. }
  165. // Default is a Posix console
  166. $className = __NAMESPACE__ . '\Adapter\Posix';
  167. return $className;
  168. }
  169. /**
  170. * Pass-thru static call to current AdapterInterface instance.
  171. *
  172. * @param $funcName
  173. * @param $arguments
  174. * @return mixed
  175. */
  176. public static function __callStatic($funcName, $arguments)
  177. {
  178. $instance = static::getInstance();
  179. return call_user_func_array(array($instance, $funcName), $arguments);
  180. }
  181. }