reset(); } /** * Set's prefix and special. * * @param string $prefix Prefix. * @param string $special Special. * * @return void * @throws Exception Every time, When called. */ public function Init($prefix, $special) { $error_msg = sprintf( 'Please use "%s" method instead of "%s" to create "%s" object.', 'makeClass', 'recallObject', '' . __CLASS__ . '' ); throw new Exception($error_msg); } /** * Resets state to allow building another process. * * @return static */ public function reset() { $this->command = null; $this->executable = null; $this->arguments = array(); $this->workingDirectory = null; $this->env = array(); $this->input = null; $this->timeout = 60; $this->options = array(); return $this; } /** * Sets command. * * @param string $command Command. * * @return static */ public function setCommand($command) { $this->command = $command; return $this; } /** * Sets executable. * * @param string $executable Executable. * * @return static */ public function setExecutable($executable) { $this->executable = $executable; return $this; } /** * Adds an argument. * * @param mixed $argument Argument. * * @return static */ public function add($argument) { $this->arguments[] = $argument; return $this; } /** * Sets the working directory. * * @param string|null $cwd The working directory or null to use the working dir of the current PHP process. * * @return static */ public function setWorkingDirectory($cwd) { $this->workingDirectory = $cwd; return $this; } /** * Sets an environment variable. * Setting a variable overrides its previous value. Use `null` to unset a * defined environment variable. * * @param string $name The variable name. * @param null|string $value The variable value. * * @return static */ public function setEnv($name, $value) { $this->env[$name] = $value; return $this; } /** * Sets the input of the process. * * @param mixed $input The input as a string. * * @return static */ public function setInput($input) { $this->input = ProcessUtils::validateInput(sprintf('%s::%s', __CLASS__, __FUNCTION__), $input); return $this; } /** * Adds a proc_open option. * * @param string $name The option name. * @param string $value The option value. * * @return static */ public function setOption($name, $value) { $this->options[$name] = $value; return $this; } /** * Sets the process timeout. * * @param float|null $timeout Timeout or null to disable. * * @return static * @throws InvalidArgumentException When negative timeout is given. */ public function setTimeout($timeout) { if ( $timeout === null ) { $this->timeout = null; return $this; } $timeout = (float)$timeout; if ( $timeout < 0 ) { throw new InvalidArgumentException('The timeout value must be a valid positive integer or float number.'); } $this->timeout = $timeout; return $this; } /** * Creates process. * * @return Process * @throws LogicException When incompatible builder parameters are set. */ public function build() { if ( isset($this->executable) && isset($this->command) ) { throw new LogicException('Use setCommand() or setExecutable(), not both.'); } if ( isset($this->command) && $this->arguments ) { throw new LogicException('The add() can only be used with setExecutable().'); } return new Process( $this->getCommandLine(), $this->workingDirectory, array_replace($_ENV, $_SERVER, $this->env), $this->input, $this->timeout, $this->options ); } /** * Builds commandline. * * @return string * @throws LogicException When none of "command" or "executable" isn't set. */ protected function getCommandLine() { if ( isset($this->command) ) { return $this->command; } if ( isset($this->executable) ) { $arguments = array_merge(array($this->executable), $this->arguments); $escaped_arguments = array_map( array('Symfony\\Component\\Process\\ProcessUtils', 'escapeArgument'), $arguments ); return implode(' ', $escaped_arguments); } throw new LogicException('You must use setCommand() or setExecutable() before getting the process.'); } }