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

Add URI factory (fix #3) #4

Merged
merged 1 commit into from
Jun 5, 2015
Merged

Conversation

ddeboer
Copy link
Contributor

@ddeboer ddeboer commented Jun 5, 2015

Fix #3.

Also refactored a bit to reduce code duplication.

*
* @author David de Boer <david@ddeboer.nl>
*/
abstract class ClassDiscovery implements Discovery
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

@ddeboer ddeboer force-pushed the uri-factory branch 2 times, most recently from 6131fd0 to 3d83557 Compare June 5, 2015 22:23
protected static $classes = [
'guzzle6' => [
'condition' => 'Http\Adapter\Guzzle6HttpAdapter',
'class' => 'Http\Adapter\Guzzle6HttpAdapter'
Copy link
Contributor Author

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.

Copy link
Member

Choose a reason for hiding this comment

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

No objections.

@ddeboer
Copy link
Contributor Author

ddeboer commented Jun 5, 2015

Updated.

*/
protected static $classes = [];

/**
Copy link
Member

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? 😃

Copy link
Contributor Author

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)
Copy link
Member

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.

Copy link
Contributor Author

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',

Copy link
Member

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.

Copy link
Contributor Author

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. 😉

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 priority matters either. It is very unlikely you have two adapters/message implementations installed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

True.

Copy link
Member

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
*/
Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

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();
Copy link
Member

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?

Copy link
Contributor Author

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.

Copy link
Member

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. 😃

@ddeboer ddeboer force-pushed the uri-factory branch 2 times, most recently from ceb1673 to 66b123e Compare June 5, 2015 23:02
@ddeboer
Copy link
Contributor Author

ddeboer commented Jun 5, 2015

Okay, should be ready now.

@sagikazarmark
Copy link
Member

Thanks

sagikazarmark added a commit that referenced this pull request Jun 5, 2015
@sagikazarmark sagikazarmark merged commit 033c51d into php-http:master Jun 5, 2015
@ddeboer ddeboer deleted the uri-factory branch June 5, 2015 23:13
@ddeboer
Copy link
Contributor Author

ddeboer commented Jun 5, 2015

You too, for the thorough review. 😄

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.

None yet

2 participants