diff --git a/docs/definitions/type-system/enum.md b/docs/definitions/type-system/enum.md index 5a3c53b4d..18d2623d4 100644 --- a/docs/definitions/type-system/enum.md +++ b/docs/definitions/type-system/enum.md @@ -19,7 +19,8 @@ Episode: #deprecationReason: "Just because" EMPIRE: # We can use a PHP constant to avoid a magic number - value: '@=constant("App\\StarWars\\Movies::MOVIE_EMPIRE")' + # in previous versions this was done with '@=constant("App\\StarWars\\Movies::MOVIE_EMPIRE")' + value: !php/const App\StarWars\Movies::MOVIE_EMPIRE description: "Released in 1980." JEDI: 6 # using the short syntax (JEDI value equal to 6) FORCEAWAKENS: # in this case FORCEAWAKENS value = FORCEAWAKENS @@ -35,6 +36,7 @@ Note: At the moment, doctrine annotations on constants are not supported. So if namespace AppBundle; +use App\StarWars\Movies; use Overblog\GraphQLBundle\Annotation as GQL; /** @@ -48,7 +50,7 @@ use Overblog\GraphQLBundle\Annotation as GQL; class Episode { const NEWHOPE = 4; - const EMPIRE = 'constant("App\\StarWars\\Movies::MOVIE_EMPIRE")'; + const EMPIRE = Movies::MOVIE_EMPIRE; const JEDI = 6; const FORCEAWAKENS = 'FORCEAWAKENS'; diff --git a/src/Config/Parser/YamlParser.php b/src/Config/Parser/YamlParser.php index 5df3a42bf..1f23df797 100644 --- a/src/Config/Parser/YamlParser.php +++ b/src/Config/Parser/YamlParser.php @@ -10,6 +10,7 @@ use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException; use Symfony\Component\Yaml\Exception\ParseException; use Symfony\Component\Yaml\Parser; +use Symfony\Component\Yaml\Yaml; use function file_get_contents; use function is_array; use function sprintf; @@ -26,7 +27,7 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container, arr $container->addResource(new FileResource($file->getRealPath())); try { - $typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname())); + $typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname()), Yaml::PARSE_CONSTANT); } catch (ParseException $e) { throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e); } diff --git a/tests/Config/Parser/Constants.php b/tests/Config/Parser/Constants.php new file mode 100644 index 000000000..57234b699 --- /dev/null +++ b/tests/Config/Parser/Constants.php @@ -0,0 +1,10 @@ + Constants::TWILEK]; + + $actual = YamlParser::parse(new SplFileInfo($fileName), $this->containerBuilder); + $this->assertSame($expected, $actual); + } +} diff --git a/tests/Config/Parser/fixtures/annotations/Enum/Race.php b/tests/Config/Parser/fixtures/annotations/Enum/Race.php index 5317966a8..0fdf67c9c 100644 --- a/tests/Config/Parser/fixtures/annotations/Enum/Race.php +++ b/tests/Config/Parser/fixtures/annotations/Enum/Race.php @@ -5,6 +5,7 @@ namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Enum; use Overblog\GraphQLBundle\Annotation as GQL; +use Overblog\GraphQLBundle\Tests\Config\Parser\Constants; /** * @GQL\Enum(values={ @@ -18,7 +19,7 @@ class Race public const HUMAIN = 1; public const CHISS = '2'; public const ZABRAK = '3'; - public const TWILEK = '4'; + public const TWILEK = Constants::TWILEK; /** * @var int|string diff --git a/tests/Config/Parser/fixtures/yaml/constants.yml b/tests/Config/Parser/fixtures/yaml/constants.yml new file mode 100644 index 000000000..bc03d2322 --- /dev/null +++ b/tests/Config/Parser/fixtures/yaml/constants.yml @@ -0,0 +1 @@ +value: !php/const Overblog\GraphQLBundle\Tests\Config\Parser\Constants::TWILEK