Commit f7b8dabe authored by Adrian Rossouw's avatar Adrian Rossouw

Introduce magic getter/setters into the service, along with the owner property.

Constructing a service requires an owner alias name, which is at the moment only
@self, or the left hand alias passed in a drush command.

If properties aren't defined in the service class, when you get or set them,
they will be set on the owner's environment object.

In the backend I am using d($this->owner) to access the properties.
parent 88d905f9
......@@ -37,16 +37,17 @@ function db_drush_help($section) {
class provisionService_db extends provisionService {
function init() {
$master_db = drush_get_option('master_db');
$db = parse_url($master_db);
drush_set_default('master_db_user', urldecode($db['user']));
drush_set_default('master_db_passwd', urldecode($db['pass']));
$this->setProperty('master_db');
$db = parse_url($this->master_db);
drush_set_default('master_db_host', urldecode($db['host']));
drush_set_default('db_host', urldecode($db['host']));
$this->setProperty('master_db_user', urldecode($db['user']));
$this->setProperty('master_db_passwd', urldecode($db['pass']));
drush_set_default('master_db_type', $db['scheme']);
drush_set_default('db_type', $db['scheme']);
$this->setProperty('master_db_host', urldecode($db['host']));
$this->setProperty('db_host', urldecode($db['host']));
$this->setProperty('master_db_type', $db['scheme']);
$this->setProperty('db_type', $db['scheme']);
$creds = array();
$options = array(
......@@ -56,7 +57,7 @@ class provisionService_db extends provisionService {
'master_db_type' => 'type');
foreach ($options as $option => $key) {
$creds[$key] = drush_get_option($option, '');
$creds[$key] = $this->$option;
}
$this->creds = $creds;
return TRUE;
......
......@@ -4,13 +4,13 @@
class provisionService_http_apache extends provisionService_http {
function init(&$owner) {
parent::init($owner);
$owner->setProperty('apache_site_conf_path', $owner->config_path . '/vhost.d');
$owner->setProperty('apache_platform_conf_path', $owner->config_path . '/platform.d');
function init() {
parent::init();
$this->setProperty('apache_site_conf_path', $this->config_path . '/vhost.d');
$this->setProperty('apache_platform_conf_path', $this->config_path . '/platform.d');
// Commands
$owner->setProperty('restart_cmd', _provision_default_restart_cmd());
$this->setProperty('restart_cmd', _provision_default_restart_cmd());
}
function create_site_config($url) {
......
......@@ -9,17 +9,17 @@ function http_provision_services() {
class provisionService_http extends provisionService {
function init(&$owner) {
parent::init($owner);
function init() {
parent::init();
// System account
$owner->setProperty('web_group', _provision_default_web_group());
$this->setProperty('web_group', _provision_default_web_group());
// Redirection urls
$owner->setProperty('web_disable_url', $this->master_url .'/hosting/disabled');
$owner->setProperty('web_maintenence_url', $this->master_url .'/hosting/maintenance');
$this->setProperty('web_disable_url', $this->master_url .'/hosting/disabled');
$this->setProperty('web_maintenence_url', $this->master_url .'/hosting/maintenance');
$owner->setProperty('web_ip', '127.0.0.1');
$owner->setProperty('web_port', 80);
$this->setProperty('web_ip', '127.0.0.1');
$this->setProperty('web_port', 80);
}
/**
......
......@@ -60,9 +60,10 @@ include_once('provision.inc');
* @see provision_load_critical()
*/
function provision_drush_init() {
d('@self', provision_environment_factory(drush_get_merged_options()));
print_r(d());
$name = drush_get_option('name', '@self', 'alias');
provision_environment_factory($name, drush_get_merged_options(), true);
print_r(d($name));
$load = sys_getloadavg();
if (provision_load_critical($load)) {
drush_set_error('PROVISION_OVERLOAD', dt("load on system too heavy (@load), aborting", array('@load' => join(" ", $load))));
......
<?php
function provision_environment_factory($options) {
function provision_environment_factory($name, $options, $root_object = FALSE) {
$classes = array('provisionServer', 'provisionPlatform', 'provisionSite');
if (provisionServer::evaluate($options)) {
......@@ -13,24 +13,31 @@ function provision_environment_factory($options) {
}
}
$object = new $classname($options);
$object->init();
$object->clean();
return $object;
d($name, new $classname($name, $options), $root_object);
d($name)->init();
d($name)->clean();
return d($name);
}
function & d($short_name = '@self', $object = null) {
function & d($name = '@self', $object = null, $root_object = FALSE) {
static $instances = null;
static $default_instance = '@self';
if (is_object($object)) {
$instances[$short_name] = $object;
}
if ($name) {
if ($root_object) {
$default_instance = $name;
}
if (isset($instances[$short_name])) {
return $instances[$short_name];
if (is_object($object)) {
$instances[$name] = $object;
}
if (isset($instances[$name])) {
return $instances[$name];
}
}
return $instances['@self'];
return $instances[$default_instance];
}
......@@ -38,7 +45,7 @@ class provisionEnvironment {
protected $options = array();
protected $properties = array();
protected $map = array();
protected $short_name = null;
protected $name = null;
protected $type = null;
function __get($name) {
......@@ -74,8 +81,10 @@ class provisionEnvironment {
}
function __construct($options) {
function __construct($name, $options) {
$this->name = $name;
$this->options = $options;
unset($this->options['packages']);
}
function init() {
......@@ -109,8 +118,10 @@ class provisionServer extends provisionEnvironment {
return TRUE;
}
function __construct($options) {
parent::__construct($options);
function init() {
parent::init();
print_r($this->options);
$this->type = 'server';
$this->setProperty('remote_host', 'localhost');
$this->setProperty('script_user', get_current_user());
......@@ -120,11 +131,6 @@ class provisionServer extends provisionEnvironment {
$this->setProperty('config_path', $this->aegir_root . '/config');
$this->setProperty('alias_path', $this->config_path . '/drush.d');
$this->setProperty('master_url');
}
function init() {
parent::init();
$this->load_services();
}
......@@ -148,8 +154,8 @@ class provisionServer extends provisionEnvironment {
if (file_exists($file)) {
drush_log("Loading $type driver for the $service service");
include_once($file);
$object = new $className();
$object->init($this->options);
$object = new $className($this->name);
$object->init();
$this->services[$service] = $object;
$this->$service = $object;
}
......@@ -167,8 +173,8 @@ class provisionPlatform extends provisionServer {
}
}
function __construct($options) {
parent::__construct($options);
function init() {
parent::init();
$this->type = 'platform';
$this->setProperty('root', $_SERVER['PWD']);
......@@ -187,8 +193,8 @@ class provisionSite extends provisionPlatform {
}
}
function __construct($options) {
parent::__construct($options);
function init() {
parent::init();
$this->type = 'site';
$this->setProperty('uri');
$this->setProperty('site_url', str_replace(array('https://', 'http://'), '', $this->uri));
......
......@@ -27,26 +27,6 @@ drush_errors_on();
* values, that allow sites to be more easily moved between different provisioned platforms.
*/
/**
* Returns the aggregated site data from both the pre-existing site.php file, and the options passed to Drush
*
* This function merges the data from the command line parser, and the information already saved by previous invokations
* of the api. This provides a single view of all data relating to the site.
* This function also provides sensible defaults for some of the settings.
*
* @param url
* The url of the site being invoked.
* @return
* An associated array containing the relevant settings for the site.
*/
function provision_get_site_data($url) {
if ($site_data['aliases'] && !is_array($site_data['aliases'])) {
$site_data['aliases'] = explode(",", $site_data['aliases']);
}
return $site_data;
}
/**
* Save modified options to the drushrc.php file
......@@ -59,7 +39,6 @@ function provision_save_site_data() {
drush_set_option('config-file', drush_get_option('config-file', null, 'site'));
drush_unset_option('config-file', 'site');
drush_save_config($context);
$filename = _drush_config_file($context);
......
<?php
class provisionService {
protected $owner_name = '@self';
protected $owner = '@self';
protected $last_status;
protected $tokens;
function __construct() {
$this->init();
function __get($name) {
return d($this->owner)->$name;
}
function __set($name, $value) {
d($this->owner)->$name = $value;
}
function __isset($name) {
return isset(d($this->owner)->$name);
}
function __unset($name) {
unset(d($this->owner)->$name);
}
function __construct($owner) {
$this->owner = $owner;
}
function verify() {
......@@ -17,6 +34,10 @@ class provisionService {
return TRUE;
}
function setProperty($field, $default = null) {
d($this->owner)->setProperty($field, $default);
}
/**
* Clear internal state
*/
......@@ -87,14 +108,27 @@ class provisionConfig {
public $template = null;
public $data = array();
public $description = null;
public $owner = '@self';
function __construct($data = array()) {
if (is_null($this->template)) {
throw(exception);
}
if (sizeof($data)) {
$this->data = $data;
}
function __get($name) {
return d($this->owner)->$name;
}
function __set($name, $value) {
d($this->owner)->$name = $value;
}
function __isset($name) {
return isset(d($this->owner)->$name);
}
function __unset($name) {
unset(d($this->owner)->$name);
}
function __construct($owner) {
$this->owner = $owner;
}
function process() {
......@@ -133,7 +167,6 @@ class provisionConfig {
return $contents; // Return the contents
}
function write() {
$filename = $this->filename();
if ($filename && is_writeable(dirname($filename))) {
......
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