FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
phpbench/lib/Executor/ScriptExecutor.php at program-executor · phpbench/phpbench · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
phpbench
/
phpbench
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
132
Star
2k
Code
Issues
28
Pull requests
10
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
phpbench
/
lib
/
Executor
/
ScriptExecutor.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
118 lines (96 loc) · 2.76 KB
Breadcrumbs
phpbench
/
lib
/
Executor
/
ScriptExecutor.php
Copy path
File metadata and controls
118 lines (96 loc) · 2.76 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
<?php
namespace
PhpBench
\
Executor
;
use
PhpBench
\
Remote
\
Exception
\
ScriptErrorException
;
use
RuntimeException
;
use
Symfony
\
Component
\
Process
\
Process
;
use
function
uniqid
;
final
class
ScriptExecutor
implements
ScriptExecutorInterface
{
/**
* @var string|null
*/
private
$
scriptPath
;
/**
* @var bool
*/
private
$
scriptRemove
;
/**
* @var PhpProcessFactory
*/
private
$
factory
;
public
function
__construct
(
PhpProcessFactory
$
factory
,
?
string
$
scriptPath
,
bool
$
scriptRemove
) {
$
this
->
scriptPath
=
$
scriptPath
;
$
this
->
factory
=
$
factory
;
$
this
->
scriptRemove
=
$
scriptRemove
;
}
/**
* @return array<string,mixed>
*/
public
function
execute
(
/** include the run ID here **/
string
$
script
):
array
{
$
scriptPath
=
$
this
->
writeTempFile
(
$
script
);
$
process
=
$
this
->
factory
->
buildProcess
(
$
scriptPath
);
$
process
->
run
();
$
this
->
removeTmpFile
(
$
scriptPath
);
if
(
false
===
$
process
->
isSuccessful
()) {
throw
new
ScriptErrorException
(
sprintf
(
'
%s%s
'
,
$
process
->
getErrorOutput
(),
$
process
->
getOutput
()
));
}
return
$
this
->
decodeResults
(
$
process
,
$
scriptPath
);
}
private
function
writeTempFile
(
string
$
script
):
string
{
$
scriptPath
=
$
this
->
scriptPath
?
sprintf
(
'
%s/script_%s.php
'
,
$
this
->
scriptPath
,
uniqid
()) :
tempnam
(
sys_get_temp_dir
(),
'
PhpBench
'
);
if
(
false
===
$
scriptPath
) {
throw
new
RuntimeException
(
'
Could not generate temporary script name
'
);
}
(
function
(
string
$
directory
):
void
{
if
(
file_exists
(
$
directory
)) {
return
;
}
if
(@
mkdir
(
$
directory
,
0744
,
true
)) {
return
;
}
throw
new
RuntimeException
(
sprintf
(
'
Could not create directory "%s"
'
,
$
directory
));
})(
dirname
(
$
scriptPath
));
file_put_contents
(
$
scriptPath
,
$
script
);
return
$
scriptPath
;
}
private
function
removeTmpFile
(
string
$
scriptPath
):
void
{
if
(!
$
this
->
scriptRemove
) {
return
;
}
unlink
(
$
scriptPath
);
}
/**
* @return array<string, mixed>
*/
private
function
decodeResults
(
Process
$
process
,
string
$
scriptPath
):
array
{
$
output
=
$
process
->
getOutput
();
$
result
= @
unserialize
(
$
output
);
if
(
is_array
(
$
result
)) {
return
$
result
;
}
throw
new
\
RuntimeException
(
sprintf
(
'
Script "%s" did not return an array, got: %s
'
,
$
scriptPath
,
$
output
));
}
}
Back
|
FazBrowse Home
|
New Git URL