Skip to content

Commit 6f479ec

Browse files
committed
IHF: Looks like refactored.
1 parent 3a152de commit 6f479ec

File tree

3 files changed

+72
-42
lines changed

3 files changed

+72
-42
lines changed

src/artisan.php

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,10 @@
11
<?php
22

3-
use Illuminated\Helpers\System\Command;
4-
use Symfony\Component\Process\PhpExecutableFinder;
5-
use Symfony\Component\Process\ProcessUtils;
3+
use Illuminated\Helpers\Artisan\Command;
64

75
if (!function_exists('call_in_background')) {
86
function call_in_background($command, $before = null, $after = null)
97
{
10-
$isInvalidCommand = empty($command) || !is_string($command);
11-
$isInvalidBefore = !empty($before) && !is_string($before);
12-
$isInvalidAfter = !empty($after) && !is_string($after);
13-
if ($isInvalidCommand || $isInvalidBefore || $isInvalidAfter) {
14-
return false;
15-
}
16-
17-
$parts = [];
18-
if (!empty($before)) {
19-
$parts[] = $before;
20-
}
21-
22-
$binary = ProcessUtils::escapeArgument((new PhpExecutableFinder)->find(false));
23-
if (defined('HHVM_VERSION')) {
24-
$binary .= ' --php';
25-
}
26-
$artisan = 'artisan';
27-
if (defined('ARTISAN_BINARY')) {
28-
$artisan = ProcessUtils::escapeArgument(ARTISAN_BINARY);
29-
}
30-
$parts[] = "{$binary} {$artisan} {$command}";
31-
32-
if (!empty($after)) {
33-
$parts[] = $after;
34-
}
35-
36-
$expression = implode(' && ', $parts);
37-
Command::exec("({$expression}) > /dev/null 2>&1 &");
8+
return (new Command($command, $before, $after))->runInBackground();
389
}
3910
}

src/classes/Artisan/Command.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Illuminated\Helpers\Artisan;
4+
5+
use Symfony\Component\Process\PhpExecutableFinder;
6+
use Symfony\Component\Process\ProcessUtils;
7+
8+
class Command
9+
{
10+
private $command;
11+
private $before;
12+
private $after;
13+
14+
public function __construct($command, $before = null, $after = null)
15+
{
16+
$this->command = $command;
17+
$this->before = $before;
18+
$this->after = $after;
19+
}
20+
21+
public function runInBackground()
22+
{
23+
exec($this->composeForRunInBackground());
24+
}
25+
26+
protected function composeForRunInBackground()
27+
{
28+
return "({$this->composeForRun()}) > /dev/null 2>&1 &";
29+
}
30+
31+
protected function composeForRun()
32+
{
33+
$parts = [];
34+
35+
if (!empty($this->before)) {
36+
$parts[] = (string) $this->before;
37+
}
38+
39+
$parts[] = "{$this->getPhpBinary()} {$this->getArtisan()} {$this->command}";
40+
41+
if (!empty($this->after)) {
42+
$parts[] = (string) $this->after;
43+
}
44+
45+
return implode(' && ', $parts);
46+
}
47+
48+
protected function getPhpBinary()
49+
{
50+
$finder = new PhpExecutableFinder();
51+
$phpBinary = $finder->find(false);
52+
53+
$phpBinary = ProcessUtils::escapeArgument($phpBinary);
54+
if (defined('HHVM_VERSION')) {
55+
$phpBinary .= ' --php';
56+
}
57+
58+
return $phpBinary;
59+
}
60+
61+
protected function getArtisan()
62+
{
63+
$artisan = 'artisan';
64+
if (defined('ARTISAN_BINARY')) {
65+
$artisan = ProcessUtils::escapeArgument(ARTISAN_BINARY);
66+
}
67+
68+
return $artisan;
69+
}
70+
}

src/classes/System/Command.php

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)