-
Notifications
You must be signed in to change notification settings - Fork 47
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
Add URI factory (fix #3) #4
Conversation
* | ||
* @author David de Boer <david@ddeboer.nl> | ||
*/ | ||
abstract class ClassDiscovery implements Discovery |
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.
I would rather call it FactoryDiscover, it tells more about it's purpose IMO.
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.
True, but I thought we could make HttpAdapterDiscovery extend the abstract class too.
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.
This isn't too much code that complexity worths avoiding duplication. The guess condition logic is a bit different there.
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.
I already took that into account in the abstract class: with or without factory. Not much complexity, no, but it’s nice to not duplicate the caching/resetting.
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.
Fine, but in that case the parameters should always be the same. See comment bellow.
6131fd0
to
3d83557
Compare
protected static $classes = [ | ||
'guzzle6' => [ | ||
'condition' => 'Http\Adapter\Guzzle6HttpAdapter', | ||
'class' => 'Http\Adapter\Guzzle6HttpAdapter' |
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.
The class name is duplicated, but I like that it is explicit about that it’s going to return the same class as it finds.
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.
No objections.
Updated. |
*/ | ||
protected static $classes = []; | ||
|
||
/** |
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.
They are not factories anymore, are they? 😃
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.
* @param string $condition | ||
* @param string $class | ||
*/ | ||
public static function register($name, $condition, $class = null) |
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.
I would change the order of arguments: the first is the class, which is always required. The second is the condition. Even in factory guessers, it is acceptable to pass the condition as a second argument. Again, just for the sake of consistency.
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.
So $class, $condition = null
? How would that work for
'condition'=> 'GuzzleHttp\Psr7\Request',
'class' => 'Http\Discovery\MessageFactory\GuzzleFactory',
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.
Yup, that seems more logical to me.
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.
By the way, $name
doesn’t really add much. A priority would be better. But that’s for another 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.
Not sure if priority matters either. It is very unlikely you have two adapters/message implementations installed.
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.
True.
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.
To answer your question: two possible way I can see: make condition required argument or check it in cases when required.
For the long term, i would convert the condition to something that can be evaluated as boolean. So we could accept a callable or boolean as well. In that case, null is obviously false.
* Cached class | ||
* | ||
* @var object | ||
*/ |
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.
Be aware, that in this case tha cache property will be the same for all child classes. You either have to add a cache property to all discovery objects or handle this case in the parent, like saving the cache in an array based on get_called_class.
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.
To be clear: when a static property is declared in the parent, but not the child then setting it in the child will set it in the parent, since there is no context.
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.
Yeah, I overlooked that. Would be a nice bug. 😊 Let’s add it to the children.
} | ||
|
||
throw new NotFoundException('No HTTP Adapter found'); | ||
return parent::find(); |
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.
Are these methods necessary? For the sake of return type?
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.
Necessary, no. But (as you may have noticed elsewhere) I value IDE auto-completion very much. Previously they had a @return object
which stops your auto-complete chain.
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.
Fine. 😃 But in that case, add a comment so that I dont remove it as auto optimalization. 😃
ceb1673
to
66b123e
Compare
Okay, should be ready now. |
Thanks |
You too, for the thorough review. 😄 |
Fix #3.
Also refactored a bit to reduce code duplication.