123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- <?php
- namespace Codeception\Extension;
- use Codeception\Events;
- use Codeception\Exception\ExtensionException;
- use Codeception\Extension;
- use Symfony\Component\Process\Process;
- class RunProcess extends Extension
- {
- public $config = ['sleep' => 0];
-
- static $events = [
- Events::SUITE_BEFORE => 'runProcess',
- Events::SUITE_AFTER => 'stopProcess'
- ];
- protected $processes = [];
-
- public function _initialize()
- {
- if (!class_exists('Symfony\Component\Process\Process')) {
- throw new ExtensionException($this, 'symfony/process package is required');
- }
- }
- public function runProcess()
- {
- $this->processes = [];
- foreach ($this->config as $key => $command) {
- if (!$command) {
- continue;
- }
- if (!is_int($key)) {
- continue;
- }
- $process = new Process($command, $this->getRootDir(), null, null, null);
- $process->start();
- $this->processes[] = $process;
- $this->output->debug('[RunProcess] Starting '.$command);
- }
- sleep($this->config['sleep']);
- }
- public function __destruct()
- {
- $this->stopProcess();
- }
- public function stopProcess()
- {
- foreach (array_reverse($this->processes) as $process) {
-
- if (!$process->isRunning()) {
- continue;
- }
- $this->output->debug('[RunProcess] Stopping ' . $process->getCommandLine());
- $process->stop();
- }
- $this->processes = [];
- }
- }
|