View source
<?php
namespace Drupal\varbase\config;
use Symfony\Component\Yaml\Yaml;
class ConfigBit {
public static function getConfigBit($config_bit_file_name, $type = 'profile', $project = 'varbase') {
$full_config_bit_file_name = drupal_get_path($type, $project) . '/' . $config_bit_file_name;
if (file_exists($full_config_bit_file_name)) {
$config_bit_data = (array) Yaml::parse(file_get_contents($full_config_bit_file_name));
if (isset($config_bit_data['config_bit'])) {
return $config_bit_data['config_bit'];
}
else {
return FALSE;
}
}
else {
throw new \Exception('Config bit file does not exist!');
}
}
public static function doWeHaveThisConfigBit($config_bit_file_name, $type = 'profile', $project = 'varbase') {
$full_config_bit_file_name = drupal_get_path($type, $project) . '/' . $config_bit_file_name;
return file_exists($full_config_bit_file_name);
}
public static function getList($config_bit_file_name, $condition_name, $condition_value, $sublist = NULL, $type = 'profile', $project = 'varbase') {
$config_bit_data = ConfigBit::getConfigBit($config_bit_file_name, $type, $project);
if (isset($config_bit_data['type']) && $config_bit_data['type'] == 'list' && isset($config_bit_data['when']) && isset($config_bit_data['when'][$condition_name]) && $config_bit_data['when'][$condition_name] == $condition_value && isset($config_bit_data['when']['list'])) {
if (isset($sublist) && $sublist !== '') {
return $config_bit_data['when']['list'][$sublist];
}
else {
return $config_bit_data['when']['list'];
}
}
else {
return [];
}
}
public static function actionArchiveFiles($config_bit_file_name, $condition_name, $condition_value, $type = 'profile', $project = 'varbase') {
$config_bit_data = ConfigBit::getConfigBit($config_bit_file_name, $type, $project);
if (isset($config_bit_data['type']) && ($config_bit_data['type'] = 'action' && isset($config_bit_data['action']) && isset($config_bit_data['action']['archive_files']) && isset($config_bit_data['action']['archive_files']['when']) && isset($config_bit_data['action']['archive_files']['when'][$condition_name]) && $config_bit_data['action']['archive_files']['when'][$condition_name] == $condition_value && isset($config_bit_data['action']['archive_files']['files']))) {
foreach ($config_bit_data['action']['archive_files']['files'] as $language_config_file) {
$config_file = drupal_get_path($type, $project) . '/' . $language_config_file;
if (file_exists($config_file)) {
$config_file_backup = $config_file . $config_bit_data['action']['archive_files']['archive_extensiton'];
file_unmanaged_move($config_file, $config_file_backup);
}
}
}
}
public static function actionUnArchiveFiles($config_bit_file_name, $condition_name, $condition_value, $type = 'profile', $project = 'varbase') {
$config_bit_data = ConfigBit::getConfigBit($config_bit_file_name, $type, $project);
if (isset($config_bit_data['type']) && ($config_bit_data['type'] = 'action' && isset($config_bit_data['action']) && isset($config_bit_data['action']['unarchive_files']) && isset($config_bit_data['action']['unarchive_files']['when']) && isset($config_bit_data['action']['unarchive_files']['when'][$condition_name]) && $config_bit_data['action']['unarchive_files']['when'][$condition_name] == $condition_value && isset($config_bit_data['action']['unarchive_files']['files']))) {
foreach ($config_bit_data['action']['unarchive_files']['files'] as $language_config_file) {
$config_file = drupal_get_path($type, $project) . '/' . $language_config_file;
$config_file_backup = $config_file . $config_bit_data['action']['unarchive_files']['archive_extensiton'];
if (!file_exists($config_file) && file_exists($config_file_backup)) {
file_unmanaged_move($config_file_backup, $config_file);
}
}
}
}
public static function actionAdd($config_bit_file_name, $condition_name, $condition_value, $target, $type = 'profile', $project = 'varbase') {
$config_bit_data = ConfigBit::getConfigBit($config_bit_file_name, $type, $project);
if (isset($config_bit_data['type']) && $config_bit_data['type'] == 'action' && isset($config_bit_data['for']) && !empty($config_bit_data['for']) && file_exists(drupal_get_path($type, $project) . '/' . $config_bit_data['for']) && isset($config_bit_data['action']) && isset($config_bit_data['action']['add']) && isset($config_bit_data['action']['add']['when']) && isset($config_bit_data['action']['add']['when'][$condition_name]) && $config_bit_data['action']['add']['when'][$condition_name] == $condition_value && isset($config_bit_data['action']['add']['target']) && $config_bit_data['action']['add']['target'] == $target && isset($config_bit_data['action']['add'][$target])) {
$config_target_data = Yaml::parse(file_get_contents(drupal_get_path($type, $project) . '/' . $config_bit_data['for']));
$configs_to_add = $config_bit_data['action']['add'][$target];
foreach ($configs_to_add as $config_to_add) {
if (!in_array($config_to_add, $config_target_data[$target])) {
$config_target_data[$target][] = (string) $config_to_add;
}
}
$updated_config_target = Yaml::dump($config_target_data, 2, 2);
file_put_contents(drupal_get_path($type, $project) . '/' . $config_bit_data['for'], $updated_config_target);
}
}
public static function actionRemove($config_bit_file_name, $condition_name, $condition_value, $target, $type = 'profile', $project = 'varbase') {
$config_bit_data = ConfigBit::getConfigBit($config_bit_file_name, $type, $project);
if (isset($config_bit_data['type']) && $config_bit_data['type'] == 'action' && isset($config_bit_data['for']) && $config_bit_data['for'] !== '' && file_exists(drupal_get_path($type, $project) . '/' . $config_bit_data['for']) && isset($config_bit_data['action']) && isset($config_bit_data['action']['remove']) && isset($config_bit_data['action']['remove']['when']) && isset($config_bit_data['action']['remove']['when'][$condition_name]) && $config_bit_data['action']['remove']['when'][$condition_name] == $condition_value && isset($config_bit_data['action']['remove']['target']) && $config_bit_data['action']['remove']['target'] == $target && isset($config_bit_data['action']['remove'][$target])) {
$config_target_data = Yaml::parse(file_get_contents(drupal_get_path($type, $project) . '/' . $config_bit_data['for']));
$configs_to_remove = $config_bit_data['action']['remove'][$target];
foreach ($configs_to_remove as $config_to_remove) {
$config_to_remove_key = array_search((string) $config_to_remove, $config_target_data[$target], TRUE);
if ($config_to_remove_key !== FALSE) {
unset($config_target_data[$target][$config_to_remove_key]);
}
}
$updated_config_target = Yaml::dump($config_target_data, 2, 2);
file_put_contents(drupal_get_path($type, $project) . '/' . $config_bit_data['for'], $updated_config_target);
}
}
}