Skip to content

Commit 78a0878

Browse files
committed
Abstracted Router and Command Bus instantiation
1 parent 6f46fe9 commit 78a0878

File tree

6 files changed

+256
-214
lines changed

6 files changed

+256
-214
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
- Fixed JSON serialization of Estimator and Data types
55
- Rename Rank and RankSample to Score and ScoreSample
66
- Move Verbose interface to Server
7+
- Abstracted Router and Command Bus instantiation
78

89
- 0.0.2-beta
910
- Changed name of Binary serializer to Igbinary

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555
"build": [
5656
"@composer install",
5757
"@analyze",
58-
"@check",
59-
"@test"
58+
"@test",
59+
"@check"
6060
],
6161
"analyze": "phpstan analyse -c phpstan.neon",
6262
"check": "php-cs-fixer fix --config=.php_cs.dist -v --dry-run --using-cache=no",
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Rubix\Server\Providers;
4+
5+
use Rubix\ML\Estimator;
6+
use Rubix\ML\Learner;
7+
use Rubix\ML\Probabilistic;
8+
use Rubix\ML\Ranking;
9+
use Rubix\Server\Server;
10+
use Rubix\Server\CommandBus;
11+
use Rubix\Server\Commands\Predict;
12+
use Rubix\Server\Commands\PredictSample;
13+
use Rubix\Server\Commands\Proba;
14+
use Rubix\Server\Commands\ProbaSample;
15+
use Rubix\Server\Commands\Score;
16+
use Rubix\Server\Commands\ScoreSample;
17+
use Rubix\Server\Commands\QueryModel;
18+
use Rubix\Server\Commands\ServerStatus;
19+
use Rubix\Server\Handlers\PredictHandler;
20+
use Rubix\Server\Handlers\PredictSampleHandler;
21+
use Rubix\Server\Handlers\ProbaHandler;
22+
use Rubix\Server\Handlers\ProbaSampleHandler;
23+
use Rubix\Server\Handlers\ScoreHandler;
24+
use Rubix\Server\Handlers\ScoreSampleHandler;
25+
use Rubix\Server\Handlers\QueryModelHandler;
26+
use Rubix\Server\Handlers\ServerStatusHandler;
27+
28+
class CommandBusProvider
29+
{
30+
/**
31+
* The command bus.
32+
*
33+
* @var \Rubix\ML\Estimator
34+
*/
35+
protected $estimator;
36+
37+
/**
38+
* The model server.
39+
*
40+
* @var \Rubix\Server\Server
41+
*/
42+
protected $server;
43+
44+
/**
45+
* Static factory for method chaining.
46+
*
47+
* @param \Rubix\ML\Estimator $estimator
48+
* @param \Rubix\Server\Server $server
49+
* @return self
50+
*/
51+
public static function with(Estimator $estimator, Server $server) : self
52+
{
53+
return new self($estimator, $server);
54+
}
55+
56+
/**
57+
* @param \Rubix\ML\Estimator $estimator
58+
* @param \Rubix\Server\Server $server
59+
*/
60+
public function __construct(Estimator $estimator, Server $server)
61+
{
62+
$this->estimator = $estimator;
63+
$this->server = $server;
64+
}
65+
66+
/**
67+
* @return \Rubix\Server\CommandBus
68+
*/
69+
public function boot() : CommandBus
70+
{
71+
$commands = [];
72+
73+
if ($this->estimator instanceof Estimator) {
74+
$commands[QueryModel::class] = new QueryModelHandler($this->estimator);
75+
$commands[Predict::class] = new PredictHandler($this->estimator);
76+
}
77+
78+
if ($this->estimator instanceof Learner) {
79+
$commands[PredictSample::class] = new PredictSampleHandler($this->estimator);
80+
}
81+
82+
if ($this->estimator instanceof Probabilistic) {
83+
$commands[Proba::class] = new ProbaHandler($this->estimator);
84+
$commands[ProbaSample::class] = new ProbaSampleHandler($this->estimator);
85+
}
86+
87+
if ($this->estimator instanceof Ranking) {
88+
$commands[Score::class] = new ScoreHandler($this->estimator);
89+
$commands[ScoreSample::class] = new ScoreSampleHandler($this->estimator);
90+
}
91+
92+
$commands[ServerStatus::class] = new ServerStatusHandler($this->server);
93+
94+
return new CommandBus($commands);
95+
}
96+
}

