Assetic\Util\Process::run PHP Method

run() public method

The callback receives the type of output (out or err) and some bytes from the output in real-time. It allows to have feedback from the independent process during execution. The STDOUT and STDERR are also available after the process is finished via the getOutput() and getErrorOutput() methods.
public run ( Closure | string | array $callback = null ) : integer
$callback Closure | string | array A PHP callback to run whenever there is some output available on STDOUT or STDERR
return integer The exit status code
    public function run($callback = null)
    {
        $this->stdout = '';
        $this->stderr = '';
        $that = $this;
        $callback = function ($type, $data) use ($that, $callback)
        {
            if ('out' == $type) {
                $that->addOutput($data);
            } else {
                $that->addErrorOutput($data);
            }

            if (null !== $callback) {
                call_user_func($callback, $type, $data);
            }
        };

        $descriptors = array(array('pipe', 'r'), array('pipe', 'w'), array('pipe', 'w'));

        $process = proc_open($this->commandline, $descriptors, $pipes, $this->cwd, $this->env, $this->options);

        if (!is_resource($process)) {
            throw new \RuntimeException('Unable to launch a new process.');
        }

        foreach ($pipes as $pipe) {
            stream_set_blocking($pipe, false);
        }

        if (null === $this->stdin) {
            fclose($pipes[0]);
            $writePipes = null;
        } else {
            $writePipes = array($pipes[0]);
            $stdinLen = strlen($this->stdin);
            $stdinOffset = 0;
        }
        unset($pipes[0]);

        while ($pipes || $writePipes) {
            $r = $pipes;
            $w = $writePipes;
            $e = null;

            $n = @stream_select($r, $w, $e, $this->timeout);

            if (false === $n) {
                break;
            } elseif ($n === 0) {
                proc_terminate($process);

                throw new \RuntimeException('The process timed out.');
            }

            if ($w) {
                $written = fwrite($writePipes[0], (binary) substr($this->stdin, $stdinOffset), 8192);
                if (false !== $written) {
                    $stdinOffset += $written;
                }
                if ($stdinOffset >= $stdinLen) {
                    fclose($writePipes[0]);
                    $writePipes = null;
                }
            }

            foreach ($r as $pipe) {
                $type = array_search($pipe, $pipes);
                $data = fread($pipe, 8192);
                if (strlen($data) > 0) {
                    call_user_func($callback, $type == 1 ? 'out' : 'err', $data);
                }
                if (false === $data || feof($pipe)) {
                    fclose($pipe);
                    unset($pipes[$type]);
                }
            }
        }

        $this->status = proc_get_status($process);

        $time = 0;
        while (1 == $this->status['running'] && $time < 1000000) {
            $time += 1000;
            usleep(1000);
            $this->status = proc_get_status($process);
        }

        proc_close($process);

        if ($this->status['signaled']) {
            throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->status['stopsig']));
        }

        return $this->exitcode = $this->status['exitcode'];
    }

Usage Example

Example #1
0
 public function filterDump(AssetInterface $asset)
 {
     $options = array($this->jpegtranBin);
     if ($this->optimize) {
         $options[] = '-optimize';
     }
     if ($this->copy) {
         $options[] = '-copy';
         $options[] = $this->copy;
     }
     if ($this->progressive) {
         $options[] = '-progressive';
     }
     if (null !== $this->restart) {
         $options[] = '-restart';
         $options[] = $this->restart;
     }
     $options[] = $input = tempnam(sys_get_temp_dir(), 'assetic_jpegtran');
     file_put_contents($input, $asset->getContent());
     $proc = new Process(implode(' ', array_map('escapeshellarg', $options)));
     $code = $proc->run();
     unlink($input);
     if (0 < $code) {
         throw new \RuntimeException($proc->getErrorOutput());
     }
     $asset->setContent($proc->getOutput());
 }
All Usage Examples Of Assetic\Util\Process::run