merged branch romainneutron/ProcessExceptions (PR #5398)

Commits
-------

c5e7793 [Process] Normalize exceptions

Discussion
----------

[Process] Normalize exceptions

Bug fix: no
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT

There were some exceptions in the Process scope but they were not implemented everywhere in the component.

This PR ensure that all exceptions thrown inside Process implements `Process\Exception\ExceptionInterface`.

---------------------------------------------------------------------------

by romainneutron at 2012-08-30T20:05:41Z

Tests passes, it's a travis issue, see http://travis-ci.org/#!/symfony/symfony/builds/2287439
This commit is contained in:
Fabien Potencier 2012-09-18 19:05:11 +02:00
commit 0d181bc06d
9 changed files with 72 additions and 23 deletions

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Process\Exception;
/**
* InvalidArgumentException for the Process Component.
*
* @author Romain Neutron <imprec@gmail.com>
*/
class InvalidArgumentException extends \InvalidArgumentException implements ExceptionInterface
{
}

View File

@ -0,0 +1,21 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Component\Process\Exception;
/**
* LogicException for the Process Component.
*
* @author Romain Neutron <imprec@gmail.com>
*/
class LogicException extends \LogicException implements ExceptionInterface
{
}

View File

@ -25,7 +25,7 @@ class ProcessFailedException extends RuntimeException
public function __construct(Process $process) public function __construct(Process $process)
{ {
if ($process->isSuccessful()) { if ($process->isSuccessful()) {
throw new \InvalidArgumentException('Expected a failed process, but the given process was successful.'); throw new InvalidArgumentException('Expected a failed process, but the given process was successful.');
} }
parent::__construct( parent::__construct(

View File

@ -11,6 +11,8 @@
namespace Symfony\Component\Process; namespace Symfony\Component\Process;
use Symfony\Component\Process\Exception\RuntimeException;
/** /**
* PhpProcess runs a PHP script in an independent process. * PhpProcess runs a PHP script in an independent process.
* *
@ -68,7 +70,7 @@ class PhpProcess extends Process
{ {
if (null === $this->getCommandLine()) { if (null === $this->getCommandLine()) {
if (false === $php = $this->executableFinder->find()) { if (false === $php = $this->executableFinder->find()) {
throw new \RuntimeException('Unable to find the PHP executable.'); throw new RuntimeException('Unable to find the PHP executable.');
} }
$this->setCommandLine($php); $this->setCommandLine($php);
} }

View File

@ -11,6 +11,9 @@
namespace Symfony\Component\Process; namespace Symfony\Component\Process;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\RuntimeException;
/** /**
* Process is a thin wrapper around proc_* functions to ease * Process is a thin wrapper around proc_* functions to ease
* start independent PHP processes. * start independent PHP processes.
@ -111,14 +114,14 @@ class Process
* @param integer $timeout The timeout in seconds * @param integer $timeout The timeout in seconds
* @param array $options An array of options for proc_open * @param array $options An array of options for proc_open
* *
* @throws \RuntimeException When proc_open is not installed * @throws RuntimeException When proc_open is not installed
* *
* @api * @api
*/ */
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array()) public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{ {
if (!function_exists('proc_open')) { if (!function_exists('proc_open')) {
throw new \RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.'); throw new RuntimeException('The Process class relies on proc_open, which is not available on your PHP installation.');
} }
$this->commandline = $commandline; $this->commandline = $commandline;
@ -158,7 +161,7 @@ class Process
* *
* @return integer The exit status code * @return integer The exit status code
* *
* @throws \RuntimeException When process can't be launch or is stopped * @throws RuntimeException When process can't be launch or is stopped
* *
* @api * @api
*/ */
@ -187,13 +190,13 @@ class Process
* @param callable $callback A PHP callback to run whenever there is some * @param callable $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR * output available on STDOUT or STDERR
* *
* @throws \RuntimeException When process can't be launch or is stopped * @throws RuntimeException When process can't be launch or is stopped
* @throws \RuntimeException When process is already running * @throws RuntimeException When process is already running
*/ */
public function start($callback = null) public function start($callback = null)
{ {
if ($this->isRunning()) { if ($this->isRunning()) {
throw new \RuntimeException('Process is already running'); throw new RuntimeException('Process is already running');
} }
$this->stdout = ''; $this->stdout = '';
@ -233,7 +236,7 @@ class Process
$this->process = proc_open($commandline, $descriptors, $this->pipes, $this->cwd, $this->env, $this->options); $this->process = proc_open($commandline, $descriptors, $this->pipes, $this->cwd, $this->env, $this->options);
if (!is_resource($this->process)) { if (!is_resource($this->process)) {
throw new \RuntimeException('Unable to launch a new process.'); throw new RuntimeException('Unable to launch a new process.');
} }
$this->status = self::STATUS_STARTED; $this->status = self::STATUS_STARTED;
@ -270,7 +273,7 @@ class Process
if ($n === 0) { if ($n === 0) {
proc_terminate($this->process); proc_terminate($this->process);
throw new \RuntimeException('The process timed out.'); throw new RuntimeException('The process timed out.');
} }
if ($w) { if ($w) {
@ -311,7 +314,7 @@ class Process
* *
* @return int The exitcode of the process * @return int The exitcode of the process
* *
* @throws \RuntimeException * @throws RuntimeException
*/ */
public function wait($callback = null) public function wait($callback = null)
{ {
@ -337,7 +340,7 @@ class Process
if (0 === $n) { if (0 === $n) {
proc_terminate($this->process); proc_terminate($this->process);
throw new \RuntimeException('The process timed out.'); throw new RuntimeException('The process timed out.');
} }
foreach ($r as $pipe) { foreach ($r as $pipe) {
@ -361,7 +364,7 @@ class Process
} }
$this->updateStatus(); $this->updateStatus();
if ($this->processInformation['signaled']) { if ($this->processInformation['signaled']) {
throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig'])); throw new RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig']));
} }
$time = 0; $time = 0;
@ -373,7 +376,7 @@ class Process
$exitcode = proc_close($this->process); $exitcode = proc_close($this->process);
if ($this->processInformation['signaled']) { if ($this->processInformation['signaled']) {
throw new \RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig'])); throw new RuntimeException(sprintf('The process stopped because of a "%s" signal.', $this->processInformation['stopsig']));
} }
$this->exitcode = $this->processInformation['running'] ? $exitcode : $this->processInformation['exitcode']; $this->exitcode = $this->processInformation['running'] ? $exitcode : $this->processInformation['exitcode'];
@ -546,7 +549,7 @@ class Process
* *
* @return integer The exitcode of the process * @return integer The exitcode of the process
* *
* @throws \RuntimeException if the process got signaled * @throws RuntimeException if the process got signaled
*/ */
public function stop($timeout=10) public function stop($timeout=10)
{ {
@ -622,7 +625,7 @@ class Process
$timeout = (integer) $timeout; $timeout = (integer) $timeout;
if ($timeout < 0) { if ($timeout < 0) {
throw new \InvalidArgumentException('The timeout value must be a valid positive integer.'); throw new InvalidArgumentException('The timeout value must be a valid positive integer.');
} }
$this->timeout = $timeout; $this->timeout = $timeout;

View File

@ -11,6 +11,9 @@
namespace Symfony\Component\Process; namespace Symfony\Component\Process;
use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\LogicException;
/** /**
* Process builder. * Process builder.
* *
@ -99,7 +102,7 @@ class ProcessBuilder
$timeout = (integer) $timeout; $timeout = (integer) $timeout;
if ($timeout < 0) { if ($timeout < 0) {
throw new \InvalidArgumentException('The timeout value must be a valid positive integer.'); throw new InvalidArgumentException('The timeout value must be a valid positive integer.');
} }
$this->timeout = $timeout; $this->timeout = $timeout;
@ -117,7 +120,7 @@ class ProcessBuilder
public function getProcess() public function getProcess()
{ {
if (!count($this->arguments)) { if (!count($this->arguments)) {
throw new \LogicException('You must add() command arguments before calling getProcess().'); throw new LogicException('You must add() command arguments before calling getProcess().');
} }
$options = $this->options; $options = $this->options;

View File

@ -85,7 +85,7 @@ class ProcessBuilderTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @expectedException \InvalidArgumentException * @expectedException Symfony\Component\Process\Exception\InvalidArgumentException
*/ */
public function testNegativeTimeoutFromSetter() public function testNegativeTimeoutFromSetter()
{ {

View File

@ -11,8 +11,7 @@
namespace Symfony\Component\Process\Tests; namespace Symfony\Component\Process\Tests;
use Symfony\Component\Process\Process, use Symfony\Component\Process\Exception\ProcessFailedException;
Symfony\Component\Process\Exception\ProcessFailedException;
/** /**
* @author Sebastian Marek <proofek@gmail.com> * @author Sebastian Marek <proofek@gmail.com>

View File

@ -19,7 +19,7 @@ use Symfony\Component\Process\Process;
class ProcessTest extends \PHPUnit_Framework_TestCase class ProcessTest extends \PHPUnit_Framework_TestCase
{ {
/** /**
* @expectedException \InvalidArgumentException * @expectedException Symfony\Component\Process\Exception\InvalidArgumentException
*/ */
public function testNegativeTimeoutFromConstructor() public function testNegativeTimeoutFromConstructor()
{ {
@ -27,7 +27,7 @@ class ProcessTest extends \PHPUnit_Framework_TestCase
} }
/** /**
* @expectedException \InvalidArgumentException * @expectedException Symfony\Component\Process\Exception\InvalidArgumentException
*/ */
public function testNegativeTimeoutFromSetter() public function testNegativeTimeoutFromSetter()
{ {