Commit 0960b2a9 authored by skwashd's avatar skwashd

Issue #2429413 by skwashd: Refactor module

Changes of note:
* Switch to composer manager for the autoloader
* Implement settings form for providing credentials
* Provide helper functions for instantiating the classes
* Add drush sql-sanitize support for removing passwords
parent 7a10d572
<?php
/**
* @file
* Acquia SDK module drush commands.
*/
/**
* Implements hook_sql_sync_sanitize().
*/
function acquiasdk_drush_sql_sync_sanitize($source) {
drush_sql_register_post_sync_op('acquiasdk',
dt('Remove Acquia SDK configuration.'),
"DELETE FROM variable WHERE name IN ('acquiasdk_cloudapi_password', 'acquiasdk_network_key');"
);
}
......@@ -2,4 +2,6 @@ name = Acquia SDK for PHP
description = Acquia SDK for PHP provides a Drupal interface to the Acquia SDK for PHP library.
package = Development
core = 7.x
php = 5.3.2
php = 5.3.3
dependencies[] = composer_manager
<?php
/**
* Acquia SDK module installation functions.
*/
/**
* Implements hook_uninstall().
*/
function acquiasdk_uninstall() {
$vars = array(
'acquiasdk_cloudapi_username',
'acquiasdk_cloudapi_password',
'acquiasdk_network_id',
'acquiasdk_network_key',
);
foreach ($vars as $var) {
variable_del($var);
}
}
<?php
/**
* @file
* Provides primary Drupal hook implementations.
* Acquia SDK module functions.
*/
use Acquia\Cloud\Api\CloudApiClient;
use Acquia\Network\AcquiaNetworkClient;
use Acquia\Common\Services;
/**
* Implements hook_menu().
*/
function acquiasdk_menu() {
$items = array();
$items['admin/config/development/acquiasdk'] = array(
'title' => 'Acquia Cloud API Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('acquiasdk_settings'),
'access arguments' => array('administer acquiasdk'),
);
return $items;
}
/**
* Implements hook_permission().
*/
function acquiasdk_permission() {
$perms = array();
$parms['administer acquiasdk'] = array(
'title' => t('Administer Acquia SDK'),
'description' => t('Perform administration functions for Acquia SDK module.'),
);
return $perms;
}
/**
* Implementation of hook_init().
* Acquia SDK module settings form.
*/
function acquiasdk_init() {
acquiasdk_classloader();
function acquiasdk_settings($form, &$form_state) {
$form = array();
$account_link = l(t('Acquia account page'), 'https://accounts.acquia.com/account', array('attributes' => array('target' => '_blank')));
$form['cloudapi'] = array(
'#type' => 'fieldset',
'#title' => t('Cloud API'),
'#description' => t('Obtain your Acquia Cloud API credentials from your !link.', array('!link' => $account_link)),
);
$form['cloudapi']['acquiasdk_cloudapi_username'] = array(
'#type' => 'textfield',
'#title' => t('Username'),
'#default_value' => variable_get('acquiasdk_cloudapi_username'),
);
$form['cloudapi']['acquiasdk_cloudapi_password'] = array(
'#type' => 'password',
'#title' => t('Password'),
'#description' => t('Leave this blank to keep the existing password.'),
);
$network_link = l(t('Acquia Insight'), 'https://insight.acquia.com/', array('attributes' => array('target' => '_blank')));
$form['network'] = array(
'#type' => 'fieldset',
'#title' => t('Acquia Network'),
'#description' => t('Obtain your Acquia Network credentials from !link.', array('!link' => $network_link)),
);
$form['network']['acquiasdk_network_id'] = array(
'#type' => 'textfield',
'#title' => t('ID'),
'#default_value' => variable_get('acquiasdk_network_id'),
);
$form['network']['acquiasdk_network_key'] = array(
'#type' => 'password',
'#title' => t('Key'),
'#description' => t('Leave this blank to keep the existing key.'),
);
$form = system_settings_form($form);
return $form;
}
/**
* Loads the class loader in order to register all Acquia SDK PHP namespaces.
* Validates Acquia SDK module settings form.
*/
function acquiasdk_classloader() {
static $loader = NULL;
// Load Composer's autoloader to expose Acquia SDK PHP.
if (!isset($loader)) {
// The vendor can either be in modules/acquiasdk/vendor, or sites/all/vendor.
foreach (array(__DIR__, dirname(dirname(__DIR__))) as $path) {
$loader = @include_once $path . '/vendor/autoload.php';
if ($loader) {
return $loader;
}
function acquiasdk_settings_validate($form, &$form_state) {
$password_fields = array(
'acquiasdk_cloudapi_password' => 'acquiasdk_cloudapi_username',
'acquiasdk_network_key' => 'acquiasdk_network_id',
);
foreach ($password_fields as $password_field => $user_field) {
$field =& $form_state['values'][$password_field];
if (!$field) {
// Hack to preserve the existing value if no new value is set.
unset($form_state['values'][$password_field]);
continue;
}
if (!$form_state['values'][$user_field]) {
form_set_error($user_field, t('Username is required when providing a password'));
}
// Display a warning about the autoloader not being able to load.
if (!$loader) {
$message = t('You must install Acquia SDK PHP by running a Composer install from the %path directory.', array(
'%path' => __DIR__,
));
drupal_set_message($message, 'warning', FALSE);
if (module_exists('encrypt')) {
$field = encrypt($field);
}
}
}
return $loader;
/**
* Instantiates a Acquia Cloud API client object.
*
* @param string $username
* (optional) Acquia Cloud API username. If NULL then global config is used.
* @param string $password
* (optional) Acquia Cloud API password. If NULL then global config is used.
*
* @return CloudApiClient
* Acquia Cloud API client object.
*/
function acquiasdk_get_cloudapi_client($username = NULL, $password = NULL) {
if (!$username) {
$username = variable_get('acquiasdk_cloudapi_username');
}
if (!$password) {
$password = acquiasdk_variable_get('acquiasdk_cloudapi_password');
}
if (!$username || !$password) {
throw new Exception('Invalid Acquia Cloud API credentials.');
}
$cloudapi = CloudApiClient::factory(array(
'username' => $username,
'password' => $password,
));
return $cloudapi;
}
/**
* Instantiates Acquia Network client object.
*
* @param string $network_id
* Acquia Network id. If NULL then global config is used. (optional)
* @param string $network_key
* Acquia Network key. If NULL then global config is used. (optional)
*
* @return AcquiaNetworkClient
* Acquia Network client object.
*/
function acquiasdk_get_network_client($network_id = NULL, $network_key = NULL) {
if (!$network_id) {
$network_id = variable_get('acquiasdk_network_id');
}
if (!$password) {
$network_key = acquiasdk_variable_get('acquiasdk_network_key');
}
if (!$network_id || !$network_key) {
throw new Exception('Invalid Acquia Network credentials.');
}
$network = AcquiaNetworkClient::factory(array(
'network_id' => $network_id,
'network_key' => $network_key,
));
return $network;
}
/**
* Retrieves variable with transparent unencryption if supported.
*
* @param string $name
* The name of the variable to return.
*
* @return mixed
* The value of the variable or NULL if not found.
*/
function acquiasdk_variable_get($name) {
$encrypted = &drupal_static(__FUNCTION__);
if (NULL == $encrypted) {
$encrypted = module_exists('encrypt');
}
$var = variable_get($name);
if (!$encrypted || 'a:3' != substr($var, 0, 3)) {
return $var;
}
return decrypt($var);
}
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