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