Skip to content

Commit

Permalink
Make twig dependency lazy
Browse files Browse the repository at this point in the history
  • Loading branch information
mtarld committed Apr 2, 2021
1 parent fc15953 commit 2894404
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php
Expand Up @@ -67,7 +67,7 @@ final class SwaggerUiAction
* @param int[] $swaggerVersions
* @param mixed|null $assetPackage
*/
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, NormalizerInterface $normalizer, TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '', string $version = '', $formats = [], $oauthEnabled = false, $oauthClientId = '', $oauthClientSecret = '', $oauthType = '', $oauthFlow = '', $oauthTokenUrl = '', $oauthAuthorizationUrl = '', $oauthScopes = [], bool $showWebby = true, bool $swaggerUiEnabled = false, bool $reDocEnabled = false, bool $graphqlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false, array $swaggerVersions = [2, 3], OpenApiSwaggerUiAction $swaggerUiAction = null, $assetPackage = null)
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, NormalizerInterface $normalizer, ?TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '', string $version = '', $formats = [], $oauthEnabled = false, $oauthClientId = '', $oauthClientSecret = '', $oauthType = '', $oauthFlow = '', $oauthTokenUrl = '', $oauthAuthorizationUrl = '', $oauthScopes = [], bool $showWebby = true, bool $swaggerUiEnabled = false, bool $reDocEnabled = false, bool $graphqlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false, array $swaggerVersions = [2, 3], OpenApiSwaggerUiAction $swaggerUiAction = null, $assetPackage = null)
{
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
$this->resourceMetadataFactory = $resourceMetadataFactory;
Expand Down Expand Up @@ -95,6 +95,10 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
$this->swaggerUiAction = $swaggerUiAction;
$this->assetPackage = $assetPackage;

if (null === $this->twig) {
throw new \RuntimeException('You cannot use the "twig" service since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
}

if (null === $this->swaggerUiAction) {
@trigger_error(sprintf('The use of "%s" is deprecated since API Platform 2.6, use "%s" instead.', __CLASS__, OpenApiSwaggerUiAction::class), \E_USER_DEPRECATED);
}
Expand Down
4 changes: 2 additions & 2 deletions src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml
Expand Up @@ -15,7 +15,7 @@
<argument type="service" id="api_platform.metadata.resource.name_collection_factory" />
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="api_platform.serializer" />
<argument type="service" id="twig" />
<argument type="service" id="twig" on-invalid="null" />
<argument type="service" id="router" />
<argument>%api_platform.title%</argument>
<argument>%api_platform.description%</argument>
Expand Down Expand Up @@ -52,7 +52,7 @@

<service id="api_platform.swagger_ui.action" class="ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction" public="true">
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="twig" />
<argument type="service" id="twig" on-invalid="null" />
<argument type="service" id="router" />
<argument type="service" id="api_platform.serializer" />
<argument type="service" id="api_platform.openapi.factory" />
Expand Down
6 changes: 5 additions & 1 deletion src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php
Expand Up @@ -41,7 +41,7 @@ final class SwaggerUiAction
private $oauthClientId;
private $oauthClientSecret;

public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, NormalizerInterface $normalizer, OpenApiFactoryInterface $openApiFactory, Options $openApiOptions, SwaggerUiContext $swaggerUiContext, array $formats = [], string $oauthClientId = null, string $oauthClientSecret = null)
public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFactory, ?TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, NormalizerInterface $normalizer, OpenApiFactoryInterface $openApiFactory, Options $openApiOptions, SwaggerUiContext $swaggerUiContext, array $formats = [], string $oauthClientId = null, string $oauthClientSecret = null)
{
$this->resourceMetadataFactory = $resourceMetadataFactory;
$this->twig = $twig;
Expand All @@ -53,6 +53,10 @@ public function __construct(ResourceMetadataFactoryInterface $resourceMetadataFa
$this->formats = $formats;
$this->oauthClientId = $oauthClientId;
$this->oauthClientSecret = $oauthClientSecret;

if (null === $this->twig) {
throw new \RuntimeException('You cannot use the "twig" service since the Twig bundle is not installed. Try running "composer require symfony/twig-bundle".');
}
}

public function __invoke(Request $request)
Expand Down

0 comments on commit 2894404

Please sign in to comment.