provision.config.inc 5.9 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
<?php

// $Id$

/**
 * Provision configuration generation classes.
 */

class provisionConfig {
  public $template = null;
  public $data = array();
  public $description = null;
13
14
15
  protected $append = FALSE;
  protected $mode = NULL;
  protected $group = NULL;
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

  function __construct($data = array()) {
    if (is_null($this->template)) {
      throw(exception);
    }
    if (sizeof($data)) {
      $this->data = $data;
    }
  }

  function process() {
    parent::process();
    return true;
  }

  function filename() {
    return false;
  }


  private function load_template() {
    $reflect = new reflectionObject($this);
    $base_dir = dirname($reflect->getFilename());

    if (isset($this->template)) {
      $file = $base_dir . '/' . $this->template;
      drush_log("Template loaded: $file");
      if (file_exists($file) && is_readable($file)) {
        return file_get_contents($file);
      }
    }
    return false;
  }


  private function render_template($template, $variables) {
    drush_errors_off();
    extract($variables, EXTR_SKIP);  // Extract the variables to a local namespace
    ob_start();                      // Start output buffering
    eval('?>'. $template);                 // Generate content
    $contents = ob_get_contents();   // Get the contents of the buffer
    ob_end_clean();                  // End buffering and discard
    drush_errors_on();
    return $contents;                // Return the contents
  }

  function write() {
    $filename = $this->filename();
drumm's avatar
drumm committed
64
65
66
67
68
69
70
71

    // Make directory structure if it does not exist.
    if (!provision_service('file')->exists(dirname($filename))->status()) {
      provision_service('file')->mkdir(dirname($filename))
        ->succeed('Created directory @path.')
        ->fail('Could not create directory @path.');
    }

72
73
74
75
76
    if ($filename && is_writeable(dirname($filename))) {
      // manipulate data before passing to template.
      $this->process();

      if ($template = $this->load_template()) {
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
        // Make sure we can write to the file
        if (!is_null($this->mode) && !($this->mode & 0200) && provision_service('file')->exists($filename)->status()) {
          provision_service('file')->chmod($filename, $this->mode | 0200)
            ->succeed('Changed permissions of @path to @perm')
            ->fail('Could not change permissions of @path to @perm');
        }

        provision_service('file')->file_put_contents($filename, $this->render_template($template, $this->data), $this->append ? FILE_APPEND : 0)
          ->succeed('Generated config ' . (empty($this->description) ? $filename : $this->description), 'success');

        // Change the permissions of the file if needed
        if (!is_null($this->mode)) {
          provision_service('file')->chmod($filename, $this->mode)
            ->succeed('Changed permissions of @path to @perm')
            ->fail('Could not change permissions of @path to @perm');
        }
        if (!is_null($this->group)) {
          provision_service('file')->chgrp($filename, $this->group)
            ->succeed('Change group ownership of @path to @gid')
            ->fail('Could not change group ownership of @path to @gid');
97
98
99
100
101
102
103
104
105
        }
      }
    }
  }

  function unlink() {
    return provision_service('file')->unlink($this->filename())->status();
  }
}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124

/**
 * Specialized class to handle the creation of drushrc.php files.
 *
 * This is based on the drush_save_config code, but has been abstracted
 * for our purposes.
 */ 
class provisionConfig_drushrc extends provisionConfig {
  public $template = 'provision_drushrc.tpl.php';
  public $description = 'Drush configuration file';
  protected $mode = 0400;
  protected $context_name = 'drush';

  function filename() {
    return _drush_config_file($this->context_name);
  }

  function __construct($data = array()) {
    parent::__construct($data);
125
126
    $this->load_data();
  }
127

128
  function load_data() {
129
130
131
132
133
134
135
136
137
138
139
    // we fetch the context to pass into the template based on the context name
    $this->data = array_merge(drush_get_context($this->context_name), $this->data);
  }

  function process() {
    unset($this->data['context-path']);
    unset($this->data['config-file']);
    $this->data['option_keys'] = array_keys($this->data);
  }
}

140
141
142
/**
 * Class to write an alias records.
 */
143
class provisionConfig_drushrc_alias extends provisionConfig_drushrc {
drumm's avatar
drumm committed
144
145
  public $template = 'provision_drushrc_alias.tpl.php';

146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
  /**
   * An array of contexts to write. The first key will determine the title.
   *
   * @param $contexts
   *   An associative array of named contexts, like
   *   @code '\@name' => array('option1', 'option2'), @endcode
   *   will save the current values of option1 and option2 to @name.
   */
  function __construct($contexts) {
    $contexts_expanded = array();
    foreach ($contexts as $name => $options) {
      $contexts_expanded[$name] = array();
      foreach ($options as $key) {
        $contexts_expanded[$name][$key] = drush_get_option($key);
      }
161
162
    }

drumm's avatar
drumm committed
163
    $this->data = array(
164
165
      'aliasname' => reset(array_keys($contexts)),
      'contexts' => $contexts_expanded,
drumm's avatar
drumm committed
166
    );
167
168
169
  }

  function filename() {
drumm's avatar
drumm committed
170
    return drush_get_option('alias-path', drush_server_home() . '/.drush/aliases') . '/' . $this->data['aliasname'] . '.alias.drushrc.php'; 
171
172
173
  }
}

174
175
176
177
178
179
180
181
/**
 * Server level config for drushrc.php files.
 */
class provisionConfig_drushrc_server extends provisionConfig_drushrc {
  protected $context_name = 'user';
  public $description = 'Server drush configuration';
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
/**
 * Class for writing $platform/drushrc.php files.
 */
class provisionConfig_drushrc_platform extends provisionConfig_drushrc {
  protected $context_name = 'drupal';
  public $description = 'Platform Drush configuration file';
}

/**
 * Class for writing $platform/sites/$url/drushrc.php files.
 */
class provisionConfig_drushrc_site extends provisionConfig_drushrc {
  protected $context_name = 'site';
  public $template = 'provision_drushrc_site.tpl.php';
  public $description = 'Site Drush configuration file';
}