1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\LabStation\Trigger; |
5
|
|
|
|
6
|
|
|
use Innmind\LabStation\{ |
7
|
|
|
Trigger, |
8
|
|
|
Activity, |
9
|
|
|
Activity\Type, |
10
|
|
|
}; |
11
|
|
|
use Innmind\CLI\Environment; |
12
|
|
|
use Innmind\OperatingSystem\Filesystem; |
13
|
|
|
use Innmind\Filesystem\{ |
14
|
|
|
Directory, |
15
|
|
|
File, |
16
|
|
|
}; |
17
|
|
|
use Innmind\Server\Control\Server\{ |
18
|
|
|
Processes, |
19
|
|
|
Command, |
20
|
|
|
}; |
21
|
|
|
use Innmind\Url\{ |
22
|
|
|
Url, |
23
|
|
|
PathInterface, |
24
|
|
|
}; |
25
|
|
|
use Symfony\Component\Dotenv\Dotenv; |
26
|
|
|
|
27
|
|
|
final class Profiler implements Trigger |
28
|
|
|
{ |
29
|
|
|
private $filesystem; |
30
|
|
|
private $processes; |
31
|
|
|
private $dotenv; |
32
|
|
|
|
33
|
20 |
|
public function __construct(Filesystem $filesystem, Processes $processes) |
34
|
|
|
{ |
35
|
20 |
|
$this->filesystem = $filesystem; |
36
|
20 |
|
$this->processes = $processes; |
37
|
20 |
|
$this->dotenv = new Dotenv; |
38
|
20 |
|
} |
39
|
|
|
|
40
|
16 |
|
public function __invoke(Activity $activity, Environment $env): void |
41
|
|
|
{ |
42
|
16 |
|
if (!$activity->is(Type::start())) { |
43
|
2 |
|
return; |
44
|
|
|
} |
45
|
|
|
|
46
|
14 |
|
$project = $this->filesystem->mount($env->workingDirectory()); |
47
|
|
|
|
48
|
14 |
|
if (!$project->has('config')) { |
49
|
2 |
|
return; |
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
$config = $project->get('config'); |
53
|
|
|
|
54
|
12 |
|
if (!$config instanceof Directory) { |
55
|
2 |
|
return; |
56
|
|
|
} |
57
|
|
|
|
58
|
10 |
|
if (!$config->has('.env')) { |
59
|
2 |
|
return; |
60
|
|
|
} |
61
|
|
|
|
62
|
8 |
|
$this->start($config->get('.env'), $env->workingDirectory()); |
63
|
8 |
|
} |
64
|
|
|
|
65
|
8 |
|
private function start(File $file, PathInterface $workingDirectory): void |
66
|
|
|
{ |
67
|
8 |
|
$env = $this->dotenv->parse((string) $file->content()); |
68
|
|
|
|
69
|
8 |
|
if (!\array_key_exists('DEBUG', $env)) { |
70
|
2 |
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
6 |
|
if ($env['DEBUG'] != true) { |
74
|
2 |
|
return; |
75
|
|
|
} |
76
|
|
|
|
77
|
4 |
|
if (!\array_key_exists('PROFILER', $env)) { |
78
|
2 |
|
return; |
79
|
|
|
} |
80
|
|
|
|
81
|
2 |
|
$workingDirectory = \rtrim((string) $workingDirectory, '/'); |
82
|
2 |
|
$workingDirectory .= '/../profiler/public'; |
83
|
|
|
|
84
|
2 |
|
$this->processes->execute( |
85
|
2 |
|
Command::background('php') |
86
|
2 |
|
->withShortOption('S') |
87
|
2 |
|
->withArgument((string) Url::fromString($env['PROFILER'])->authority()) |
|
|
|
|
88
|
2 |
|
->withWorkingDirectory($workingDirectory) |
89
|
|
|
); |
90
|
2 |
|
} |
91
|
|
|
} |
92
|
|
|
|