Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
2.4 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;
        $multi = true;

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

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

            foreach ($clusters as $cluster) {
                $endpoint=[
                    'cirkwi' => [
                        'host' => $cluster["ip"],
                        'port' => $cluster["port"],
                        'path' => '',
                        'core' => 'objet_modulesbox',
                    ],
                ];

                $handle = $this->createHandle($request, new Endpoint($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));
            curl_close($handle);
            return $response;
        }
    }
}
Leave a Comment