-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigPropsMiddleware.php
More file actions
90 lines (80 loc) · 3.22 KB
/
ConfigPropsMiddleware.php
File metadata and controls
90 lines (80 loc) · 3.22 KB
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
<?php
namespace MaplePHP\Unitary\Console\Middlewares;
use MaplePHP\Emitron\Configs\ConfigPropsFactory;
use MaplePHP\Emitron\Contracts\ConfigPropsInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use MaplePHP\Unitary\Config\ConfigProps;
use Throwable;
class ConfigPropsMiddleware implements MiddlewareInterface
{
protected ?ConfigPropsInterface $props = null;
private ContainerInterface $container;
/**
* Get the active Container instance with the Dependency injector
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}
/**
* Will bind current Response and Stream to the Command CLI library class
* this is initialized and passed to the Container
*
* @param ServerRequestInterface $request
* @param RequestHandlerInterface $handler
* @return ResponseInterface
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$this->container->set("props", $this->getInitProps());
return $handler->handle($request);
}
/**
* Builds the list of allowed CLI arguments from ConfigProps.
*
* These properties can be defined either in the configuration file or as CLI arguments.
* If invalid arguments are passed, and verbose mode is enabled, an error will be displayed
* along with a warning about the unknown properties.
*
* @return ConfigProps
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
private function getInitProps(): ConfigPropsInterface
{
if ($this->props === null) {
$args = $this->container->get("args");
$configs = $this->container->get("configuration");
$command = $this->container->get("command");
try {
$props = array_merge($configs->getProps()->toArray(), $args);
$this->props = ConfigPropsFactory::create($props, $configs->getConfigPropsClass());
if ($this->props->hasMissingProps() !== [] && isset($args['verbose'])) {
$command->error('The properties (' .
implode(", ", $this->props->hasMissingProps()) . ') is not exist in ' . get_class($this->props));
$command->message(
"One or more arguments you passed are not recognized as valid options.\n" .
"Check your command parameter syntax for spellings or configuration."
);
}
} catch (Throwable $e) {
$this->props = $configs->getProps();
if (isset($args['verbose'])) {
$command->error($e->getMessage());
exit(1);
}
}
}
return $this->props;
}
}