-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocalCommand.php
More file actions
executable file
·89 lines (63 loc) · 2.52 KB
/
LocalCommand.php
File metadata and controls
executable file
·89 lines (63 loc) · 2.52 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
<?php
/*
* This file is part of the ObjRef package.
*
* (c) Uwe Mueller <uwe@namez.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace ObjRef\ExampleBundle\Command;
use ObjRef\Transport\FDTransport;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use ObjRef\Host;
use ObjRef\Proxy;
class LocalCommand extends ContainerAwareCommand {
protected function configure() {
$this
->setName('example:local')
->setDescription('Show local two-instance communication')
;
}
protected function execute(InputInterface $input, OutputInterface $output) {
// start a second symfony process with objref:client command
$cmd = '';
foreach($_SERVER['argv'] as $v) {
if($v == 'example:local') $v = 'objref:client';
$cmd .= escapeshellarg($v).' ';
if($v == 'objref:client') break;
}
// note: due to a php bug, we need to set the return value of proc_open to a variable
// otherwise $pipes has no pipes
$proc=proc_open($cmd,
[
['pipe','r'],
['pipe','w'],
['file','php://stderr', 'w']
],
$pipes);
$output->writeln('Second process launched');
// connect the pipes to a objref transport
$transport = new FDTransport($pipes[1], $pipes[0]);
$host = $this->getContainer()->get('objref.host');
$host->setTransport($transport);
// get the initial object of the other side.
// in symfony, this is the container of the second process
/** @var ContainerInterface $remoteContainer */
$remoteContainer = $host->getRemoteInitialObject();
// now we can access remote services!
$remoteExample = $remoteContainer->get('objref.example');
// to compare, we get another local one
$localExample = $this->getContainer()->get('objref.example');
$output->writeln('First PID: '.$localExample->getPid());
$output->writeln('Second PID: '.$remoteExample->getPid());
$output->write('Write local: ');
$localExample->outputTest($output);
$output->write('Write remote: ');
$remoteExample->outputTest($output);
proc_close($proc);
}
}