Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.5 kB
1
Indexable
Never
<?php

namespace Kwap\Api\Solr;

use Solarium\Core\Client\Request;
use Solarium\Core\Client\Endpoint;
use Solarium\Core\Client\Response;
use Solarium\Core\Client\Adapter\Curl;

class CustomAdapter extends Curl
{
    /**
     * Si on doit répliquer les requêtes sur plusieurs serveurs
     * @var bool
     */
    private $multi;

    public function __construct(bool $multi)
    {
        $multi = $multi;
    }

    protected function getData($request, $endpoint): Response
    {
        $method = $request->getMethod();
        // $isWrite = $method === Request::METHOD_POST || $method === Request::METHOD_PUT || $method === Request::METHOD_DELETE;
        $isWrite = true;
        $this->multi = true;

        // A récuperer depuis le .env
        $clusters = [
            [
                'ip' => "solr.solr_default",
                "port" => 8983,
            ],
            [
                'ip' => "solr.solr_default",
                "port" => 8983,
            ]
        ];

        // Si on doit répliquer la requête sur plusieurs serveurs (seulement dans le cas des requêtes POST, PUT et DELETE)
        if ($this->multi && $isWrite && !empty($clusters)) {
            $handles = [];
            $mh = curl_multi_init();

            foreach ($clusters as $cluster) {
                $endpoint = new Endpoint();
                $endpoint->setScheme('http');
                $endpoint->setHost($cluster['ip']);
                $endpoint->setPort($cluster['port']);
                $endpoint->setPath('');
                $endpoint->setCore('objet_modulesbox');
                $endpoint->setKey('cirkwi');

                $handle = $this->createHandle($request, $endpoint);
                curl_multi_add_handle($mh, $handle);

                $handles[] = $handle;
            }

            $running = null;

            do {
                curl_multi_exec($mh, $running);
            } while ($running);

            foreach ($handles as $handle) {
                curl_multi_remove_handle($mh, $handle);
            }

            curl_multi_close($mh);

            $responses = [];

            foreach ($handles as $handle) {
                $responses[] = $this->getResponse($handle, curl_multi_getcontent($handle));
            }

            return $responses[0];
        } else {
            $handle = $this->createHandle($request, $endpoint);
            $response = $this->getResponse($handle, curl_exec($handle));
            return $response;
        }
    }
}
Leave a Comment