-
Notifications
You must be signed in to change notification settings - Fork 0
/
smt.php
133 lines (110 loc) · 3.9 KB
/
smt.php
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
/**
* @file
* Provides main functionality.
*/
// Include composer dependencies.
// Manual installation.
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
// Installation with composer.
elseif (file_exists(__DIR__ . '/../../autoload.php')) {
require_once __DIR__ . '/../../autoload.php';
}
else {
echo 'You must set up the project dependencies using "composer install"' . PHP_EOL;
}
// Include base project config and functions.
require __DIR__ . '/includes/bootstrap.inc';
use Symfony\Component\Console\Application;
use Symfony\Component\Console\ConsoleEvents;
use Symfony\Component\Console\Event\ConsoleCommandEvent;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\EventDispatcher\EventDispatcher;
// Registering commands.
$command_list = [
'SSHFSMountTool\Command\MountCommand',
'SSHFSMountTool\Command\UnmountCommand',
'SSHFSMountTool\Command\AddCommand',
'SSHFSMountTool\Command\RemoveCommand',
'SSHFSMountTool\Command\ListCommand',
'SSHFSMountTool\Command\StatusCommand',
'SSHFSMountTool\Command\ConfigCommand',
'SSHFSMountTool\Command\InfoCommand',
'SSHFSMountTool\Command\HelpCommand',
'SSHFSMountTool\Command\CdCommand',
'SSHFSMountTool\Command\SshCommand',
];
$command_names = ['completion'];
$command_aliases = [];
$commands = [];
foreach ($command_list as $key => $command_class) {
$command = new $command_class();
$commands[] = $command;
$command_names[] = $command->getname();
$command_aliases = array_merge($command->getAliases(), $command_aliases);
}
$aliases = array_merge($command_names, $command_aliases);
// Emulate $app->setDefaultCommand for handling arguments.
$first_arg_is_command = FALSE;
if (isset($_SERVER['argv'][1])) {
foreach ($aliases as $key => $alias) {
if ($alias == $_SERVER['argv'][1]) {
$first_arg_is_command = TRUE;
break;
}
}
}
if (!$first_arg_is_command) {
array_splice($_SERVER['argv'], 1, 0, 'mount');
}
// Define app.
$app = new Application('SSHFS Mount Tool', get_version());
// Add global option to the app.
$app->getDefinition()->addOption(
new InputOption('global', 'g', InputOption::VALUE_NONE, 'Use global connections')
);
// Add info command as app option.
$app->getDefinition()->addOption(
new InputOption('info', 'i', InputOption::VALUE_NONE, 'Display information about dependencies')
);
// Handle global, info and help options.
$dispatcher = new EventDispatcher();
$app->setDispatcher($dispatcher);
$dispatcher->addListener(ConsoleEvents::COMMAND, function (ConsoleCommandEvent $event) {
global $preferences;
$input = $event->getInput();
if ($input->getOption('global')) {
$preferences['global'] = TRUE;
}
if ($input->getOption('info')) {
$command = $event->getCommand();
$application = $command->getApplication();
$info_command = $application->find('info');
$output = $event->getOutput();
$exitCode = $info_command->run($input, $output);
exit($exitCode);
// TODO: find proper way to exit without running default command after
// return 0;
}
// Seems like something broken after overriding help command
// and command like "smt mount -h" don't work anymore, so handle it manually.
// Not sure that this is the best way, but it works now.
if ($input->getOption('help')) {
$command = $event->getCommand();
$application = $command->getApplication();
$help_command = $application->find('help');
// $input->setArgument('command_name', $input->getArgument('command'));
$new_input = new ArrayInput(array('command_name' => $input->getArgument('command')));
$output = $event->getOutput();
$exitCode = $help_command->run($new_input, $output);
exit($exitCode);
// TODO: find proper way to exit without running default command after
// return 0;
}
});
$app->addCommands($commands);
$app->setDefaultCommand('mount');
$app->run();