Commit 19a68535 authored by Adrian Rossouw's avatar Adrian Rossouw

playing with the factory pattern to create an environment object

parent 6b78a6d6
......@@ -12,40 +12,11 @@
include_once(dirname(__FILE__) . '/../provision.service.inc');
function db_drush_init() {
include_once('mysql/mysql_service.inc');
$command = drush_get_command();
$command = explode(" ", $command['command']);
if (preg_match("/^provision-/", $command[0])) {
// TODO - remove this shit
$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']));
drush_set_default('master_db_host', urldecode($db['host']));
drush_set_default('db_host', urldecode($db['host']));
drush_set_default('master_db_type', $db['scheme']);
drush_set_default('db_type', $db['scheme']);
$creds = array();
$options = array(
'master_db_user' => 'user',
'master_db_passwd' => 'pass',
'master_db_host' => 'host',
'master_db_type' => 'type');
foreach ($options as $option => $key) {
$creds[$key] = drush_get_option($option, '');
}
provision_service('db', new provisionService_db_mysql($creds));
provisionService::spawn('db', 'mysql');
}
// this is where we generate the db service object.
}
function db_drush_exit() {
......@@ -67,11 +38,33 @@ function db_drush_help($section) {
class provisionService_db extends provisionService {
function __construct($creds) {
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']));
drush_set_default('master_db_host', urldecode($db['host']));
drush_set_default('db_host', urldecode($db['host']));
drush_set_default('master_db_type', $db['scheme']);
drush_set_default('db_type', $db['scheme']);
$creds = array();
$options = array(
'master_db_user' => 'user',
'master_db_passwd' => 'pass',
'master_db_host' => 'host',
'master_db_type' => 'type');
foreach ($options as $option => $key) {
$creds[$key] = drush_get_option($option, '');
}
$this->creds = $creds;
return TRUE;
}
function verify() {
if (!$this->can_create_database()) {
drush_set_error('PROVISION_CREATE_DB_FAILED');
......@@ -302,10 +295,8 @@ class provisionService_db_pdo extends provisionService_db {
protected $creds;
private $dsn;
function __construct($creds) {
parent::__construct($creds);
print_r($creds);
function init() {
parent::init();
$this->dsn = sprintf("%s:dbname=%s;host=%s", $this->PDO_type, $this->creds['name'], $this->creds['host']);
}
......
......@@ -5,12 +5,13 @@ include_once(dirname(__FILE__) . '/../provision.service.inc');
function http_drush_init() {
$command = drush_get_command();
$command = explode(" ", $command['command']);
if (preg_match("/^provision-/", $command[0])) {
include_once('apache/apache_service.inc');
provision_service('http', new provisionService_http_apache());
if (preg_match("/^provision-/", $command[0])) {
provisionService::spawn('http', 'apache');
}
}
class provisionService_http extends provisionService {
function init() {
// Set up defines for platform
$docroot = drush_get_option(array("r", "root"), $_SERVER['PWD']);
......@@ -48,11 +49,6 @@ function http_drush_init() {
drush_set_default('site_port', 80);
}
}
class provisionService_http extends provisionService {
/**
* Ask the web server to check for and load configuration changes.
*/
......
<?php
function provision_environment_factory($options) {
$classes = array('provisionServer', 'provisionPlatform', 'provisionSite');
if (provisionServer::evaluate($options)) {
$classname = array_shift($classes);
if (provisionPlatform::evaluate($options)) {
$classname = array_shift($classes);
if (provisionSite::evaluate($options)) {
$classname = array_shift($classes);
}
}
}
$object = new $classname($options);
$object->clean();
return $object;
}
class provisionEnvironment {
protected $options = array();
protected $properties = array();
protected $map = array();
protected $type = null;
function __get($name) {
if (array_key_exists($name, $this->properties)) {
return $this->properties[$name];
}
}
function __set($name, $value) {
if (!property_exists($this, $name)) {
$this->properties[$name] = $value;
$this->map[$name] = (array_key_exists($name, $this->map)) ? $this->map[$name] : $this->type;
}
else {
$this->$name = $value;
}
}
function __isset($name) {
if (property_exists($this->properties, $name) || property_exists($this, $name)) {
return TRUE;
}
return FALSE;
}
function __unset($name) {
if (property_exists($this->properties, $name)) {
unset($this->properties[$name]);
}
elseif (property_exists($this, $name)) {
unset($this->$name);
}
}
function __construct($options) {
$this->options = $options;
}
function clean() {
$this->options = array();
}
function setProperty($field, $default = null) {
if (isset($this->options[$field])) {
$this->$field = $this->options[$field];
}
else {
$this->$field = $default;
}
}
function setMap($field, $context) {
$this->map[$field] = $context;
}
}
class provisionServer extends provisionEnvironment {
public static function evaluate($options) {
return TRUE;
}
function __construct($options) {
parent::__construct($options);
$this->type = 'server';
$this->setProperty('remote_host', 'localhost');
$this->setProperty('script_user', get_current_user());
$this->setProperty('aegir_root', '/var/aegir');
$this->setProperty('config_path', $this->aegir_root . '/config');
$this->setProperty('backup_path', $this->aegir_root . '/backup');
}
}
class provisionPlatform extends provisionServer {
public static function evaluate($options) {
if ($options['root']) {
return true;
}
}
function __construct($options) {
parent::__construct($options);
$this->type = 'platform';
$this->setProperty('root', $_SERVER['PWD']);
}
}
class provisionSite extends provisionPlatform {
public static function evaluate($options) {
if ($options['uri']) {
return true;
}
}
function __construct($options) {
parent::__construct($options);
$this->type = 'site';
$this->setProperty('uri');
}
}
<?php
class provisionService {
function __construct() {
$this->init();
}
function verify() {
return TRUE;
......@@ -13,6 +10,27 @@ class provisionService {
return TRUE;
}
static function spawn($service, $default = null) {
$reflect = new reflectionClass('provisionService_' . $service);
$base_dir = dirname($reflect->getFilename());
$type = drush_get_option($service . '-service-type', $default);
if ($type) {
$file = sprintf("%s/%s/%s_service.inc", $base_dir, $type, $type);
$className = sprintf("provisionService_%s_%s", $service, $type);
if (file_exists($file)) {
drush_log("Loading $type driver for the $service service");
include_once($file);
provision_service($service, new $className())->init();
return TRUE;
}
}
return false;
}
}
class provisionConfig {
......
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