Skip to content

Commit

Permalink
Merge 78c2b1f into 9064f8f
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kalkbrenner committed Feb 4, 2019
2 parents 9064f8f + 78c2b1f commit 8556b2c
Show file tree
Hide file tree
Showing 25 changed files with 354 additions and 114 deletions.
12 changes: 12 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,6 +4,18 @@ All notable changes to the solarium library will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [4.3.0-alpha.2]
### Added
- Basic V2 API support
- Endpoint::getV2BaseUri

### Changed
- AdapterHelper functions are static

### Fixed
- In the past, the V1 API endpoint `solr` was not added automatically, so most users set it as path on the endpoint. This bug was discovered with the addition of V2 API support. In almost every setup, the path has to be set to `/` instead of `/solr` with this release!


## [4.3.0-alpha.1]
### Added
- Experimental support for collection API
Expand Down
15 changes: 14 additions & 1 deletion src/Core/Client/Adapter/AdapterHelper.php
Expand Up @@ -2,21 +2,34 @@

namespace Solarium\Core\Client\Adapter;

use Solarium\Core\Client\Endpoint;
use Solarium\Core\Client\Request;

/**
* Helper class for shared adapter functionality.
*/
class AdapterHelper
{
public static function buildUri(Request $request, Endpoint $endpoint): string
{
if (Request::API_V2 == $request->getApi()) {
$baseUri = $endpoint->getV2BaseUri();
} elseif ($request->getIsServerRequest()) {
$baseUri = $endpoint->getV1BaseUri();
} else {
$baseUri = $endpoint->getBaseUri();
}
return $baseUri.$request->getUri();
}

/**
* This method is used to build the upload body for a file upload with the boundary markers.
*
* @param Request $request
*
* @return string
*/
public function buildUploadBodyFromRequest(Request $request)
public static function buildUploadBodyFromRequest(Request $request): string
{
$baseName = basename($request->getFileUpload());
$body = "--{$request->getHash()}\r\n";
Expand Down
9 changes: 3 additions & 6 deletions src/Core/Client/Adapter/Curl.php
Expand Up @@ -72,8 +72,7 @@ public function getResponse($handle, $httpResponse)
public function createHandle($request, $endpoint)
{
// @codeCoverageIgnoreStart
$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getBaseUri();
$uri = $baseUri.$request->getUri();
$uri = AdapterHelper::buildUri($request, $endpoint);

$method = $request->getMethod();
$options = $this->createOptions($request, $endpoint);
Expand Down Expand Up @@ -122,8 +121,7 @@ public function createHandle($request, $endpoint)
curl_setopt($handler, CURLOPT_POST, true);

if ($request->getFileUpload()) {
$helper = new AdapterHelper();
$data = $helper->buildUploadBodyFromRequest($request);
$data = AdapterHelper::buildUploadBodyFromRequest($request);
curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData());
Expand All @@ -138,8 +136,7 @@ public function createHandle($request, $endpoint)
curl_setopt($handler, CURLOPT_CUSTOMREQUEST, 'PUT');

if ($request->getFileUpload()) {
$helper = new AdapterHelper();
$data = $helper->buildUploadBodyFromRequest($request);
$data = AdapterHelper::buildUploadBodyFromRequest($request);
curl_setopt($handler, CURLOPT_POSTFIELDS, $data);
} else {
curl_setopt($handler, CURLOPT_POSTFIELDS, $request->getRawData());
Expand Down
6 changes: 2 additions & 4 deletions src/Core/Client/Adapter/Guzzle.php
Expand Up @@ -56,8 +56,7 @@ public function execute($request, $endpoint)
}

try {
$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getBaseUri();
$uri = $baseUri.$request->getUri();
$uri = AdapterHelper::buildUri($request, $endpoint);

$guzzleResponse = $this->getGuzzleClient()->request(
$request->getMethod(),
Expand Down Expand Up @@ -113,8 +112,7 @@ private function getRequestBody(Request $request)
}

if ($request->getFileUpload()) {
$helper = new AdapterHelper();
return $helper->buildUploadBodyFromRequest($request);
return AdapterHelper::buildUploadBodyFromRequest($request);
}

return $request->getRawData();
Expand Down
6 changes: 2 additions & 4 deletions src/Core/Client/Adapter/Guzzle3.php
Expand Up @@ -31,8 +31,7 @@ class Guzzle3 extends Configurable implements AdapterInterface
*/
public function execute($request, $endpoint)
{
$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getBaseUri();
$uri = $baseUri.$request->getUri();
$uri = AdapterHelper::buildUri($request, $endpoint);
$guzzleRequest = $this->getGuzzleClient()->createRequest(
$request->getMethod(),
$uri,
Expand Down Expand Up @@ -107,8 +106,7 @@ private function getRequestBody(Request $request)
}

if ($request->getFileUpload()) {
$helper = new AdapterHelper();
$body = $helper->buildUploadBodyFromRequest($request);
$body = AdapterHelper::buildUploadBodyFromRequest($request);
return $body;
}

Expand Down
6 changes: 2 additions & 4 deletions src/Core/Client/Adapter/Http.php
Expand Up @@ -27,8 +27,7 @@ class Http extends Configurable implements AdapterInterface
public function execute($request, $endpoint)
{
$context = $this->createContext($request, $endpoint);
$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getBaseUri();
$uri = $baseUri.$request->getUri();
$uri = AdapterHelper::buildUri($request, $endpoint);

list($data, $headers) = $this->getData($uri, $context);

Expand Down Expand Up @@ -88,8 +87,7 @@ public function createContext($request, $endpoint)

if (Request::METHOD_POST == $method) {
if ($request->getFileUpload()) {
$helper = new AdapterHelper();
$data = $helper->buildUploadBodyFromRequest($request);
$data = AdapterHelper::buildUploadBodyFromRequest($request);

$content_length = strlen($data);
$request->addHeader("Content-Length: $content_length\r\n");
Expand Down
6 changes: 2 additions & 4 deletions src/Core/Client/Adapter/Zend2Http.php
Expand Up @@ -167,8 +167,7 @@ public function execute($request, $endpoint)
break;
}

$baseUri = $request->getIsServerRequest() ? $endpoint->getServerUri() : $endpoint->getBaseUri();
$uri = $baseUri.$request->getUri();
$uri = AdapterHelper::buildUri($request, $endpoint);

$client->setUri($uri);
$client->setHeaders($request->getHeaders());
Expand Down Expand Up @@ -222,8 +221,7 @@ protected function prepareResponse($request, $response)
*/
protected function prepareFileUpload($client, $request)
{
$helper = new AdapterHelper();
$data = $helper->buildUploadBodyFromRequest($request);
$data = AdapterHelper::buildUploadBodyFromRequest($request);
$client->setRawBody($data);
}
}
18 changes: 18 additions & 0 deletions src/Core/Client/Client.php
Expand Up @@ -117,6 +117,11 @@ class Client extends Configurable implements ClientInterface
*/
const QUERY_COLLECTIONS = 'collections';

/**
* Querytype API.
*/
const QUERY_API = 'api';

/**
* Querytype managed resource.
*/
Expand Down Expand Up @@ -165,6 +170,7 @@ class Client extends Configurable implements ClientInterface
self::QUERY_REALTIME_GET => 'Solarium\QueryType\RealtimeGet\Query',
self::QUERY_CORE_ADMIN => 'Solarium\QueryType\Server\CoreAdmin\Query\Query',
self::QUERY_COLLECTIONS => 'Solarium\QueryType\Server\Collections\Query\Query',
self::QUERY_API => 'Solarium\QueryType\Server\Api\Query',
self::QUERY_MANAGED_RESOURCES => 'Solarium\QueryType\ManagedResources\Query\Resources',
self::QUERY_MANAGED_STOPWORDS => 'Solarium\QueryType\ManagedResources\Query\Stopwords',
self::QUERY_MANAGED_SYNONYMS => 'Solarium\QueryType\ManagedResources\Query\Synonyms',
Expand Down Expand Up @@ -1317,6 +1323,18 @@ public function createCollections($options = null)
return $this->createQuery(self::QUERY_COLLECTIONS, $options);
}

/**
* Create an API query instance.
*
* @param mixed $options
*
* @return \Solarium\Core\Query\AbstractQuery|\Solarium\QueryType\Server\Api\Query
*/
public function createApi($options = null)
{
return $this->createQuery(self::QUERY_API, $options);
}

/**
* Initialization hook.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/Core/Client/ClientInterface.php
Expand Up @@ -632,4 +632,11 @@ public function createCoreAdmin($options = null);
* @return \Solarium\QueryType\Server\Collections\Query\Query
*/
public function createCollections($options = null);

/**
* @param mixed $options
*
* @return \Solarium\QueryType\Server\Api\Query
*/
public function createApi($options = null);
}
37 changes: 31 additions & 6 deletions src/Core/Client/Endpoint.php
Expand Up @@ -22,7 +22,7 @@ class Endpoint extends Configurable
'scheme' => 'http',
'host' => '127.0.0.1',
'port' => 8983,
'path' => '/solr',
'path' => '/',
'collection' => null,
'core' => null,
'timeout' => 5,
Expand Down Expand Up @@ -235,7 +235,7 @@ public function getScheme(): string
}

/**
* Get the base url for all SolrCloud requests.
* Get the V1 base url for all SolrCloud requests.
*
* Based on host, path, port and collection options.
*
Expand All @@ -249,7 +249,7 @@ public function getCollectionBaseUri(): string
$collection = $this->getCollection();

if ($collection) {
$uri .= $collection.'/';
$uri .= 'solr/'.$collection.'/';
} else {
throw new UnexpectedValueException('No collection set.');
}
Expand All @@ -258,7 +258,7 @@ public function getCollectionBaseUri(): string
}

/**
* Get the base url for all requests.
* Get the V1 base url for all requests.
*
* Based on host, path, port and core options.
*
Expand All @@ -272,7 +272,8 @@ public function getCoreBaseUri(): string
$core = $this->getCore();

if ($core) {
$uri .= $core.'/';
// V1 API
$uri .= 'solr/'.$core.'/';
} else {
throw new UnexpectedValueException('No core set.');
}
Expand All @@ -281,7 +282,7 @@ public function getCoreBaseUri(): string
}

/**
* Get the base url for all requests.
* Get the base url for all V1 API requests.
*
* @return string
*
Expand All @@ -300,6 +301,30 @@ public function getBaseUri(): string
}
}

/**
* Get the base url for all V1 API requests.
*
* @return string
*
* @throws UnexpectedValueException
*/
public function getV1BaseUri(): string
{
return $this->getServerUri().'solr/';
}

/**
* Get the base url for all V2 API requests.
*
* @return string
*
* @throws UnexpectedValueException
*/
public function getV2BaseUri(): string
{
return $this->getServerUri().'api/';
}

/**
* Get the server uri, required for non core/collection specific requests.
*
Expand Down
37 changes: 36 additions & 1 deletion src/Core/Client/Request.php
Expand Up @@ -39,13 +39,24 @@ class Request extends Configurable implements RequestParamsInterface
*/
const METHOD_PUT = 'PUT';

/**
* V1 API.
*/
const API_V1 = 'v1';

/**
* V2 API.
*/
const API_V2 = 'v2';

/**
* Default options.
*
* @var array
*/
protected $options = [
'method' => self::METHOD_GET,
'api' => self::API_V1,
];

/**
Expand Down Expand Up @@ -295,9 +306,11 @@ public function getAuthentication()
*
* @param bool $isServerRequest
*/
public function setIsServerRequest($isServerRequest = false)
public function setIsServerRequest($isServerRequest = false): self
{
$this->setOption('isserverrequest', $isServerRequest);

return $this;
}

/**
Expand All @@ -311,6 +324,28 @@ public function getIsServerRequest(): bool
return $this->getOption('isserverrequest') ?? false;
}

/**
* Set Solr API version.
*
* @param string $api
*/
public function setApi($api): self
{
$this->setOption('api', $api);

return $this;
}

/**
* Returns Solr API version.
*
* @return string
*/
public function getApi(): string
{
return $this->getOption('api');
}

/**
* Initialization hook.
*/
Expand Down

0 comments on commit 8556b2c

Please sign in to comment.