-
Notifications
You must be signed in to change notification settings - Fork 1
/
vhost_gen.drush.inc
117 lines (107 loc) · 3.26 KB
/
vhost_gen.drush.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
107
108
109
110
111
112
113
114
115
116
117
<?php
/**
* @file
* A drush command generating Apache VirtualHost configuration file.
*
* @author
* Martin Martinov <[email protected]>
*/
/**
* Implementation of hook_drush_command().
*
* @return
* An associative array describing our command(s).
*/
function vhost_gen_drush_command() {
$items = array();
// The 'vhost-generate' command
$items['vhost-generate'] = array(
'description' => "Generate Apache 2 VirtualHost configuration.",
'arguments' => array(
'server-name' => 'Value to use for ServerName, e.g. example.com.',
),
'options' => array(
'[email protected]' => 'Value to use for ServerAdmin (email address).',
'document-root=path' => 'Path to DocumentRoot. Default is /var/www/[server-name].',
'log-level=level' => 'Possible values: debug, info, notice, warn, error,
crit, alert, emerg. Default is warn.',
),
'examples' => array(
'drush vhg example.com' => 'Generate a VirtualHost for example.com.',
'drush vhg example.com [email protected] --log-level=error'
=> 'Adds ServerAdmin line to the generated configuration file and
sets the LogLevel to error.',
),
'aliases' => array('vhg'),
'bootstrap' => DRUSH_BOOTSTRAP_DRUSH, // No bootstrap at all.
);
return $items;
}
/**
* Implementation of hook_drush_help().
*
* @param
* A string with the help section (prepend with 'drush:')
*
* @return
* A string with the help text for your command.
*/
function vhost_gen_drush_help($section) {
switch ($section) {
case 'drush:vhost-generate':
return dt("A command to generate Apache 2 VirtualHost configurations.
See README.txt for real-life usage example.");
}
}
/**
* Drush command callback. This is where the action takes place.
*
* @todo
* Validation for file names and paths.
* @todo
* Check if the value for LogLevel param is valid.
*
* @param $server_name
* A string with value for ServerName line.
*/
function drush_vhost_generate($server_name) {
if (!isset($server_name)) {
drush_set_error('DRUSH_VHOST_GEN_ARG_FAIL',
dt('You have to provide an argument for ServerName.'));
}
else {
$options = array(
'ServerName' => $server_name,
'ServerAdmin' => drush_get_option('server-admin', ''),
'DocumentRoot' => drush_get_option('document-root',
'/var/www/' . $server_name),
'LogLevel' => drush_get_option('log-level', 'warn'),
);
drush_vhost_gen_generate($options);
}
}
/**
* Generate the config file using the template.
*
* @todo
* Add support for ServerAlias
*
* @param $options
* An array with values for different options.
*/
function drush_vhost_gen_generate($options) {
$vhost_cfg = file_get_contents(dirname(__FILE__) . '/vhost.tpl.txt');
if (!empty($options['ServerAdmin'])) {
$options['ServerAdmin'] = "ServerAdmin {$options['ServerAdmin']}" . PHP_EOL;
}
$vhost_cfg = str_replace('!ServerAdmin', $options['ServerAdmin'], $vhost_cfg);
unset($options['ServerAdmin']);
$needles = array();
$replacements = array();
foreach ($options as $option_name => $option_value) {
$needles[] = "!{$option_name}";
$replacements[] = $option_value;
}
$vhost_cfg = str_replace($needles, $replacements, $vhost_cfg);
drush_print($vhost_cfg);
}