Skip to content

Flexible discovery #59

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

Merged
merged 1 commit into from
Jun 25, 2016
Merged

Flexible discovery #59

merged 1 commit into from
Jun 25, 2016

Conversation

Nyholm
Copy link
Member

@Nyholm Nyholm commented Jun 12, 2016

I was not too happy about #58. We treat Puli in a special manner which feels weird. Also, I was not too happy about the logic in the fallback strategies.

This PR continues the work and replaces #58. The user has now the possibility to select what strategies to use and also add some more custom if there is a need for it.

Changes

  • Introduced Stratiegies that do the discovery. We try the strategies one by one until we find what we're looking for
  • many public functions are moved from ClassDiscovery to Strategy\Puli.
  • I've also created some new exceptions.
  • Added a simple cache mechanism
  • When Puli is missing we will not throw a \RuntimeException anymore. There will be a NotFoundException which has previous exception so you can see that Puli was missing.
  • The NotFoundException is moved from Http\Discovery to Http\Discovery\Exception

TODO

This is just a proof of concept. Do we like to go this way?

  • Add tests
  • Update documentation

@Nyholm Nyholm force-pushed the flexible-discovery branch 2 times, most recently from a5b302c to 96a650b Compare June 12, 2016 17:05
$exceptions = [];
foreach (self::$strategies as $strategy) {
try {
$bindings = call_user_func($strategy.'::find', $type);
Copy link
Member Author

Choose a reason for hiding this comment

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

Is "binding" the best name here?

Copy link
Member

Choose a reason for hiding this comment

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

Puli terminology

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, but Im hesitant over using "binding" since this is not in the Puli scope. I'll use "binding"

@sagikazarmark
Copy link
Member

Huh, nice work. Haven't reviewed yet, but definitely will.

About strategies: not sure if we want discoveries to be made configurable. It's like creating a custom configuration layer which we don't want to.

I think the idea in the linked ticket was to use puli if available, fallback to our known resources if they are available. If this is what strategies intend to do (with a better abstraction) then it's fine, but we should keep it minimal: not too much extension points, etc

@Nyholm
Copy link
Member Author

Nyholm commented Jun 13, 2016

Thank you Mark.

I think the idea in the linked ticket was to use puli if available, fallback to our known resources if they are available.

Have a look at #58. It did that exactly. But it felt weird and it did not follow the Open/Close principle. But maybe you are correct, this is too many of extension points.

@sagikazarmark
Copy link
Member

Discovery is for convenience, so I would rather follow KISS.

@@ -47,7 +47,7 @@ function it_throws_an_exception_when_binding_not_found(Discovery $discovery)
{
$discovery->findBindings('InvalidBinding')->willReturn([]);

$this->shouldThrow('Http\Discovery\NotFoundException')->duringFindOneByType('InvalidBinding');
$this->shouldThrow('Http\Discovery\Exception\NotFoundException')->duringFindOneByType('InvalidBinding');
Copy link
Contributor

Choose a reason for hiding this comment

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

should we use NotFoundException::class here?

@dbu
Copy link
Contributor

dbu commented Jun 14, 2016

i like it. its indeed quite a thing you built here, but i think its clearly structured. i think there are too many public methods that are additional extension points we should not need. i think hard-setting the answer for a $type on the discovery itself should be all the configuration we allow. the whole discovery interface and implementations should have @internal annotations and never be interacted with. if you want to interact, have your application/library accept instantiated clients in addition to the discovery.

i also feel a bit unsure about the number of static methods. but as we don't want the discoveries to be configurable anyways, it should not matter.

@Nyholm
Copy link
Member Author

Nyholm commented Jun 14, 2016

FYI, If Puli is not installed AND we can't find a client:

For a client:
image

For a message factory:
image

@dbu
Copy link
Contributor

dbu commented Jun 15, 2016

i like this! the implementation looks good to me.

['class' => Curl::class, 'condition' => Curl::class],
['class' => Socket::class, 'condition' => Socket::class],
['class' => Buzz::class, 'condition' => Buzz::class],
['class' => React::class, 'condition' => React::class],
Copy link
Member

Choose a reason for hiding this comment

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

Not sure if this work when you have only one HttpClient and not the other since you will only have one class, the other will not be found and this will throw an autoloading exception.

Copy link
Member Author

Choose a reason for hiding this comment

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

That is the beauty of Foo::class, it doesn't throw an exception if the class not exists. =)

@dbu
Copy link
Contributor

dbu commented Jun 15, 2016

@Nyholm can you check the build failures please?

@Nyholm
Copy link
Member Author

Nyholm commented Jun 15, 2016

Yes of course. I started to fix the tests this morning but did not have time to finish.

@dbu
Copy link
Contributor

dbu commented Jun 16, 2016

sure, no hurry. just wanted to be sure we understand who is waiting on whom ;-)

@Nyholm
Copy link
Member Author

Nyholm commented Jun 16, 2016

Yooho. Tests are passing. I added a fix for the composer/puli thing.

Please feel free to make suggestions on the tests. PHPSpec is not my strongest suit.

{
self::$classes = [];
}
}
Copy link
Member

Choose a reason for hiding this comment

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

Missing newline

@sagikazarmark
Copy link
Member

Let me see if I understand well: we have x strategies which all return some resources, which we cache and then use this cache to find the actual resource?

@Nyholm
Copy link
Member Author

Nyholm commented Jun 24, 2016

Thank you for your review.

We work with a strategy at the time, if a strategy return a "candidate" were its dependencies are met we have found a class and we will return without investigating further strategies. This candidate is saved to the cache. The cache is only read from the second time we ask for the same resource.

The great thing about the strategies is that we can easily support Puli2 or WhatveverDiscovery without editing any existing class.

*/
private static $puliFactory;
public static $strategies = [
Copy link
Member

Choose a reason for hiding this comment

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

Do we need to configure the strategies in the abstract class? Can't we do it on a per discovery basis?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we are using strategies that we know would fail. Example; the HttpClients strategy will always fail for the FactoryDiscovery. I'll fix this by merging the strategies suggested bellow.

@sagikazarmark
Copy link
Member

This is cool, I like it, great job @Nyholm.

I've added a few more comments besides the CS ones from earlier. Can you please take a look at them and at least answer them? If you have no time, I can do the improvements I suggested, but I need your comment to make sure I understood everything right. Thank you!

@Nyholm
Copy link
Member Author

Nyholm commented Jun 24, 2016

Thank you. I've address all your comments, either with a reply or a code change (or both).

@sagikazarmark
Copy link
Member

This is still an open question from my POV: #59 (comment)

@sagikazarmark
Copy link
Member

Great, I am happy with the current state. Can you please add some change log and squash?

@Nyholm Nyholm force-pushed the flexible-discovery branch from 7ca6695 to 1c37482 Compare June 25, 2016 09:07
@Nyholm
Copy link
Member Author

Nyholm commented Jun 25, 2016

Awesome. I'll must go afk now. Please feel free to make some changes or merge this when the tests passes.

Thank you for the reviews!

@sagikazarmark sagikazarmark merged commit ff4c775 into master Jun 25, 2016
@sagikazarmark sagikazarmark deleted the flexible-discovery branch June 25, 2016 10:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants