-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSoureCodeDaemonBundle.php
More file actions
90 lines (76 loc) · 3.4 KB
/
SoureCodeDaemonBundle.php
File metadata and controls
90 lines (76 loc) · 3.4 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 SoureCode\Bundle\Daemon;
use RuntimeException;
use SoureCode\Bundle\Daemon\Command\DaemonStartCommand;
use SoureCode\Bundle\Daemon\Command\DaemonStopCommand;
use SoureCode\Bundle\Daemon\Manager\DaemonManager;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\HttpKernel\Bundle\AbstractBundle;
use function Symfony\Component\DependencyInjection\Loader\Configurator\param;
use function Symfony\Component\DependencyInjection\Loader\Configurator\service;
class SoureCodeDaemonBundle extends AbstractBundle
{
public function configure(DefinitionConfigurator $definition): void
{
// @formatter:off
$definition->rootNode()
->children()
->scalarNode('directory')
->defaultValue('%kernel.project_dir%/etc/daemons')
->info('The directory where the daemons are located. The daemons must be named <name>.service for systemd or <name>.plist for launchd.')
->end()
->scalarNode('adapter')
->defaultValue('auto')
->info('The adapter to use. Currently only systemd and launchd are supported.')
->validate()
->ifNotInArray(['systemd', 'launchd', 'auto'])
->thenInvalid('Invalid adapter "%s".')
->end()
->end()
->end();
// @formatter:on
}
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$container->parameters()
->set('soure_code.daemon.directory', $config['directory']);
$services = $container->services();
$services->set('soure_code.daemon.adapter.systemd', Adapter\SystemdAdapter::class)
->args([
service('filesystem'),
]);
$services->set('soure_code.daemon.adapter.launchd', Adapter\LaunchdAdapter::class);
$adapter = $config['adapter'];
if ($adapter === 'auto') {
if (PHP_OS_FAMILY === 'Linux') {
$adapter = 'systemd';
} elseif (PHP_OS_FAMILY === 'Darwin') {
$adapter = 'launchd';
} else {
throw new RuntimeException('Could not detect adapter.');
}
}
$services->set('soure_code.daemon.daemon_manager', DaemonManager::class)
->args([
service('soure_code.daemon.adapter.' . $adapter),
service('filesystem'),
param('soure_code.daemon.directory'),
]);
$services->alias(DaemonManager::class, 'soure_code.daemon.daemon_manager')
->public();
$services->set('soure_code.daemon.command.daemon.start', DaemonStartCommand::class)
->args([
service('soure_code.daemon.daemon_manager'),
])
->public()
->tag('console.command', ['command' => 'daemon:start']);
$services->set('soure_code.daemon.command.daemon.stop', DaemonStopCommand::class)
->args([
service('soure_code.daemon.daemon_manager'),
])
->public()
->tag('console.command', ['command' => 'daemon:stop']);
}
}