4 * This file is part of Psy Shell.
6 * (c) 2012-2017 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Command;
16 use Symfony\Component\Console\Input\InputArgument;
17 use Symfony\Component\Console\Input\InputInterface;
18 use Symfony\Component\Console\Input\InputOption;
19 use Symfony\Component\Console\Output\OutputInterface;
21 class EditCommand extends Command implements ContextAware
26 private $runtimeDir = '';
36 * @param string $runtimeDir The directory to use for temporary files
37 * @param string|null $name The name of the command; passing null means it must be set in configure()
39 * @throws \Symfony\Component\Console\Exception\LogicException When the command name is empty
41 public function __construct($runtimeDir, $name = null)
43 parent::__construct($name);
45 $this->runtimeDir = $runtimeDir;
48 protected function configure()
52 ->setDefinition(array(
53 new InputArgument('file', InputArgument::OPTIONAL, 'The file to open for editing. If this is not given, edits a temporary file.', null),
57 InputOption::VALUE_NONE,
58 'Execute the file content after editing. This is the default when a file name argument is not given.',
64 InputOption::VALUE_NONE,
65 'Do not execute the file content after editing. This is the default when a file name argument is given.',
69 ->setDescription('Open an external editor. Afterwards, get produced code in input buffer.')
70 ->setHelp('Set the EDITOR environment variable to something you\'d like to use.');
74 * @param InputInterface $input
75 * @param OutputInterface $output
77 * @throws \InvalidArgumentException when both exec and no-exec flags are given or if a given variable is not found in the current context
78 * @throws \UnexpectedValueException if file_get_contents on the edited file returns false instead of a string
80 protected function execute(InputInterface $input, OutputInterface $output)
82 if ($input->getOption('exec') &&
83 $input->getOption('no-exec')) {
84 throw new \InvalidArgumentException('The --exec and --no-exec flags are mutually exclusive.');
87 $filePath = $this->extractFilePath($input->getArgument('file'));
89 $execute = $this->shouldExecuteFile(
90 $input->getOption('exec'),
91 $input->getOption('no-exec'),
95 $shouldRemoveFile = false;
97 if ($filePath === null) {
98 $filePath = tempnam($this->runtimeDir, 'psysh-edit-command');
99 $shouldRemoveFile = true;
102 $editedContent = $this->editFile($filePath, $shouldRemoveFile);
105 $this->getApplication()->addInput($editedContent);
110 * @param bool $execOption
111 * @param bool $noExecOption
112 * @param string|null $filePath
116 private function shouldExecuteFile($execOption, $noExecOption, $filePath)
126 // By default, code that is edited is executed if there was no given input file path
127 return $filePath === null;
131 * @param string|null $fileArgument
133 * @return string|null The file path to edit, null if the input was null, or the value of the referenced variable
135 * @throws \InvalidArgumentException If the variable is not found in the current context
137 private function extractFilePath($fileArgument)
139 // If the file argument was a variable, get it from the context
140 if ($fileArgument !== null &&
141 strlen($fileArgument) > 0 &&
142 $fileArgument[0] === '$') {
143 $fileArgument = $this->context->get(preg_replace('/^\$/', '', $fileArgument));
146 return $fileArgument;
150 * @param string $filePath
151 * @param string $shouldRemoveFile
155 * @throws \UnexpectedValueException if file_get_contents on $filePath returns false instead of a string
157 private function editFile($filePath, $shouldRemoveFile)
159 $escapedFilePath = escapeshellarg($filePath);
162 $proc = proc_open((getenv('EDITOR') ?: 'nano') . " {$escapedFilePath}", array(STDIN, STDOUT, STDERR), $pipes);
165 $editedContent = @file_get_contents($filePath);
167 if ($shouldRemoveFile) {
171 if ($editedContent === false) {
172 throw new \UnexpectedValueException("Reading {$filePath} returned false");
175 return $editedContent;
179 * Set the Context reference.
181 * @param Context $context
183 public function setContext(Context $context)
185 $this->context = $context;