comparison vendor/psy/psysh/src/Configuration.php @ 4:a9cd425dd02b

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:11:55 +0000
parents c75dbcec494b
children
comparison
equal deleted inserted replaced
3:307d7a7fd348 4:a9cd425dd02b
112 $this->setColorMode(self::COLOR_MODE_AUTO); 112 $this->setColorMode(self::COLOR_MODE_AUTO);
113 113
114 // explicit configFile option 114 // explicit configFile option
115 if (isset($config['configFile'])) { 115 if (isset($config['configFile'])) {
116 $this->configFile = $config['configFile']; 116 $this->configFile = $config['configFile'];
117 } elseif ($configFile = getenv('PSYSH_CONFIG')) { 117 } elseif ($configFile = \getenv('PSYSH_CONFIG')) {
118 $this->configFile = $configFile; 118 $this->configFile = $configFile;
119 } 119 }
120 120
121 // legacy baseDir option 121 // legacy baseDir option
122 if (isset($config['baseDir'])) { 122 if (isset($config['baseDir'])) {
143 * is available, it will be loaded and merged with the current config. 143 * is available, it will be loaded and merged with the current config.
144 */ 144 */
145 public function init() 145 public function init()
146 { 146 {
147 // feature detection 147 // feature detection
148 $this->hasReadline = function_exists('readline'); 148 $this->hasReadline = \function_exists('readline');
149 $this->hasPcntl = function_exists('pcntl_signal') && function_exists('posix_getpid'); 149 $this->hasPcntl = \function_exists('pcntl_signal') && \function_exists('posix_getpid');
150 150
151 if ($configFile = $this->getConfigFile()) { 151 if ($configFile = $this->getConfigFile()) {
152 $this->loadConfigFile($configFile); 152 $this->loadConfigFile($configFile);
153 } 153 }
154 154
178 } 178 }
179 179
180 $files = ConfigPaths::getConfigFiles(['config.php', 'rc.php'], $this->configDir); 180 $files = ConfigPaths::getConfigFiles(['config.php', 'rc.php'], $this->configDir);
181 181
182 if (!empty($files)) { 182 if (!empty($files)) {
183 if ($this->warnOnMultipleConfigs && count($files) > 1) { 183 if ($this->warnOnMultipleConfigs && \count($files) > 1) {
184 $msg = sprintf('Multiple configuration files found: %s. Using %s', implode($files, ', '), $files[0]); 184 $msg = \sprintf('Multiple configuration files found: %s. Using %s', \implode($files, ', '), $files[0]);
185 trigger_error($msg, E_USER_NOTICE); 185 \trigger_error($msg, E_USER_NOTICE);
186 } 186 }
187 187
188 return $files[0]; 188 return $files[0];
189 } 189 }
190 } 190 }
197 * 197 *
198 * @return string 198 * @return string
199 */ 199 */
200 public function getLocalConfigFile() 200 public function getLocalConfigFile()
201 { 201 {
202 $localConfig = getcwd() . '/.psysh.php'; 202 $localConfig = \getcwd() . '/.psysh.php';
203 203
204 if (@is_file($localConfig)) { 204 if (@\is_file($localConfig)) {
205 return $localConfig; 205 return $localConfig;
206 } 206 }
207 } 207 }
208 208
209 /** 209 /**
213 */ 213 */
214 public function loadConfig(array $options) 214 public function loadConfig(array $options)
215 { 215 {
216 foreach (self::$AVAILABLE_OPTIONS as $option) { 216 foreach (self::$AVAILABLE_OPTIONS as $option) {
217 if (isset($options[$option])) { 217 if (isset($options[$option])) {
218 $method = 'set' . ucfirst($option); 218 $method = 'set' . \ucfirst($option);
219 $this->$method($options[$option]); 219 $this->$method($options[$option]);
220 } 220 }
221 } 221 }
222 222
223 // legacy `tabCompletion` option 223 // legacy `tabCompletion` option
224 if (isset($options['tabCompletion'])) { 224 if (isset($options['tabCompletion'])) {
225 $msg = '`tabCompletion` is deprecated; use `useTabCompletion` instead.'; 225 $msg = '`tabCompletion` is deprecated; use `useTabCompletion` instead.';
226 @trigger_error($msg, E_USER_DEPRECATED); 226 @\trigger_error($msg, E_USER_DEPRECATED);
227 227
228 $this->setUseTabCompletion($options['tabCompletion']); 228 $this->setUseTabCompletion($options['tabCompletion']);
229 } 229 }
230 230
231 foreach (['commands', 'matchers', 'casters'] as $option) { 231 foreach (['commands', 'matchers', 'casters'] as $option) {
232 if (isset($options[$option])) { 232 if (isset($options[$option])) {
233 $method = 'add' . ucfirst($option); 233 $method = 'add' . \ucfirst($option);
234 $this->$method($options[$option]); 234 $this->$method($options[$option]);
235 } 235 }
236 } 236 }
237 237
238 // legacy `tabCompletionMatchers` option 238 // legacy `tabCompletionMatchers` option
239 if (isset($options['tabCompletionMatchers'])) { 239 if (isset($options['tabCompletionMatchers'])) {
240 $msg = '`tabCompletionMatchers` is deprecated; use `matchers` instead.'; 240 $msg = '`tabCompletionMatchers` is deprecated; use `matchers` instead.';
241 @trigger_error($msg, E_USER_DEPRECATED); 241 @\trigger_error($msg, E_USER_DEPRECATED);
242 242
243 $this->addMatchers($options['tabCompletionMatchers']); 243 $this->addMatchers($options['tabCompletionMatchers']);
244 } 244 }
245 } 245 }
246 246
265 } 265 }
266 }; 266 };
267 $result = $load($this); 267 $result = $load($this);
268 268
269 if (!empty($result)) { 269 if (!empty($result)) {
270 if (is_array($result)) { 270 if (\is_array($result)) {
271 $this->loadConfig($result); 271 $this->loadConfig($result);
272 } else { 272 } else {
273 throw new \InvalidArgumentException('Psy Shell configuration must return an array of options'); 273 throw new \InvalidArgumentException('Psy Shell configuration must return an array of options');
274 } 274 }
275 } 275 }
357 { 357 {
358 if (!isset($this->runtimeDir)) { 358 if (!isset($this->runtimeDir)) {
359 $this->runtimeDir = ConfigPaths::getRuntimeDir(); 359 $this->runtimeDir = ConfigPaths::getRuntimeDir();
360 } 360 }
361 361
362 if (!is_dir($this->runtimeDir)) { 362 if (!\is_dir($this->runtimeDir)) {
363 mkdir($this->runtimeDir, 0700, true); 363 \mkdir($this->runtimeDir, 0700, true);
364 } 364 }
365 365
366 return $this->runtimeDir; 366 return $this->runtimeDir;
367 } 367 }
368 368
391 } 391 }
392 392
393 $files = ConfigPaths::getConfigFiles(['psysh_history', 'history'], $this->configDir); 393 $files = ConfigPaths::getConfigFiles(['psysh_history', 'history'], $this->configDir);
394 394
395 if (!empty($files)) { 395 if (!empty($files)) {
396 if ($this->warnOnMultipleConfigs && count($files) > 1) { 396 if ($this->warnOnMultipleConfigs && \count($files) > 1) {
397 $msg = sprintf('Multiple history files found: %s. Using %s', implode($files, ', '), $files[0]); 397 $msg = \sprintf('Multiple history files found: %s. Using %s', \implode($files, ', '), $files[0]);
398 trigger_error($msg, E_USER_NOTICE); 398 \trigger_error($msg, E_USER_NOTICE);
399 } 399 }
400 400
401 $this->setHistoryFile($files[0]); 401 $this->setHistoryFile($files[0]);
402 } else { 402 } else {
403 // fallback: create our own history file 403 // fallback: create our own history file
460 * 460 *
461 * @return string Temporary file name 461 * @return string Temporary file name
462 */ 462 */
463 public function getTempFile($type, $pid) 463 public function getTempFile($type, $pid)
464 { 464 {
465 return tempnam($this->getRuntimeDir(), $type . '_' . $pid . '_'); 465 return \tempnam($this->getRuntimeDir(), $type . '_' . $pid . '_');
466 } 466 }
467 467
468 /** 468 /**
469 * Get a filename suitable for a FIFO pipe of $type for process $pid. 469 * Get a filename suitable for a FIFO pipe of $type for process $pid.
470 * 470 *
475 * 475 *
476 * @return string Pipe name 476 * @return string Pipe name
477 */ 477 */
478 public function getPipe($type, $pid) 478 public function getPipe($type, $pid)
479 { 479 {
480 return sprintf('%s/%s_%s', $this->getRuntimeDir(), $type, $pid); 480 return \sprintf('%s/%s_%s', $this->getRuntimeDir(), $type, $pid);
481 } 481 }
482 482
483 /** 483 /**
484 * Check whether this PHP instance has Readline available. 484 * Check whether this PHP instance has Readline available.
485 * 485 *
859 * 859 *
860 * @param string|OutputPager $pager 860 * @param string|OutputPager $pager
861 */ 861 */
862 public function setPager($pager) 862 public function setPager($pager)
863 { 863 {
864 if ($pager && !is_string($pager) && !$pager instanceof OutputPager) { 864 if ($pager && !\is_string($pager) && !$pager instanceof OutputPager) {
865 throw new \InvalidArgumentException('Unexpected pager instance'); 865 throw new \InvalidArgumentException('Unexpected pager instance');
866 } 866 }
867 867
868 $this->pager = $pager; 868 $this->pager = $pager;
869 } 869 }
877 * @return string|OutputPager 877 * @return string|OutputPager
878 */ 878 */
879 public function getPager() 879 public function getPager()
880 { 880 {
881 if (!isset($this->pager) && $this->usePcntl()) { 881 if (!isset($this->pager) && $this->usePcntl()) {
882 if ($pager = ini_get('cli.pager')) { 882 if ($pager = \ini_get('cli.pager')) {
883 // use the default pager 883 // use the default pager
884 $this->pager = $pager; 884 $this->pager = $pager;
885 } elseif ($less = exec('which less 2>/dev/null')) { 885 } elseif ($less = \exec('which less 2>/dev/null')) {
886 // check for the presence of less... 886 // check for the presence of less...
887 $this->pager = $less . ' -R -S -F -X'; 887 $this->pager = $less . ' -R -S -F -X';
888 } 888 }
889 } 889 }
890 890
935 * 935 *
936 * @param array $matchers 936 * @param array $matchers
937 */ 937 */
938 public function addMatchers(array $matchers) 938 public function addMatchers(array $matchers)
939 { 939 {
940 $this->newMatchers = array_merge($this->newMatchers, $matchers); 940 $this->newMatchers = \array_merge($this->newMatchers, $matchers);
941 if (isset($this->shell)) { 941 if (isset($this->shell)) {
942 $this->doAddMatchers(); 942 $this->doAddMatchers();
943 } 943 }
944 } 944 }
945 945
975 * 975 *
976 * @param array $commands 976 * @param array $commands
977 */ 977 */
978 public function addCommands(array $commands) 978 public function addCommands(array $commands)
979 { 979 {
980 $this->newCommands = array_merge($this->newCommands, $commands); 980 $this->newCommands = \array_merge($this->newCommands, $commands);
981 if (isset($this->shell)) { 981 if (isset($this->shell)) {
982 $this->doAddCommands(); 982 $this->doAddCommands();
983 } 983 }
984 } 984 }
985 985
1031 return $this->manualDbFile; 1031 return $this->manualDbFile;
1032 } 1032 }
1033 1033
1034 $files = ConfigPaths::getDataFiles(['php_manual.sqlite'], $this->dataDir); 1034 $files = ConfigPaths::getDataFiles(['php_manual.sqlite'], $this->dataDir);
1035 if (!empty($files)) { 1035 if (!empty($files)) {
1036 if ($this->warnOnMultipleConfigs && count($files) > 1) { 1036 if ($this->warnOnMultipleConfigs && \count($files) > 1) {
1037 $msg = sprintf('Multiple manual database files found: %s. Using %s', implode($files, ', '), $files[0]); 1037 $msg = \sprintf('Multiple manual database files found: %s. Using %s', \implode($files, ', '), $files[0]);
1038 trigger_error($msg, E_USER_NOTICE); 1038 \trigger_error($msg, E_USER_NOTICE);
1039 } 1039 }
1040 1040
1041 return $this->manualDbFile = $files[0]; 1041 return $this->manualDbFile = $files[0];
1042 } 1042 }
1043 } 1043 }
1049 */ 1049 */
1050 public function getManualDb() 1050 public function getManualDb()
1051 { 1051 {
1052 if (!isset($this->manualDb)) { 1052 if (!isset($this->manualDb)) {
1053 $dbFile = $this->getManualDbFile(); 1053 $dbFile = $this->getManualDbFile();
1054 if (is_file($dbFile)) { 1054 if (\is_file($dbFile)) {
1055 try { 1055 try {
1056 $this->manualDb = new \PDO('sqlite:' . $dbFile); 1056 $this->manualDb = new \PDO('sqlite:' . $dbFile);
1057 } catch (\PDOException $e) { 1057 } catch (\PDOException $e) {
1058 if ($e->getMessage() === 'could not find driver') { 1058 if ($e->getMessage() === 'could not find driver') {
1059 throw new RuntimeException('SQLite PDO driver not found', 0, $e); 1059 throw new RuntimeException('SQLite PDO driver not found', 0, $e);
1131 self::COLOR_MODE_AUTO, 1131 self::COLOR_MODE_AUTO,
1132 self::COLOR_MODE_FORCED, 1132 self::COLOR_MODE_FORCED,
1133 self::COLOR_MODE_DISABLED, 1133 self::COLOR_MODE_DISABLED,
1134 ]; 1134 ];
1135 1135
1136 if (in_array($colorMode, $validColorModes)) { 1136 if (\in_array($colorMode, $validColorModes)) {
1137 $this->colorMode = $colorMode; 1137 $this->colorMode = $colorMode;
1138 } else { 1138 } else {
1139 throw new \InvalidArgumentException('invalid color mode: ' . $colorMode); 1139 throw new \InvalidArgumentException('invalid color mode: ' . $colorMode);
1140 } 1140 }
1141 } 1141 }
1224 Checker::WEEKLY, 1224 Checker::WEEKLY,
1225 Checker::MONTHLY, 1225 Checker::MONTHLY,
1226 Checker::NEVER, 1226 Checker::NEVER,
1227 ]; 1227 ];
1228 1228
1229 if (!in_array($interval, $validIntervals)) { 1229 if (!\in_array($interval, $validIntervals)) {
1230 throw new \InvalidArgumentException('invalid update check interval: ' . $interval); 1230 throw new \InvalidArgumentException('invalid update check interval: ' . $interval);
1231 } 1231 }
1232 1232
1233 $this->updateCheck = $interval; 1233 $this->updateCheck = $interval;
1234 } 1234 }