AcsfVariablesCommands.php in Acquia Cloud Site Factory Connector 8.2
File
acsf_variables/src/Commands/AcsfVariablesCommands.php
View source
<?php
namespace Drupal\acsf_variables\Commands;
use Consolidation\OutputFormatters\StructuredData\PropertyList;
use Drupal\acsf\AcsfException;
use Drush\Commands\DrushCommands;
class AcsfVariablesCommands extends DrushCommands {
public function vget($name, array $options = [
'exact' => FALSE,
]) {
if (!\Drupal::moduleHandler()
->moduleExists('acsf_variables')) {
throw new AcsfException(dt('The acsf_variables module must be enabled.'));
}
if (empty($name)) {
throw new \InvalidArgumentException(dt('You must provide the name of the variable to retrieve as the first argument.'));
}
$exact = $options['exact'];
if ($exact) {
if (($value = \Drupal::service('acsf.variable_storage')
->get($name)) && !is_null($value)) {
$variables[$name] = $value;
}
}
else {
$variables = \Drupal::service('acsf.variable_storage')
->getMatch($name);
}
if (!empty($variables)) {
return new PropertyList($variables);
}
else {
throw new AcsfException(dt('@name not found.', [
'@name' => $name,
]));
}
}
public function vset($name, $value, array $options = [
'group' => NULL,
]) {
if (!\Drupal::moduleHandler()
->moduleExists('acsf_variables')) {
throw new AcsfException(dt('The acsf_variables module must be enabled.'));
}
if (empty($name)) {
throw new \InvalidArgumentException(dt('You must provide the name of the variable to set as the first argument.'));
}
if (empty($value)) {
throw new \InvalidArgumentException(dt('You must provide the value of the variable to set as the second argument.'));
}
if (\Drupal::service('acsf.variable_storage')
->set($name, $value, $options['group'])) {
$this
->output()
->writeln(dt('@name was set to !value', [
'@name' => $name,
'!value' => $value,
]));
}
else {
throw new AcsfException(dt('The @name variable could not be set.'));
}
}
public function vgetGroup($group) {
if (!\Drupal::moduleHandler()
->moduleExists('acsf_variables')) {
throw new AcsfException(dt('The acsf_variables module must be enabled.'));
}
if (empty($group)) {
throw new \InvalidArgumentException(dt('You must provide the group name of the variables to retrieve as the first argument.'));
}
if ($data = \Drupal::service('acsf.variable_storage')
->getGroup($group)) {
return new PropertyList($data);
}
else {
throw new AcsfException(dt('@group group not found.', [
'@group' => $group,
]));
}
}
public function vdel($name) {
if (!\Drupal::moduleHandler()
->moduleExists('acsf_variables')) {
throw new AcsfException(dt('The acsf_variables module must be enabled.'));
}
if (empty($name)) {
throw new \InvalidArgumentException(dt('You must provide the name of the variable to delete as the first argument.'));
}
$storage = \Drupal::service('acsf.variable_storage');
if ($variable = $storage
->get($name)) {
if ($storage
->delete($name)) {
$this
->output()
->writeln(dt('@name was deleted.', [
'@name' => $name,
]));
}
else {
throw new AcsfException(dt('Unable to delete the @name variable.', [
'@name' => $name,
]));
}
}
else {
throw new AcsfException(dt('@name not found.', [
'@name' => $name,
]));
}
}
public function info() {
$data = \Drupal::service('acsf.variable_storage')
->get('acsf_site_info', []);
return new PropertyList($data);
}
}