annotate vendor/psy/psysh/src/Psy/Command/EditCommand.php @ 12:7a779792577d

Update Drupal core to v8.4.5 (via Composer)
author Chris Cannam
date Fri, 23 Feb 2018 15:52:07 +0000
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of Psy Shell.
Chris@0 5 *
Chris@0 6 * (c) 2012-2017 Justin Hileman
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Psy\Command;
Chris@0 13
Chris@0 14 use Psy\Context;
Chris@0 15 use Psy\ContextAware;
Chris@0 16 use Symfony\Component\Console\Input\InputArgument;
Chris@0 17 use Symfony\Component\Console\Input\InputInterface;
Chris@0 18 use Symfony\Component\Console\Input\InputOption;
Chris@0 19 use Symfony\Component\Console\Output\OutputInterface;
Chris@0 20
Chris@0 21 class EditCommand extends Command implements ContextAware
Chris@0 22 {
Chris@0 23 /**
Chris@0 24 * @var string
Chris@0 25 */
Chris@0 26 private $runtimeDir = '';
Chris@0 27
Chris@0 28 /**
Chris@0 29 * @var Context
Chris@0 30 */
Chris@0 31 private $context;
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Constructor.
Chris@0 35 *
Chris@0 36 * @param string $runtimeDir The directory to use for temporary files
Chris@0 37 * @param string|null $name The name of the command; passing null means it must be set in configure()
Chris@0 38 *
Chris@0 39 * @throws \Symfony\Component\Console\Exception\LogicException When the command name is empty
Chris@0 40 */
Chris@0 41 public function __construct($runtimeDir, $name = null)
Chris@0 42 {
Chris@0 43 parent::__construct($name);
Chris@0 44
Chris@0 45 $this->runtimeDir = $runtimeDir;
Chris@0 46 }
Chris@0 47
Chris@0 48 protected function configure()
Chris@0 49 {
Chris@0 50 $this
Chris@0 51 ->setName('edit')
Chris@0 52 ->setDefinition(array(
Chris@0 53 new InputArgument('file', InputArgument::OPTIONAL, 'The file to open for editing. If this is not given, edits a temporary file.', null),
Chris@0 54 new InputOption(
Chris@0 55 'exec',
Chris@0 56 'e',
Chris@0 57 InputOption::VALUE_NONE,
Chris@0 58 'Execute the file content after editing. This is the default when a file name argument is not given.',
Chris@0 59 null
Chris@0 60 ),
Chris@0 61 new InputOption(
Chris@0 62 'no-exec',
Chris@0 63 'E',
Chris@0 64 InputOption::VALUE_NONE,
Chris@0 65 'Do not execute the file content after editing. This is the default when a file name argument is given.',
Chris@0 66 null
Chris@0 67 ),
Chris@0 68 ))
Chris@0 69 ->setDescription('Open an external editor. Afterwards, get produced code in input buffer.')
Chris@0 70 ->setHelp('Set the EDITOR environment variable to something you\'d like to use.');
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * @param InputInterface $input
Chris@0 75 * @param OutputInterface $output
Chris@0 76 *
Chris@0 77 * @throws \InvalidArgumentException when both exec and no-exec flags are given or if a given variable is not found in the current context
Chris@0 78 * @throws \UnexpectedValueException if file_get_contents on the edited file returns false instead of a string
Chris@0 79 */
Chris@0 80 protected function execute(InputInterface $input, OutputInterface $output)
Chris@0 81 {
Chris@0 82 if ($input->getOption('exec') &&
Chris@0 83 $input->getOption('no-exec')) {
Chris@0 84 throw new \InvalidArgumentException('The --exec and --no-exec flags are mutually exclusive.');
Chris@0 85 }
Chris@0 86
Chris@0 87 $filePath = $this->extractFilePath($input->getArgument('file'));
Chris@0 88
Chris@0 89 $execute = $this->shouldExecuteFile(
Chris@0 90 $input->getOption('exec'),
Chris@0 91 $input->getOption('no-exec'),
Chris@0 92 $filePath
Chris@0 93 );
Chris@0 94
Chris@0 95 $shouldRemoveFile = false;
Chris@0 96
Chris@0 97 if ($filePath === null) {
Chris@0 98 $filePath = tempnam($this->runtimeDir, 'psysh-edit-command');
Chris@0 99 $shouldRemoveFile = true;
Chris@0 100 }
Chris@0 101
Chris@0 102 $editedContent = $this->editFile($filePath, $shouldRemoveFile);
Chris@0 103
Chris@0 104 if ($execute) {
Chris@0 105 $this->getApplication()->addInput($editedContent);
Chris@0 106 }
Chris@0 107 }
Chris@0 108
Chris@0 109 /**
Chris@0 110 * @param bool $execOption
Chris@0 111 * @param bool $noExecOption
Chris@0 112 * @param string|null $filePath
Chris@0 113 *
Chris@0 114 * @return bool
Chris@0 115 */
Chris@0 116 private function shouldExecuteFile($execOption, $noExecOption, $filePath)
Chris@0 117 {
Chris@0 118 if ($execOption) {
Chris@0 119 return true;
Chris@0 120 }
Chris@0 121
Chris@0 122 if ($noExecOption) {
Chris@0 123 return false;
Chris@0 124 }
Chris@0 125
Chris@0 126 // By default, code that is edited is executed if there was no given input file path
Chris@0 127 return $filePath === null;
Chris@0 128 }
Chris@0 129
Chris@0 130 /**
Chris@0 131 * @param string|null $fileArgument
Chris@0 132 *
Chris@0 133 * @return string|null The file path to edit, null if the input was null, or the value of the referenced variable
Chris@0 134 *
Chris@0 135 * @throws \InvalidArgumentException If the variable is not found in the current context
Chris@0 136 */
Chris@0 137 private function extractFilePath($fileArgument)
Chris@0 138 {
Chris@0 139 // If the file argument was a variable, get it from the context
Chris@0 140 if ($fileArgument !== null &&
Chris@0 141 strlen($fileArgument) > 0 &&
Chris@0 142 $fileArgument[0] === '$') {
Chris@0 143 $fileArgument = $this->context->get(preg_replace('/^\$/', '', $fileArgument));
Chris@0 144 }
Chris@0 145
Chris@0 146 return $fileArgument;
Chris@0 147 }
Chris@0 148
Chris@0 149 /**
Chris@0 150 * @param string $filePath
Chris@0 151 * @param string $shouldRemoveFile
Chris@0 152 *
Chris@0 153 * @return string
Chris@0 154 *
Chris@0 155 * @throws \UnexpectedValueException if file_get_contents on $filePath returns false instead of a string
Chris@0 156 */
Chris@0 157 private function editFile($filePath, $shouldRemoveFile)
Chris@0 158 {
Chris@0 159 $escapedFilePath = escapeshellarg($filePath);
Chris@0 160
Chris@0 161 $pipes = array();
Chris@0 162 $proc = proc_open((getenv('EDITOR') ?: 'nano') . " {$escapedFilePath}", array(STDIN, STDOUT, STDERR), $pipes);
Chris@0 163 proc_close($proc);
Chris@0 164
Chris@0 165 $editedContent = @file_get_contents($filePath);
Chris@0 166
Chris@0 167 if ($shouldRemoveFile) {
Chris@0 168 @unlink($filePath);
Chris@0 169 }
Chris@0 170
Chris@0 171 if ($editedContent === false) {
Chris@0 172 throw new \UnexpectedValueException("Reading {$filePath} returned false");
Chris@0 173 }
Chris@0 174
Chris@0 175 return $editedContent;
Chris@0 176 }
Chris@0 177
Chris@0 178 /**
Chris@0 179 * Set the Context reference.
Chris@0 180 *
Chris@0 181 * @param Context $context
Chris@0 182 */
Chris@0 183 public function setContext(Context $context)
Chris@0 184 {
Chris@0 185 $this->context = $context;
Chris@0 186 }
Chris@0 187 }