Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions src/Drupal/Component/Annotation/Variable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
<?php

/**
* @file
* Contains \Drupal\Component\Annotation\PluginID.
*/

namespace Drupal\Component\Annotation;

/**
* @Annotation
*
* Defines a Plugin annotation object that just contains an ID.
*/
class Variable extends AnnotationBase {

/**
* The string that is used for the environment variable.
*
* @var string
*/
public $environment;

/**
* A description about the variable.
*
* @var string
*/
public $description;

/**
* The parser that needs to be used to parse the environment variable.
*
* @var string
*/
public $parser;

/**
* An array containing aliases of environment variables.
*
* @var array
*/
public $environmentAlias;

/**
* The default value for a variable.
*
* @var string
*/
public $defaultValue;

/**
* @return array
*/
public function getEnvironmentAlias(): array {
return $this->environmentAlias;
}

/**
* @param array $environmentAlias
*/
public function setEnvironmentAlias(array $environmentAlias) {
$this->environmentAlias = $environmentAlias;
}

/**
* @return string
*/
public function getDefaultValue(): string {
return $this->default_value;
}

/**
* @param string $default_value
*/
public function setDefaultValue(string $default_value) {
$this->default_value = $default_value;
}

/**
* {@inheritdoc}
*/
public function get() {
return array(
'environment' => $this->environment,
'description' => $this->class,
'parser' => $this->parser,
'environment_alias' => $this->environmentAlias,
'default_value' => $this->defaultValue
);
}

/**
* @return string
*/
public function getEnvironment(): string {
return $this->environment;
}

/**
* @param string $environment
*/
public function setEnvironment(string $environment) {
$this->environment = $environment;
}

/**
* @return string
*/
public function getDescription(): string {
return $this->description;
}

/**
* @param string $description
*/
public function setDescription(string $description) {
$this->description = $description;
}

/**
* @return mixed
*/
public function getParser() {
return $this->parser;
}

/**
* @param mixed $parser
*/
public function setParser($parser) {
$this->parser = $parser;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php
/**
* @file
* Contains \Drupal\Component\Configuration\Exception\ConfigurationException
*/

namespace Drupal\Component\Configuration\Exception;

/**
* Generic Configuration exception class to be thrown when an issue occurs with
* the configuration component.
*/
class ConfigurationException extends \Exception implements ExceptionInterface { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @file
* Contains \Drupal\Component\Configuration\Exception\EnvironmentVariableNotSetException.
*/

namespace Drupal\Component\Configuration\Exception;

/**
* Exception that can be thrown when an environment variable was not found.
*/
class EnvironmentVariableNotSetException extends ConfigurationException { }
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php
/**
* @file
* Contains \Drupal\Component\Configuration\Exception\ExceptionInterface.
*/

namespace Drupal\Component\Configuration\Exception;

/**
* Exception interface for all exceptions thrown by the Configuration component.
*/
interface ExceptionInterface { }
33 changes: 33 additions & 0 deletions src/DrupalCI/Configuration/Configuration.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace DrupalCI\Configuration;

class Configuration {

/**
* @param $vars
*/
protected $vars = [];

/**
* @return string
*/
public function __get($property) {
return array_key_exists($property, $this->vars) ? $this->vars[$property] : "";
}

public function __set($property, $value) {
$this->vars[$property] = $value;
}

public function __isset($key) {
return array_key_exists($key, $this->vars);
}

public function override($values) {
foreach ($values as $key => $value) {
$this->{$key} = $value;
}
}

}
98 changes: 98 additions & 0 deletions src/DrupalCI/Configuration/ConfigurationManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
<?php

namespace DrupalCI\Configuration;

use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Drupal\Component\Annotation\Variable;
use Drupal\Component\Configuration\Exception\EnvironmentVariableNotSetException;

class ConfigurationManager {

/**
* A list of all environment variables.
*
* @var array
*/
protected $environmentVariables = [];

/**
* A list of defined variables.
*
* @var array
*/
protected $variables;

/**
* Constructs a ConfigurationManager.
*/
public function __construct() {
AnnotationRegistry::registerLoader('class_exists');
$this->discoverVariables();
$this->parseEnvironmentVariables();
}

public function test() {
$this->discoverVariables();
}

/**
* Load all the environment variables.
*/
private function parseEnvironmentVariables() {
$environment_stacks = [$_ENV, $_SERVER];

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dont think(?) we need to worry about $_SERVER in a CLI application?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I only used it for developing purpose. For some reason, my environment variables didn't get injected in $_ENV so therefore I used $_SERVER. I will need to remove it when development is done. I don't use vagrant for testing this code.

foreach ($environment_stacks as $environment_stack) {
foreach ($environment_stack as $item => $value) {
if (substr($item, 0, 4) == "DCI_") {
$this->environmentVariables[$item] = $value;
}
}
}
}

/**
* Discovers the list of defined variables.
*/
protected function discoverVariables() {
$reflectionClassName = 'DrupalCI\\Configuration\\EnvironmentVariables';

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than have all the properties defined in one EnvironmentVariables class, we should have the plugin specifc properties defined on each specific plugin. That way we keep simpletest variables contained in the simpletest BuildTask etc. There is a risk that multiple plugins define the same ENV, but tests ought to catch something like that, or even just maintainer eyeballs.

$reader = new AnnotationReader();
$reflectionClass = new \ReflectionClass($reflectionClassName);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And once The properties are defined on the plugin class, we need a way to iterate over all the BuildTasks in the system, so we can getPropertyAnnotations on each of them.

foreach ($reflectionClass->getProperties() as $property) {
$reflectionProperty = new \ReflectionProperty($reflectionClassName, $property->getName());
$this->variables[$property->getName()] = $reader->getPropertyAnnotations($reflectionProperty)[0];
}
}

/**
* Loads all available properties.
*/
public function loadProperties(Configuration &$configuration) {
foreach ($this->variables as $property => $annotation) {
/** @var $annotation Variable */

try {
$configuration->{$property} = $this->getEnvironmentVariableValue($annotation->getEnvironment());
}
catch (EnvironmentVariableNotSetException $e) { }
}

return $configuration;
}

/**
* Try to load a given property from the current environment variables.
*
* @param string $variable
* The variable that needs to be loaded.
*/
protected function getEnvironmentVariableValue($variable) {
if (array_key_exists($variable, $this->environmentVariables)) {
return $this->environmentVariables[$variable];
}
else {
throw new EnvironmentVariableNotSetException();
}
}

}
Loading