-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathShell.php
More file actions
104 lines (93 loc) · 3.26 KB
/
Copy pathShell.php
File metadata and controls
104 lines (93 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
<?php
/*
* Copyright (c) Default Value LLC.
* This source file is subject to the License https://github.com/DefaultValue/dockerizer_for_php/LICENSE.txt
* Do not change this file if you want to upgrade the tool to the newer versions in the future
* Please, contact us at https://default-value.com/#contact if you wish to customize this tool
* according to you business needs
*/
declare(strict_types=1);
namespace DefaultValue\Dockerizer\Shell;
use Symfony\Component\Process\Process;
// @TODO: get command > return and log it > run it. We should be able to return commands and log them.
class Shell
{
public const EXECUTION_TIMEOUT_SHORT = 60;
public const EXECUTION_TIMEOUT_MEDIUM = 600;
public const EXECUTION_TIMEOUT_LONG = 3600;
// 6 hours for extra long operations like importing a huge DB dump
public const EXECUTION_TIMEOUT_VERY_LONG = 21600;
public const EXECUTION_TIMEOUT_INFINITE = 0;
/**
* @param string[] $command
* @param string|null $cwd
* @param string[] $env
* @param string|null $input
* @param float|null $timeout
* @param callable|null $callback
* @return Process
*/
public function start(
array $command,
string $cwd = null,
array $env = [],
string $input = null,
?float $timeout = self::EXECUTION_TIMEOUT_SHORT,
?callable $callback = null
): Process {
// Not yet sure we need to throw exception on error
$process = new Process($command, $cwd, $env, $input, $timeout);
$process->start($callback, $env);
return $process;
}
/**
* @param string[]|string $command
* @param string|null $cwd
* @param string[] $env
* @param string|null $input
* @param float|null $timeout
* @param callable|null $callback
* @return Process
*/
public function run(
array|string $command,
string $cwd = null,
array $env = [],
string $input = null,
?float $timeout = self::EXECUTION_TIMEOUT_SHORT,
?callable $callback = null
): Process {
// Not yet sure we need to throw exception on error
// @TODO: replace with `new Process()`
$process = is_array($command)
? new Process($command, $cwd, $env, $input, $timeout)
: Process::fromShellCommandline($command, $cwd, $env, $input, $timeout);
$process->run($callback, $env);
return $process;
}
/**
* @param string[]|string $command
* @param string|null $cwd
* @param string[] $env
* @param string|null $input
* @param float|null $timeout
* @param callable|null $callback
* @return Process
*/
public function mustRun(
array|string $command,
string $cwd = null,
array $env = [],
string $input = null,
?float $timeout = self::EXECUTION_TIMEOUT_SHORT,
?callable $callback = null
): Process {
// Not yet sure we need to throw exception on error
// @TODO: replace with `new Process()`
$process = is_array($command)
? new Process($command, $cwd, $env, $input, $timeout)
: Process::fromShellCommandline($command, $cwd, $env, $input, $timeout);
$process->mustRun($callback, $env);
return $process;
}
}