Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

X-HTTP-Method-Override listener #81

Closed
wants to merge 14 commits into from

Conversation

Wilt
Copy link
Contributor

@Wilt Wilt commented Aug 23, 2016

X-HTTP-Method-Override listener feature.
For this issue: zfcampus/zf-apigility#175 (comment)


$result = $listener($event);
$this->assertEquals($method, $request->getMethod());

Copy link
Member

Choose a reason for hiding this comment

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

Please remove empty line above.

if (!in_array($method, $this->methods)) {
return new ApiProblemResponse(new ApiProblem(
400,
'unrecognized method in X-HTTP-Method-Ovverride header'
Copy link
Member

Choose a reason for hiding this comment

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

Please change to uppercase U in unrecognized word

@Wilt
Copy link
Contributor Author

Wilt commented Aug 23, 2016

PR for this issue: zfcampus/zf-apigility#175 (comment)

I think enabling this listener should be optional, feels not good to connect such a feature by default to the application so I connected the listener depending on a x_http_method_override_enabled key in the config.

Not sure exactly on what is a valid priority value for this listener. I would say as early as possible, currently set to -40 to be triggered before all other zf-campus onRoute listeners, but might earlier might be even better. I added a TODO to check this value...

if (! in_array($method, $this->methods)) {
return new ApiProblemResponse(new ApiProblem(
400,
'unrecognized method in X-HTTP-Method-Ovverride header'
Copy link
Member

Choose a reason for hiding this comment

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

U in unrecognized please

Wilt added 2 commits August 24, 2016 09:19
Attached listener depending on `x_http_method_override_enabled`
Added $xHttpMethodOverrideEnabled to ContentNegotiationOptions (defaults to false);
@Wilt
Copy link
Contributor Author

Wilt commented Aug 24, 2016

Updated the PR:

  • Changed all according to @webimpress his comments (thanks)
  • Made HttpMethodOverrideListener extend AbstractListenerAggregate
  • Attached listener depending on x_http_method_override_enabled
  • Added $xHttpMethodOverrideEnabled to ContentNegotiationOptions (defaults to false);
  • Added and updated unit tests

@snapshotpl
Copy link
Contributor

@Wilt there is already existing module https://github.com/rstgroup/http-method-override

@Wilt
Copy link
Contributor Author

Wilt commented Aug 24, 2016

@snapshotpl OK, thanks.
If there is no interest to implement this as part of Apigility/zf-content-negotiation, then someone can just close and I will delete my PR.

<?php
/**
* @license http://opensource.org/licenses/BSD-3-Clause BSD-3-Clause
* @copyright Copyright (c) 2014-2016 Zend Technologies USA Inc. (http://www.zend.com)
Copy link
Member

Choose a reason for hiding this comment

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

Please use only 2016 year in new files.

@michalbundyra
Copy link
Member

@Wilt I like it, I think will be nice to have this functionality out-of-the-box. 👍

Wilt added 2 commits August 24, 2016 14:41
removed trailing comma
removed space
@Wilt
Copy link
Contributor Author

Wilt commented Aug 24, 2016

@webimpress Thanks for the feedback.

I changed to prophecy. I don't like the fact that the get method and getHttpOverrideMethods in my test aren't recognized. Is there a workaround?

HttpRequest::METHOD_PATCH
],
HttpRequest::METHOD_POST => [
]
Copy link
Member

Choose a reason for hiding this comment

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

please add trailing comma

@michalbundyra
Copy link
Member

@Wilt do you mean your IDE doesn't recognize these methods?
You can always add comment /** @var YourClass $variable */ before variable, and if you are using PHPStorm it will be recognized properly.
But then it's not 100% true, because prophecy object is ProphecyInterface, so maybe better if your comment will be:
/** @var YourClass|ProphecyInterface $variable */

added php doc
@jdukleth
Copy link

Looks great. I think this should be baked into Apigility.

@weierophinney weierophinney added this to the 1.3.0 milestone Oct 11, 2016
Copy link
Member

@weierophinney weierophinney left a comment

Choose a reason for hiding this comment

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

This is solid - thanks, @Wilt !


/**
* Priority is set very high (should be executed before all other listeners that rely on the request method value).
* TODO: Check priority value, maybe value should be even higher??
Copy link
Member

Choose a reason for hiding this comment

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

This is an appropriate value. The OPTIONS handling (i.e., HTTP method negotiation) happens at -100. Currently, we already have versioning (for Accept headers) happening at -40 (route-based version checking happens at -41). The main thing is that it happens before the OPTIONS handling, so this works fine.

@@ -48,6 +48,11 @@ public function onBootstrap(MvcEvent $e)
$services->get(AcceptFilterListener::class)->attach($eventManager);
$services->get(ContentTypeFilterListener::class)->attach($eventManager);

$contentNegotiationOptions = $services->get(ContentNegotiationOptions::class);
if ($contentNegotiationOptions->getXHttpMethodOverrideEnabled()) {
$services->get(HttpMethodOverrideListener::class)->attach($eventManager);
Copy link
Member

Choose a reason for hiding this comment

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

I very much like this conditional attachment.

@weierophinney weierophinney self-assigned this Oct 11, 2016
weierophinney added a commit that referenced this pull request Oct 11, 2016
weierophinney added a commit that referenced this pull request Oct 11, 2016
weierophinney added a commit that referenced this pull request Oct 11, 2016
@weierophinney
Copy link
Member

Thanks, @Wilt!

@Wilt
Copy link
Contributor Author

Wilt commented Oct 27, 2016

You are very welcome!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants