Skip to content

Commit

Permalink
Added support for Symfony 3.* components
Browse files Browse the repository at this point in the history
  • Loading branch information
pkruithof committed Dec 16, 2015
1 parent 7899d8b commit 8715458
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 21 deletions.
8 changes: 4 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@
"require": {
"php": ">=5.3.9",
"webmozart/assert": "^1.0",
"symfony/console": "^2.7",
"symfony/process": "^2.5",
"symfony/event-dispatcher": "^2.5"
"symfony/console": "^2.7|^3.0",
"symfony/process": "^2.5|^3.0",
"symfony/event-dispatcher": "^2.5|^3.0"
},
"require-dev": {
"symfony/filesystem": "^2.3",
"symfony/filesystem": "^2.3|^3.0",
"phpunit/phpunit": "^4.6",
"sebastian/version": "^1.0.1"
},
Expand Down
18 changes: 15 additions & 3 deletions src/Adapter/ArgsInput.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ public function getFirstArgument()
/**
* {@inheritdoc}
*/
public function hasParameterOption($values)
public function hasParameterOption($values, $onlyParams = false)
{
$tokens = $this->rawArgs->getTokens();

foreach ((array) $values as $value) {
foreach ($tokens as $token) {
// end of options (--) signal reached, stop now
if ($onlyParams && $token === '--') {
return false;
}

if ($token === $value || 0 === strpos($token, $value.'=')) {
return true;
}
Expand All @@ -96,17 +101,24 @@ public function hasParameterOption($values)
/**
* {@inheritdoc}
*/
public function getParameterOption($values, $default = false)
public function getParameterOption($values, $default = false, $onlyParams = false)
{
$tokens = $this->rawArgs->getTokens();

foreach ((array) $values as $value) {
for (reset($tokens); null !== key($tokens); next($tokens)) {
$token = current($tokens);

if ($onlyParams && ($token === '--')) {
// end of options (--) signal reached, stop now
return $default;
}

// Long/short option with value in the next argument
if ($token === $value) {
return next($tokens) ?: null;
$next = next($tokens);

return ($next && ($next !== '--')) ? $next : null;
}

// Long option with =
Expand Down
2 changes: 1 addition & 1 deletion src/Adapter/CommandAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function setHelperSet(HelperSet $helperSet)
*
* @return static The current instance.
*/
public function setCode($code)
public function setCode(callable $code)
{
return $this;
}
Expand Down
40 changes: 36 additions & 4 deletions src/Adapter/IOOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,25 +106,57 @@ public function setVerbosity($level)
*/
public function getVerbosity()
{
if ($this->io->isQuiet()) {
if ($this->isQuiet()) {
return self::VERBOSITY_QUIET;
}

if ($this->io->isDebug()) {
if ($this->isDebug()) {
return self::VERBOSITY_DEBUG;
}

if ($this->io->isVeryVerbose()) {
if ($this->isVeryVerbose()) {
return self::VERBOSITY_VERY_VERBOSE;
}

if ($this->io->isVerbose()) {
if ($this->isVerbose()) {
return self::VERBOSITY_VERBOSE;
}

return self::VERBOSITY_NORMAL;
}

/**
* @inheritdoc
*/
public function isQuiet()
{
return $this->io->isQuiet();
}

/**
* @inheritdoc
*/
public function isVerbose()
{
return $this->io->isVerbose();
}

/**
* @inheritdoc
*/
public function isVeryVerbose()
{
return $this->io->isVeryVerbose();
}

/**
* @inheritdoc
*/
public function isDebug()
{
return $this->io->isDebug();
}

/**
* {@inheritdoc}
*/
Expand Down
19 changes: 13 additions & 6 deletions tests/Adapter/ArgsInputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,33 +80,40 @@ public function testGetNoFirstArgument()

public function testHasParameterOption()
{
$input = new ArgsInput(new StringArgs('-o --option --value=value'));
$input = new ArgsInput(new StringArgs('-o --option --value=value -- --foo=bar'));

$this->assertTrue($input->hasParameterOption('-o'));
$this->assertTrue($input->hasParameterOption('--option'));
$this->assertTrue($input->hasParameterOption('--value'));
$this->assertFalse($input->hasParameterOption('--foo'));
$this->assertTrue($input->hasParameterOption('--foo'));
// only check real parameters, skip those following an end of options (--) signal
$this->assertFalse($input->hasParameterOption('--foo', true));
}

public function testHasMultipleParameterOptions()
{
$input = new ArgsInput(new StringArgs('-o --option --value=value'));
$input = new ArgsInput(new StringArgs('-o --option --value=value -- --foo=bar'));

$this->assertTrue($input->hasParameterOption(array('-o', '--option')));
// sufficient if any of the options exists
$this->assertTrue($input->hasParameterOption(array('-o', '--foo')));
$this->assertFalse($input->hasParameterOption(array('--foo', '--bar')));
$this->assertFalse($input->hasParameterOption(array('--bar', '--baz')));
// only check real parameters, skip those following an end of options (--) signal
$this->assertTrue($input->hasParameterOption(array('--foo', '--bar')));
$this->assertFalse($input->hasParameterOption(array('--foo', '--bar'), true));
}

public function testGetParameterOption()
{
$input = new ArgsInput(new StringArgs('-vvalue1 --value=value2 --space value3 --last'));
$input = new ArgsInput(new StringArgs('-vvalue1 --value=value2 --space value3 --last -- --foo=bar'));

$this->assertSame('value1', $input->getParameterOption('-v'));
$this->assertSame('value2', $input->getParameterOption('--value'));
$this->assertSame('value3', $input->getParameterOption('--space'));
$this->assertNull($input->getParameterOption('--last'));
$this->assertSame('default', $input->getParameterOption('--foo', 'default'));
$this->assertSame('bar', $input->getParameterOption('--foo', 'default'));
// only check real parameters, skip those following an end of options (--) signal
$this->assertSame('default', $input->getParameterOption('--foo', 'default', true));
}

public function testGetArguments()
Expand Down
6 changes: 3 additions & 3 deletions tests/Args/DefaultArgsParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ public function testParseIgnoresMissingOptionalArguments()

/**
* @expectedException \Webmozart\Console\Api\Args\CannotParseArgsException
* @expectedExceptionMessage Not enough arguments.
* @expectedExceptionMessage Not enough arguments
*/
public function testParseFailsIfMissingRequiredArgument()
{
Expand Down Expand Up @@ -223,7 +223,7 @@ public function testParseDoesNotFailIfMissingRequiredArgumentAndLenient()

/**
* @expectedException \Webmozart\Console\Api\Args\CannotParseArgsException
* @expectedExceptionMessage Not enough arguments.
* @expectedExceptionMessage Not enough arguments
*/
public function testParseFailsIfMissingRequiredArgumentWithMissingCommandNames()
{
Expand Down Expand Up @@ -254,7 +254,7 @@ public function testParseDoesNotFailIfMissingRequiredArgumentWithMissingCommandN

/**
* @expectedException \Webmozart\Console\Api\Args\CannotParseArgsException
* @expectedExceptionMessage Not enough arguments.
* @expectedExceptionMessage Not enough arguments
*/
public function testParseFailsIfMissingRequiredArgumentWithMissingCommandOptions()
{
Expand Down

0 comments on commit 8715458

Please sign in to comment.