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