New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Console route improvements #4449

Merged
merged 13 commits into from May 22, 2013

Conversation

Projects
None yet
4 participants
@mtymek
Contributor

mtymek commented May 8, 2013

This PR attempts to finish missing features in console routes. It also extracts code responsible for parsing command line arguments into separate class under Zend\Console namespace. This allows using console commands without full MVC stack (even in non ZF2-based projects), for instance:

use Zend\Console\ConsoleRouteMatcher;

array_shift($argv);
$matcher = new ConsoleRouteMatcher("action [--verbose|-v] <param>");
$matches = $matcher->match($argv);
print_r($matches);

ConsoleRouteMatcher is now used internally by Zend\Mvc\Router\Console\Simple.

@weierophinney

This comment has been minimized.

Show comment
Hide comment
@weierophinney

weierophinney May 8, 2013

Member

I like this approach! :) Scheduling for 2.3.0.

Member

weierophinney commented May 8, 2013

I like this approach! :) Scheduling for 2.3.0.

@mtymek

This comment has been minimized.

Show comment
Hide comment
@mtymek

mtymek May 8, 2013

Contributor

Updated!

Contributor

mtymek commented May 8, 2013

Updated!

@prolic

This comment has been minimized.

Show comment
Hide comment
@prolic

prolic May 8, 2013

Contributor

definitely a good approach, thanks!

Contributor

prolic commented May 8, 2013

definitely a good approach, thanks!

@mtymek

This comment has been minimized.

Show comment
Hide comment
@mtymek

mtymek May 17, 2013

Contributor

OK. Currently route constructor works like before:

$route = new Simple('--foo=');

... or it can be injected with custom matcher:

use Zend\Console\RouteMatcher\RouteMatcherInterface;
use Zend\Mvc\Router\Console\Simple;

class CustomRouteMatcher implements RouteMatcherInterface
{
    public function match($params)
    {
    // ...
    }
}

$matcher = new CustomRouteMatcher('custom:route:definition');
$route = new Simple($matcher);

@Thinkscape: I will try to find some time to implement missing features you mentioned, and open separate PR.

Contributor

mtymek commented May 17, 2013

OK. Currently route constructor works like before:

$route = new Simple('--foo=');

... or it can be injected with custom matcher:

use Zend\Console\RouteMatcher\RouteMatcherInterface;
use Zend\Mvc\Router\Console\Simple;

class CustomRouteMatcher implements RouteMatcherInterface
{
    public function match($params)
    {
    // ...
    }
}

$matcher = new CustomRouteMatcher('custom:route:definition');
$route = new Simple($matcher);

@Thinkscape: I will try to find some time to implement missing features you mentioned, and open separate PR.

@Thinkscape

This comment has been minimized.

Show comment
Hide comment
@Thinkscape

Thinkscape May 17, 2013

Member

@weierophinney agreed. Chain actually implements respective Interface, so it's better to hint for FilterInterface and ValidatorInterface. This means one could provide a single or a chain of filters/validators.

@mtymek forget arrays, see above. Separate PR means this PR will have BC break. It'd be best if the second one came before this one. Just watch out for merge conflicts.

Member

Thinkscape commented May 17, 2013

@weierophinney agreed. Chain actually implements respective Interface, so it's better to hint for FilterInterface and ValidatorInterface. This means one could provide a single or a chain of filters/validators.

@mtymek forget arrays, see above. Separate PR means this PR will have BC break. It'd be best if the second one came before this one. Just watch out for merge conflicts.

@mtymek

This comment has been minimized.

Show comment
Hide comment
@mtymek

mtymek May 17, 2013

Contributor

@Thinkscape just thinking, there's still plenty of time before v2.3, so I could actually do everything in one go. Expect some questions though, here you have a few to start with :-)

  • you mentioned constraints and defaults as if they were not implemented, but after looking at the code (+ doing running some test scripts), they seem to be working. I guess it's enough to add some unit tests, right?
  • it is not clear for me how aliases should work. How are they different from alternatives ("[-v|--verbose]", etc)?
Contributor

mtymek commented May 17, 2013

@Thinkscape just thinking, there's still plenty of time before v2.3, so I could actually do everything in one go. Expect some questions though, here you have a few to start with :-)

  • you mentioned constraints and defaults as if they were not implemented, but after looking at the code (+ doing running some test scripts), they seem to be working. I guess it's enough to add some unit tests, right?
  • it is not clear for me how aliases should work. How are they different from alternatives ("[-v|--verbose]", etc)?
@Thinkscape

This comment has been minimized.

Show comment
Hide comment
@Thinkscape

Thinkscape May 17, 2013

Member
  1. yes
  2. -a|-b|-c is an alternative, not alias. Alias means foo == bar - allows you to map any number of aliases (keys) to any number of (named) params. This is a useful feature i.e. for keeping BC without changing controller logic.
Member

