|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Overblog\GraphQLBundle\Tests; |
| 4 | + |
| 5 | +use Symfony\Component\Process\Exception\ProcessFailedException; |
| 6 | +use Symfony\Component\Process\Process; |
| 7 | + |
| 8 | +class VersionHelper |
| 9 | +{ |
| 10 | + const KEYS_WEIGHT = [ |
| 11 | + 'message' => 100, |
| 12 | + 'extensions' => 90, |
| 13 | + 'locations' => 80, |
| 14 | + 'path' => 70, |
| 15 | + ]; |
| 16 | + |
| 17 | + public static function normalizedPayload(array $payload) |
| 18 | + { |
| 19 | + if (self::needNormalized()) { |
| 20 | + if (!empty($payload['errors'])) { |
| 21 | + $payload['errors'] = self::normalizedErrors($payload['errors']); |
| 22 | + } |
| 23 | + |
| 24 | + if (!empty($payload['extensions']['warnings'])) { |
| 25 | + $payload['extensions']['warnings'] = self::normalizedErrors($payload['extensions']['warnings']); |
| 26 | + } |
| 27 | + } |
| 28 | + |
| 29 | + return $payload; |
| 30 | + } |
| 31 | + |
| 32 | + public static function normalizedErrors(array $errors) |
| 33 | + { |
| 34 | + if (self::needNormalized()) { |
| 35 | + foreach ($errors as &$error) { |
| 36 | + foreach ($error as $key => $value) { |
| 37 | + if (!\in_array($key, ['message', 'locations', 'path', 'extensions'])) { |
| 38 | + $error['extensions'] = isset($error['extensions']) ? $error['extensions'] : []; |
| 39 | + $error['extensions'][$key] = $value; |
| 40 | + unset($error[$key]); |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + \uksort($error, function ($key1, $key2) { |
| 45 | + return self::KEYS_WEIGHT[$key1] > self::KEYS_WEIGHT[$key2] ? -1 : 1; |
| 46 | + }); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + return $errors; |
| 51 | + } |
| 52 | + |
| 53 | + public static function needNormalized() |
| 54 | + { |
| 55 | + return self::compareWebonyxGraphQLPHPVersion('0.13', '>='); |
| 56 | + } |
| 57 | + |
| 58 | + public static function compareWebonyxGraphQLPHPVersion($version, $operator) |
| 59 | + { |
| 60 | + return \version_compare(self::webonyxGraphQLPHPVersion(), $version, $operator); |
| 61 | + } |
| 62 | + |
| 63 | + public static function webonyxGraphQLPHPVersion() |
| 64 | + { |
| 65 | + static $version = null; |
| 66 | + |
| 67 | + if (empty($version)) { |
| 68 | + $composerBinPath = \file_exists(__DIR__.'/../composer.phar') ? __DIR__.'/../composer.phar' : '`which composer`'; |
| 69 | + $process = new Process($composerBinPath.' show \'webonyx/graphql-php\' | grep \'versions\' | grep -o -E \'\*\ .+\' | cut -d\' \' -f2 | cut -d\',\' -f1'); |
| 70 | + $process->setWorkingDirectory(__DIR__.'/../'); |
| 71 | + $process->run(); |
| 72 | + if ($process->isSuccessful()) { |
| 73 | + $version = $process->getOutput(); |
| 74 | + $version = \preg_replace('/[^.0-9]/', '', $version); |
| 75 | + } else { |
| 76 | + throw new ProcessFailedException($process); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + return $version; |
| 81 | + } |
| 82 | +} |
0 commit comments