provision.service.inc 2.8 KB
Newer Older
Adrian Rossouw's avatar
Adrian Rossouw committed
1
2
<?php

3
4
include_once('provision.config.inc');

Adrian Rossouw's avatar
Adrian Rossouw committed
5
class provisionService {
6
7
8
  protected $last_status;
  protected $tokens;

9
10
  function __construct() {
    $this->init();
11
12
  }

13
14
  function verify() {
    return TRUE;
15
16
  }

17
18
  function init() {
    return TRUE;
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
  /**
   * Clear internal state
   */
  protected function _clear_state() {
    $this->last_status = NULL;
    $this->tokens = NULL;
  }

  /**
   * Return the status of the last operation.
   *
   * @return
   *   TRUE or FALSE for success or failure; NULL if there was not a previous
   *   operation.
   */
  function status() {
    return $this->last_status;
  }

  /**
   * Log a notice into the logging system, if the last operation completed
   * succesfully.
   * 
   * @param $message
   *   The message to log, a string.
   */
  function succeed($message) {
    if ($this->last_status === TRUE) {
      drush_log(dt($message, $this->tokens), 'message');      
    }

    return $this;
  }

  /**
   * Log a notice into the logging system, if the last operation did not
   * complete succesfully.
   * 
   * @param $message
   *   Log this as a error to the logging system, if the $error_codes parameter
   *   has been set, otherwise, log this as a warning. If the operation
   *   specifies an additional reason for the operation failing, it will be
   *   appended to this message.
   *
   * @param error_codes
   *   Generate these system level errors using the provision error bitmasks.
   */
  function fail($message, $error_codes = NULL) {
    if (!empty($this->tokens['@reason'])) {
      $message .= ' (@reason)';
    }
    if ($this->last_status === FALSE) {
      if (is_null($error_codes)) {
        // Trigger a warning
        drush_log(dt($message, $this->tokens), 'warning');
      }
      else {
        // Trigger a sysem halting error
        drush_set_error($error_codes, dt($message, $this->tokens));
      }
    }

    return $this;
  }
85

drumm's avatar
drumm committed
86
87
88
  function write_alias() {
  }
}
89

Adrian Rossouw's avatar
Adrian Rossouw committed
90
91
92
93
94
95
96
97
98
99
100
101
102
class provisionService_null extends provisionService {

  function __get($name) {
    return false;
  }

  function __call($name, $args) {
    return false;
  }
}

provision_service('null', new provisionService_null());

103
104
105
106
107
108
109
110
111
112
113
114
115
116
/**
 * Retrieve and register provisionService objects. There should be at most one
 * of each service.
 *
 * @param $type
 *   The service type, like 'db', 'http', or 'file'. 'all' for an associative
 *   array of all services.
 * @param $_object
 *   Set the service type to $_object, an instance of a provisionService
 *   subclass.
 *
 * @return A provisionService object.
 */
function provision_service($type, $_object = NULL) {
Adrian Rossouw's avatar
Adrian Rossouw committed
117
118
  static $instances = null;

119
120
121
122
123
124
  if ($type === 'all') {
    return $instances;
  }

  if (is_object($_object)) {
    $instances[$type] = $_object;
Adrian Rossouw's avatar
Adrian Rossouw committed
125
126
127
128
129
130
131
132
  }

  if (isset($instances[$type])) {
    return $instances[$type];
  }

  return $instances['null'];
}