/*
* This file is part of Psy Shell.
*
- * (c) 2012-2017 Justin Hileman
+ * (c) 2012-2018 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
use Psy\Exception\DeprecatedException;
use Psy\Exception\RuntimeException;
-use Psy\ExecutionLoop\ForkingLoop;
-use Psy\ExecutionLoop\Loop;
use Psy\Output\OutputPager;
use Psy\Output\ShellOutput;
use Psy\Readline\GNUReadline;
use Psy\VersionUpdater\GitHubChecker;
use Psy\VersionUpdater\IntervalChecker;
use Psy\VersionUpdater\NoopChecker;
-use XdgBaseDir\Xdg;
/**
* The Psy Shell configuration.
const COLOR_MODE_FORCED = 'forced';
const COLOR_MODE_DISABLED = 'disabled';
- private static $AVAILABLE_OPTIONS = array(
+ private static $AVAILABLE_OPTIONS = [
'codeCleaner',
'colorMode',
'configDir',
'errorLoggingLevel',
'forceArrayIndexes',
'historySize',
- 'loop',
'manualDbFile',
'pager',
'prompt',
'requireSemicolons',
'runtimeDir',
'startupMessage',
- 'tabCompletion',
'updateCheck',
'useBracketedPaste',
'usePcntl',
'useReadline',
+ 'useTabCompletion',
'useUnicode',
'warnOnMultipleConfigs',
- );
+ ];
private $defaultIncludes;
private $configDir;
private $useBracketedPaste;
private $hasPcntl;
private $usePcntl;
- private $newCommands = array();
+ private $newCommands = [];
private $requireSemicolons = false;
private $useUnicode;
- private $tabCompletion;
- private $tabCompletionMatchers = array();
- private $errorLoggingLevel = E_ALL;
+ private $useTabCompletion;
+ private $newMatchers = [];
+ private $errorLoggingLevel = E_ALL;
private $warnOnMultipleConfigs = false;
private $colorMode;
private $updateCheck;
private $shell;
private $cleaner;
private $pager;
- private $loop;
private $manualDb;
private $presenter;
- private $completer;
+ private $autoCompleter;
private $checker;
private $prompt;
*
* @param array $config Optional array of configuration values
*/
- public function __construct(array $config = array())
+ public function __construct(array $config = [])
{
$this->setColorMode(self::COLOR_MODE_AUTO);
// legacy baseDir option
if (isset($config['baseDir'])) {
- $msg = "The 'baseDir' configuration option is deprecated. " .
- "Please specify 'configDir' and 'dataDir' options instead.";
+ $msg = "The 'baseDir' configuration option is deprecated; " .
+ "please specify 'configDir' and 'dataDir' options instead";
throw new DeprecatedException($msg);
}
return $this->configFile;
}
- $files = ConfigPaths::getConfigFiles(array('config.php', 'rc.php'), $this->configDir);
+ $files = ConfigPaths::getConfigFiles(['config.php', 'rc.php'], $this->configDir);
if (!empty($files)) {
if ($this->warnOnMultipleConfigs && count($files) > 1) {
}
}
- foreach (array('commands', 'tabCompletionMatchers', 'casters') as $option) {
+ // legacy `tabCompletion` option
+ if (isset($options['tabCompletion'])) {
+ $msg = '`tabCompletion` is deprecated; use `useTabCompletion` instead.';
+ @trigger_error($msg, E_USER_DEPRECATED);
+
+ $this->setUseTabCompletion($options['tabCompletion']);
+ }
+
+ foreach (['commands', 'matchers', 'casters'] as $option) {
if (isset($options[$option])) {
$method = 'add' . ucfirst($option);
$this->$method($options[$option]);
}
}
+
+ // legacy `tabCompletionMatchers` option
+ if (isset($options['tabCompletionMatchers'])) {
+ $msg = '`tabCompletionMatchers` is deprecated; use `matchers` instead.';
+ @trigger_error($msg, E_USER_DEPRECATED);
+
+ $this->addMatchers($options['tabCompletionMatchers']);
+ }
}
/**
*
* @param array $includes
*/
- public function setDefaultIncludes(array $includes = array())
+ public function setDefaultIncludes(array $includes = [])
{
$this->defaultIncludes = $includes;
}
*/
public function getDefaultIncludes()
{
- return $this->defaultIncludes ?: array();
+ return $this->defaultIncludes ?: [];
}
/**
return $this->historyFile;
}
- // Deprecation warning for incorrect psysh_history path.
- // @todo remove this before v0.9.0
- $xdg = new Xdg();
- $oldHistory = $xdg->getHomeConfigDir() . '/psysh_history';
- if (@is_file($oldHistory)) {
- $dir = $this->configDir ?: ConfigPaths::getCurrentConfigDir();
- $newHistory = $dir . '/psysh_history';
-
- $msg = sprintf(
- "PsySH history file found at '%s'. Please delete it or move it to '%s'.",
- strtr($oldHistory, '\\', '/'),
- $newHistory
- );
- @trigger_error($msg, E_USER_DEPRECATED);
- $this->setHistoryFile($oldHistory);
-
- return $this->historyFile;
- }
-
- $files = ConfigPaths::getConfigFiles(array('psysh_history', 'history'), $this->configDir);
+ $files = ConfigPaths::getConfigFiles(['psysh_history', 'history'], $this->configDir);
if (!empty($files)) {
if ($this->warnOnMultipleConfigs && count($files) > 1) {
/**
* Enable or disable tab completion.
*
- * @param bool $tabCompletion
+ * @param bool $useTabCompletion
*/
- public function setTabCompletion($tabCompletion)
+ public function setUseTabCompletion($useTabCompletion)
{
- $this->tabCompletion = (bool) $tabCompletion;
+ $this->useTabCompletion = (bool) $useTabCompletion;
+ }
+
+ /**
+ * @deprecated Call `setUseTabCompletion` instead
+ *
+ * @param bool $useTabCompletion
+ */
+ public function setTabCompletion($useTabCompletion)
+ {
+ $this->setUseTabCompletion($useTabCompletion);
}
/**
* Check whether to use tab completion.
*
- * If `setTabCompletion` has been set to true, but readline is not actually
- * available, this will return false.
+ * If `setUseTabCompletion` has been set to true, but readline is not
+ * actually available, this will return false.
*
* @return bool True if the current Shell should use tab completion
*/
+ public function useTabCompletion()
+ {
+ return isset($this->useTabCompletion) ? ($this->hasReadline && $this->useTabCompletion) : $this->hasReadline;
+ }
+
+ /**
+ * @deprecated Call `useTabCompletion` instead
+ *
+ * @return bool
+ */
public function getTabCompletion()
{
- return isset($this->tabCompletion) ? ($this->hasReadline && $this->tabCompletion) : $this->hasReadline;
+ return $this->useTabCompletion();
}
/**
public function setPager($pager)
{
if ($pager && !is_string($pager) && !$pager instanceof OutputPager) {
- throw new \InvalidArgumentException('Unexpected pager instance.');
+ throw new \InvalidArgumentException('Unexpected pager instance');
}
$this->pager = $pager;
}
/**
- * Set the Shell evaluation Loop service.
+ * Set the Shell AutoCompleter service.
*
- * @param Loop $loop
+ * @param AutoCompleter $autoCompleter
*/
- public function setLoop(Loop $loop)
+ public function setAutoCompleter(AutoCompleter $autoCompleter)
{
- $this->loop = $loop;
+ $this->autoCompleter = $autoCompleter;
}
/**
- * Get a Shell evaluation Loop service instance.
- *
- * If none has been explicitly defined, this will create a new instance.
- * If Pcntl is available and enabled, the new instance will be a ForkingLoop.
+ * Get an AutoCompleter service instance.
*
- * @return Loop
+ * @return AutoCompleter
*/
- public function getLoop()
+ public function getAutoCompleter()
{
- if (!isset($this->loop)) {
- if ($this->usePcntl()) {
- $this->loop = new ForkingLoop($this);
- } else {
- $this->loop = new Loop($this);
- }
+ if (!isset($this->autoCompleter)) {
+ $this->autoCompleter = new AutoCompleter();
}
- return $this->loop;
+ return $this->autoCompleter;
}
/**
- * Set the Shell autocompleter service.
+ * @deprecated Nothing should be using this anymore
*
- * @param AutoCompleter $completer
+ * @return array
*/
- public function setAutoCompleter(AutoCompleter $completer)
+ public function getTabCompletionMatchers()
{
- $this->completer = $completer;
+ return [];
}
/**
- * Get an AutoCompleter service instance.
+ * Add tab completion matchers to the AutoCompleter.
*
- * @return AutoCompleter
+ * This will buffer new matchers in the event that the Shell has not yet
+ * been instantiated. This allows the user to specify matchers in their
+ * config rc file, despite the fact that their file is needed in the Shell
+ * constructor.
+ *
+ * @param array $matchers
*/
- public function getAutoCompleter()
+ public function addMatchers(array $matchers)
{
- if (!isset($this->completer)) {
- $this->completer = new AutoCompleter();
+ $this->newMatchers = array_merge($this->newMatchers, $matchers);
+ if (isset($this->shell)) {
+ $this->doAddMatchers();
}
-
- return $this->completer;
}
/**
- * Get user specified tab completion matchers for the AutoCompleter.
- *
- * @return array
+ * Internal method for adding tab completion matchers. This will set any new
+ * matchers once a Shell is available.
*/
- public function getTabCompletionMatchers()
+ private function doAddMatchers()
{
- return $this->tabCompletionMatchers;
+ if (!empty($this->newMatchers)) {
+ $this->shell->addMatchers($this->newMatchers);
+ $this->newMatchers = [];
+ }
}
/**
- * Add additional tab completion matchers to the AutoCompleter.
+ * @deprecated Use `addMatchers` instead
*
* @param array $matchers
*/
public function addTabCompletionMatchers(array $matchers)
{
- $this->tabCompletionMatchers = array_merge($this->tabCompletionMatchers, $matchers);
- if (isset($this->shell)) {
- $this->shell->addTabCompletionMatchers($this->tabCompletionMatchers);
- }
+ $this->addMatchers($matchers);
}
/**
{
if (!empty($this->newCommands)) {
$this->shell->addCommands($this->newCommands);
- $this->newCommands = array();
+ $this->newCommands = [];
}
}
{
$this->shell = $shell;
$this->doAddCommands();
+ $this->doAddMatchers();
}
/**
return $this->manualDbFile;
}
- $files = ConfigPaths::getDataFiles(array('php_manual.sqlite'), $this->dataDir);
+ $files = ConfigPaths::getDataFiles(['php_manual.sqlite'], $this->dataDir);
if (!empty($files)) {
if ($this->warnOnMultipleConfigs && count($files) > 1) {
$msg = sprintf('Multiple manual database files found: %s. Using %s', implode($files, ', '), $files[0]);
*/
public function setColorMode($colorMode)
{
- $validColorModes = array(
+ $validColorModes = [
self::COLOR_MODE_AUTO,
self::COLOR_MODE_FORCED,
self::COLOR_MODE_DISABLED,
- );
+ ];
if (in_array($colorMode, $validColorModes)) {
$this->colorMode = $colorMode;
*/
public function setUpdateCheck($interval)
{
- $validIntervals = array(
+ $validIntervals = [
Checker::ALWAYS,
Checker::DAILY,
Checker::WEEKLY,
Checker::MONTHLY,
Checker::NEVER,
- );
+ ];
if (!in_array($interval, $validIntervals)) {
throw new \InvalidArgumentException('invalid update check interval: ' . $interval);