From 86ca7be1cb27f54fe4295655412404a62473c1e4 Mon Sep 17 00:00:00 2001 From: Maksim Kuznetsov Date: Sat, 25 Feb 2017 09:47:22 +0300 Subject: [PATCH] Fix some issues from SensioLabsInsight --- src/Console/InitCommand.php | 1 - src/Util/Reporter.php | 65 ++++++++++++++++++++----------------- 2 files changed, 36 insertions(+), 30 deletions(-) diff --git a/src/Console/InitCommand.php b/src/Console/InitCommand.php index e2a14159..6374de26 100644 --- a/src/Console/InitCommand.php +++ b/src/Console/InitCommand.php @@ -17,7 +17,6 @@ use Deployer\Initializer\Template\SymfonyTemplate; use Deployer\Initializer\Template\YiiTemplate; use Deployer\Initializer\Template\ZendTemplate; use Symfony\Component\Console\Command\Command; -use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; diff --git a/src/Util/Reporter.php b/src/Util/Reporter.php index 3a180a17..52c8f3ca 100644 --- a/src/Util/Reporter.php +++ b/src/Util/Reporter.php @@ -14,40 +14,47 @@ class Reporter /** * @param array $stats */ - public static function report($stats) + public static function report(array $stats) { - $send = function () use ($stats) { - if (extension_loaded('curl')) { - $body = json_encode($stats, JSON_PRETTY_PRINT); - $ch = curl_init(self::ENDPOINT); - curl_setopt($ch, CURLOPT_HTTPHEADER, [ - 'Content-Type: application/json', - 'Content-Length: ' . strlen($body) - ]); - curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); - curl_setopt($ch, CURLOPT_POSTFIELDS, $body); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); - curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); - curl_setopt($ch, CURLOPT_MAXREDIRS, 10); - curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); - curl_setopt($ch, CURLOPT_TIMEOUT, 5); - curl_exec($ch); - } else { - file_get_contents(self::ENDPOINT . '?' . http_build_query($stats)); - } - }; - - + $pid = null; if (extension_loaded('pcntl')) { declare(ticks = 1); $pid = pcntl_fork(); - if ($pid === 0) { - posix_setsid(); - $send(); - exit(0); - } + } + + if (is_null($pid) || $pid === -1) { + // Fork fails or there is no `pcntl` extension. + self::send($stats); + } elseif ($pid === 0) { + // Child process. + self::send($stats); + // Close child process after doing job. + exit(0); + } + } + + /** + * @param array $stats + */ + private static function send(array $stats) + { + if (extension_loaded('curl')) { + $body = json_encode($stats, JSON_PRETTY_PRINT); + $ch = curl_init(self::ENDPOINT); + curl_setopt($ch, CURLOPT_HTTPHEADER, [ + 'Content-Type: application/json', + 'Content-Length: ' . strlen($body) + ]); + curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET'); + curl_setopt($ch, CURLOPT_POSTFIELDS, $body); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); + curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); + curl_setopt($ch, CURLOPT_MAXREDIRS, 10); + curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); + curl_setopt($ch, CURLOPT_TIMEOUT, 5); + curl_exec($ch); } else { - $send(); + file_get_contents(self::ENDPOINT . '?' . http_build_query($stats)); } } }