PageRenderTime 49ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/psy/psysh/src/Psy/functions.php

https://gitlab.com/Marwein/tnjobs
PHP | 220 lines | 154 code | 33 blank | 33 comment | 19 complexity | acc2e16175c058132f9cbd505e6d2a35 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of Psy Shell.
  4. *
  5. * (c) 2012-2015 Justin Hileman
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Psy;
  11. use Symfony\Component\Console\Input\ArgvInput;
  12. use Symfony\Component\Console\Input\InputArgument;
  13. use Symfony\Component\Console\Input\InputDefinition;
  14. use Symfony\Component\Console\Input\InputOption;
  15. if (!function_exists('Psy\sh')) {
  16. /**
  17. * Command to return the eval-able code to startup PsySH.
  18. *
  19. * eval(\Psy\sh());
  20. *
  21. * @return string
  22. */
  23. function sh()
  24. {
  25. return 'extract(\Psy\Shell::debug(get_defined_vars(), isset($this) ? $this : null));';
  26. }
  27. }
  28. if (!function_exists('Psy\info')) {
  29. function info()
  30. {
  31. $config = new Configuration();
  32. $core = array(
  33. 'PsySH version' => Shell::VERSION,
  34. 'PHP version' => PHP_VERSION,
  35. 'default includes' => $config->getDefaultIncludes(),
  36. 'require semicolons' => $config->requireSemicolons(),
  37. 'error logging level' => $config->errorLoggingLevel(),
  38. 'config file' => array(
  39. 'default config file' => $config->getConfigFile(),
  40. 'local config file' => $config->getLocalConfigFile(),
  41. 'PSYSH_CONFIG env' => getenv('PSYSH_CONFIG'),
  42. ),
  43. // 'config dir' => $config->getConfigDir(),
  44. // 'data dir' => $config->getDataDir(),
  45. // 'runtime dir' => $config->getRuntimeDir(),
  46. );
  47. if ($config->hasReadline()) {
  48. $info = readline_info();
  49. $readline = array(
  50. 'readline available' => true,
  51. 'readline enabled' => $config->useReadline(),
  52. 'readline service' => get_class($config->getReadline()),
  53. 'readline library' => $info['library_version'],
  54. );
  55. if ($info['readline_name'] !== '') {
  56. $readline['readline name'] = $info['readline_name'];
  57. }
  58. } else {
  59. $readline = array(
  60. 'readline available' => false,
  61. );
  62. }
  63. $pcntl = array(
  64. 'pcntl available' => function_exists('pcntl_signal'),
  65. 'posix available' => function_exists('posix_getpid'),
  66. );
  67. $history = array(
  68. 'history file' => $config->getHistoryFile(),
  69. 'history size' => $config->getHistorySize(),
  70. 'erase duplicates' => $config->getEraseDuplicates(),
  71. );
  72. $docs = array(
  73. 'manual db file' => $config->getManualDbFile(),
  74. 'sqlite available' => true,
  75. );
  76. try {
  77. if ($db = $config->getManualDb()) {
  78. if ($q = $db->query('SELECT * FROM meta;')) {
  79. $q->setFetchMode(\PDO::FETCH_KEY_PAIR);
  80. $meta = $q->fetchAll();
  81. foreach ($meta as $key => $val) {
  82. switch ($key) {
  83. case 'built_at':
  84. $d = new \DateTime('@' . $val);
  85. $val = $d->format(\DateTime::RFC2822);
  86. break;
  87. }
  88. $key = 'db ' . str_replace('_', ' ', $key);
  89. $docs[$key] = $val;
  90. }
  91. } else {
  92. $docs['db schema'] = '0.1.0';
  93. }
  94. }
  95. } catch (Exception\RuntimeException $e) {
  96. if ($e->getMessage() === 'SQLite PDO driver not found') {
  97. $docs['sqlite available'] = false;
  98. } else {
  99. throw $e;
  100. }
  101. }
  102. $autocomplete = array(
  103. 'tab completion enabled' => $config->getTabCompletion(),
  104. 'custom matchers' => array_map('get_class', $config->getTabCompletionMatchers()),
  105. );
  106. return array_merge($core, compact('pcntl', 'readline', 'history', 'docs', 'autocomplete'));
  107. }
  108. }
  109. if (!function_exists('Psy\bin')) {
  110. /**
  111. * `psysh` command line executable.
  112. *
  113. * @return Closure
  114. */
  115. function bin()
  116. {
  117. return function () {
  118. $usageException = null;
  119. $input = new ArgvInput();
  120. try {
  121. $input->bind(new InputDefinition(array(
  122. new InputOption('help', 'h', InputOption::VALUE_NONE),
  123. new InputOption('config', 'c', InputOption::VALUE_REQUIRED),
  124. new InputOption('version', 'v', InputOption::VALUE_NONE),
  125. new InputOption('cwd', null, InputOption::VALUE_REQUIRED),
  126. new InputOption('color', null, InputOption::VALUE_NONE),
  127. new InputOption('no-color', null, InputOption::VALUE_NONE),
  128. new InputArgument('include', InputArgument::IS_ARRAY),
  129. )));
  130. } catch (\RuntimeException $e) {
  131. $usageException = $e;
  132. }
  133. $config = array();
  134. // Handle --config
  135. if ($configFile = $input->getOption('config')) {
  136. $config['configFile'] = $configFile;
  137. }
  138. // Handle --color and --no-color
  139. if ($input->getOption('color') && $input->getOption('no-color')) {
  140. $usageException = new \RuntimeException('Using both "--color" and "--no-color" options is invalid.');
  141. } elseif ($input->getOption('color')) {
  142. $config['colorMode'] = Configuration::COLOR_MODE_FORCED;
  143. } elseif ($input->getOption('no-color')) {
  144. $config['colorMode'] = Configuration::COLOR_MODE_DISABLED;
  145. }
  146. $shell = new Shell(new Configuration($config));
  147. // Handle --help
  148. if ($usageException !== null || $input->getOption('help')) {
  149. if ($usageException !== null) {
  150. echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
  151. }
  152. $version = $shell->getVersion();
  153. $name = basename(reset($_SERVER['argv']));
  154. echo <<<EOL
  155. $version
  156. Usage:
  157. $name [--version] [--help] [files...]
  158. Options:
  159. --help -h Display this help message.
  160. --config -c Use an alternate PsySH config file location.
  161. --cwd Use an alternate working directory.
  162. --version -v Display the PsySH version.
  163. --color Force colors in output.
  164. --no-color Disable colors in output.
  165. EOL;
  166. exit($usageException === null ? 0 : 1);
  167. }
  168. // Handle --version
  169. if ($input->getOption('version')) {
  170. echo $shell->getVersion() . PHP_EOL;
  171. exit(0);
  172. }
  173. // Pass additional arguments to Shell as 'includes'
  174. $shell->setIncludes($input->getArgument('include'));
  175. try {
  176. // And go!
  177. $shell->run();
  178. } catch (Exception $e) {
  179. echo $e->getMessage() . PHP_EOL;
  180. // TODO: this triggers the "exited unexpectedly" logic in the
  181. // ForkingLoop, so we can't exit(1) after starting the shell...
  182. // fix this :)
  183. // exit(1);
  184. }
  185. };
  186. }
  187. }