Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F103672222
PhagePHPAgent.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Mar 3, 22:14
Size
2 KB
Mime Type
text/x-php
Expires
Wed, Mar 5, 22:14 (2 d)
Engine
blob
Format
Raw Data
Handle
24635891
Attached To
rPHU libphutil
PhagePHPAgent.php
View Options
<?php
final
class
PhagePHPAgent
{
private
$stdin
;
private
$master
;
private
$exec
=
array
();
public
function
__construct
(
$stdin
)
{
$this
->
stdin
=
$stdin
;
}
public
function
execute
()
{
while
(
true
)
{
if
(
$this
->
exec
)
{
$iterator
=
new
FutureIterator
(
$this
->
exec
);
$iterator
->
setUpdateInterval
(
0.050
);
foreach
(
$iterator
as
$key
=>
$future
)
{
if
(
$future
===
null
)
{
break
;
}
$this
->
resolveFuture
(
$key
,
$future
);
break
;
}
}
else
{
PhutilChannel
::
waitForAny
(
array
(
$this
->
getMaster
()));
}
$this
->
processInput
();
}
}
private
function
getMaster
()
{
if
(!
$this
->
master
)
{
$raw_channel
=
new
PhutilSocketChannel
(
$this
->
stdin
,
fopen
(
'php://stdout'
,
'w'
));
$json_channel
=
new
PhutilJSONProtocolChannel
(
$raw_channel
);
$this
->
master
=
$json_channel
;
}
return
$this
->
master
;
}
private
function
processInput
()
{
$channel
=
$this
->
getMaster
();
$open
=
$channel
->
update
();
if
(!
$open
)
{
throw
new
Exception
(
'Channel closed!'
);
}
while
(
true
)
{
$command
=
$channel
->
read
();
if
(
$command
===
null
)
{
break
;
}
$this
->
processCommand
(
$command
);
}
}
private
function
processCommand
(
array
$spec
)
{
switch
(
$spec
[
'type'
])
{
case
'EXEC'
:
$key
=
$spec
[
'key'
];
$cmd
=
$spec
[
'command'
];
$future
=
new
ExecFuture
(
'%C'
,
$cmd
);
$this
->
exec
[
$key
]
=
$future
;
break
;
case
'EXIT'
:
$this
->
terminateAgent
();
break
;
}
}
private
function
resolveFuture
(
$key
,
Future
$future
)
{
$result
=
$future
->
resolve
();
$master
=
$this
->
getMaster
();
$master
->
write
(
array
(
'type'
=>
'RSLV'
,
'key'
=>
$key
,
'err'
=>
$result
[
0
],
'stdout'
=>
$result
[
1
],
'stderr'
=>
$result
[
2
],
));
}
public
function
__destruct
()
{
$this
->
terminateAgent
();
}
private
function
terminateAgent
()
{
foreach
(
$this
->
exec
as
$key
=>
$future
)
{
$future
->
resolveKill
();
}
exit
(
0
);
}
}
Event Timeline
Log In to Comment