Skip to content

Instantly share code, notes, and snippets.

@grasmash
Created December 1, 2015 18:42
Show Gist options
  • Save grasmash/99a64560c06ccda83599 to your computer and use it in GitHub Desktop.
Save grasmash/99a64560c06ccda83599 to your computer and use it in GitHub Desktop.
This Phing tasks will read a PHP variable from a PHP file, allowing it to be output as a Phing property.
<?php
/**
* Class PhpVariableTask
*
* This task allows php variables from php files to be output into Phing
* properties.
*/
class PhpVariableTask extends Task {
/**
* The PHP file from which to read the variable.
* @var string
*/
protected $file = NULL;
/**
* The PHP variable name. This may be a nested array.
* @var string
*/
protected $variable = NULL;
/**
* Property name to set with output value.
* @var string
*/
private $outputProperty;
public function init() {}
public function main()
{
require $this->file;
// @see http://php.net/manual/en/language.variables.variable.php
$variable = $this->variable;
// If variable is an array, parse array keys from string. Example value
// would be databases[default][default][database].
if (strstr($variable, '[')) {
// Split string parts.
$keys = preg_split('~(])?(\\[|$)~', $variable, -1, PREG_SPLIT_NO_EMPTY);
// Determine the variable name. E.g., $database is variable in example.
$variable_name = array_shift($keys);
$value = $$variable_name;
// Loop through nested array keys.
foreach ($keys as $key)
{
if (!is_array($value) || !array_key_exists($key, $value)) {
$value[$key] = NULL;
}
$value = &$value[$key];
}
}
// If this is not an array, simply take variable value.
else {
$value = $$variable;
}
if (null !== $this->outputProperty) {
$this->project->setProperty($this->outputProperty, $value);
}
}
/**
* Sets $this->file property.
*
* @param string $file
* The PHP file from which to read the variable.
*/
public function setFile($file)
{
$this->file = $file;
}
/**
* Sets $this->variable
*
* @param string $variable
* The PHP variable name. This may be a nested array.
*/
public function setVariable($variable)
{
$this->variable = $variable;
}
/**
* Sets $this->outputProperty property.
*
* @param string $prop
* The name of the Phing property whose value to set.
*/
public function setOutputProperty($prop)
{
$this->outputProperty = $prop;
}
}
@grasmash
Copy link
Author

grasmash commented Dec 1, 2015

Example usage:

  <!-- Extract PHP variable values from local.settings.php. -->
  <property name="local.settings.file" value="${repo.root}/sites/default/settings/local.settings.php" />
  <!-- Extracts value of $base_url and outputs to local_url property in Phing. -->
  <phpVariable file="${local.settings.file}" variable="base_url" outputProperty="local_url" />
  <!-- Extracts values from the $databases array and outputs to db.* properties. -->
  <phpVariable file="${local.settings.file}" variable="databases[default][default][database]" outputProperty="db.database" />
  <phpVariable file="${local.settings.file}" variable="databases[default][default][username]" outputProperty="db.username" />
  <phpVariable file="${local.settings.file}" variable="databases[default][default][password]" outputProperty="db.password" />
  <phpVariable file="${local.settings.file}" variable="databases[default][default][host]" outputProperty="db.host" />
  <phpVariable file="${local.settings.file}" variable="databases[default][default][port]" outputProperty="db.port" />

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment