1 | <?php |
||
25 | class CommandExecutor |
||
26 | { |
||
27 | /** |
||
28 | * @var string |
||
29 | */ |
||
30 | protected $host; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $path; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | protected $cwd; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | protected $console; |
||
46 | |||
47 | /** |
||
48 | * @var PhpFinder |
||
49 | */ |
||
50 | protected $finder; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | const TIMEOUT = 2; |
||
56 | |||
57 | /** |
||
58 | * @param PhpFinder $finder |
||
59 | * @param RouterInterface $router |
||
60 | * @param RequestStack $request_stack |
||
61 | * @param string $root_dir |
||
62 | */ |
||
63 | 5 | public function __construct(PhpFinder $finder, RouterInterface $router, RequestStack $request_stack, $root_dir) |
|
64 | { |
||
65 | 5 | $this->finder = $finder; |
|
66 | 5 | $this->cwd = $root_dir.'/../'; |
|
67 | 5 | $this->console = escapeshellarg($root_dir.DIRECTORY_SEPARATOR.'console'); |
|
68 | 5 | $this->path = $router->generate('command_exec'); |
|
69 | 5 | if ($request = $request_stack->getCurrentRequest()) { |
|
70 | 4 | $this->host = $request->getHost().':'.$request->getPort(); |
|
71 | } |
||
72 | 5 | } |
|
73 | |||
74 | /** |
||
75 | * @deprecated see self::send() |
||
76 | * |
||
77 | * @throws \InvalidArgumentException |
||
78 | * |
||
79 | * @param string $command |
||
80 | */ |
||
81 | public function exec($command) |
||
88 | |||
89 | /** |
||
90 | * Execute command. |
||
91 | * |
||
92 | * If timeout <= 0 and callback is null then command will be executed in background |
||
93 | * |
||
94 | * @param string $command |
||
95 | * @param int $timeout |
||
96 | * @param callable|null $callback |
||
97 | */ |
||
98 | public function execute($command, $timeout = 300, $callback = null) |
||
99 | { |
||
100 | if ($timeout > 0 || is_callable($callback)) { |
||
101 | $this->executeCommand($command, $timeout, $callback); |
||
102 | } else { |
||
103 | $this->executeCommandInBackground($command); |
||
104 | } |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @param string $command |
||
109 | * @param int $timeout |
||
110 | * @param callable|null $callback |
||
111 | */ |
||
112 | public function console($command, $timeout = 300, $callback = null) |
||
116 | |||
117 | /** |
||
118 | * @throws \RuntimeException |
||
119 | * |
||
120 | * @param string $command |
||
121 | * @param int $timeout |
||
122 | * @param callable|null $callback |
||
123 | */ |
||
124 | protected function executeCommand($command, $timeout = 300, $callback = null) |
||
132 | |||
133 | /** |
||
134 | * @param string $command |
||
135 | */ |
||
136 | protected function executeCommandInBackground($command) |
||
137 | { |
||
138 | $cwd = getcwd(); |
||
139 | chdir($this->cwd); |
||
140 | |||
141 | $command = $this->prepare($command); |
||
142 | if (defined('PHP_WINDOWS_VERSION_BUILD') && function_exists('popen')) { |
||
143 | pclose(popen('start /b call '.$command, 'r')); |
||
144 | } else { |
||
145 | exec($command.' &'); |
||
146 | } |
||
147 | |||
148 | chdir($cwd); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Send the command to perform in a new thread. |
||
153 | * |
||
154 | * @param string $command |
||
155 | * @param string $host |
||
156 | */ |
||
157 | 1 | public function send($command, $host = '') |
|
175 | |||
176 | /** |
||
177 | * @param string $command |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | 3 | public function prepare($command) |
|
198 | } |
||
199 |