Skip to content

Conversation

@fgrandjean
Copy link

Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #1149
License MIT
Doc PR

The goal here is to make it failsafe to install api-platform/core on a symfony/skeleton project.
It remove hardcoded dependencies for doctrine/doctrine-bundle and symfony/twig-bundle.
Corresponding features get toggle back on when the package are added to the project.

if (class_exists(Annotation::class)) {
// The "annotation_reader" service is only available if DoctrineBundle is loaded.
$bundles = $container->getParameter('kernel.bundles');
if (class_exists(Annotation::class) && isset($bundles['DoctrineBundle'])) {
Copy link
Member

@soyuka soyuka Apr 9, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this check on bundles really mandatory? I think it'd look cleaner with class_exists but maybe I'm wrong

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know it's a bit weird but in theory it's possible to require "doctrine/annotation" without "doctrine/doctrine-bundle", in that case the class exists but not the service definition.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No problem, can we replace this check by class_exists(DoctrineBundle::class) then?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first it does look cleaner but it's not as reliable. Again, in theory, you can install a bundle with composer but not register it in Symfony's kernel. But since it's an edge case, maybe it's acceptable to simply check for class existance?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd need some expert hands to answer this as I'm not qualified enough :p

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with @fgrandjean it should not be tested alone, since it could lead to unexpected behaviours (as the bundle may not be in the Kernel). So I guess it's better to test that the bundle is loaded.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm for keeping the current code too.

@Simperfit Simperfit requested a review from dunglas April 10, 2018 09:34
$loader->load('swagger.xml');

if ($config['enable_swagger_ui']) {
$bundles = $container->getParameter('kernel.bundles');
Copy link
Contributor

@Simperfit Simperfit Apr 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really need the variable $bundles ?


$loader->load('graphql.xml');

$bundles = $container->getParameter('kernel.bundles');
Copy link
Contributor

@Simperfit Simperfit Apr 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you really need the variable $bundles ?

if (class_exists(Annotation::class)) {
// The "annotation_reader" service is only available if DoctrineBundle is loaded.
$bundles = $container->getParameter('kernel.bundles');
if (class_exists(Annotation::class) && isset($bundles['DoctrineBundle'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm for keeping the current code too.


if (class_exists(Annotation::class)) {
// The "annotation_reader" service is only available if DoctrineBundle is loaded.
$bundles = $container->getParameter('kernel.bundles');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


if ($config['enable_swagger_ui']) {
$bundles = $container->getParameter('kernel.bundles');
if ($config['enable_swagger_ui'] && isset($bundles['TwigBundle'])) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe should we just throw if enable_swagger_ui is true but Twig isn't available. It would be easier to debug (the exception's message should recommend to install twig-bundle.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default value of enable_swagger_ui can be false if twig isn't available btw (in the Configuration class). As you'll not be able to access to the installed bundle list in this class, you can fallback on checking if class_exists(TwigBundle::class).


$loader->load('graphql.xml');

if (isset($bundles['TwigBundle'])) {
Copy link
Member

@dunglas dunglas May 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks wrong to me. It means that you can't get GraphQL support without Twig.

As for Swagger UI, you should throw if api_platform.graphql.graphiql.enabled is true but Twig isn't installed.

@@ -0,0 +1,21 @@
<?xml version="1.0" ?>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be reverted. This action isn't here for GraphiQL in the first place. It is the main GraphQL entrypoint.

Throw exception when graphql is turn on but Twig is missing.
Fixing tests to cover exceptions.

if (class_exists(Annotation::class)) {
// The "annotation_reader" service is only available if DoctrineBundle is loaded.
if (class_exists(Annotation::class) && isset($bundles['DoctrineBundle'])) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should use

$container->hasExtension('doctrine')

There is no need to pass the $bundles.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, they're not strictly the same thing. But we can safely assume that the doctrine extension must be registered if DoctrineBundle is loaded.

WDYT @dunglas? I think we can simplify the existing code too...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me it's mostly the same thing, except that we check for TwigBundle using isset($bundles['TwigBundle']) it made sens to do it the same way for doctrine to keep the code easy to read. But I let @dunglas decide.

@fgrandjean fgrandjean closed this Oct 2, 2018
@shouze shouze mentioned this pull request Aug 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants