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

// $Id$

/**
 * Provision configuration generation classes.
 */

class provisionConfig {
  public $template = null;
  public $data = array();
12
  public $owner = null;
13
  public $description = null;
14 15 16
  protected $append = FALSE;
  protected $mode = NULL;
  protected $group = NULL;
17

18
  function __get($name) {
19 20
    if (isset($this->owner)) {
      return $this->owner->$name;
21 22 23 24
    }
  }
  
  function __construct($owner, $data = array()) {
25 26 27
    if (is_null($this->template)) {
      throw(exception);
    }
28
    $this->owner = d($owner);
29
    // we need to do this first so it's accessible to parse()
30
    if (sizeof($data)) {
31
      $this->data = $data;
32
    }
33 34
    $this->parsed_data = $this->parse();
    $this->data = array_merge($this->parsed_data, $this->data);
drumm's avatar
drumm committed
35 36
  }

37 38 39 40 41 42 43
  /**
   * Parse the existing config to load data
   *
   * This is called in the constructor, and should take care of
   * opening the file and parsing it into the data array.
   */
  function parse() {
44
    return array();
45 46
  }

47 48 49 50 51 52 53
  /**
   * process the data before writing it
   *
   * this is called right before the file is written
   *
   * this is a stub to be implemented by subclasses
   */
54 55 56 57
  function process() {
    return true;
  }

58 59 60 61 62
  /**
   * the filename where the filename is read/written from/to
   * 
   * this is a stub to be implemented by subclasses
   */
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
  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
96
    // Make directory structure if it does not exist.
97 98
    if (!provision_file()->exists(dirname($filename))->status()) {
      provision_file()->mkdir(dirname($filename))
drumm's avatar
drumm committed
99 100 101 102
        ->succeed('Created directory @path.')
        ->fail('Could not create directory @path.');
    }

103 104 105 106 107
    if ($filename && is_writeable(dirname($filename))) {
      // manipulate data before passing to template.
      $this->process();

      if ($template = $this->load_template()) {
108
        // Make sure we can write to the file
109 110
        if (!is_null($this->mode) && !($this->mode & 0200) && provision_file()->exists($filename)->status()) {
          provision_file()->chmod($filename, $this->mode | 0200)
111 112 113 114
            ->succeed('Changed permissions of @path to @perm')
            ->fail('Could not change permissions of @path to @perm');
        }

115
        provision_file()->file_put_contents($filename, $this->render_template($template, $this->data), $this->append ? FILE_APPEND : 0)
116 117 118 119
          ->succeed('Generated config ' . (empty($this->description) ? $filename : $this->description), 'success');

        // Change the permissions of the file if needed
        if (!is_null($this->mode)) {
120
          provision_file()->chmod($filename, $this->mode)
121 122 123 124
            ->succeed('Changed permissions of @path to @perm')
            ->fail('Could not change permissions of @path to @perm');
        }
        if (!is_null($this->group)) {
125
          provision_file()->chgrp($filename, $this->group)
126 127
            ->succeed('Change group ownership of @path to @gid')
            ->fail('Could not change group ownership of @path to @gid');
128 129 130 131 132 133
        }
      }
    }
  }

  function unlink() {
134
    provision_file()->unlink($this->filename())->status();
135 136
  }
}
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

/**
 * 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);
  }

154 155
  function __construct($owner, $data = array()) {
    parent::__construct($owner, $data);
156 157
    $this->load_data();
  }
158

159
  function load_data() {
160 161 162 163 164 165 166 167 168 169 170
    // 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);
  }
}

171 172 173
/**
 * Class to write an alias records.
 */
174
class provisionConfig_drushrc_alias extends provisionConfig_drushrc {
drumm's avatar
drumm committed
175 176
  public $template = 'provision_drushrc_alias.tpl.php';

177
  /**
drumm's avatar
drumm committed
178 179 180 181
   * @param $name
   *   String '\@name' for named context.
   * @param $options
   *   Array of string option names to save.
182
   */
183 184
  function __construct($owner, $data = array()) {
    parent::__construct($owner, $data);
drumm's avatar
drumm committed
185
    $this->data = array(
186
      'aliasname' => ltrim($owner, '@'),
187
      'options' => $data,
drumm's avatar
drumm committed
188
    );
189 190 191
  }

  function filename() {
drumm's avatar
drumm committed
192
    return drush_server_home() . '/.drush/' . $this->data['aliasname'] . '.alias.drushrc.php'; 
193 194 195
  }
}

196 197 198 199 200 201 202 203
/**
 * Server level config for drushrc.php files.
 */
class provisionConfig_drushrc_server extends provisionConfig_drushrc {
  protected $context_name = 'user';
  public $description = 'Server drush configuration';
}

204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
/**
 * 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';
}