| Conditions | 30 |
| Paths | > 20000 |
| Total Lines | 172 |
| Code Lines | 117 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 6 | ||
| Bugs | 3 | 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 |
||
| 67 | protected function execute(InputInterface $input, OutputInterface $output) |
||
| 68 | { |
||
| 69 | $drafts = $input->getOption('drafts'); |
||
| 70 | $open = $input->getOption('open'); |
||
| 71 | $host = $input->getOption('host') ?? 'localhost'; |
||
| 72 | $port = $input->getOption('port') ?? '8000'; |
||
| 73 | $optimize = $input->getOption('optimize'); |
||
| 74 | $clearcache = $input->getOption('clear-cache'); |
||
| 75 | $verbose = $input->getOption('verbose'); |
||
| 76 | $page = $input->getOption('page'); |
||
| 77 | $noignorevcs = $input->getOption('no-ignore-vcs'); |
||
| 78 | |||
| 79 | $this->setUpServer($host, $port); |
||
| 80 | |||
| 81 | $phpFinder = new PhpExecutableFinder(); |
||
| 82 | $php = $phpFinder->find(); |
||
| 83 | if ($php === false) { |
||
| 84 | throw new RuntimeException('Can\'t find a local PHP executable.'); |
||
| 85 | } |
||
| 86 | |||
| 87 | $command = \sprintf( |
||
| 88 | '"%s" -S %s:%d -t "%s" "%s"', |
||
| 89 | $php, |
||
| 90 | $host, |
||
| 91 | $port, |
||
| 92 | Util::joinFile($this->getPath(), (string) $this->getBuilder()->getConfig()->get('output.dir')), |
||
| 93 | Util::joinFile($this->getPath(), self::TMP_DIR, 'router.php') |
||
| 94 | ); |
||
| 95 | $process = Process::fromShellCommandline($command); |
||
| 96 | |||
| 97 | $buildProcessArguments = [ |
||
| 98 | $php, |
||
| 99 | $_SERVER['argv'][0], |
||
| 100 | ]; |
||
| 101 | $buildProcessArguments[] = 'build'; |
||
| 102 | $buildProcessArguments[] = $this->getPath(); |
||
| 103 | if (!empty($this->getConfigFiles())) { |
||
| 104 | $buildProcessArguments[] = '--config'; |
||
| 105 | $buildProcessArguments[] = implode(',', $this->getConfigFiles()); |
||
| 106 | } |
||
| 107 | if ($drafts) { |
||
| 108 | $buildProcessArguments[] = '--drafts'; |
||
| 109 | } |
||
| 110 | if ($optimize === null) { |
||
| 111 | $buildProcessArguments[] = '--optimize'; |
||
| 112 | } |
||
| 113 | if (!empty($optimize)) { |
||
| 114 | $buildProcessArguments[] = '--optimize'; |
||
| 115 | $buildProcessArguments[] = $optimize; |
||
| 116 | } |
||
| 117 | if ($clearcache === null) { |
||
| 118 | $buildProcessArguments[] = '--clear-cache'; |
||
| 119 | } |
||
| 120 | if (!empty($clearcache)) { |
||
| 121 | $buildProcessArguments[] = '--clear-cache'; |
||
| 122 | $buildProcessArguments[] = $clearcache; |
||
| 123 | } |
||
| 124 | if ($verbose) { |
||
| 125 | $buildProcessArguments[] = '-' . str_repeat('v', $_SERVER['SHELL_VERBOSITY']); |
||
| 126 | } |
||
| 127 | if (!empty($page)) { |
||
| 128 | $buildProcessArguments[] = '--page'; |
||
| 129 | $buildProcessArguments[] = $page; |
||
| 130 | } |
||
| 131 | |||
| 132 | $buildProcess = new Process( |
||
| 133 | $buildProcessArguments, |
||
| 134 | null, |
||
| 135 | ['BOX_REQUIREMENT_CHECKER' => '0'] // prevents double check (build then serve) |
||
| 136 | ); |
||
| 137 | |||
| 138 | $buildProcess->setTty(Process::isTtySupported()); |
||
| 139 | $buildProcess->setPty(Process::isPtySupported()); |
||
| 140 | $buildProcess->setTimeout(3600 * 2); // timeout = 2 minutes |
||
| 141 | |||
| 142 | $processOutputCallback = function ($type, $buffer) use ($output) { |
||
| 143 | $output->write($buffer, false, OutputInterface::OUTPUT_RAW); |
||
| 144 | }; |
||
| 145 | |||
| 146 | // (re)builds before serve |
||
| 147 | $output->writeln(\sprintf('<comment>Build process: %s</comment>', implode(' ', $buildProcessArguments)), OutputInterface::VERBOSITY_DEBUG); |
||
| 148 | $buildProcess->run($processOutputCallback); |
||
| 149 | if ($buildProcess->isSuccessful()) { |
||
| 150 | $this->buildSuccess($output); |
||
| 151 | } |
||
| 152 | if ($buildProcess->getExitCode() !== 0) { |
||
| 153 | return 1; |
||
| 154 | } |
||
| 155 | |||
| 156 | // handles process |
||
| 157 | if (!$process->isStarted()) { |
||
| 158 | // set resource watcher |
||
| 159 | $finder = new Finder(); |
||
| 160 | $finder->files() |
||
| 161 | ->in($this->getPath()) |
||
| 162 | ->exclude((string) $this->getBuilder()->getConfig()->get('output.dir')); |
||
| 163 | if (file_exists(Util::joinFile($this->getPath(), '.gitignore')) && $noignorevcs === false) { |
||
| 164 | $finder->ignoreVCSIgnored(true); |
||
| 165 | } |
||
| 166 | $hashContent = new Crc32ContentHash(); |
||
| 167 | $resourceCache = new ResourceCacheMemory(); |
||
| 168 | $resourceWatcher = new ResourceWatcher($resourceCache, $finder, $hashContent); |
||
| 169 | $resourceWatcher->initialize(); |
||
| 170 | |||
| 171 | // starts server |
||
| 172 | try { |
||
| 173 | if (\function_exists('\pcntl_signal')) { |
||
| 174 | pcntl_async_signals(true); |
||
| 175 | pcntl_signal(SIGINT, [$this, 'tearDownServer']); |
||
| 176 | pcntl_signal(SIGTERM, [$this, 'tearDownServer']); |
||
| 177 | } |
||
| 178 | $output->writeln(\sprintf('<comment>Server process: %s</comment>', $command), OutputInterface::VERBOSITY_DEBUG); |
||
| 179 | $output->writeln(\sprintf('Starting server (<href=http://%s:%d>http://%s:%d</>)...', $host, $port, $host, $port)); |
||
| 180 | $process->start(function ($type, $buffer) { |
||
| 181 | if ($type === Process::ERR) { |
||
| 182 | error_log($buffer, 3, Util::joinFile($this->getPath(), self::TMP_DIR, 'errors.log')); |
||
| 183 | } |
||
| 184 | }); |
||
| 185 | if ($open) { |
||
| 186 | $output->writeln('Opening web browser...'); |
||
| 187 | Util\Plateform::openBrowser(\sprintf('http://%s:%s', $host, $port)); |
||
| 188 | } |
||
| 189 | while ($process->isRunning()) { |
||
| 190 | sleep(1); // wait for server is ready |
||
| 191 | if (!fsockopen($host, (int) $port)) { |
||
| 192 | $output->writeln('<info>Server is not ready.</info>'); |
||
| 193 | |||
| 194 | return 1; |
||
| 195 | } |
||
| 196 | $watcher = $resourceWatcher->findChanges(); |
||
| 197 | if ($watcher->hasChanges()) { |
||
| 198 | // prints deleted/new/updated files in debug mode |
||
| 199 | $output->writeln('<comment>Changes detected.</comment>'); |
||
| 200 | if (\count($watcher->getDeletedFiles()) > 0) { |
||
| 201 | $output->writeln('<comment>Deleted files:</comment>', OutputInterface::VERBOSITY_DEBUG); |
||
| 202 | foreach ($watcher->getDeletedFiles() as $file) { |
||
| 203 | $output->writeln("<comment>- $file</comment>", OutputInterface::VERBOSITY_DEBUG); |
||
| 204 | } |
||
| 205 | } |
||
| 206 | if (\count($watcher->getNewFiles()) > 0) { |
||
| 207 | $output->writeln('<comment>New files:</comment>', OutputInterface::VERBOSITY_DEBUG); |
||
| 208 | foreach ($watcher->getNewFiles() as $file) { |
||
| 209 | $output->writeln("<comment>- $file</comment>", OutputInterface::VERBOSITY_DEBUG); |
||
| 210 | } |
||
| 211 | } |
||
| 212 | if (\count($watcher->getUpdatedFiles()) > 0) { |
||
| 213 | $output->writeln('<comment>Updated files:</comment>', OutputInterface::VERBOSITY_DEBUG); |
||
| 214 | foreach ($watcher->getUpdatedFiles() as $file) { |
||
| 215 | $output->writeln("<comment>- $file</comment>", OutputInterface::VERBOSITY_DEBUG); |
||
| 216 | } |
||
| 217 | } |
||
| 218 | $output->writeln(''); |
||
| 219 | // re-builds |
||
| 220 | $buildProcess->run($processOutputCallback); |
||
| 221 | if ($buildProcess->isSuccessful()) { |
||
| 222 | $this->buildSuccess($output); |
||
| 223 | } |
||
| 224 | |||
| 225 | $output->writeln('<info>Server is runnning...</info>'); |
||
| 226 | } |
||
| 227 | } |
||
| 228 | if ($process->getExitCode() > 0) { |
||
| 229 | $output->writeln(\sprintf('<comment>%s</comment>', trim($process->getErrorOutput()))); |
||
| 230 | } |
||
| 231 | } catch (ProcessFailedException $e) { |
||
| 232 | $this->tearDownServer(); |
||
| 233 | |||
| 234 | throw new RuntimeException(\sprintf($e->getMessage())); |
||
| 235 | } |
||
| 236 | } |
||
| 237 | |||
| 238 | return 0; |
||
| 239 | } |
||
| 318 |