Skip to content

Add more native support for php constants in yaml files #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions docs/definitions/type-system/enum.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

/**
Expand All @@ -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';

Expand Down
3 changes: 2 additions & 1 deletion src/Config/Parser/YamlParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions tests/Config/Parser/Constants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Tests\Config\Parser;

class Constants
{
public const TWILEK = '4';
}
21 changes: 21 additions & 0 deletions tests/Config/Parser/YamlParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Overblog\GraphQLBundle\Tests\Config\Parser;

use Overblog\GraphQLBundle\Config\Parser\YamlParser;
use SplFileInfo;
use const DIRECTORY_SEPARATOR;

class YamlParserTest extends TestCase
{
public function testParseConstants(): void
{
$fileName = __DIR__.DIRECTORY_SEPARATOR.'fixtures'.DIRECTORY_SEPARATOR.'yaml'.DIRECTORY_SEPARATOR.'constants.yml';
$expected = ['value' => Constants::TWILEK];

$actual = YamlParser::parse(new SplFileInfo($fileName), $this->containerBuilder);
$this->assertSame($expected, $actual);
}
}
3 changes: 2 additions & 1 deletion tests/Config/Parser/fixtures/annotations/Enum/Race.php
Original file line number Diff line number Diff line change
Expand Up @@ -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={
Expand All @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/Config/Parser/fixtures/yaml/constants.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
value: !php/const Overblog\GraphQLBundle\Tests\Config\Parser\Constants::TWILEK