Created
December 19, 2012 10:08
-
-
Save chx/4335702 to your computer and use it in GitHub Desktop.
Start of ConfigMetadata
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment