|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Overblog\GraphQLBundle\Config; |
| 4 | + |
| 5 | +use Symfony\Component\ExpressionLanguage\Expression; |
| 6 | + |
| 7 | +class Processor |
| 8 | +{ |
| 9 | + const DEFAULT_EXPRESSION_LANGUAGE_TRIGGER = '@='; |
| 10 | + |
| 11 | + /** @var string */ |
| 12 | + private $expressionLanguageTrigger; |
| 13 | + |
| 14 | + /** @var callable[] */ |
| 15 | + private $processors = []; |
| 16 | + |
| 17 | + public function __construct($expressionLanguageTrigger = self::DEFAULT_EXPRESSION_LANGUAGE_TRIGGER, array $processors = []) |
| 18 | + { |
| 19 | + // add the default $processors to let users override it easily |
| 20 | + array_push($processors, [$this, 'convertStringToExpressionObject']); |
| 21 | + |
| 22 | + $this->processors = $processors; |
| 23 | + $this->expressionLanguageTrigger = $expressionLanguageTrigger; |
| 24 | + } |
| 25 | + |
| 26 | + public function process(array $configs) |
| 27 | + { |
| 28 | + foreach ($this->processors as $processor) { |
| 29 | + $configs = call_user_func($processor, $configs); |
| 30 | + } |
| 31 | + |
| 32 | + return $configs; |
| 33 | + } |
| 34 | + |
| 35 | + public static function staticProcess( |
| 36 | + array $configs, |
| 37 | + $expressionLanguageTrigger = self::DEFAULT_EXPRESSION_LANGUAGE_TRIGGER, |
| 38 | + array $processors = [] |
| 39 | + ) { |
| 40 | + return (new static($expressionLanguageTrigger, $processors))->process($configs); |
| 41 | + } |
| 42 | + |
| 43 | + public function convertStringToExpressionObject(array $configs) |
| 44 | + { |
| 45 | + return array_map(function ($v) { |
| 46 | + if (is_array($v)) { |
| 47 | + return $this->convertStringToExpressionObject($v); |
| 48 | + } elseif (is_string($v) && 0 === strpos($v, $this->expressionLanguageTrigger)) { |
| 49 | + return new Expression(substr($v, 2)); |
| 50 | + } |
| 51 | + |
| 52 | + return $v; |
| 53 | + }, $configs); |
| 54 | + } |
| 55 | + |
| 56 | + public static function processBeforeNormalization(array $configs) |
| 57 | + { |
| 58 | + $configs = static::processConfigsExtends($configs); |
| 59 | + $configs = static::removedVirtualTypes($configs); |
| 60 | + |
| 61 | + return $configs; |
| 62 | + } |
| 63 | + |
| 64 | + public static function removedVirtualTypes(array $configs) |
| 65 | + { |
| 66 | + return array_filter($configs, function ($config) { |
| 67 | + return !isset($config['virtual']) || true !== $config['virtual']; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + public static function processConfigsExtends(array $configs) |
| 72 | + { |
| 73 | + foreach ($configs as $name => &$config) { |
| 74 | + if (!isset($config['type'])) { |
| 75 | + continue; |
| 76 | + } |
| 77 | + |
| 78 | + $allowedTypes = [$config['type']]; |
| 79 | + if ('object' === $config['type']) { |
| 80 | + $allowedTypes[] = 'interface'; |
| 81 | + } |
| 82 | + $flattenExtends = self::flattenExtends($name, $configs, $allowedTypes); |
| 83 | + if (empty($flattenExtends)) { |
| 84 | + continue; |
| 85 | + } |
| 86 | + $config = self::extendsTypeConfig($name, $flattenExtends, $configs); |
| 87 | + } |
| 88 | + |
| 89 | + return $configs; |
| 90 | + } |
| 91 | + |
| 92 | + private static function extendsTypeConfig($child, array $parents, array $configs) |
| 93 | + { |
| 94 | + $parentTypes = array_intersect_key($configs, array_flip($parents)); |
| 95 | + $parentTypes = array_reverse($parentTypes); |
| 96 | + $mergedParentsConfig = call_user_func_array('array_replace_recursive', array_column($parentTypes, 'config')); |
| 97 | + $childType = $configs[$child]; |
| 98 | + // unset resolveType field resulting from the merge of a "interface" type |
| 99 | + if ('object' === $childType['type']) { |
| 100 | + unset($mergedParentsConfig['resolveType']); |
| 101 | + } |
| 102 | + |
| 103 | + $configs = array_replace_recursive(['config' => $mergedParentsConfig], $childType); |
| 104 | + |
| 105 | + return $configs; |
| 106 | + } |
| 107 | + |
| 108 | + private static function flattenExtends($name, array $configs, array $allowedTypes, $child = null, array $typesTreated = []) |
| 109 | + { |
| 110 | + self::checkTypeExists($name, $configs, $child); |
| 111 | + self::checkCircularReferenceExtendsTypes($name, $typesTreated); |
| 112 | + self::checkAllowedExtendsTypes($name, $configs[$name], $allowedTypes, $child); |
| 113 | + |
| 114 | + // flatten |
| 115 | + $config = $configs[$name]; |
| 116 | + if (empty($config['extends']) || !is_array($config['extends'])) { |
| 117 | + return []; |
| 118 | + } |
| 119 | + $typesTreated[$name] = true; |
| 120 | + $flattenExtendsTypes = []; |
| 121 | + foreach ($config['extends'] as $typeToExtend) { |
| 122 | + $flattenExtendsTypes[] = $typeToExtend; |
| 123 | + $flattenExtendsTypes = array_merge( |
| 124 | + $flattenExtendsTypes, |
| 125 | + self::flattenExtends($typeToExtend, $configs, $allowedTypes, $name, $typesTreated) |
| 126 | + ); |
| 127 | + } |
| 128 | + |
| 129 | + return $flattenExtendsTypes; |
| 130 | + } |
| 131 | + |
| 132 | + private static function checkTypeExists($name, array $configs, $child) |
| 133 | + { |
| 134 | + if (!isset($configs[$name])) { |
| 135 | + throw new \InvalidArgumentException(sprintf( |
| 136 | + 'Type %s extends by %s not be found.', |
| 137 | + json_encode($name), |
| 138 | + json_encode($child) |
| 139 | + )); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + private static function checkCircularReferenceExtendsTypes($name, array $typesTreated) |
| 144 | + { |
| 145 | + if (isset($typesTreated[$name])) { |
| 146 | + throw new \InvalidArgumentException(sprintf( |
| 147 | + 'Type circular inheritance detected (%s).', |
| 148 | + implode('->', array_merge(array_keys($typesTreated), [$name])) |
| 149 | + )); |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private static function checkAllowedExtendsTypes($name, array $config, array $allowedTypes, $child) |
| 154 | + { |
| 155 | + if (!in_array($config['type'], $allowedTypes)) { |
| 156 | + throw new \InvalidArgumentException(sprintf( |
| 157 | + 'Type %s can\'t extends %s because %s is not allowed type (%s).', |
| 158 | + json_encode($name), |
| 159 | + json_encode($child), |
| 160 | + json_encode($config['type']), |
| 161 | + json_encode($allowedTypes) |
| 162 | + )); |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments