Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.x] Add graceful handler for SIGINT for .env restoration #682

Merged
merged 6 commits into from
Oct 18, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"php": ">=7.1.0",
"ext-json": "*",
"ext-zip": "*",
"ext-pcntl": "*",
"facebook/webdriver": "^1.7",
"nesbot/carbon": "^1.20|^2.0",
"illuminate/console": "~5.7.0|~5.8.0|^6.0|^7.0",
Expand Down
65 changes: 55 additions & 10 deletions src/Console/DuskCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Illuminate\Console\Command;
use Illuminate\Support\Str;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Process\Exception\ProcessSignaledException;
use Symfony\Component\Process\Exception\RuntimeException;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -68,9 +69,15 @@ public function handle()
$this->output->writeln('Warning: '.$e->getMessage());
}

return $process->run(function ($type, $line) {
$this->output->write($line);
});
try {
return $process->run(function ($type, $line) {
$this->output->write($line);
});
} catch (ProcessSignaledException $e) {
if ($e->getSignal() !== SIGINT) {
throw $e;
}
}
});
}

Expand Down Expand Up @@ -158,6 +165,22 @@ protected function purgeConsoleLogs()
* @return mixed
*/
protected function withDuskEnvironment($callback)
{
$this->setupDuskEnvironment();

try {
$callback();
} finally {
$this->teardownDuskEnviroment();
}
}

/**
* Restore the original environment.
*
* @return void
*/
protected function setupDuskEnvironment()
{
if (file_exists(base_path($this->duskFile()))) {
if (file_get_contents(base_path('.env')) !== file_get_contents(base_path($this->duskFile()))) {
Expand All @@ -169,15 +192,37 @@ protected function withDuskEnvironment($callback)

$this->writeConfiguration();

return tap($callback(), function () {
$this->removeConfiguration();
$this->setupSignalHandler();
}

if (file_exists(base_path($this->duskFile())) && file_exists(base_path('.env.backup'))) {
$this->restoreEnvironment();
}
/**
* Setup the SIGINT signal handler for CTRL+C exits.
*
* @return void
*/
protected function setupSignalHandler()
{
declare(ticks=1);

pcntl_signal(SIGINT, function () {
driesvints marked this conversation as resolved.
Show resolved Hide resolved
$this->teardownDuskEnviroment();
});
}

/**
* Setup the dusk environment.
*
* @return void
*/
protected function teardownDuskEnviroment()
{
$this->removeConfiguration();

if (file_exists(base_path($this->duskFile())) && file_exists(base_path('.env.backup'))) {
$this->restoreEnvironment();
}
}

/**
* Backup the current environment file.
*
Expand Down Expand Up @@ -243,8 +288,8 @@ protected function writeConfiguration()
*/
protected function removeConfiguration()
{
if (! $this->hasPhpUnitConfiguration) {
unlink(base_path('phpunit.dusk.xml'));
if (! $this->hasPhpUnitConfiguration && file_exists($file = base_path('phpunit.dusk.xml'))) {
unlink($file);
}
}

Expand Down