-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcli
executable file
·82 lines (68 loc) · 1.96 KB
/
cli
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
#!/bin/env php
<?php
/**
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
if (php_sapi_name() !== 'cli') {
die('This script must be called from command line.');
}
// Setup the Minz framework
$app_path = __DIR__;
include $app_path . '/autoload.php';
\Minz\Configuration::load('dotenv', $app_path);
\Minz\Environment::initialize();
// Read command line parameters to create a Request
$shortopts = 'p::';
$longopts = [
'request:',
];
$options = getopt($shortopts, $longopts);
if (!isset($options['request']) || !$options['request']) {
$options['request'] = '/';
}
$parameters = [];
if (isset($options['p'])) {
$cli_parameters = $options['p'];
if (!is_array($cli_parameters)) {
$cli_parameters = array($cli_parameters);
}
foreach ($cli_parameters as $parameter) {
if (strpos($parameter, '=') === false) {
die("Parameters must be in the form key=value ({$parameter}).\n");
}
list($key, $value) = explode('=', $parameter, 2);
$parameters[$key] = $value;
}
}
try {
$request = new \Minz\Request('cli', $options['request'], $parameters);
} catch (\Minz\Errors\RequestError $e) {
die($e->getMessage() . "\n");
}
// Initialize the Application and execute the request to get a Response
$application = new \flusio\cli\Application();
$response = $application->run($request);
if ($response instanceof Generator) {
// This is used by the JobsWorker#watch method in order to provide a
// long-running service.
foreach ($response as $response_part) {
$output = $response_part->render();
if ($output) {
echo $output . "\n";
}
}
exit(0);
} else {
// Display the content
$output = $response->render();
if ($output) {
echo $output . "\n";
}
$code = $response->code();
if ($code >= 200 && $code < 300) {
exit(0);
} else {
exit(1);
}
}