-
-
Notifications
You must be signed in to change notification settings - Fork 907
Generator compat improvements #1429
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
Generator compat improvements #1429
Conversation
return $dataProvider->getCollection($resourceClass, $operationName); | ||
} catch (ResourceClassNotSupportedException $e) { | ||
@trigger_error(sprintf('Throwing a "%s" is deprecated in favor of implementing "%s"', get_class($e), RestrictedDataProviderInterface::class), E_USER_DEPRECATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Following the sf style (or lack thereof?) here... yuck!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not ResourceClassNotSupportedException::class
constant? Also, could you mention "Throwing a "..." in a data provider is deprecated..."?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you just make this change and we'll merge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sure I forgot. Let's see what those constants are
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right I understand now that I see the code in my editor, I just put the wrong class constant. I could have understood this earlier had it been legible on github :P
Fun fact: this line does not even fit on a 1920x1200 display.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed @dunglas
4334e57
to
4703ced
Compare
Looks good to me.
We should do it at least for consistency. |
4703ced
to
00455fc
Compare
@dunglas done, but it's no longer possible not to support something based on |
00455fc
to
6b83e0c
Compare
Shouldn't the builtin Doctrine providers implement this new interface? |
@dunglas they should, and the build should probably not pass because of the deprecations, should it? something seems to be wrong... |
Oh wait no it's normal, they are not called when testing the chain providers |
6b83e0c
to
abcad30
Compare
/** | ||
* @expectedException \ApiPlatform\Core\Exception\ResourceClassNotSupportedException | ||
*/ | ||
public function testThrowResourceClassNotSupportedException() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we keep this test anyway to ensure backward compatibility?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind I saw it in the chain!
It's not really a bug fix or is it? I think it should target master. Anyway it's a nice improvement! |
I should probably call |
What "other methods" do you had in mind? The chain provider continues when an exception is catched so this looks fine! |
@soyuka |
Nope, the service exposed is the ChainDataProvider:
|
Doesn't prevent people from just instanciating the class and misusing it, but I'm not sure you require that level of defensiveness |
Maybe we can consider flagging the doctrine data providers as Internal then. Also, they may be final (I see no reason to extend them). In the docs the recommended way is to implement the DataProviderInterface. |
Final would be a BC break though |
@sroze @julienfalque @soyuka can we have your stance on this? We were wondering if there wouldn't be a better way to add support for generators, without deprecating anything / introducing a new interface. Technically, right now, you can achieve that by writing something like this: <?php
public function getCollection()
{
// conditionally throw exception here, just like before
return $this->getGenerator();
}
private function getGenerator();
{
yield new Item;
} But this feels hackish to me, I don't think we should recommend that. |
I like the clean way you added in this PR and I won't mind a deprecation on the subject especially if we remove an exception (try/catching is slow) |
ping @api-platform/core-team |
@dunglas do you consider this as a bug fix rather then a new feature? |
It introduces deprecation so it should be merged in master. |
You cannot throw and return.
abcad30
to
ad1f93b
Compare
@dunglas rebased |
As well as the docs PR |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great feature!
ad1f93b
to
18c37ff
Compare
Tests fails though: -%A Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" is deprecated in favor of implementing "ApiPlatform\Core\DataProvider\RestrictedDataProviderInterface"
+ Throwing a "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" in a data provider is deprecated in favor of implementing "ApiPlatform\Core\Exception\ResourceClassNotSupportedException" |
This should make the collection data provider generator-friendly, because it will allow deferring code execution to the piece of code where the generator is iterated over instead of requiring it to be executed inside the try/catch block. Fixes api-platform#1422
18c37ff
to
cecc022
Compare
return $dataProvider->getCollection($resourceClass, $operationName); | ||
} catch (ResourceClassNotSupportedException $e) { | ||
@trigger_error(sprintf('Throwing a "%s" in a data provider is deprecated in favor of implementing "%s"', ResourceClassNotSupportedException::class, RestrictedDataProviderInterface::class), E_USER_DEPRECATED); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@soyuka I misunderstood your comment at first. I hope you all see how easier it would have been had each argument been on its own line: you could have commented directly under get_class($e)
. BTW, the sf standard is to have the message on one line, but is the sprintf
part of the message? If yes, that's really a stupid rule who just made us loose precious time. It just makes life harder for everyone for no good reason.
Thanks @greg0ire! |
* GraphQL Query support (api-platform#1358) * Fix missing service when no Varnish URL is defined * [PropertyFilter] Fix whitelist comparison (api-platform#1379) * Remove wrong doc * Swagger subcollection documentation issue (api-platform#1395) * Make the requirements configurable Closes api-platform#1401 * Provide a better exception message and type Getting "Not found" on a route where you are getting an object can get really confusing. * Bump branch alias to 2.2.x-dev 2.1.x-dev is already taken by the 2.1 branch, and this should represent the next minor version anyway. * Fix tests * Reuse PriorityTaggedServiceTrait from symfony * Improve payload support and remove duplicate code in ConstraintViolationListNormalizer (api-platform#1416) * Filter Annotation implementation Parent class filters (needs test) Support for nested properties Tests Fix some comments and remove id=>id in compiler pass * Throw on abstract data providers / filters * Remove an unused var * Remove useless badges * Enable the coverage * Fix some quality issues * Add job to test upstream libs deprecations If api-platform uses upstream libs in a deprecated way, we should be aware of it. * Add job to test upstream libs deprecations If api-platform uses upstream libs in a deprecated way, we should be aware of it. * Fix missing cache tag on empty collections * Allow plain IDs with `allow_plain_identifiers` * Fix indentation for GraphQL features. * Add JSON API basic support (api-platform#785, api-platform#1036, api-platform#1175) * Clean Behat tests * Fix tags addition with an empty value * Document swagger-specific description options … and where to find them should there be newer ones. * Fix PHPUnit tests * Fix missing return statement * Support & compatibility for PHP7.2 * Add feature to update swagger context for properties * Generator compat improvements (api-platform#1429) * Add support for resource names without namespace * [SF 4.0] Make actions explicitly public * Allow phpdocumentor/reflection-docblock 4 * fix hydra documentation normalizer with subresources * Create a base collection normalizer * Fix request auto-runner * Update the changelog * Update changelog
This PR should enable people to use generators when implementing
DataCollectionProviderInterface
TODO