This repository has been archived by the owner on Dec 12, 2023. It is now read-only.
/
Application.php
168 lines (154 loc) · 4.95 KB
/
Application.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
declare(strict_types = 1);
namespace Innmind\CLI\Framework;
use Innmind\CLI\{
Environment,
Command,
Commands,
};
use Innmind\OperatingSystem\OperatingSystem;
use Innmind\Debug\Profiler\Section;
use Innmind\Url\{
Path,
Url,
};
use Innmind\Immutable\Set;
use function Innmind\SilentCartographer\bootstrap as cartographer;
use function Innmind\Debug\bootstrap as debug;
use function Innmind\Immutable\unwrap;
final class Application
{
private Environment $env;
private OperatingSystem $os;
/** @var \Closure(Environment, OperatingSystem): list<Command> */
private \Closure $commands;
/** @var \Closure(Environment, OperatingSystem): Environment */
private \Closure $loadDotEnv;
/** @var \Closure(Environment, OperatingSystem): OperatingSystem */
private \Closure $enableSilentCartographer;
/** @var list<class-string<Section>> */
private array $disabledSections;
/**
* @param callable(Environment, OperatingSystem): list<Command> $commands
* @param callable(Environment, OperatingSystem): Environment $loadDotEnv
* @param callable(Environment, OperatingSystem): OperatingSystem $enableSilentCartographer
* @param list<class-string<Section>> $disabledSections
*/
private function __construct(
Environment $env,
OperatingSystem $os,
callable $commands,
callable $loadDotEnv,
callable $enableSilentCartographer,
array $disabledSections
) {
$this->env = $env;
$this->os = $os;
$this->commands = \Closure::fromCallable($commands);
$this->loadDotEnv = \Closure::fromCallable($loadDotEnv);
$this->enableSilentCartographer = \Closure::fromCallable($enableSilentCartographer);
$this->disabledSections = $disabledSections;
}
public static function of(Environment $env, OperatingSystem $os): self
{
return new self(
$env,
$os,
static fn(): array => [],
static fn(Environment $env): Environment => $env,
static fn(Environment $env, OperatingSystem $os): OperatingSystem => cartographer($os)['cli'](
Url::of('/')->withPath(
$env->workingDirectory(),
),
),
[],
);
}
/**
* @param callable(Environment, OperatingSystem): list<Command> $commands
*/
public function commands(callable $commands): self
{
return new self(
$this->env,
$this->os,
fn(Environment $env, OperatingSystem $os): array => \array_merge(
($this->commands)($env, $os),
$commands($env, $os),
),
$this->loadDotEnv,
$this->enableSilentCartographer,
[],
);
}
public function configAt(Path $path): self
{
return new self(
$this->env,
$this->os,
$this->commands,
static fn(Environment $env, OperatingSystem $os): Environment => new DotEnvAware(
$env,
$os->filesystem(),
$path,
),
$this->enableSilentCartographer,
[],
);
}
public function disableSilentCartographer(): self
{
return new self(
$this->env,
$this->os,
$this->commands,
$this->loadDotEnv,
static fn(Environment $env, OperatingSystem $os): OperatingSystem => $os,
[],
);
}
/**
* @param list<class-string<Section>> $sections
*/
public function disableProfilerSection(string ...$sections): self
{
return new self(
$this->env,
$this->os,
$this->commands,
$this->loadDotEnv,
$this->enableSilentCartographer,
\array_merge(
$this->disabledSections,
$sections,
),
);
}
public function run(): void
{
$os = ($this->enableSilentCartographer)($this->env, $this->os);
$env = ($this->loadDotEnv)($this->env, $os);
$debugEnabled = $env->variables()->contains('PROFILER');
$wrapCommands = static fn(Command ...$commands): array => $commands;
if ($debugEnabled) {
$debug = debug(
$os,
Url::of($env->variables()->get('PROFILER')),
$env->variables(),
null,
Set::strings(...$this->disabledSections),
);
$os = $debug['os']();
$wrapCommands = static fn(Command ...$commands): array => unwrap(
$debug['cli'](...$commands),
);
}
$commands = ($this->commands)($env, $os);
$commands = \count($commands) === 0 ? [new HelloWorld] : $commands;
if ($debugEnabled) {
$commands = $wrapCommands(...$commands);
}
$run = new Commands(...$commands);
$run($env);
}
}