View source
<?php
namespace HookUpdateDeployTools;
class HudtInternal {
public static function canReadFile($filename, $storage_type) {
$path = self::getStoragePath($storage_type);
$file = "{$path}{$filename}";
if (file_exists($file)) {
return TRUE;
}
else {
$variables = array(
'@path' => $path,
'!filename' => $filename,
'!storage' => $storage_type,
);
$message = "The requested !storage read failed because the file '!filename' was not found in '@path'. \nRe-run update when the file has been placed there and is readable.";
Message::make($message, $variables, WATCHDOG_ERROR);
throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
}
}
public static function readFileToArray($filename, $storage_type) {
$path = self::getStoragePath($storage_type);
$file = "{$path}{$filename}";
if (self::canReadFile($filename, $storage_type)) {
$file_contents = file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
}
else {
$file_contents = array();
}
return $file_contents;
}
public static function readFileToString($filename, $storage_type) {
$path = self::getStoragePath($storage_type);
$file = "{$path}{$filename}";
if (self::canReadFile($filename, $storage_type)) {
$file_contents = file_get_contents($file);
}
else {
$file_contents = FALSE;
}
return $file_contents;
}
public static function getSummary($completed, $total_requested, $operation) {
$t = get_t();
$count = count($completed);
$completed_string = print_r($completed, TRUE);
$remove = array(
"Array",
"(\n",
")\n",
);
$completed_string = str_replace($remove, '', $completed_string);
$completed_string = str_replace(' [', ' [', $completed_string);
$vars = array(
'@count' => $count,
'!completed' => $completed_string,
'@total' => $total_requested,
'@operation' => $operation,
);
return $t("Summary: @operation @count/@total. Completed the following:\n !completed", $vars);
}
public static function getStoragePath($storage_type, $safe_check = FALSE) {
$var_storage = self::getStorageVars();
$t = get_t();
if (!empty($var_storage[$storage_type])) {
$storage_module = variable_get($var_storage[$storage_type], '');
$storage_module = !empty($storage_module) ? $storage_module : variable_get($var_storage['default'], '');
$storage_module = check_plain($storage_module);
$storage_module = !empty($storage_module) ? $storage_module : 'site_deploy';
if (module_exists($storage_module)) {
$module_path = drupal_get_path('module', $storage_module);
$storage_path = "{$module_path}/{$storage_type}_source/";
return $storage_path;
}
elseif ($safe_check) {
return $t('The module "@module" does not exits, please add it or adjust accordingly.', array(
'@module' => $storage_module,
));
}
else {
$variables = array(
'!path' => '/admin/config/development/hook_update_deploy_tools',
'!storage' => $storage_type,
'%module' => $storage_module,
);
$message = "The storage module '%module' does not exist. Visit !path to set the correct module for !storage import.";
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
}
else {
$message = 'There is no storage of type !type to read/write. Internal Hook Update Deploy Tools error.';
$variables = array(
'!type' => $storage_type,
);
throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
}
}
public static function getStorageVars() {
$storage_map = array(
'default' => 'hook_update_deploy_tools_deploy_module',
'menu' => 'hook_update_deploy_tools_menu_feature',
'node' => 'hook_update_deploy_tools_node_feature',
'page_manager' => 'hook_update_deploy_tools_page_manager_feature',
'redirect' => 'hook_update_deploy_tools_redirect_feature',
'rules' => 'hook_update_deploy_tools_rules_feature',
);
return $storage_map;
}
public static function normalizeMachineName($quasi_machinename) {
$items = array(
'-export.txt' => '',
'-' => '_',
);
$machine_name = str_replace(array_keys($items), array_values($items), $quasi_machinename);
return $machine_name;
}
public static function normalizeFileName($quasi_name) {
$items = array(
'-export.txt' => '',
'_' => '-',
);
$file_name = str_replace(array_keys($items), array_values($items), $quasi_name);
$file_name = "{$file_name}-export.txt";
return $file_name;
}
public static function writeFile($file_uri, $file_contents) {
$variables = array(
'@file_uri' => $file_uri,
);
$msg_return = FALSE;
try {
$fh = fopen($file_uri, 'w');
if ($fh) {
fwrite($fh, $file_contents);
fclose($fh);
$msg_return = $file_uri;
}
else {
$message = "Error (likely permissions) creating the file: @file_uri";
throw new HudtException($message, $variables, WATCHDOG_ERROR, FALSE);
}
} catch (\Exception $e) {
$variables['@error'] = $e
->getMessage();
$msg = dt("Failed writing to @file_uri. Caught exception: @error", $variables);
drush_log($msg, 'error');
drush_print(dt("The file was not generated. Outputting contents to terminal.\n"));
drush_print('------------------------------------------------------------');
drush_print($file_contents);
drush_print("------------------------------------------------------------\n\n");
}
return $msg_return;
}
}