src/Providers/RouterProvider.php

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
<?php
2+
3+
namespace Rubix\Server\Providers;
4+
5+
use Rubix\ML\Estimator;
6+
use Rubix\ML\Learner;
7+
use Rubix\ML\Probabilistic;
8+
use Rubix\ML\Ranking;
9+
use Rubix\Server\CommandBus;
10+
use Rubix\Server\Http\Controllers\PredictionsController;
11+
use Rubix\Server\Http\Controllers\SamplePredictionController;
12+
use Rubix\Server\Http\Controllers\ProbabilitiesController;
13+
use Rubix\Server\Http\Controllers\SampleProbabilitiesController;
14+
use Rubix\Server\Http\Controllers\QueryModelController;
15+
use Rubix\Server\Http\Controllers\ScoresController;
16+
use Rubix\Server\Http\Controllers\SampleScoreController;
17+
use Rubix\Server\Http\Controllers\ServerStatusController;
18+
use FastRoute\RouteCollector;
19+
use FastRoute\RouteParser\Std;
20+
use FastRoute\DataGenerator\GroupCountBased as GroupCountBasedDataGenerator;
21+
use FastRoute\Dispatcher\GroupCountBased as GroupCountBasedDispatcher;
22+
use FastRoute\Dispatcher;
23+
24+
class RouterProvider
25+
{
26+
public const MODEL_PREFIX = '/model';
27+
28+
public const SERVER_PREFIX = '/server';
29+
30+
public const PREDICT_ENDPOINT = '/predictions';
31+
32+
public const PREDICT_SAMPLE_ENDPOINT = '/sample_prediction';
33+
34+
public const PROBA_ENDPOINT = '/probabilities';
35+
36+
public const PROBA_SAMPLE_ENDPOINT = '/sample_probabilities';
37+
38+
public const SCORE_ENDPOINT = '/scores';
39+
40+
public const SCORE_SAMPLE_ENDPOINT = '/sample_score';
41+
42+
public const SERVER_STATUS_ENDPOINT = '/status';
43+
44+
/**
45+
* The command bus.
46+
*
47+
* @var \Rubix\ML\Estimator
48+
*/
49+
protected $estimator;
50+
51+
/**
52+
* The command bus.
53+
*
54+
* @var \Rubix\Server\CommandBus
55+
*/
56+
protected $bus;
57+
58+
/**
59+
* Static factory for method chaining.
60+
*
61+
* @param \Rubix\ML\Estimator $estimator
62+
* @param \Rubix\Server\CommandBus $bus
63+
* @return self
64+
*/
65+
public static function with(Estimator $estimator, CommandBus $bus) : self
66+
{
67+
return new self($estimator, $bus);
68+
}
69+
70+
/**
71+
* @param \Rubix\ML\Estimator $estimator
72+
* @param \Rubix\Server\CommandBus $bus
73+
*/
74+
public function __construct(Estimator $estimator, CommandBus $bus)
75+
{
76+
$this->estimator = $estimator;
77+
$this->bus = $bus;
78+
}
79+
80+
/**
81+
* @return \FastRoute\Dispatcher
82+
*/
83+
public function boot() : Dispatcher
84+
{
85+
$collector = new RouteCollector(new Std(), new GroupCountBasedDataGenerator());
86+
87+
$collector->get(self::MODEL_PREFIX, new QueryModelController($this->bus));
88+
89+
$collector->addGroup(
90+
self::MODEL_PREFIX,
91+
function ($group) {
92+
$group->post(
93+
self::PREDICT_ENDPOINT,
94+
new PredictionsController($this->bus)
95+
);
96+
97+
if ($this->estimator instanceof Learner) {
98+
$group->post(
99+
self::PREDICT_SAMPLE_ENDPOINT,
100+
new SamplePredictionController($this->bus)
101+
);
102+
}
103+
104+
if ($this->estimator instanceof Probabilistic) {
105+
$group->post(
106+
self::PROBA_ENDPOINT,
107+
new ProbabilitiesController($this->bus)
108+
);
109+
110+
$group->post(
111+
self::PROBA_SAMPLE_ENDPOINT,
112+
new SampleProbabilitiesController($this->bus)
113+
);
114+
}
115+
116+
if ($this->estimator instanceof Ranking) {
117+
$group->post(
118+
self::SCORE_ENDPOINT,
119+
new ScoresController($this->bus)
120+
);
121+
122+
$group->post(
123+
self::SCORE_SAMPLE_ENDPOINT,
124+
new SampleScoreController($this->bus)
125+
);
126+
}
127+
}
128+
);
129+
130+
$collector->addGroup(
131+
self::SERVER_PREFIX,
132+
function ($group) {
133+
$group->get(
134+
self::SERVER_STATUS_ENDPOINT,
135+
new ServerStatusController($this->bus)
136+
);
137+
}
138+
);
139+
140+
return new GroupCountBasedDispatcher($collector->getData());
141+
}
142+
}

0 commit comments

Comments
 (0)