public
Created

Start of ConfigMetadata

  • Download Gist
ConfigMetadata.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
<?php
 
namespace Drupal\Core\Config;
 
class ConfigMetadata {
/**
* @var \Drupal\Core\Config\Config
*/
protected $config;
 
/**
* @var array
*/
protected $data;
 
/**
* @var \Drupal\Core\Config\StorageInterface
*/
protected $storage;
 
/**
* @var array
*/
protected $metadata;
 
/**
* @var string
*/
protected $pattern;
 
function __construct(Config $config, StorageInterface $storage) {
$this->config = $config;
$this->storage = $storage;
}
 
public function get() {
$this->flatten($this->config->get());
return $this->read();
}
 
protected function flatten($config, $prefix = '') {
foreach ($config as $key => $data) {
$index = $prefix . $key;
if (is_array($data)) {
$this->flatten($data, $index . '.');
}
else {
$this->data[$index] = $data;
}
}
}
 
protected function read() {
$metadata = $this->readMeta($this->config->getName());
$return = array();
foreach ($this->data as $key => $value) {
$matches = FALSE;
if (isset($this->metadata[$key])) {
$current_key = $key;
$metadata_key= $key;
}
else {
for ($current_key = $key; $current_key ; $current_key = substr($current_key, 0, strrpos($current_key, '.'))) {
if (preg_match($this->pattern, $current_key, $matches)) {
$metadata_key = $matches[1] . '*' . $matches[3];
break;
}
}
}
if ($current_key) {
$current = $metadata[$metadata_key];
if (isset($current['include'])) {
if (preg_match('/\[(.*)]/', $current['include'], $include_matches)) {
$include_key = preg_replace($this->convertPattern($include_matches[1], '\[', '\]'), $matches[2], $current['include']);
}
else {
$include_key = $current['include'];
}
$metadata += $this->readMeta($include_key, "$current_key.");
}
if (isset($metadata[$key])) {
$return[$key] = $metadata[$key];
}
}
}
return $return;
}
 
protected function pattern($data) {
$patterns = array();
foreach (array_keys($data) as $key) {
if (strpos($key, '*') !== FALSE) {
$patterns[$key] = $this->convertPattern($key, '^', '$');
}
}
if ($patterns) {
if ($this->pattern) {
$this->pattern .= '|';
}
$this->pattern .= implode('|', $patterns);
}
}
 
protected function convertPattern($string, $start, $end) {
return "/$start(" . str_replace('\*', ')(.*)(', preg_quote($string, '/')) . ")$end/";
}
 
protected function readMeta($filename, $prefix = '') {
if (!$read = $this->storage->read("meta." . $filename)) {
return array();
}
 
$metadata = array();
foreach ($read as $key => $value) {
$metadata[$prefix . $key] = $value;
}
$this->pattern($metadata);
return $metadata;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.