[Process] changed ExecutableFinder to return false instead of throwing an exception when the executable is not found

I've made the change as the executable goal is to find the executable. The fact that it does not find it is
part of the contract and it is not exceptional.
This commit is contained in:
Fabien Potencier 2011-04-26 15:18:21 +02:00
parent f12146d0ec
commit e2741cefc4
7 changed files with 23 additions and 60 deletions

View File

@ -12,7 +12,6 @@
namespace Symfony\Bundle\AsseticBundle\DependencyInjection;
use Symfony\Component\Process\ExecutableFinder;
use Symfony\Component\Process\Exception\ExecutableNotFoundException;
use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;
@ -58,9 +57,9 @@ class Configuration implements ConfigurationInterface
->booleanNode('use_controller')->defaultValue($this->debug)->end()
->scalarNode('read_from')->defaultValue('%kernel.root_dir%/../web')->end()
->scalarNode('write_to')->defaultValue('%assetic.read_from%')->end()
->scalarNode('java')->defaultValue(function() use ($finder) { try { return $finder->find('java'); } catch(ExecutableNotFoundException $ex) { return null; } })->end()
->scalarNode('node')->defaultValue(function() use ($finder) { try { return $finder->find('node'); } catch(ExecutableNotFoundException $ex) { return null; } })->end()
->scalarNode('sass')->defaultValue(function() use ($finder) { try { return $finder->find('sass'); } catch(ExecutableNotFoundException $ex) { return null; } })->end()
->scalarNode('java')->defaultValue(function() use ($finder) { $java = $finder->find('java'); return $java ? $java : '/usr/bin/java'; })->end()
->scalarNode('node')->defaultValue(function() use ($finder) { $node = $finder->find('node'); return $node ? $node : '/usr/bin/node'; })->end()
->scalarNode('sass')->defaultValue(function() use ($finder) { $saas = $finder->find('saas'); return $saas ? $saas : '/usr/bin/saas'; })->end()
->end()
// bundles

View File

@ -1,12 +0,0 @@
<?php
namespace Symfony\Component\Process\Exception;
/**
* Base exception interface for this component.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
interface Exception
{
}

View File

@ -1,25 +0,0 @@
<?php
namespace Symfony\Component\Process\Exception;
/**
* This exception is thrown when an executable is not found.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class ExecutableNotFoundException extends RuntimeException
{
private $name;
public function __construct($name)
{
parent::__construct(sprintf('Could not find executable "%s".', $name));
$this->name = $name;
}
public function getName()
{
return $this->name;
}
}

View File

@ -1,12 +0,0 @@
<?php
namespace Symfony\Component\Process\Exception;
/**
* RuntimeException for this component.
*
* @author Johannes M. Schmitt <schmittjoh@gmail.com>
*/
class RuntimeException extends \RuntimeException implements Exception
{
}

View File

@ -11,8 +11,6 @@
namespace Symfony\Component\Process;
use Symfony\Component\Process\Exception\ExecutableNotFoundException;
/**
* Generic executable finder.
*
@ -33,6 +31,13 @@ class ExecutableFinder
$this->suffixes[] = $suffix;
}
/**
* Finds an executable by name.
*
* @param string $name The executable name (without the extension)
*
* @return string|false The executable path or false if it cannot be found
*/
public function find($name)
{
$dirs = explode(PATH_SEPARATOR, getenv('PATH') ? getenv('PATH') : getenv('Path'));
@ -45,6 +50,6 @@ class ExecutableFinder
}
}
throw new ExecutableNotFoundException($name);
return false;
}
}
}

View File

@ -28,11 +28,16 @@ class PhpExecutableFinder
$this->executableFinder = new ExecutableFinder();
}
/**
* Finds The PHP executable.
*
* @return string|false The PHP executable path or false if it cannot be found
*/
public function find()
{
if ($php = getenv('PHP_PATH')) {
if (!is_executable($php)) {
throw new RuntimeException('The defined PHP_PATH environment variable is not a valid PHP executable.');
return false;
}
return $php;
@ -53,4 +58,4 @@ class PhpExecutableFinder
return $this->executableFinder->find('php');
}
}
}

View File

@ -67,7 +67,10 @@ class PhpProcess extends Process
public function run($callback = null)
{
if (null === $this->getCommandLine()) {
$this->setCommandLine($this->executableFinder->find());
if (false === $php = $this->executableFinder->find()) {
throw new \RuntimeException('Unable to find the PHP executable.');
}
$this->setCommandLine($php);
}
return parent::run($callback);