Skip to content

Commit df87392

Browse files
committed
ExtraModule: Add support for special ExtraModule which can define some extra logic in project modules and connect to other packages. Now it is used to search videos on Mathematicator.com and return results.
1 parent 59d72b9 commit df87392

File tree

6 files changed

+167
-4
lines changed

6 files changed

+167
-4
lines changed

config.neon

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
services:
2-
- Mathematicator\Engine\Engine
3-
- Mathematicator\Engine\QueryNormalizer
4-
- Mathematicator\Engine\Translator
2+
mathematicator.engine:
3+
factory: Mathematicator\Engine\Engine
4+
setup:
5+
- addExtraModule(@Mathematicator\Engine\SampleModule)
6+
mathematicator.queryNormalizer:
7+
factory: Mathematicator\Engine\QueryNormalizer
8+
mathematicator.translator:
9+
factory: Mathematicator\Engine\Translator
510
- Mathematicator\NumberRewriter
611
- Mathematicator\NaturalTextFormatter
12+
- Mathematicator\Engine\SampleModule
713

814
orm.annotations:
915
paths:

src/Engine.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ final class Engine
3030
*/
3131
private $serviceFactory;
3232

33+
/**
34+
* @var ExtraModule[]
35+
*/
36+
private $extraModules = [];
37+
3338
/**
3439
* @param Router $router
3540
* @param QueryNormalizer $queryNormalizer
@@ -72,9 +77,30 @@ public function compute(string $query): EngineResult
7277
$return = new EngineSingleResult($queryEntity->getQuery(), $matchedRoute);
7378
}
7479

80+
foreach ($this->extraModules as $extraModule) {
81+
$extraModule->setEngineSingleResult($return);
82+
if ($extraModule->match($queryEntity->getQuery()) === true) {
83+
foreach (InjectExtension::getInjectProperties(\get_class($extraModule)) as $property => $service) {
84+
$extraModule->{$property} = $this->serviceFactory->getByType($service);
85+
}
86+
if (method_exists($extraModule, 'setQuery')) {
87+
$extraModule->setQuery($queryEntity->getQuery());
88+
}
89+
$extraModule->actionDefault();
90+
}
91+
}
92+
7593
return $return->setTime((int) (Debugger::timer('search_request') * 1000));
7694
}
7795

96+
/**
97+
* @param ExtraModule $extraModule
98+
*/
99+
public function addExtraModule(ExtraModule $extraModule): void
100+
{
101+
$this->extraModules[] = $extraModule;
102+
}
103+
78104
/**
79105
* @param Query $query
80106
* @param string $serviceName

src/Entity/EngineSingleResult.php

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,32 @@ public function __construct(string $query, string $matchedRoute, ?Box $interpret
4545
*/
4646
public function getBoxes(): array
4747
{
48-
return $this->boxes;
48+
$withoutNoResult = [];
49+
50+
foreach ($this->boxes as $box) {
51+
if ($box->getTag() !== 'no-results') {
52+
$withoutNoResult[] = $box;
53+
}
54+
}
55+
56+
$return = $withoutNoResult === [] ? $this->boxes : $withoutNoResult;
57+
58+
usort($return, static function (Box $a, Box $b): int {
59+
return $a->getRank() < $b->getRank() ? 1 : -1;
60+
});
61+
62+
return $return;
63+
}
64+
65+
/**
66+
* @param Box $box
67+
* @return EngineSingleResult
68+
*/
69+
public function addBox(Box $box): self
70+
{
71+
$this->boxes[] = $box;
72+
73+
return $this;
4974
}
5075

5176
/**
@@ -64,4 +89,15 @@ public function getSources(): array
6489
return $this->sources;
6590
}
6691

92+
/**
93+
* @param Source $source
94+
* @return EngineSingleResult
95+
*/
96+
public function addSource(Source $source): self
97+
{
98+
$this->sources[] = $source;
99+
100+
return $this;
101+
}
102+
67103
}

src/ExtraModule/BaseModule.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mathematicator\Engine;
6+
7+
8+
abstract class BaseModule implements ExtraModule
9+
{
10+
11+
/**
12+
* @var EngineSingleResult
13+
*/
14+
protected $result;
15+
16+
/**
17+
* @var string
18+
*/
19+
protected $query;
20+
21+
/**
22+
* @internal
23+
* @param EngineSingleResult $result
24+
*/
25+
final public function setEngineSingleResult(EngineSingleResult $result): void
26+
{
27+
$this->result = $result;
28+
}
29+
30+
/**
31+
* @internal
32+
* @param string $query
33+
*/
34+
final public function setQuery(string $query): void
35+
{
36+
$this->query = $query;
37+
}
38+
39+
}

src/ExtraModule/ExtraModule.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mathematicator\Engine;
6+
7+
8+
interface ExtraModule
9+
{
10+
11+
/**
12+
* @internal
13+
* @param EngineSingleResult $result
14+
*/
15+
public function setEngineSingleResult(EngineSingleResult $result): void;
16+
17+
/**
18+
* @param string $query
19+
* @return bool
20+
*/
21+
public function match(string $query): bool;
22+
23+
public function actionDefault(): void;
24+
25+
}

src/ExtraModule/SampleModule.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Mathematicator\Engine;
6+
7+
8+
use Mathematicator\Search\Box;
9+
10+
final class SampleModule extends BaseModule
11+
{
12+
13+
/**
14+
* @param string $query
15+
* @return bool
16+
*/
17+
public function match(string $query): bool
18+
{
19+
return $query === 'help';
20+
}
21+
22+
public function actionDefault(): void
23+
{
24+
$this->result->addBox(
25+
(new Box(Box::TYPE_TEXT))
26+
->setTitle('Help')
27+
->setText('What can I help you with?')
28+
);
29+
}
30+
31+
}

0 commit comments

Comments
 (0)