Skip to content

Commit 3da583f

Browse files
authored
Engine: Rewrite to QueryObject and use Context.
1 parent 7734f5b commit 3da583f

File tree

1 file changed

+40
-19
lines changed

1 file changed

+40
-19
lines changed

src/Engine.php

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77

88
use Mathematicator\Router\Router;
9+
use Mathematicator\Search\Query;
910
use Mathematicator\SearchController\IController;
1011
use Nette\DI\Container;
1112
use Tracy\Debugger;
@@ -18,66 +19,74 @@ final class Engine
1819
*/
1920
private $router;
2021

22+
/**
23+
* @var QueryNormalizer
24+
*/
25+
private $queryNormalizer;
26+
2127
/**
2228
* @var Container
2329
*/
2430
private $serviceFactory;
2531

2632
/**
2733
* @param Router $router
34+
* @param QueryNormalizer $queryNormalizer
2835
* @param Container $container
2936
*/
30-
public function __construct(Router $router, Container $container)
37+
public function __construct(Router $router, QueryNormalizer $queryNormalizer, Container $container)
3138
{
3239
$this->router = $router;
40+
$this->queryNormalizer = $queryNormalizer;
3341
$this->serviceFactory = $container;
3442
}
3543

3644
/**
3745
* @param string $query
38-
* @return EngineResult
46+
* @return EngineResult|EngineMultiResult
3947
* @throws InvalidDataException
4048
*/
4149
public function compute(string $query): EngineResult
4250
{
43-
if (preg_match('/^(?<left>.+?)\s+vs\.?\s+(?<right>.+?)$/', $query, $versus)) {
44-
return (new EngineMultiResult($query, null))
51+
$queryEntity = $this->buildQuery($query);
52+
53+
if (preg_match('/^(?<left>.+?)\s+vs\.?\s+(?<right>.+?)$/', $queryEntity->getQuery(), $versus)) {
54+
return (new EngineMultiResult($queryEntity->getQuery(), null))
4555
->addResult($this->compute($versus['left']), 'left')
4656
->addResult($this->compute($versus['right']), 'right');
4757
}
4858

49-
$callback = $this->router->routeQuery($query);
50-
$matchedRoute = (string) preg_replace('/^.+\\\\([^\\\\]+)$/', '$1', $callback);
59+
$controller = $this->router->routeQuery($queryEntity->getQuery());
60+
$matchedRoute = (string) preg_replace('/^.+\\\\([^\\\\]+)$/', '$1', $controller);
5161

52-
if ($result = $this->processCallback($query, $callback)) {
62+
if ($result = $this->processCallback($queryEntity, $controller)) {
5363
$return = new EngineSingleResult(
54-
$query,
64+
$queryEntity->getQuery(),
5565
$matchedRoute,
56-
$result->getInterpret(),
57-
$result->getBoxes(),
58-
$result->getSources()
66+
$result->getContext()->getInterpret(),
67+
$result->getContext()->getBoxes(),
68+
$result->getContext()->getSources()
5969
);
6070
} else {
61-
$return = new EngineSingleResult($query, $matchedRoute);
71+
$return = new EngineSingleResult($queryEntity->getQuery(), $matchedRoute);
6272
}
6373

64-
return $return->setTime((int) round(Debugger::timer('search_request') * 1000));
74+
return $return->setTime((int) (Debugger::timer('search_request') * 1000));
6575
}
6676

6777
/**
68-
* @param string $query
69-
* @param string $callback
78+
* @param Query $query
79+
* @param string $controller
7080
* @return IController|null
7181
* @throws InvalidDataException
7282
*/
73-
private function processCallback(string $query, string $callback): ?IController
83+
private function processCallback(Query $query, string $controller): ?IController
7484
{
7585
/** @var IController|null $return */
76-
$return = $this->serviceFactory->getByType($callback);
86+
$return = $this->serviceFactory->getByType($controller);
7787

7888
if ($return !== null) {
79-
$return->setQuery($query);
80-
$return->resetBoxes();
89+
$return->createContext($query);
8190

8291
try {
8392
$return->actionDefault();
@@ -88,4 +97,16 @@ private function processCallback(string $query, string $callback): ?IController
8897
return $return ?? null;
8998
}
9099

100+
/**
101+
* @param string $query
102+
* @return Query
103+
*/
104+
private function buildQuery(string $query): Query
105+
{
106+
return new Query(
107+
$query,
108+
$this->queryNormalizer->normalize($query)
109+
);
110+
}
111+
91112
}

0 commit comments

Comments
 (0)