Skip to content

Commit 21b3532

Browse files
committed
refs #17 Added alternative for binding params
1 parent ea4f409 commit 21b3532

18 files changed

+314
-10
lines changed

composer.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@
5656
"laravel": {
5757
"providers": [
5858
"Sajya\\Server\\ServerServiceProvider"
59-
]
59+
],
60+
"aliases": {
61+
"RPC": "Sajya\\Server\\Facades\\RPC"
62+
}
6063
}
6164
}
6265
}

src/Binding.php

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sajya\Server;
6+
7+
use Closure;
8+
use Illuminate\Container\Container;
9+
use Illuminate\Routing\RouteBinding;
10+
use Illuminate\Support\Arr;
11+
use Illuminate\Support\Str;
12+
use Sajya\Server\Http\Request;
13+
14+
class Binding
15+
{
16+
/**
17+
* The IoC container instance.
18+
*
19+
* @var \Illuminate\Container\Container
20+
*/
21+
protected $container;
22+
23+
/**
24+
* The registered route value binders.
25+
*
26+
* @var array
27+
*/
28+
protected $binders = [];
29+
30+
/**
31+
* Application constructor.
32+
*
33+
* @param Container $container
34+
*/
35+
public function __construct(Container $container)
36+
{
37+
$this->container = $container;
38+
}
39+
40+
/**
41+
* Register a model binder for a wildcard.
42+
*
43+
* @param string $key
44+
* @param string $class
45+
* @param \Closure|null $callback
46+
*
47+
* @return void
48+
*/
49+
public function model(string $key, string $class, Closure $callback = null)
50+
{
51+
$this->bind($key, RouteBinding::forModel($this->container, $class, $callback));
52+
}
53+
54+
/**
55+
* Add a new route parameter binder.
56+
*
57+
* @param string $key
58+
* @param Closure|string $binder
59+
*
60+
* @return void
61+
*/
62+
public function bind(string $key, $binder)
63+
{
64+
$this->binders[$key] = RouteBinding::forCallback(
65+
$this->container, $binder
66+
);
67+
}
68+
69+
/**
70+
* @param Request $request
71+
*
72+
* @return array
73+
*/
74+
public function bindResolve(Request $request): array
75+
{
76+
$possibleBindings = Arr::dot($request->getParams());
77+
78+
return collect($possibleBindings)
79+
->map(fn($value, string $key) => with($value, $this->binders[$key] ?? null))
80+
->mapWithKeys(function ($value, string $key) {
81+
$nameForArgument = (string)Str::of($key)->replace('.', '_')->camel();
82+
83+
return [$nameForArgument => $value];
84+
})
85+
->toArray();
86+
}
87+
88+
}

src/Facades/RPC.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Sajya\Server\Facades;
6+
7+
use Illuminate\Support\Facades\Facade;
8+
use Sajya\Server\Binding;
9+
10+
/**
11+
* Class RPC
12+
*
13+
* @method static void bind(string $key, string|callable $binder)
14+
* @method static void model(string $key, string $class, \Closure|null $callback = null)
15+
*
16+
* @see \Sajya\Server\Binding
17+
*/
18+
class RPC extends Facade
19+
{
20+
/**
21+
* Initiate a mock expectation on the facade.
22+
*
23+
* @return string
24+
*/
25+
protected static function getFacadeAccessor(): string
26+
{
27+
return Binding::class;
28+
}
29+
}

src/Guide.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public function handleProcedure(Request $request, bool $notification): Response
104104
}
105105

106106
$result = $notification
107-
? HandleProcedure::dispatchAfterResponse($procedure)
108-
: HandleProcedure::dispatchNow($procedure);
107+
? HandleProcedure::dispatchAfterResponse($procedure, $request)
108+
: HandleProcedure::dispatchNow($procedure, $request);
109109

110110
return $this->makeResponse($result, $request);
111111
}

src/HandleProcedure.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,28 +16,34 @@
1616
use Sajya\Server\Exceptions\InternalErrorException;
1717
use Sajya\Server\Exceptions\InvalidParams;
1818
use Sajya\Server\Exceptions\RuntimeRpcException;
19+
use Sajya\Server\Facades\RPC;
20+
use Sajya\Server\Http\Request;
1921
use Symfony\Component\HttpKernel\Exception\HttpException;
2022

2123
class HandleProcedure implements ShouldQueue
2224
{
23-
use Dispatchable;
24-
use InteractsWithQueue;
25-
use Queueable;
26-
use SerializesModels;
25+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
2726

2827
/**
2928
* @var string
3029
*/
3130
protected string $procedure;
3231

32+
/**
33+
* @var Request
34+
*/
35+
protected $request;
36+
3337
/**
3438
* Create a new job instance.
3539
*
36-
* @param string $procedure
40+
* @param string $procedure
41+
* @param Request $request
3742
*/
38-
public function __construct(string $procedure)
43+
public function __construct(string $procedure, Request $request)
3944
{
4045
$this->procedure = $procedure;
46+
$this->request = $request;
4147
}
4248

4349
/**
@@ -48,7 +54,7 @@ public function __construct(string $procedure)
4854
public function handle()
4955
{
5056
try {
51-
return App::call($this->procedure);
57+
return App::call($this->procedure, RPC::bindResolve($this->request));
5258
} catch (HttpException | RuntimeException | Exception $exception) {
5359
$message = $exception->getMessage();
5460

src/ServerServiceProvider.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ public function register(): void
4242
'procedures' => $procedures,
4343
'delimiter' => $delimiter,
4444
]));
45+
46+
$this->app->singleton(Binding::class, fn($container) => new Binding($container));
4547
}
4648

4749
/**
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"jsonrpc": "2.0",
3+
"method": "binding@deepValue",
4+
"params": {
5+
"name": {
6+
"deep": {
7+
"value": "Alexandr"
8+
}
9+
}
10+
},
11+
"id": 1
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"jsonrpc": "2.0",
3+
"method": "binding@getModel",
4+
"params": {
5+
"fixtureModel": 13
6+
},
7+
"id": 1
8+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jsonrpc": "2.0",
3+
"method": "binding@subtract",
4+
"params": {
5+
"a": 4,
6+
"b": 2
7+
},
8+
"id": 1
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"jsonrpc": "2.0",
3+
"method": "binding@subtract",
4+
"params": {
5+
"a": 4,
6+
"b": 2
7+
},
8+
"id": 1
9+
}

0 commit comments

Comments
 (0)