|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace aleksip\DataTransformPlugin; |
| 4 | + |
| 5 | +use Drupal\Core\Template\Attribute; |
| 6 | +use PatternLab\Data; |
| 7 | +use PatternLab\PatternData; |
| 8 | +use PatternLab\PatternEngine; |
| 9 | + |
| 10 | +class DataTransformer |
| 11 | +{ |
| 12 | + protected $env; |
| 13 | + protected $reservedKeys; |
| 14 | + protected $patternDataStore; |
| 15 | + protected $processed; |
| 16 | + |
| 17 | + public function __construct(\Twig_Environment $env) |
| 18 | + { |
| 19 | + $this->env = $env; |
| 20 | + // TODO: Add an accessor function for $reservedKeys to the Data class? |
| 21 | + $this->reservedKeys = array("cacheBuster","link","patternSpecific","patternLabHead","patternLabFoot"); |
| 22 | + $this->patternDataStore = PatternData::get(); |
| 23 | + $this->processed = array(); |
| 24 | + } |
| 25 | + |
| 26 | + public function run() |
| 27 | + { |
| 28 | + // Process global data. |
| 29 | + $dataStore = $this->processData(Data::get()); |
| 30 | + Data::replaceStore($dataStore); |
| 31 | + // Process pattern specific data. |
| 32 | + foreach (array_keys($this->patternDataStore) as $pattern) { |
| 33 | + $this->processPattern($pattern); |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + protected function isProcessed($pattern) |
| 38 | + { |
| 39 | + return isset($this->processed[$pattern]); |
| 40 | + } |
| 41 | + |
| 42 | + protected function setProcessed($pattern) |
| 43 | + { |
| 44 | + $this->processed[$pattern] = true; |
| 45 | + } |
| 46 | + |
| 47 | + protected function processPattern($pattern) |
| 48 | + { |
| 49 | + if ( |
| 50 | + $this->isProcessed($pattern) |
| 51 | + || !isset($this->patternDataStore[$pattern]) |
| 52 | + || $this->patternDataStore[$pattern]['category'] != 'pattern' |
| 53 | + ) { |
| 54 | + return; |
| 55 | + } |
| 56 | + $patternSpecificData = |
| 57 | + $this->processData(Data::getPatternSpecificData($pattern)) |
| 58 | + ; |
| 59 | + $dataStore = Data::get(); |
| 60 | + foreach (array_keys($patternSpecificData) as $key) { |
| 61 | + if (!isset($dataStore['patternSpecific'][$pattern]['data'][$key])) { |
| 62 | + // Value is default global data. |
| 63 | + if (is_object($dataStore[$key])) { |
| 64 | + $patternSpecificData[$key] = clone $dataStore[$key]; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + Data::initPattern($pattern); |
| 69 | + Data::setPatternData($pattern, $patternSpecificData); |
| 70 | + $this->setProcessed($pattern); |
| 71 | + } |
| 72 | + |
| 73 | + protected function processData($data) |
| 74 | + { |
| 75 | + foreach (array_keys($data) as $key) { |
| 76 | + if (!in_array($key, $this->reservedKeys)) { |
| 77 | + $data = $this->processKey($data, $key); |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + return $data; |
| 82 | + } |
| 83 | + |
| 84 | + protected function processKey($data, $key) |
| 85 | + { |
| 86 | + $value = $data[$key]; |
| 87 | + if (is_array($value)) { |
| 88 | + foreach (array_keys($value) as $subKey) { |
| 89 | + $value = $this->processKey($value, $subKey); |
| 90 | + } |
| 91 | + if (isset($value['Attribute()']) && is_array($value['Attribute()'])) { |
| 92 | + $data[$key] = new Attribute($value['Attribute()']); |
| 93 | + } |
| 94 | + elseif (isset($value['include()']) && is_array($value['include()']) && isset($value['include()']['pattern'])) { |
| 95 | + $pattern = $value['include()']['pattern']; |
| 96 | + if (is_string($pattern) && isset($this->patternDataStore[$pattern])) { |
| 97 | + if (!isset($value['include()']['with']) || !is_array($value['include()']['with'])) { |
| 98 | + if (!isset($value['include()']['only'])) { |
| 99 | + $patternData = $this->getProcessedPatternSpecificData($pattern); |
| 100 | + } |
| 101 | + else { |
| 102 | + $patternData = array(); |
| 103 | + } |
| 104 | + } |
| 105 | + elseif (!isset($value['include()']['only'])) { |
| 106 | + $patternData = $this->getProcessedPatternSpecificData($pattern, $value['include()']['with']); |
| 107 | + } |
| 108 | + else { |
| 109 | + $patternData = $value['include()']['with']; |
| 110 | + } |
| 111 | + $data[$key] = $this->renderPattern($pattern, $patternData); |
| 112 | + } |
| 113 | + } |
| 114 | + elseif (isset($value['join()']) && is_array($value['join()'])) { |
| 115 | + $data[$key] = join($value['join()']); |
| 116 | + } |
| 117 | + else { |
| 118 | + $data[$key] = $value; |
| 119 | + } |
| 120 | + } |
| 121 | + elseif (is_string($value) && isset($this->patternDataStore[$value]) && $key !== 'pattern') { |
| 122 | + $data[$key] = $this->renderPattern($value, $this->getProcessedPatternSpecificData($value)); |
| 123 | + } |
| 124 | + |
| 125 | + return $data; |
| 126 | + } |
| 127 | + |
| 128 | + public function getProcessedPatternSpecificData($pattern, $extraData = array()) |
| 129 | + { |
| 130 | + $this->processPattern($pattern); |
| 131 | + |
| 132 | + return Data::getPatternSpecificData($pattern, $extraData); |
| 133 | + } |
| 134 | + |
| 135 | + protected function renderPattern($pattern, $data) |
| 136 | + { |
| 137 | + if (isset($this->patternDataStore[$pattern]['patternRaw'])) { |
| 138 | + foreach (array_keys($data) as $key) { |
| 139 | + $data = $this->cloneObjects($data, $key); |
| 140 | + } |
| 141 | + $pattern = $this->env->render( |
| 142 | + $this->patternDataStore[$pattern]['patternRaw'], |
| 143 | + $data |
| 144 | + ); |
| 145 | + } |
| 146 | + |
| 147 | + return $pattern; |
| 148 | + } |
| 149 | + |
| 150 | + protected function cloneObjects($data, $key) |
| 151 | + { |
| 152 | + $value = $data[$key]; |
| 153 | + if (is_array($value)) { |
| 154 | + foreach (array_keys($value) as $subKey) { |
| 155 | + $value = $this->cloneObjects($value, $subKey); |
| 156 | + } |
| 157 | + $data[$key] = $value; |
| 158 | + } |
| 159 | + elseif (is_object($value)) { |
| 160 | + $data[$key] = clone $value; |
| 161 | + } |
| 162 | + |
| 163 | + return $data; |
| 164 | + } |
| 165 | +} |
0 commit comments