Conditions | 32 |
Paths | > 20000 |
Total Lines | 177 |
Code Lines | 122 |
Lines | 0 |
Ratio | 0 % |
Changes | 6 | ||
Bugs | 2 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
101 | protected function execute(InputInterface $input, OutputInterface $output) |
||
102 | { |
||
103 | $open = $input->getOption('open'); |
||
104 | $host = $input->getOption('host'); |
||
105 | $port = $input->getOption('port'); |
||
106 | $drafts = $input->getOption('drafts'); |
||
107 | $optimize = $input->getOption('optimize'); |
||
108 | $clearcache = $input->getOption('clear-cache'); |
||
109 | $page = $input->getOption('page'); |
||
110 | $noignorevcs = $input->getOption('no-ignore-vcs'); |
||
111 | $metrics = $input->getOption('metrics'); |
||
112 | $timeout = $input->getOption('timeout'); |
||
113 | $verbose = $input->getOption('verbose'); |
||
114 | |||
115 | $resourceWatcher = null; |
||
116 | $this->watcherEnabled = $input->getOption('watch'); |
||
117 | |||
118 | // checks if PHP executable is available |
||
119 | $phpFinder = new PhpExecutableFinder(); |
||
120 | $php = $phpFinder->find(); |
||
121 | if ($php === false) { |
||
122 | throw new RuntimeException('Can\'t find a local PHP executable.'); |
||
123 | } |
||
124 | |||
125 | // setup server |
||
126 | $this->setUpServer(); |
||
127 | $command = \sprintf( |
||
128 | '"%s" -S %s:%d -t "%s" "%s"', |
||
129 | $php, |
||
130 | $host, |
||
131 | $port, |
||
132 | Util::joinFile($this->getPath(), self::SERVE_OUTPUT), |
||
133 | Util::joinFile($this->getPath(), self::TMP_DIR, 'router.php') |
||
134 | ); |
||
135 | $process = Process::fromShellCommandline($command); |
||
136 | |||
137 | // setup build process |
||
138 | $buildProcessArguments = [ |
||
139 | $php, |
||
140 | $_SERVER['argv'][0], |
||
141 | ]; |
||
142 | $buildProcessArguments[] = 'build'; |
||
143 | $buildProcessArguments[] = $this->getPath(); |
||
144 | if (!empty($this->getConfigFiles())) { |
||
145 | $buildProcessArguments[] = '--config'; |
||
146 | $buildProcessArguments[] = implode(',', $this->getConfigFiles()); |
||
147 | } |
||
148 | if ($drafts) { |
||
149 | $buildProcessArguments[] = '--drafts'; |
||
150 | } |
||
151 | if ($optimize === true) { |
||
152 | $buildProcessArguments[] = '--optimize'; |
||
153 | } |
||
154 | if ($optimize === false) { |
||
155 | $buildProcessArguments[] = '--no-optimize'; |
||
156 | } |
||
157 | if ($clearcache === null) { |
||
158 | $buildProcessArguments[] = '--clear-cache'; |
||
159 | } |
||
160 | if (!empty($clearcache)) { |
||
161 | $buildProcessArguments[] = '--clear-cache'; |
||
162 | $buildProcessArguments[] = $clearcache; |
||
163 | } |
||
164 | if ($verbose) { |
||
165 | $buildProcessArguments[] = '-' . str_repeat('v', $_SERVER['SHELL_VERBOSITY']); |
||
166 | } |
||
167 | if (!empty($page)) { |
||
168 | $buildProcessArguments[] = '--page'; |
||
169 | $buildProcessArguments[] = $page; |
||
170 | } |
||
171 | if (!empty($metrics)) { |
||
172 | $buildProcessArguments[] = '--metrics'; |
||
173 | } |
||
174 | $buildProcessArguments[] = '--baseurl'; |
||
175 | $buildProcessArguments[] = "http://$host:$port/"; |
||
176 | $buildProcessArguments[] = '--output'; |
||
177 | $buildProcessArguments[] = self::SERVE_OUTPUT; |
||
178 | $buildProcess = new Process( |
||
179 | $buildProcessArguments, |
||
180 | null, |
||
181 | ['BOX_REQUIREMENT_CHECKER' => '0'] // prevents double check (build then serve) |
||
182 | ); |
||
183 | $buildProcess->setTty(Process::isTtySupported()); |
||
184 | $buildProcess->setPty(Process::isPtySupported()); |
||
185 | $buildProcess->setTimeout((float) $timeout); |
||
186 | $processOutputCallback = function ($type, $buffer) use ($output) { |
||
187 | $output->write($buffer, false, OutputInterface::OUTPUT_RAW); |
||
188 | }; |
||
189 | |||
190 | // builds before serve |
||
191 | $output->writeln(\sprintf('<comment>Build process: %s</comment>', implode(' ', $buildProcessArguments)), OutputInterface::VERBOSITY_DEBUG); |
||
192 | $buildProcess->run($processOutputCallback); |
||
193 | if ($buildProcess->isSuccessful()) { |
||
194 | $this->buildSuccessActions($output); |
||
195 | } |
||
196 | if ($buildProcess->getExitCode() !== 0) { |
||
197 | return 1; |
||
198 | } |
||
199 | |||
200 | // handles serve process |
||
201 | if (!$process->isStarted()) { |
||
202 | $messageSuffix = ''; |
||
203 | // setup resource watcher |
||
204 | if ($this->watcherEnabled) { |
||
205 | $resourceWatcher = $this->setupWatcher($noignorevcs); |
||
206 | $resourceWatcher->initialize(); |
||
207 | $messageSuffix = ' with changes watcher'; |
||
208 | } |
||
209 | // starts server |
||
210 | try { |
||
211 | if (\function_exists('\pcntl_signal')) { |
||
212 | pcntl_async_signals(true); |
||
213 | pcntl_signal(SIGINT, [$this, 'tearDownServer']); |
||
214 | pcntl_signal(SIGTERM, [$this, 'tearDownServer']); |
||
215 | } |
||
216 | $output->writeln(\sprintf('<comment>Server process: %s</comment>', $command), OutputInterface::VERBOSITY_DEBUG); |
||
217 | $output->writeln(\sprintf('Starting server (<href=http://%s:%d>http://%s:%d</>)%s...', $host, $port, $host, $port, $messageSuffix)); |
||
218 | $process->start(function ($type, $buffer) { |
||
219 | if ($type === Process::ERR) { |
||
220 | error_log($buffer, 3, Util::joinFile($this->getPath(), self::TMP_DIR, 'errors.log')); |
||
221 | } |
||
222 | }); |
||
223 | if ($open) { |
||
224 | $output->writeln('Opening web browser...'); |
||
225 | Util\Platform::openBrowser(\sprintf('http://%s:%s', $host, $port)); |
||
226 | } |
||
227 | while ($process->isRunning()) { |
||
228 | sleep(1); // wait for server is ready |
||
229 | if (!fsockopen($host, (int) $port)) { |
||
230 | $output->writeln('<info>Server is not ready.</info>'); |
||
231 | |||
232 | return 1; |
||
233 | } |
||
234 | if ($this->watcherEnabled && $resourceWatcher instanceof ResourceWatcher) { |
||
235 | $watcher = $resourceWatcher->findChanges(); |
||
236 | if ($watcher->hasChanges()) { |
||
237 | $output->writeln('<comment>Changes detected.</comment>'); |
||
238 | // prints deleted/new/updated files in debug mode |
||
239 | if (\count($watcher->getDeletedFiles()) > 0) { |
||
240 | $output->writeln('<comment>Deleted files:</comment>', OutputInterface::VERBOSITY_DEBUG); |
||
241 | foreach ($watcher->getDeletedFiles() as $file) { |
||
242 | $output->writeln("<comment>- $file</comment>", OutputInterface::VERBOSITY_DEBUG); |
||
243 | } |
||
244 | } |
||
245 | if (\count($watcher->getNewFiles()) > 0) { |
||
246 | $output->writeln('<comment>New files:</comment>', OutputInterface::VERBOSITY_DEBUG); |
||
247 | foreach ($watcher->getNewFiles() as $file) { |
||
248 | $output->writeln("<comment>- $file</comment>", OutputInterface::VERBOSITY_DEBUG); |
||
249 | } |
||
250 | } |
||
251 | if (\count($watcher->getUpdatedFiles()) > 0) { |
||
252 | $output->writeln('<comment>Updated files:</comment>', OutputInterface::VERBOSITY_DEBUG); |
||
253 | foreach ($watcher->getUpdatedFiles() as $file) { |
||
254 | $output->writeln("<comment>- $file</comment>", OutputInterface::VERBOSITY_DEBUG); |
||
255 | } |
||
256 | } |
||
257 | $output->writeln(''); |
||
258 | // re-builds |
||
259 | $buildProcess->run($processOutputCallback); |
||
260 | if ($buildProcess->isSuccessful()) { |
||
261 | $this->buildSuccessActions($output); |
||
262 | } |
||
263 | $output->writeln('<info>Server is runnning...</info>'); |
||
264 | } |
||
265 | } |
||
266 | } |
||
267 | if ($process->getExitCode() > 0) { |
||
268 | $output->writeln(\sprintf('<comment>%s</comment>', trim($process->getErrorOutput()))); |
||
269 | } |
||
270 | } catch (ProcessFailedException $e) { |
||
271 | $this->tearDownServer(); |
||
272 | |||
273 | throw new RuntimeException(\sprintf($e->getMessage())); |
||
274 | } |
||
275 | } |
||
276 | |||
277 | return 0; |
||
278 | } |
||
371 |