Thinkscape commented May 17, 2013

  1. yes
  2. -a|-b|-c is an alternative, not alias. Alias means foo == bar - allows you to map any number of aliases (keys) to any number of (named) params. This is a useful feature i.e. for keeping BC without changing controller logic.
@mtymek

This comment has been minimized.

Show comment
Hide comment
@mtymek

mtymek May 18, 2013

Contributor

I added support for aliases and tests.

As for filters and validators, if I change them from arrays to FilterInterface and ValidatorInterface, how can I validate multiple parameters? I thought they should be an array with each element representing validators/filters applied on single param.

Contributor

mtymek commented May 18, 2013

I added support for aliases and tests.

As for filters and validators, if I change them from arrays to FilterInterface and ValidatorInterface, how can I validate multiple parameters? I thought they should be an array with each element representing validators/filters applied on single param.

@Thinkscape

This comment has been minimized.

Show comment
Hide comment
@Thinkscape

Thinkscape May 18, 2013

Member

Take a look at InputFilter for some inspiration. That might be a decent approach...

Member

Thinkscape commented May 18, 2013

Take a look at InputFilter for some inspiration. That might be a decent approach...

@mtymek

This comment has been minimized.

Show comment
Hide comment
@mtymek

mtymek May 18, 2013

Contributor

I don't get it. Imagine following route:

add-user <name> <email>

I need different validation rules for name and email, so I cannot pass single ValidatorInterface to route matcher. I have to use an array:

$nameValidator = new Zend\Validator\ValidatorChain();
$nameValidator->add(new Zend\Validator\StringLength());
$nameValidator->add(new Zend\I18n\Validator\Alpha());

$emailValidator = new Zend\Validator\EmailAddress();

$validators = ;
$matcher = new DefaultRouteMatcher(
    'add-user <name> <email>', 
    array(), 
    array(), 
    array(), 
    array(), 
    array(
    'email' => $emailValidator,
    'name' => $nameValidator,
    )
);

Or am I missing something here?

Contributor

mtymek commented May 18, 2013

I don't get it. Imagine following route:

add-user <name> <email>

I need different validation rules for name and email, so I cannot pass single ValidatorInterface to route matcher. I have to use an array:

$nameValidator = new Zend\Validator\ValidatorChain();
$nameValidator->add(new Zend\Validator\StringLength());
$nameValidator->add(new Zend\I18n\Validator\Alpha());

$emailValidator = new Zend\Validator\EmailAddress();

$validators = ;
$matcher = new DefaultRouteMatcher(
    'add-user <name> <email>', 
    array(), 
    array(), 
    array(), 
    array(), 
    array(
    'email' => $emailValidator,
    'name' => $nameValidator,
    )
);

Or am I missing something here?

@Thinkscape

This comment has been minimized.

Show comment
Hide comment
@Thinkscape

Thinkscape May 19, 2013

Member

Yes, "array of".

Member

Thinkscape commented May 19, 2013

Yes, "array of".

@mtymek

This comment has been minimized.

Show comment
Hide comment
@mtymek

mtymek May 20, 2013

Contributor

Done - please review.

Contributor

mtymek commented May 20, 2013

Done - please review.

@Thinkscape

This comment has been minimized.

Show comment
Hide comment
@Thinkscape

Thinkscape May 22, 2013

Member

Looks clear. Great job :-)

Member

Thinkscape commented May 22, 2013

Looks clear. Great job :-)

@@ -68,19 +50,14 @@ class Simple implements RouteInterface
protected $assembledParams = array();
/**
* @var \Zend\Validator\ValidatorChain
* @var \Zend\Console\RouteMatcher\DefaultRouteMatcher

This comment has been minimized.

@weierophinney

weierophinney May 22, 2013

Member

Shouldn't this be RouteMatcherInterface instead of DefaultRouteMatcher?

@weierophinney

weierophinney May 22, 2013

Member

Shouldn't this be RouteMatcherInterface instead of DefaultRouteMatcher?

@ghost ghost assigned weierophinney May 22, 2013

weierophinney added a commit that referenced this pull request May 22, 2013

weierophinney added a commit that referenced this pull request May 22, 2013

[#4449] docblock fixes
- Ensured docblock references were correct

weierophinney added a commit that referenced this pull request May 22, 2013

@weierophinney weierophinney merged commit 4421715 into zendframework:develop May 22, 2013

1 check passed

default The Travis CI build passed
Details

weierophinney added a commit to zendframework/zend-console that referenced this pull request May 15, 2015

Merge pull request zendframework/zendframework#4449 from mtymek/featu…
…re/separate_console_route_matcher

Console route improvements

weierophinney added a commit to zendframework/zend-console that referenced this pull request May 15, 2015

[zendframework/zendframework#4449] docblock fixes
- Ensured docblock references were correct

weierophinney added a commit to zendframework/zend-console that referenced this pull request May 15, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment