| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Console\Scheduling;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Console\Application;
|
|
|
6 |
use Illuminate\Support\ProcessUtils;
|
|
|
7 |
|
|
|
8 |
class CommandBuilder
|
|
|
9 |
{
|
|
|
10 |
/**
|
|
|
11 |
* Build the command for the given event.
|
|
|
12 |
*
|
|
|
13 |
* @param \Illuminate\Console\Scheduling\Event $event
|
|
|
14 |
* @return string
|
|
|
15 |
*/
|
|
|
16 |
public function buildCommand(Event $event)
|
|
|
17 |
{
|
|
|
18 |
if ($event->runInBackground) {
|
|
|
19 |
return $this->buildBackgroundCommand($event);
|
|
|
20 |
}
|
|
|
21 |
|
|
|
22 |
return $this->buildForegroundCommand($event);
|
|
|
23 |
}
|
|
|
24 |
|
|
|
25 |
/**
|
|
|
26 |
* Build the command for running the event in the foreground.
|
|
|
27 |
*
|
|
|
28 |
* @param \Illuminate\Console\Scheduling\Event $event
|
|
|
29 |
* @return string
|
|
|
30 |
*/
|
|
|
31 |
protected function buildForegroundCommand(Event $event)
|
|
|
32 |
{
|
|
|
33 |
$output = ProcessUtils::escapeArgument($event->output);
|
|
|
34 |
|
|
|
35 |
return $this->ensureCorrectUser(
|
|
|
36 |
$event, $event->command.($event->shouldAppendOutput ? ' >> ' : ' > ').$output.' 2>&1'
|
|
|
37 |
);
|
|
|
38 |
}
|
|
|
39 |
|
|
|
40 |
/**
|
|
|
41 |
* Build the command for running the event in the background.
|
|
|
42 |
*
|
|
|
43 |
* @param \Illuminate\Console\Scheduling\Event $event
|
|
|
44 |
* @return string
|
|
|
45 |
*/
|
|
|
46 |
protected function buildBackgroundCommand(Event $event)
|
|
|
47 |
{
|
|
|
48 |
$output = ProcessUtils::escapeArgument($event->output);
|
|
|
49 |
|
|
|
50 |
$redirect = $event->shouldAppendOutput ? ' >> ' : ' > ';
|
|
|
51 |
|
|
|
52 |
$finished = Application::formatCommandString('schedule:finish').' "'.$event->mutexName().'"';
|
|
|
53 |
|
|
|
54 |
if (windows_os()) {
|
|
|
55 |
return 'start /b cmd /v:on /c "('.$event->command.' & '.$finished.' ^!ERRORLEVEL^!)'.$redirect.$output.' 2>&1"';
|
|
|
56 |
}
|
|
|
57 |
|
|
|
58 |
return $this->ensureCorrectUser($event,
|
|
|
59 |
'('.$event->command.$redirect.$output.' 2>&1 ; '.$finished.' "$?") > '
|
|
|
60 |
.ProcessUtils::escapeArgument($event->getDefaultOutput()).' 2>&1 &'
|
|
|
61 |
);
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Finalize the event's command syntax with the correct user.
|
|
|
66 |
*
|
|
|
67 |
* @param \Illuminate\Console\Scheduling\Event $event
|
|
|
68 |
* @param string $command
|
|
|
69 |
* @return string
|
|
|
70 |
*/
|
|
|
71 |
protected function ensureCorrectUser(Event $event, $command)
|
|
|
72 |
{
|
|
|
73 |
return $event->user && ! windows_os() ? 'sudo -u '.$event->user.' -- sh -c \''.$command.'\'' : $command;
|
|
|
74 |
}
|
|
|
75 |
}
|