-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkernel.drush.run.inc
106 lines (93 loc) · 3.46 KB
/
kernel.drush.run.inc
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
<?php
/**
* @file
* Provides the Composer integration in a seperate file so that PHP < 5.3 works.
*/
/**
* Executes the given command in the current working directory.
*/
function drush_kernel_app_run($arguments = array()) {
$c = pimple_get_container('app');
// Append all the command line options (--option and -o).
foreach ($_SERVER['argv'] as $argument) {
if ($argument[0] == '-') {
$arguments[] = $argument;
}
}
// Remove any invalid command options to construct the final arguments.
$options = drush_kernel_app_get_options(isset($arguments[0]) ? $arguments[0] : 'list');
$args = array('app');
foreach ($arguments as $argument) {
// Only act on options.
if ($argument[0] == '-') {
// Check if it's a valid option for the given command.
foreach ($options as $option) {
// Validate it as a shortcut or option.
if (stripos($argument, $option['name']) === 0 || stripos($argument, $option['shortcut']) === 0) {
// Append it to the valid argument list.
$args[] = $argument;
// Continue on with the next argument.
continue 2;
}
}
// It's invalid, so do not add it into the final $args.
}
else {
// Append all normal arguments to the valid list.
$args[] = $argument;
}
}
// Backup and override the server command line arguments.
$backup = $_SERVER['argv'];
$_SERVER['argv'] = $args;
$_SERVER['argc'] = count($args);
set_time_limit(0);
$input = new \Symfony\Component\Console\Input\ArgvInput();
$env = $input->getParameterOption(array('--env', '-e'), getenv('SYMFONY_ENV') ?: 'dev');
$debug = getenv('SYMFONY_DEBUG') !== '0' && !$input->hasParameterOption(array('--no-debug', '')) && $env !== 'prod';
$c['console.application']->run($input);
// Now that Composer has run, restore the original server arguments.
$_SERVER['argv'] = $backup;
$_SERVER['argc'] = count($backup);
}
/**
* Retrieves any valid options available from Composer.
*
* @param string $command
* The command to get the options for.
*
* @return array
* A list of valid options for the given command.
*/
function drush_kernel_app_get_options($command = 'list') {
static $commands = array();
$c = pimple_get_container('app');
// Retrieve all the commands available from Composer.
if (empty($commands)) {
$info = kernel_info();
if (!file_exists($info['console list'])) {
$file = $info['console list'];
/** @var \Symfony\Bundle\FrameworkBundle\Console\Application $application */
$application = $c['console.application'];
$output = new \Symfony\Component\Console\Output\StreamOutput(fopen($file, 'a', false));
$helper = new \Symfony\Component\Console\Helper\DescriptorHelper();
$helper->describe($output, $application, array(
'format' => 'json',
));
}
$contents = file_get_contents($info['console list']);
$json = json_decode($contents, TRUE);
$commands = $json['commands'];
}
// Find the available options for the given command and the "help" command.
$options = array();
foreach ($commands as $index => $cmd) {
// The name of the command determines what options are around.
if ($cmd['name'] == $command || $cmd['name'] == 'help') {
// Retrieve the options array and merge it into the given options.
$array = isset($cmd['definition']['options']) ? $cmd['definition']['options'] : array();
$options = array_merge($options, $array);
}
}
return $options;
}