Skip to content
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

3.0 middleware events #3495

Merged
merged 24 commits into from May 16, 2014
Merged

3.0 middleware events #3495

merged 24 commits into from May 16, 2014

Conversation

markstory
Copy link
Member

This set of changes updates dispatcher filters. I explored the 'more robust' middleware implementation described in #2544 and decided it was a bad idea for a few reasons:

  • The end result would be totally unlike everything else in CakePHP. The wrapping middleware approach while similar to other frameworks/tools would be totally unique in CakePHP.
  • Event based middleware works well in symfony, and has worked well for us thus far.
  • I couldn't come up with a use case that couldn't be implemented with the existing event based middleware.

So instead of throwing out the existing code, I've done a few incremental changes, mostly around making it easy for filters to only run on specific URL's or when specific conditions are met. I've also introduced what I think is a simpler/better way to manage filters with the DispatcherFactory.

I have a few todo's left, but I wanted to get some feedback on this before finishing everything.

TODO

  • Missing doc blocks and file headers.
  • Clean up history.

@markstory markstory added this to the 3.0.0 milestone May 14, 2014
* example, if you only wanted a filter applied to blog requests you could do:
*
* {{{
* $ware = new BlogMiddleware(['for' => '/blog']);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is routes connected yet? e.g. can 'for' be an ['controller' => 'something'] ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wasn't the suffix Filter instead of Middleware?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jippi that can be achieved with when

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bah, I will fix this when I complete the doc block changes.

@lorenzo
Copy link
Member

lorenzo commented May 14, 2014

looking awesome!

The renames to middleware still need to happen, but my gut feeling is
that there isn't anything seriously broken with how filters work right
now beyond the configration bits.
Default to the property (which is now protected) but allow users of
a filter to redefine the priority.
Clean up the remaining dispatcher tests and try to cover the bulk of the
cases.
Like many other places in CakePHP we should allow string classnames for
filters, and let the framework handle construction for simple cases.
The additionalParams parameter isn't very useful as it simply gets
merged into the request later on. We can easily do that earlier in the
process now.

Fix incorrectlty dispatched array based requestAction() calls. They were
going back through Router::parse() when they did not need to.
Add in knowledge of dispatcher filters. Also workaround Routing being
applied in a filter vs. a Dispatcher method so that the rest of
ControllerTestCase can continue to work.
Use a Filter suffix for dispatcher filters. I think this will make them
easier to recognize at a glance and mirrors other classname conventions
in CakePHP.
The feature is no longer called middleware, instead continue using
dispatcher filters. We can't really call this feature middleware as it
acts very differently from middleware in other frameworks.
When using the string form, it is handy to be able to control the
constructor arguments as well.
Add some detail around how priorities are handled.
@markstory
Copy link
Member Author

@lorenzo I've added the ControllerFactoryFilter.

public function handle(Event $event) {
$name = $event->name();
list($unused, $method) = explode('.', $name);
if (empty($this->_config['for']) && empty($this->_config['when'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@markstory Shouldn't this condition be part of matches? Seems it can be an earlier return as true.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could, I was prematurely optimizing away the method call to matches().

markstory and others added 4 commits May 15, 2014 23:47
Having a separate filter that handles controller resolution provides
a few nice things:

* It makes it easy for applications to extend/replace the controller
  conventions.
* It makes dispatcher super simple internally.
* It makes CakePHP less magic.

No unit tests were added as the existing Dispatcher test cases cover all
the functionality in the ControllerFactoryFilter.

Fix up the ControllerTestCase as it can no longer override the
Dispatcher internals to do its work. Instead a special filter is applied
that will replace the controller with a mock if necessary.
Spooky side-effects at a distance on object construction is a no-no.
This allows cached/asset requests to not hit the router.
@lorenzo
Copy link
Member

lorenzo commented May 16, 2014

Awesome work @markstory !

lorenzo added a commit that referenced this pull request May 16, 2014
@lorenzo lorenzo merged commit 27d2a69 into cakephp:3.0 May 16, 2014
@markstory markstory deleted the 3.0-middleware-events branch May 16, 2014 13:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants