-
Notifications
You must be signed in to change notification settings - Fork 0
/
Module.php
115 lines (100 loc) · 2.76 KB
/
Module.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<?php
namespace Phale;
class Module {
/**
* @var string
*/
public $name;
/**
* @var Module[]
*/
public $modules = [];
/**
* @var Factory[]
*/
public $factories = [];
/**
* @var Endpoint[][]
*/
public $endpoints = [
'GET' => [],
'POST' => [],
'PUT' => [],
'DELETE' => [],
];
/**
* @param string $name
* @throws \InvalidArgumentException
*/
public function __construct($name) {
if (!$name) {
throw new \InvalidArgumentException(sprintf('Invalid module name %s.', $name));
}
$this->name = $name;
}
/**
* Register a module.
*
* @param string $path
* @param Module $module
*/
public function module($path, Module $module) {
$this->modules[$path] = $module;
$this->factories = array_merge($this->factories, $module->factories);
foreach ($module->endpoints as $method => $endpoints) {
foreach ($endpoints as $endpointPath => $endpoint) {
$this->endpoints[$method][$path . $endpointPath] = $endpoint;
}
}
}
/**
* Register a dependency factory.
*
* @param string $name
* @param string[] $dependencies
* @param callable $factory
*/
public function factory($name, array $dependencies, callable $factory) {
$this->factories[$name] = new Factory($name, $dependencies, $factory);
}
/**
* Register a GET request handler.
*
* @param string $path
* @param string[] $dependencies
* @param callable $handler
*/
public function get($path, array $dependencies, callable $handler) {
$this->endpoints['GET'][$path] = new Endpoint($path, $dependencies, $handler);
}
/**
* Register a POST request handler.
*
* @param string $path
* @param string[] $dependencies
* @param callable $handler
*/
public function post($path, array $dependencies, callable $handler) {
$this->endpoints['POST'][$path] = new Endpoint($path, $dependencies, $handler);
}
/**
* Register a PUT request handler.
*
* @param string $path
* @param string[] $dependencies
* @param callable $handler
*/
public function put($path, array $dependencies, callable $handler) {
$this->endpoints['PUT'][$path] = new Endpoint($path, $dependencies, $handler);
}
/**
* Register a DELETE request handler.
*
* @param string $path
* @param string[] $dependencies
* @param callable $handler
*/
public function delete($path, array $dependencies, callable $handler) {
$this->endpoints['DELETE'][$path] = new Endpoint($path, $dependencies, $handler);
}
}