Skip to content
This repository has been archived by the owner on Jan 21, 2020. It is now read-only.

Commit

Permalink
Merge pull request #2417 from fvovan/class_constants_internal
Browse files Browse the repository at this point in the history
Make app generate-config-internal use ::class constants instead of strings
  • Loading branch information
fvovan authored Dec 12, 2016
2 parents b942d51 + 9a500c5 commit babab9b
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
6 changes: 4 additions & 2 deletions library/CM/Config/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

class CM_Config_Generator extends CM_Class_Abstract {

/** @var int */
/** @var int */
private $_typesMaxValue = 0;

/** @var string[] */
Expand Down Expand Up @@ -56,7 +56,9 @@ public function getConfigClassTypes() {
$this->generateClassTypes();
$config = new CM_Config_Node();
foreach ($this->getNamespaceTypes() as $namespaceClass => $typeList) {
$config->$namespaceClass->types = $typeList;
$config->$namespaceClass->types = \Functional\map($typeList, function ($className) {
return "$className::class";
});
}
$classTypes = $this->getClassTypes();
foreach ($classTypes as $type => $class) {
Expand Down
26 changes: 17 additions & 9 deletions library/CM/Config/Node.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function export() {
$object->$key = $value->export();
} else {
if (is_array($value)) {
$value = $this->_evaluateConstantsInKeys($value);
$value = $this->_evaluateConstantsInArray($value);
}
$object->$key = $value;
}
Expand Down Expand Up @@ -59,7 +59,12 @@ public function exportAsString($baseKey, $property = null) {
if (!is_scalar($value)) {
$output .= $this->exportAsString($getFullKey($baseKey, $key), $value);
} else {
$output .= $getFullKey($baseKey, $key) . " = " . var_export($value, true) . ";" . PHP_EOL;
if (preg_match('/^(\w+)::class$/', $value, $matches) && class_exists($matches[1])) {
$configValue = $value;
} else {
$configValue = var_export($value, true);
}
$output .= $getFullKey($baseKey, $key) . " = " . $configValue . ";" . PHP_EOL;
}
}
return $output;
Expand Down Expand Up @@ -112,23 +117,26 @@ public function extendWithFile(CM_File $configFile) {
* @param array $list
* @return array
*/
private function _evaluateConstantsInKeys(array $list) {
$keys = array_keys($list);
$keys = \Functional\map($keys, function ($key) {
if (null !== ($value = $this->_evaluateClassConstant($key))) {
private function _evaluateConstantsInArray(array $list) {
$constantEvaluator = function ($key) {
if (is_scalar($key) && null !== ($value = $this->_evaluateClassConstant($key))) {
return $value;
}
return $key;
});
return array_combine($keys, array_values($list));
};
$keys = \Functional\map(array_keys($list), $constantEvaluator);
$values = \Functional\map(array_values($list), $constantEvaluator);
return array_combine($keys, $values);
}

/**
* @param string $reference
* @return mixed|null
*/
private function _evaluateClassConstant($reference) {
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*::[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $reference)) {
if (preg_match('/^(\w+)::class$/', $reference, $matches) && class_exists($matches[1])) {
return $matches[1];
} elseif (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*::[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $reference)) {
return @constant($reference);
}
return null;
Expand Down

0 comments on commit babab9b

Please sign in to comment.