PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Zend/Console/Console.php

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