Commit cb967c59 authored by Adrian Rossouw's avatar Adrian Rossouw

Add some more comments to the config class changes.

parent ee1572de
......@@ -309,7 +309,17 @@ class provisionConfig_drushrc_site extends provisionConfig_drushrc {
}
}
/**
* Base class for data storage.
*
* This class provides a file locking mechanism for configuration
* files that may be susceptible to race conditions.
*
* The records loaded from the config and the records set in this
* instance are kept in separate arrays.
*
* When we lock the file, we load the latest stored info.
*/
class provisionConfig_data_store extends provisionConfig {
public $template = 'data_store.tpl.php';
public $key = 'record';
......@@ -329,17 +339,29 @@ class provisionConfig_data_store extends provisionConfig {
$this->load_data();
}
/**
* Ensure the file pointer is closed and the lock released upon destruction.
*/
function __destruct() {
// release the file lock if we have it.
$this->close();
}
/**
* Open the file.
*/
function open() {
if (!is_resource($this->fp)) {
$this->fp = fopen($this->filename(), "w+");
}
}
/**
* Lock the file from other writes.
*
* After the file has been locked, we reload the data from the file
* so that any changes we make will not override previous changes.
*/
function lock() {
if (!$this->locked) {
$this->open();
......@@ -351,12 +373,20 @@ class provisionConfig_data_store extends provisionConfig {
}
}
/**
* Put the contents in the locked file.
*
* We call the lock method here to insure we have the lock.
*/
function file_put_contents($filename, $text) {
$this->lock();
fwrite($this->fp, $text);
fflush($this->fp);
}
/**
* Release the write log on the data store file.
*/
function unlock() {
if ($this->locked && is_resource($this->fp)) {
flock($this->fp, LOCK_UN);
......@@ -364,12 +394,18 @@ class provisionConfig_data_store extends provisionConfig {
}
}
/**
* Close the file pointer and release the lock (if applicable).
*/
function close() {
if (is_resource($this->fp)) {
fclose($this->fp);
}
}
/**
* Load the data from the data store into our loaded_records property.
*/
function load_data() {
if (!$this->locked) {
// Once we have the lock we dont need to worry about it changing
......@@ -384,10 +420,18 @@ class provisionConfig_data_store extends provisionConfig {
}
}
/**
* Return the merged contents of the records from the data store , and the values set by us.
*
* This is basically the data that would be written to the file if we were to write it right now.
*/
function merged_records() {
return array_merge($this->loaded_records, $this->records);
}
/**
* Expose the merged records to the template file.
*/
function process() {
$this->data['records'] = array_filter(array_merge($this->loaded_records, $this->records));
}
......
......@@ -17,8 +17,8 @@ class provisionService extends provisionChainedState {
protected $configs = array();
protected $config_cache = array();
protected $config_cache = array();
private $_config = null;
// All services have the ability to have an associated restart command and listen port.
......@@ -42,6 +42,15 @@ class provisionService extends provisionChainedState {
return false;
}
/**
* Set the currently active configuration object.
*
* @param $config
* String: Name of config file. The key to the $this->configs array.
* @param $data
* Any optional information to be made available to templates. If a string, it will be
* turned into an array with the 'name' property the value of the string.
*/
function config($config, $data = array()) {
$this->_config = null;
......@@ -62,6 +71,9 @@ class provisionService extends provisionChainedState {
return $this;
}
/**
* Unlink the currently active config file.
*/
function unlink() {
if (is_object($this->_config)) {
$this->_config->unlink();
......@@ -70,6 +82,9 @@ class provisionService extends provisionChainedState {
return $this;
}
/**
* Write the currently active config file.
*/
function write() {
if (is_object($this->_config)) {
$this->_config->write();
......@@ -78,6 +93,9 @@ class provisionService extends provisionChainedState {
return $this;
}
/**
* Set a record on the data store of the currently active config file (if applicable).
*/
function record_set($arg1, $arg2 = null) {
if (is_object($this->_config)) {
if (is_object($this->_config->store)) {
......@@ -92,10 +110,16 @@ class provisionService extends provisionChainedState {
return $this;
}
/**
* Delete a record from the data store of the currently active config file (if applicable).
*/
function record_del($record) {
return $this->record_set($record, null);
}
/**
* Check if a record exists in the data store of the currently active config file (if applicable).
*/
function record_exists($record) {
if (is_object($this->_config)) {
if (is_object($this->_config->store)) {
......@@ -105,6 +129,9 @@ class provisionService extends provisionChainedState {
return FALSE;
}
/**
* Fetch record(s) from the data store of the currently active config file (if applicable).
*/
function record_get($key = null, $default = null) {
if (is_object($this->_config)) {
if (is_object($this->_config->store)) {
......@@ -193,10 +220,16 @@ class provisionService extends provisionChainedState {
$this->server = is_object($server) ? $server : d($server);
}
/**
* Set the currently active context of the service.
*/
function setContext($context) {
$this->context = is_object($context) ? $context : d($context);
}
/**
* Sync filesystem changes to the server hosting this service.
*/
function sync($path = NULL, $additional_options = array()) {
return $this->server->sync($path, $additional_options);
}
......@@ -216,6 +249,13 @@ class provisionService extends provisionChainedState {
}
}
/**
* Null service class.
*
* Not all services are necessary or turned on.
* This class ensures that not having a specific service
* doesnt result in catastrophic failure.
*/
class provisionService_null extends provisionService {
function __get($name) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment