Skip to content

Commit 2d5692c

Browse files
authored
Merge pull request #719 from thedustin/add-yaml-const-support
Add more native support for php constants in yaml files
2 parents 964b7a5 + adfa2a8 commit 2d5692c

File tree

6 files changed

+40
-4
lines changed

6 files changed

+40
-4
lines changed

docs/definitions/type-system/enum.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ Episode:
1919
#deprecationReason: "Just because"
2020
EMPIRE:
2121
# We can use a PHP constant to avoid a magic number
22-
value: '@=constant("App\\StarWars\\Movies::MOVIE_EMPIRE")'
22+
# in previous versions this was done with '@=constant("App\\StarWars\\Movies::MOVIE_EMPIRE")'
23+
value: !php/const App\StarWars\Movies::MOVIE_EMPIRE
2324
description: "Released in 1980."
2425
JEDI: 6 # using the short syntax (JEDI value equal to 6)
2526
FORCEAWAKENS: # in this case FORCEAWAKENS value = FORCEAWAKENS
@@ -35,6 +36,7 @@ Note: At the moment, doctrine annotations on constants are not supported. So if
3536

3637
namespace AppBundle;
3738

39+
use App\StarWars\Movies;
3840
use Overblog\GraphQLBundle\Annotation as GQL;
3941

4042
/**
@@ -48,7 +50,7 @@ use Overblog\GraphQLBundle\Annotation as GQL;
4850
class Episode
4951
{
5052
const NEWHOPE = 4;
51-
const EMPIRE = 'constant("App\\StarWars\\Movies::MOVIE_EMPIRE")';
53+
const EMPIRE = Movies::MOVIE_EMPIRE;
5254
const JEDI = 6;
5355
const FORCEAWAKENS = 'FORCEAWAKENS';
5456

src/Config/Parser/YamlParser.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Symfony\Component\DependencyInjection\Exception\InvalidArgumentException;
1111
use Symfony\Component\Yaml\Exception\ParseException;
1212
use Symfony\Component\Yaml\Parser;
13+
use Symfony\Component\Yaml\Yaml;
1314
use function file_get_contents;
1415
use function is_array;
1516
use function sprintf;
@@ -26,7 +27,7 @@ public static function parse(SplFileInfo $file, ContainerBuilder $container, arr
2627
$container->addResource(new FileResource($file->getRealPath()));
2728

2829
try {
29-
$typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname()));
30+
$typesConfig = self::$yamlParser->parse(file_get_contents($file->getPathname()), Yaml::PARSE_CONSTANT);
3031
} catch (ParseException $e) {
3132
throw new InvalidArgumentException(sprintf('The file "%s" does not contain valid YAML.', $file), 0, $e);
3233
}

tests/Config/Parser/Constants.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Config\Parser;
6+
7+
class Constants
8+
{
9+
public const TWILEK = '4';
10+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Overblog\GraphQLBundle\Tests\Config\Parser;
6+
7+
use Overblog\GraphQLBundle\Config\Parser\YamlParser;
8+
use SplFileInfo;
9+
use const DIRECTORY_SEPARATOR;
10+
11+
class YamlParserTest extends TestCase
12+
{
13+
public function testParseConstants(): void
14+
{
15+
$fileName = __DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'yaml'.DIRECTORY_SEPARATOR.'constants.yml';
16+
$expected = ['value' => Constants::TWILEK];
17+
18+
$actual = YamlParser::parse(new SplFileInfo($fileName), $this->containerBuilder);
19+
$this->assertSame($expected, $actual);
20+
}
21+
}

tests/Config/Parser/fixtures/annotations/Enum/Race.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Overblog\GraphQLBundle\Tests\Config\Parser\fixtures\annotations\Enum;
66

77
use Overblog\GraphQLBundle\Annotation as GQL;
8+
use Overblog\GraphQLBundle\Tests\Config\Parser\Constants;
89

910
/**
1011
* @GQL\Enum(values={
@@ -18,7 +19,7 @@ class Race
1819
public const HUMAIN = 1;
1920
public const CHISS = '2';
2021
public const ZABRAK = '3';
21-
public const TWILEK = '4';
22+
public const TWILEK = Constants::TWILEK;
2223

2324
/**
2425
* @var int|string
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
value: !php/const Overblog\GraphQLBundle\Tests\Config\Parser\Constants::TWILEK

0 commit comments

Comments
 (0)