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

Create transport from class which don't has prefix name Elastica\Transport #1169

Merged

Conversation

Gasol
Copy link
Contributor

@Gasol Gasol commented Aug 31, 2016

Allow we store configuration in JSON format, and simply restore to in-memory array and pass to Elastica\Client.

…sport

Allow we store configuration in json format, and simply restore to
in-memory array and pass to Elastica\Client.
@ruflin
Copy link
Owner

ruflin commented Aug 31, 2016

I don't fully understand this change yet. Can you share some more details and example on this?

@Gasol
Copy link
Contributor Author

Gasol commented Aug 31, 2016

Assume we define configuration of Elastica in JSON format.

// config.json

{
    "servers": [
        {
            "host": "elasticsearch-1",
            "port": "9200",
            "transport": "ACME\Transport\Stream"
        }
    ],
    "timeout": "1s",
    "connectionStrategy": "RoundRobin"
}

We can simply call json_decode to get configuration in array structure, then pass it to constructor of Elastica\Client. If transport in configuration does not pre-defined in Elastica\Transport\AbstractTransport, We will get exception shows Invalid transportwithout this change.

For example:

<?php
$data = file_get_contents('/path/to/config.json');
$config = json_decode($data, true);
$client = new Elastica\Client($config);


if (!class_exists($className)) {
throw new InvalidException('Invalid transport');
$classNames = ["Elastica\\Transport\\$transport", $transport];
Copy link
Owner

Choose a reason for hiding this comment

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

I'm kind of thinking if we should turn this around. Have the base assumption that $transport is the full class name, and only have as fall back the Elastica once. I would have to check where in our code we would have to adjust these paths.

Copy link
Contributor Author

@Gasol Gasol Sep 6, 2016

Choose a reason for hiding this comment

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

Maybe we could check whether namespace separator () exists or not? It means it could be FQCN or built-in transport of Elastica right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check namespace seperator is bad idea, We should not assume that class has a namespace.

@ruflin
Copy link
Owner

ruflin commented Sep 2, 2016

Nice, I was never thinking of this use case. I left some minor comments in the PR. Could you also update the CHANGELOG.md?

@Gasol
Copy link
Contributor Author

Gasol commented Sep 6, 2016

@ruflin I have updated this PR for adding changelog.

@ruflin ruflin merged commit 72cafba into ruflin:master Sep 6, 2016
@ruflin
Copy link
Owner

ruflin commented Sep 6, 2016

@Gasol Thanks, merged.

@Gasol Gasol deleted the creating-transport-in-whatever-namespace branch September 7, 2016 03:00
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.

2 participants