Chris@0: Chris@0: * Chris@0: * For the full copyright and license information, please view the LICENSE Chris@0: * file that was distributed with this source code. Chris@0: */ Chris@0: Chris@0: namespace Symfony\Component\Process; Chris@0: Chris@0: use Symfony\Component\Process\Exception\RuntimeException; Chris@0: Chris@0: /** Chris@0: * PhpProcess runs a PHP script in an independent process. Chris@0: * Chris@17: * $p = new PhpProcess(''); Chris@17: * $p->run(); Chris@17: * print $p->getOutput()."\n"; Chris@0: * Chris@0: * @author Fabien Potencier Chris@0: */ Chris@0: class PhpProcess extends Process Chris@0: { Chris@0: /** Chris@0: * @param string $script The PHP script to run (as a string) Chris@0: * @param string|null $cwd The working directory or null to use the working dir of the current PHP process Chris@0: * @param array|null $env The environment variables or null to use the same environment as the current PHP process Chris@0: * @param int $timeout The timeout in seconds Chris@0: * @param array $options An array of options for proc_open Chris@0: */ Chris@14: public function __construct($script, $cwd = null, array $env = null, $timeout = 60, array $options = null) Chris@0: { Chris@0: $executableFinder = new PhpExecutableFinder(); Chris@14: if (false === $php = $executableFinder->find(false)) { Chris@0: $php = null; Chris@14: } else { Chris@17: $php = array_merge([$php], $executableFinder->findArguments()); Chris@0: } Chris@17: if ('phpdbg' === \PHP_SAPI) { Chris@0: $file = tempnam(sys_get_temp_dir(), 'dbg'); Chris@0: file_put_contents($file, $script); Chris@0: register_shutdown_function('unlink', $file); Chris@14: $php[] = $file; Chris@0: $script = null; Chris@0: } Chris@14: if (null !== $options) { Chris@14: @trigger_error(sprintf('The $options parameter of the %s constructor is deprecated since Symfony 3.3 and will be removed in 4.0.', __CLASS__), E_USER_DEPRECATED); Chris@0: } Chris@0: Chris@0: parent::__construct($php, $cwd, $env, $script, $timeout, $options); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Sets the path to the PHP binary to use. Chris@0: */ Chris@0: public function setPhpBinary($php) Chris@0: { Chris@0: $this->setCommandLine($php); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@17: public function start(callable $callback = null/*, array $env = []*/) Chris@0: { Chris@0: if (null === $this->getCommandLine()) { Chris@0: throw new RuntimeException('Unable to find the PHP executable.'); Chris@0: } Chris@17: $env = 1 < \func_num_args() ? func_get_arg(1) : null; Chris@0: Chris@14: parent::start($callback, $env); Chris@0: } Chris@0: }