Skip to content

Commit d07a625

Browse files
Added flag shutdown functionality to command
1 parent 7e582f4 commit d07a625

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

src/Command/DaemonRunCommand.php

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class DaemonRunCommand extends Command
2727
*/
2828
private $driverContainer;
2929

30+
/**
31+
* @var DaemonInterface
32+
*/
33+
private $daemon;
34+
3035
/**
3136
* Constructor.
3237
*
@@ -39,6 +44,7 @@ public function __construct(KernelInterface $kernel, DriverContainerInterface $d
3944
{
4045
$this->kernel = $kernel;
4146
$this->driverContainer = $driverContainer;
47+
$this->daemon = null;
4248

4349
$name = $name ?: self::DEFAULT_NAME;
4450
$description = $description ?: self::DEFAULT_DESCRIPTION;
@@ -97,15 +103,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
97103

98104
if (null !== $port) {
99105
// If we have the port, create a TCP daemon
100-
$daemon = $daemonFactory->createTcpDaemon($this->kernel, $daemonOptions, $host ?: 'localhost', $port);
106+
$this->daemon = $daemonFactory->createTcpDaemon($this->kernel, $daemonOptions, $host ?: 'localhost', $port);
101107
} elseif (null !== $host) {
102108
// If we have the host but not the port, we cant create a TCP daemon - throw exception
103109
throw new \InvalidArgumentException('TCP port option must be set if host option is set');
104110
} else {
105111
// With no host or port, listen on FCGI_LISTENSOCK_FILENO (default)
106-
$daemon = $daemonFactory->createDaemon($this->kernel, $daemonOptions, $fd);
112+
$this->daemon = $daemonFactory->createDaemon($this->kernel, $daemonOptions, $fd);
113+
}
114+
115+
$this->daemon->run();
116+
}
117+
118+
/**
119+
* Flag the daemon for shutdown.
120+
*/
121+
public function flagShutdown()
122+
{
123+
if (null === $this->daemon) {
124+
throw new \RuntimeException('There is no daemon running');
107125
}
108126

109-
$daemon->run();
127+
$this->daemon->flagShutdown();
110128
}
111129
}

test/Command/DaemonRunCommandTest.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,35 @@ public function testPortOptionOnly()
129129
$this->assertEquals('getFactory', $context['driverContainer']->getDelegatedCalls()[0][0]);
130130
}
131131

132+
/**
133+
* Test flag shutdown.
134+
*/
135+
public function testShutdown()
136+
{
137+
$expectations = ['driver' => 'userland'];
138+
139+
$context = $this->createTestingContext($expectations);
140+
141+
$context['command']->run(new ArrayInput([]), new NullOutput());
142+
$context['command']->flagShutdown();
143+
144+
$this->assertEquals('run', $context['daemon']->getDelegatedCalls()[0][0]);
145+
$this->assertEquals('flagShutdown', $context['daemon']->getDelegatedCalls()[1][0]);
146+
$this->assertEquals('createDaemon', $context['daemonFactory']->getDelegatedCalls()[0][0]);
147+
$this->assertEquals('getFactory', $context['driverContainer']->getDelegatedCalls()[0][0]);
148+
}
149+
150+
/**
151+
* Test flag shutdown with no daemon.
152+
*
153+
* @expectedException \RuntimeException
154+
*/
155+
public function testShutdownNoDaemon()
156+
{
157+
$command = new DaemonRunCommand(new MockKernel(), new MockDriverContainer());
158+
$command->flagShutdown();
159+
}
160+
132161
/**
133162
* Construct mock objects set to test for expected values of different
134163
* parameters when specified.
@@ -146,7 +175,7 @@ private function createTestingContext(array $expectations)
146175
};
147176

148177
$mockKernel = new MockKernel();
149-
$mockDaemon = new MockDaemon(['run' => false]);
178+
$mockDaemon = new MockDaemon(['run' => false, 'flagShutdown' => false]);
150179

151180
$mockDaemonFactory = new MockDaemonFactory([
152181
'createTcpDaemon' => function ($kernel, $options, $host, $port) use ($assertExpected, $mockKernel, $mockDaemon) {

test/Helper/Mocker/MockDaemon.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,6 @@ public function run()
1515

1616
public function flagShutdown()
1717
{
18+
return $this->delegateCall('flagShutdown', func_get_args());
1819
}
1920
}

0 commit comments

Comments
 (0)