- PHP Client: other versions:
- Overview
- Quickstart
- Installation
- Configuration
- Inline Host Configuration
- Extended Host Configuration
- Authorization and Encryption
- Set retries
- Enabling the Logger
- Configure the HTTP Handler
- Setting the Connection Pool
- Setting the Connection Selector
- Setting the Serializer
- Setting a custom ConnectionFactory
- Set the Endpoint closure
- Building the client from a configuration hash
- Per-request configuration
- Future Mode
- Dealing with JSON Arrays and Objects in PHP
- Index Management Operations
- Indexing Documents
- Getting Documents
- Updating Documents
- Deleting documents
- Search Operations
- Namespaces
- Security
- Connection Pool
- Selectors
- Serializers
- PHP Version Requirement
- Breaking changes from 5.x
- Community DSLs
- Community Integrations
- Reference - Endpoints
- Elasticsearch\Client
- Elasticsearch\ClientBuilder
- Elasticsearch\Namespaces\CatNamespace
- Elasticsearch\Namespaces\ClusterNamespace
- Elasticsearch\Namespaces\IndicesNamespace
- Elasticsearch\Namespaces\IngestNamespace
- Elasticsearch\Namespaces\NodesNamespace
- Elasticsearch\Namespaces\RemoteNamespace
- Elasticsearch\Namespaces\SnapshotNamespace
- Elasticsearch\Namespaces\TasksNamespace
Setting a custom ConnectionFactory
editSetting a custom ConnectionFactory
editThe ConnectionFactory instantiates new Connection objects when requested by the ConnectionPool. A single Connection represents a single node. Since the client hands actual networking work over to RingPHP, the Connection’s main job is book-keeping: Is this node alive? Did it fail a ping request? What is the host and port?
There is little reason to provide your own ConnectionFactory, but if you need to do so, you need to supply an intact
ConnectionFactory object to the setConnectionFactory()
method. The object should implement the ConnectionFactoryInterface
interface.
class MyConnectionFactory implements ConnectionFactoryInterface { public function __construct($handler, array $connectionParams, SerializerInterface $serializer, LoggerInterface $logger, LoggerInterface $tracer) { // Code here } /** * @param $hostDetails * * @return ConnectionInterface */ public function create($hostDetails) { // Code here...must return a Connection object } } $connectionFactory = new MyConnectionFactory( $handler, $connectionParams, $serializer, $logger, $tracer ); $client = ClientBuilder::create() ->setConnectionFactory($connectionFactory); ->build();
As you can see, if you decide to inject your own ConnectionFactory, you take over the responsibiltiy of wiring it correctly. The ConnectionFactory requires a working HTTP handler, serializer, logger and tracer.