Commit 3a73e1a9 authored by Steven Jones's avatar Steven Jones

Refactor the example service to use the autoloader.

parent 00ab730f
<?php
/**
* A basic configuration file class.
*
* Just a file containing the value passed to us.
*/
class Provision_Config_Example extends Provision_Config {
/**
* Template file to load. In the same directory as this class definition.
*/
public $template = 'basic.tpl.php';
/**
* Where the file generated will end up.
*
* It is extremely important that this path is only made up of information
* relative to this class, and does not use drush_get_option or the d() accessor.
*/
function filename() {
return $this->example_config_path . '/example.conf';
}
/**
* Override the write method.
*/
function write() {
parent::write();
// Sync the config to a remote server if necessary.
$this->data['server']->sync($this->filename());
}
/**
* Override the unlink method.
*/
function unlink() {
parent::unlink();
// Remove the config from a remote server if necessary.
$this->data['server']->sync($this->filename());
}
}
<?php
/**
* The service type base class.
*
* All implementations of the service type will inherit this class.
* This class should define the 'public API' to be used by the rest
* of the system, which should not expose implementation details.
*/
class Provision_Service_example extends Provision_Service {
public $service = 'example';
/**
* Initialize the service along with the server object.
*/
function init() {
// REMEMBER TO CALL THE PARENT!
parent::init();
/**
* We do not need to use this in our example.
*
* You would extend this if you needed to save values
* for all possible implementations of this service type.
*/
}
/**
* Called on provision-verify.
*
* We change what we will do based on what the
* type of object the command is being run against.
*/
function verify() {
$this->create_config(d()->type);
$this->parse_configs();
}
/**
* PUBLIC API!
*
* These are just a basic example, the thing to notice here is that these
* methods are just stubs for later implementations to extend.
*
* If a specific implementation doesn't need to parse the configuration files
* that are generated for instance, it can just not implement the stub.
*/
/**
* Commonly something like running the restart_cmd or sending SIGHUP to a process.
*/
function parse_configs() {
return TRUE;
}
/**
* Generate a site specific configuration file
*/
function create_site_config() {
return TRUE;
}
/**
* Remove an existing site configuration file.
*/
function delete_site_config() {
return TRUE;
}
/**
* Add a new platform specific configuration file.
*/
function create_platform_config() {
return TRUE;
}
/**
* Remove an existing platform configuration file.
*/
function delete_platform_config() {
return TRUE;
}
/**
* Create a new server specific configuration file.
*/
function create_server_config() {
return TRUE;
}
/**
* Remove an existing server specific configuration file
*/
function delete_server_config() {
return TRUE;
}
}
<?php
// $Id$
/**
* @file
* A 'basic' implementation of the 'example' service type.
*/
/**
* A class containing the 'basic' implementation of the 'example' service.
*
* This class is conditionally loaded when the "--example_service_type=basic"
* option is passed to provision-save commands run on servers.
*
* The above flag is generated by the hosting counterpart of this class, which
* provides the front end to configure all these fields.
*
* The responsibilities of this class include responding and saving any
* values that are passed to it, and also to override the portions of
* the public API for this service that are necessary.
*/
class Provision_Service_example_basic extends Provision_Service_example {
/**
* Some common options handled upstream by the base service classes.
*/
/**
* This service needs to have a port specified for it.
*/
public $has_port = TRUE;
/**
* The default value for the port input.
*/
function default_port() {
return 12345;
}
/**
* This service needs to be restarted with a shell command.
*/
public $has_restart_cmd = TRUE;
/**
* The default value for the restart command input.
*/
function default_restart_cmd() {
return "/usr/bin/true";
}
/**
* Initialize this class, including option handling.
*/
function init() {
// REMEMBER TO CALL THE PARENT!
parent::init();
/**
* Register configuration classes for the create_config / delete_config methods.
*/
$this->configs['server'][] = 'Provision_Config_Example';
/**
* Setting and storing a value.
*
* You will most commonly use :
* $this->server->setProperty('example_field', 'default');
*
* This helper will check for an existing saved value, overridden
* by a command line option falling back to the default.
*
* This is the format used by everything you want configurable from
* the front end or command line.
*
* These values will be saved in ~/.drush/server_name.drush.alias.inc.
*/
$this->server->setProperty('example_field', 'default');
/**
* Non configurable values.
*
* If you want to generate values for use in your code or templates,
* but don't want them to be overridden you would use the following format.
*
* $this->server->example_config_path = $this->server->config_path . '/example.d'
*
* This will mean the value will change if you change the config path, but
* you dont need to pass the right input to your command to get there,
* and it's impossible to change the values.
*/
$this->server->example_config_path = $this->server->config_path . '/example.d';
}
/**
* Pass additional values to the config file templates.
*
* Even though the $server variable will be available in your template files,
* you may wish to pass additional calculated values to your template files.
*
* Consider this something like the hook_preprocess stuff in drupal.
*/
function config_data($config = null, $class = null) {
// This format of calling the parent is very important!
$data = parent::config_data($config, $class);
/**
* This value will become available as $example_current_time
* in all the config files generated by this service.
*
* You could also choose to only conditionally pass values based on
* the parameters.
*/
$data['example_current_time'] = date(DATE_COOKIE, mktime());
return $data;
}
/**
* Implementation of service verify.
*/
function verify() {
parent::verify();
if ($this->context->type == 'server') {
// Create the configuration file directory.
provision_file()->create_dir($this->server->example_config_path, dt("Example configuration"), 0700);
// Sync the directory to the remote server if needed.
$this->sync($this->server->example_config_path);
}
}
}
<?php
// $Id$
/**
* @file
* A 'basic' implementation of the 'example' service type.
*/
/**
* A class containing the 'basic' implementation of the 'example' service.
*
* This class is conditionally loaded when the "--example_service_type=basic"
* option is passed to provision-save commands run on servers.
*
* The above flag is generated by the hosting counterpart of this class, which
* provides the front end to configure all these fields.
*
* The responsibilities of this class include responding and saving any
* values that are passed to it, and also to override the portions of
* the public API for this service that are necessary.
*/
class provisionService_example_basic extends provisionService_example {
/**
* Some common options handled upstream by the base service classes.
*/
/**
* This service needs to have a port specified for it.
*/
public $has_port = TRUE;
/**
* The default value for the port input.
*/
function default_port() {
return 12345;
}
/**
* This service needs to be restarted with a shell command.
*/
public $has_restart_cmd = TRUE;
/**
* The default value for the restart command input.
*/
function default_restart_cmd() {
return "/usr/bin/true";
}
/**
* Initialize this class, including option handling.
*/
function init() {
// REMEMBER TO CALL THE PARENT!
parent::init();
/**
* Register configuration classes for the create_config / delete_config methods.
*/
$this->configs['server'][] = 'provisionConfig_example';
/**
* Setting and storing a value.
*
* You will most commonly use :
* $this->server->setProperty('example_field', 'default');
*
* This helper will check for an existing saved value, overridden
* by a command line option falling back to the default.
*
* This is the format used by everything you want configurable from
* the front end or command line.
*
* These values will be saved in ~/.drush/server_name.drush.alias.inc.
*/
$this->server->setProperty('example_field', 'default');
/**
* Non configurable values.
*
* If you want to generate values for use in your code or templates,
* but don't want them to be overridden you would use the following format.
*
* $this->server->example_config_path = $this->server->config_path . '/example.d'
*
* This will mean the value will change if you change the config path, but
* you dont need to pass the right input to your command to get there,
* and it's impossible to change the values.
*/
$this->server->example_config_path = $this->server->config_path . '/example.d';
}
/**
* Pass additional values to the config file templates.
*
* Even though the $server variable will be available in your template files,
* you may wish to pass additional calculated values to your template files.
*
* Consider this something like the hook_preprocess stuff in drupal.
*/
function config_data($config = null, $class = null) {
// This format of calling the parent is very important!
$data = parent::config_data($config, $class);
/**
* This value will become available as $example_current_time
* in all the config files generated by this service.
*
* You could also choose to only conditionally pass values based on
* the parameters.
*/
$data['example_current_time'] = date(DATE_COOKIE, mktime());
return $data;
}
/**
* Implementation of service verify.
*/
function verify() {
parent::verify();
if ($this->context->type == 'server') {
// Create the configuration file directory.
provision_file()->create_dir($this->server->example_config_path, dt("Example configuration"), 0700);
// Sync the directory to the remote server if needed.
$this->sync($this->server->example_config_path);
}
}
}
/**
* A basic configuration file class.
*
* Just a file containing the value passed to us.
*/
class provisionConfig_example extends Provision_Config {
/**
* Template file to load. In the same directory as this class definition.
*/
public $template = 'basic.tpl.php';
/**
* Where the file generated will end up.
*
* It is extremely important that this path is only made up of information
* relative to this class, and does not use drush_get_option or the d() accessor.
*/
function filename() {
return $this->example_config_path . '/example.conf';
}
/**
* Override the write method.
*/
function write() {
parent::write();
// Sync the config to a remote server if necessary.
$this->data['server']->sync($this->filename());
}
/**
* Override the unlink method.
*/
function unlink() {
parent::unlink();
// Remove the config from a remote server if necessary.
$this->data['server']->sync($this->filename());
}
}
// TODO: Remove this file.
// Classes are now found in the 'Provision' folder.
......@@ -9,7 +9,27 @@
* It matches the same service definition in the hosting front end.
*/
include_once(dirname(__FILE__) . '/../provision.service.inc');
/**
* Implements hook_drush_init().
*/
function example_drush_init() {
example_provision_register_autoload();
}
/**
* Register our directory as a place to find provision classes.
*
* This is needed so that provision can autoload our classes, which means we
* don't need to specifically include the files before we use the class, which
* is quite useful!
*/
function example_provision_register_autoload() {
static $loaded = FALSE;
if (!$loaded) {
$loaded = TRUE;
provision_autoload_register_prefix('Provision_', __DIR__);
}
}
/**
* Expose the service type this extension defines to provision.
......@@ -25,101 +45,6 @@ include_once(dirname(__FILE__) . '/../provision.service.inc');
* An array with the service type the key, and the default implementation the value.
*/
function example_provision_services() {
example_provision_register_autoload();
return array('example' => NULL);
}
/**
* The service type base class.
*
* All implementations of the service type will inherit this class.
* This class should define the 'public API' to be used by the rest
* of the system, which should not expose implementation details.
*/
class provisionService_example extends Provision_Service {
public $service = 'http';
/**
* Initialize the service along with the server object.
*/
function init() {
// REMEMBER TO CALL THE PARENT!
parent::init();
/**
* We do not need to use this in our example.
*
* You would extend this if you needed to save values
* for all possible implementations of this service type.
*/
}
/**
* Called on provision-verify.
*
* We change what we will do based on what the
* type of object the command is being run against.
*/
function verify() {
$this->create_config(d()->type);
$this->parse_configs();
}
/**
* PUBLIC API!
*
* These are just a basic example, the thing to notice here is that these
* methods are just stubs for later implementations to extend.
*
* If a specific implementation doesn't need to parse the configuration files
* that are generated for instance, it can just not implement the stub.
*/
/**
* Commonly something like running the restart_cmd or sending SIGHUP to a process.
*/
function parse_configs() {
return TRUE;
}
/**
* Generate a site specific configuration file
*/
function create_site_config() {
return TRUE;
}
/**
* Remove an existing site configuration file.
*/
function delete_site_config() {
return TRUE;
}
/**
* Add a new platform specific configuration file.
*/
function create_platform_config() {
return TRUE;
}
/**
* Remove an existing platform configuration file.
*/
function delete_platform_config() {
return TRUE;
}
/**
* Create a new server specific configuration file.
*/
function create_server_config() {
return TRUE;
}
/**
* Remove an existing server specific configuration file
*/
function delete_server_config() {
return TRUE;
}
}
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