Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions src/Application/Cli/PreviewCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\ProcessBuilder;

/**
Expand Down Expand Up @@ -98,6 +99,19 @@ protected function execute(InputInterface $input, OutputInterface $output)

$serverProcess = $this->startWebServer($input, $output, $targetDirectory);

if (function_exists('pcntl_signal')) {
declare(ticks=1);

$handler = function ($signal) use ($serverProcess, $output) {
$this->stopWebServer($serverProcess, $output, $signal);
};

foreach ([SIGINT, SIGTERM] as $signal) {
pcntl_signal($signal, $handler);
}
}


// Watch for changes
while ($serverProcess->isRunning()) {
$files = $watchlist->getChangedFiles();
Expand Down Expand Up @@ -146,6 +160,23 @@ private function startWebServer(InputInterface $input, OutputInterface $output,
return $process;
}

private function stopWebServer(Process $serverProcess, OutputInterface $output, $signal = null)
{
$signal = $signal ?: SIGTERM;

if ($serverProcess->isRunning()) {
$output->writeln(sprintf('Killing server with signal <comment>%s</comment>', $signal));

$serverProcess->stop(0, $signal);
}

if ($serverProcess->isRunning()) {
$output->writeln(sprintf('Server was killed with signal <comment>%s</comment>', $signal));
} else {
$output->writeln(sprintf('Unable to kill the server with signal <comment>%s</comment>', $signal));
}
}

private function startLivereload($executablePath, OutputInterface $output, $sourceDirectory, $targetDirectory)
{
$builder = new ProcessBuilder([$executablePath, $targetDirectory, '-w', '3']);
Expand Down