Skip to content

Commit

Permalink
Merge branch 'master' into issue-626-managed-resources
Browse files Browse the repository at this point in the history
  • Loading branch information
Markus Kalkbrenner authored Oct 31, 2018
2 parents 88aa75e + 53c1239 commit 5be312f
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- Core Admin Queries
- Endpoint::getServerUri
- Endpoint::getCoreBaseUri
- Expression::indent
- Set erroneous expression on StreamException
- Managed resources, stopwords and synonyms query types

### Changed
Expand Down
24 changes: 24 additions & 0 deletions src/Exception/StreamException.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,28 @@
*/
class StreamException extends \UnexpectedValueException implements ExceptionInterface
{
/**
* @var string the streaming expression
*/
protected $expression = '';

/**
* Set the streaming expression that caused the exception.
*
* @param string $expression
*/
public function setExpression(string $expression)
{
$this->expression = $expression;
}

/**
* Get the streaming expression that caused the exception.
*
* @return string
*/
public function getExpression()
{
return $this->expression;
}
}
34 changes: 34 additions & 0 deletions src/QueryType/Stream/Expression.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,38 @@ public function __call(string $name, array $arguments)
return '' !== $value;
})).')';
}

/**
* Format and indent a streaming expression.
*
* @param string $expression
*
* @return string
*/
public static function indent(string $expression)
{
$current_indentation = 0;
$indentation_step = 2;
$indented_expression = '';
for ($c = 0; $c < strlen($expression); ++$c) {
if ('(' === $expression[$c]) {
$indented_expression .= $expression[$c].PHP_EOL;
$current_indentation += $indentation_step;
$indented_expression .= str_pad('', $current_indentation);
} elseif (')' === $expression[$c]) {
$current_indentation -= $indentation_step;
$indented_expression .= PHP_EOL;
$indented_expression .= str_pad('', $current_indentation).$expression[$c];
} elseif (',' === $expression[$c]) {
$indented_expression .= $expression[$c].PHP_EOL.str_pad('', $current_indentation);
// swallow space if any
if (' ' === @$expression[$c + 1]) {
++$c;
}
} else {
$indented_expression .= $expression[$c];
}
}
return $indented_expression;
}
}
12 changes: 9 additions & 3 deletions src/QueryType/Stream/ResponseParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ public function parse($result)
$fields = (array) $doc;
if (isset($fields['EXCEPTION'])) {
// Use Solr's exception as message.
throw new StreamException($fields['EXCEPTION']);
$e = new StreamException($fields['EXCEPTION']);
$e->setExpression($query->getExpression());
throw $e;
}
if (isset($fields['EOF'])) {
// End of stream.
Expand All @@ -53,12 +55,16 @@ public function parse($result)
$documents[] = new $documentClass($fields);
}
if (!isset($fields['EOF'])) {
throw new StreamException('Streaming expression returned an incomplete result-set.');
$e = new StreamException('Streaming expression returned an incomplete result-set.');
$e->setExpression($query->getExpression());
throw $e;
}
$data['responseHeader']['QTime'] = $fields['RESPONSE_TIME'];
$data['responseHeader']['status'] = 0;
} else {
throw new StreamException('Streaming expression did not return a result-set.');
$e = new StreamException('Streaming expression did not return a result-set.');
$e->setExpression($query->getExpression());
throw $e;
}

return $this->addHeaderInfo(
Expand Down

0 comments on commit 5be312f

Please sign in to comment.