Skip to content

Commit

Permalink
merged branch romainneutron/ProcessExceptions (PR #5398)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
fabpot committed Sep 18, 2012
2 parents bf41d8b + c5e7793 commit 0d181bc
Show file tree
Hide file tree
Showing 9 changed files with 72 additions and 23 deletions.
@@ -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
{
}
21 changes: 21 additions & 0 deletions src/Symfony/Component/Process/Exception/LogicException.php
@@ -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
{
}
Expand Up @@ -25,7 +25,7 @@ class ProcessFailedException extends RuntimeException
public function __construct(Process $process)
{
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(
Expand Down
4 changes: 3 additions & 1 deletion src/Symfony/Component/Process/PhpProcess.php
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Process;

use Symfony\Component\Process\Exception\RuntimeException;

/**
* PhpProcess runs a PHP script in an independent process.
*
Expand Down Expand Up @@ -68,7 +70,7 @@ public function run($callback = null)
{
if (null === $this->getCommandLine()) {
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);
}
Expand Down
31 changes: 17 additions & 14 deletions src/Symfony/Component/Process/Process.php
Expand Up @@ -11,6 +11,9 @@

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
* start independent PHP processes.
Expand Down Expand Up @@ -111,14 +114,14 @@ class Process
* @param integer $timeout The timeout in seconds
* @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
*/
public function __construct($commandline, $cwd = null, array $env = null, $stdin = null, $timeout = 60, array $options = array())
{
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;
Expand Down Expand Up @@ -158,7 +161,7 @@ public function __destruct()
*
* @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
*/
Expand Down Expand Up @@ -187,13 +190,13 @@ public function run($callback = null)
* @param callable $callback A PHP callback to run whenever there is some
* output available on STDOUT or STDERR
*
* @throws \RuntimeException When process can't be launch or is stopped
* @throws \RuntimeException When process is already running
* @throws RuntimeException When process can't be launch or is stopped
* @throws RuntimeException When process is already running
*/
public function start($callback = null)
{
if ($this->isRunning()) {
throw new \RuntimeException('Process is already running');
throw new RuntimeException('Process is already running');
}

$this->stdout = '';
Expand Down Expand Up @@ -233,7 +236,7 @@ public function start($callback = null)
$this->process = proc_open($commandline, $descriptors, $this->pipes, $this->cwd, $this->env, $this->options);

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;

Expand Down Expand Up @@ -270,7 +273,7 @@ public function start($callback = null)
if ($n === 0) {
proc_terminate($this->process);

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

if ($w) {
Expand Down Expand Up @@ -311,7 +314,7 @@ public function start($callback = null)
*
* @return int The exitcode of the process
*
* @throws \RuntimeException
* @throws RuntimeException
*/
public function wait($callback = null)
{
Expand All @@ -337,7 +340,7 @@ public function wait($callback = null)
if (0 === $n) {
proc_terminate($this->process);

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

foreach ($r as $pipe) {
Expand All @@ -361,7 +364,7 @@ public function wait($callback = null)
}
$this->updateStatus();
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;
Expand All @@ -373,7 +376,7 @@ public function wait($callback = null)
$exitcode = proc_close($this->process);

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'];
Expand Down Expand Up @@ -546,7 +549,7 @@ public function isRunning()
*
* @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)
{
Expand Down Expand Up @@ -622,7 +625,7 @@ public function setTimeout($timeout)
$timeout = (integer) $timeout;

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;
Expand Down
7 changes: 5 additions & 2 deletions src/Symfony/Component/Process/ProcessBuilder.php
Expand Up @@ -11,6 +11,9 @@

namespace Symfony\Component\Process;

use Symfony\Component\Process\Exception\InvalidArgumentException;
use Symfony\Component\Process\Exception\LogicException;

/**
* Process builder.
*
Expand Down Expand Up @@ -99,7 +102,7 @@ public function setTimeout($timeout)
$timeout = (integer) $timeout;

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;
Expand All @@ -117,7 +120,7 @@ public function setOption($name, $value)
public function getProcess()
{
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;
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Process/Tests/ProcessBuilderTest.php
Expand Up @@ -85,7 +85,7 @@ public function shouldNotReplaceExplicitlySetVars()
}

/**
* @expectedException \InvalidArgumentException
* @expectedException Symfony\Component\Process\Exception\InvalidArgumentException
*/
public function testNegativeTimeoutFromSetter()
{
Expand Down
Expand Up @@ -11,8 +11,7 @@

namespace Symfony\Component\Process\Tests;

use Symfony\Component\Process\Process,
Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Exception\ProcessFailedException;

/**
* @author Sebastian Marek <proofek@gmail.com>
Expand Down
4 changes: 2 additions & 2 deletions src/Symfony/Component/Process/Tests/ProcessTest.php
Expand Up @@ -19,15 +19,15 @@
class ProcessTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \InvalidArgumentException
* @expectedException Symfony\Component\Process\Exception\InvalidArgumentException
*/
public function testNegativeTimeoutFromConstructor()
{
new Process('', null, null, null, -1);
}

/**
* @expectedException \InvalidArgumentException
* @expectedException Symfony\Component\Process\Exception\InvalidArgumentException
*/
public function testNegativeTimeoutFromSetter()
{
Expand Down

0 comments on commit 0d181bc

Please sign in to comment.