-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebServerExtension.php
145 lines (127 loc) · 4.63 KB
/
WebServerExtension.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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php declare(strict_types = 1);
/**
* WebServerExtension.php
*
* @license More in LICENSE.md
* @copyright https://www.fastybird.com
* @author Adam Kadlec <adam.kadlec@fastybird.com>
* @package FastyBird:WebServerPlugin!
* @subpackage DI
* @since 1.0.0
*
* @date 21.03.20
*/
namespace FastyBird\Plugin\WebServer\DI;
use FastyBird\Library\Application\Boot as ApplicationBoot;
use FastyBird\Plugin\WebServer\Application;
use FastyBird\Plugin\WebServer\Commands;
use FastyBird\Plugin\WebServer\Http;
use FastyBird\Plugin\WebServer\Middleware;
use FastyBird\Plugin\WebServer\Router;
use FastyBird\Plugin\WebServer\Server;
use FastyBird\Plugin\WebServer\Subscribers;
use Fig\Http\Message\RequestMethodInterface;
use Nette\DI;
use Nette\Schema;
use stdClass;
use function assert;
/**
* Simple web server extension container
*
* @package FastyBird:WebServerPlugin!
* @subpackage DI
*
* @author Adam Kadlec <adam.kadlec@fastybird.com>
*/
class WebServerExtension extends DI\CompilerExtension
{
public const NAME = 'fbWebServerPlugin';
public static function register(
ApplicationBoot\Configurator $config,
string $extensionName = self::NAME,
): void
{
$config->onCompile[] = static function (
ApplicationBoot\Configurator $config,
DI\Compiler $compiler,
) use (
$extensionName,
): void {
$compiler->addExtension($extensionName, new self());
};
}
public function getConfigSchema(): Schema\Schema
{
return Schema\Expect::structure([
'static' => Schema\Expect::structure([
'publicRoot' => Schema\Expect::string()->nullable(),
'enabled' => Schema\Expect::bool(false),
]),
'server' => Schema\Expect::structure([
'address' => Schema\Expect::string('127.0.0.1'),
'port' => Schema\Expect::int(8_000),
'certificate' => Schema\Expect::string()
->nullable(),
]),
'cors' => Schema\Expect::structure([
'enabled' => Schema\Expect::bool(false),
'allow' => Schema\Expect::structure([
'origin' => Schema\Expect::string('*'),
'methods' => Schema\Expect::arrayOf('string')
->default([
RequestMethodInterface::METHOD_GET,
RequestMethodInterface::METHOD_POST,
RequestMethodInterface::METHOD_PATCH,
RequestMethodInterface::METHOD_DELETE,
RequestMethodInterface::METHOD_OPTIONS,
]),
'credentials' => Schema\Expect::bool(true),
'headers' => Schema\Expect::arrayOf('string')
->default([
'Content-Type',
'Authorization',
'X-Requested-With',
]),
]),
]),
]);
}
public function loadConfiguration(): void
{
$builder = $this->getContainerBuilder();
$configuration = $this->getConfig();
assert($configuration instanceof stdClass);
$builder->addDefinition($this->prefix('routing.responseFactory'), new DI\Definitions\ServiceDefinition())
->setType(Http\ResponseFactory::class);
$builder->addDefinition($this->prefix('routing.router'), new DI\Definitions\ServiceDefinition())
->setType(Router\Router::class);
$builder->addDefinition($this->prefix('commands.server'), new DI\Definitions\ServiceDefinition())
->setType(Commands\HttpServer::class)
->setArguments([
'serverAddress' => $configuration->server->address,
'serverPort' => $configuration->server->port,
'serverCertificate' => $configuration->server->certificate,
]);
$builder->addDefinition($this->prefix('middlewares.cors'), new DI\Definitions\ServiceDefinition())
->setType(Middleware\Cors::class)
->setArguments([
'enabled' => $configuration->cors->enabled,
'allowOrigin' => $configuration->cors->allow->origin,
'allowMethods' => $configuration->cors->allow->methods,
'allowCredentials' => $configuration->cors->allow->credentials,
'allowHeaders' => $configuration->cors->allow->headers,
]);
$builder->addDefinition($this->prefix('middlewares.staticFiles'), new DI\Definitions\ServiceDefinition())
->setType(Middleware\StaticFiles::class)
->setArgument('publicRoot', $configuration->static->publicRoot)
->setArgument('enabled', $configuration->static->enabled);
$builder->addDefinition($this->prefix('middlewares.router'), new DI\Definitions\ServiceDefinition())
->setType(Middleware\Router::class);
$builder->addDefinition($this->prefix('application.classic'), new DI\Definitions\ServiceDefinition())
->setType(Application\Application::class);
$builder->addDefinition($this->prefix('server.factory'), new DI\Definitions\ServiceDefinition())
->setType(Server\Factory::class);
$builder->addDefinition($this->prefix('subscribers.server'), new DI\Definitions\ServiceDefinition())
->setType(Subscribers\Server::class);
}
}