PageRenderTime 45ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/4gdevs/online-class-record-system
PHP | 207 lines | 143 code | 32 blank | 32 comment | 17 complexity | 8085c8e11c81847921cf06c502569b3e 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 InputArgument('include', InputArgument::IS_ARRAY),
  127. )));
  128. } catch (\RuntimeException $e) {
  129. $usageException = $e;
  130. }
  131. $config = array();
  132. // Handle --config
  133. if ($configFile = $input->getOption('config')) {
  134. $config['configFile'] = $configFile;
  135. }
  136. $shell = new Shell(new Configuration($config));
  137. // Handle --help
  138. if ($usageException !== null || $input->getOption('help')) {
  139. if ($usageException !== null) {
  140. echo $usageException->getMessage() . PHP_EOL . PHP_EOL;
  141. }
  142. $version = $shell->getVersion();
  143. $name = basename(reset($_SERVER['argv']));
  144. echo <<<EOL
  145. $version
  146. Usage:
  147. $name [--version] [--help] [files...]
  148. Options:
  149. --help -h Display this help message.
  150. --config -c Use an alternate PsySH config file location.
  151. --cwd Use an alternate working directory.
  152. --version -v Display the PsySH version.
  153. EOL;
  154. exit($usageException === null ? 0 : 1);
  155. }
  156. // Handle --version
  157. if ($input->getOption('version')) {
  158. echo $shell->getVersion() . PHP_EOL;
  159. exit(0);
  160. }
  161. // Pass additional arguments to Shell as 'includes'
  162. $shell->setIncludes($input->getArgument('include'));
  163. try {
  164. // And go!
  165. $shell->run();
  166. } catch (Exception $e) {
  167. echo $e->getMessage() . PHP_EOL;
  168. // TODO: this triggers the "exited unexpectedly" logic in the
  169. // ForkingLoop, so we can't exit(1) after starting the shell...
  170. // fix this :)
  171. // exit(1);
  172. }
  173. };
  174. }
  175. }