NelmioApiDocBundle/DependencyInjection/NelmioApiDocExtension.php

92 lines
3.4 KiB
PHP
Raw Normal View History

2016-07-12 00:33:55 +02:00
<?php
/*
2016-12-29 12:09:26 +01:00
* This file is part of the NelmioApiDocBundle package.
2016-07-12 00:33:55 +02:00
*
2016-12-29 12:09:26 +01:00
* (c) Nelmio
2016-07-12 00:33:55 +02:00
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2016-12-29 12:09:26 +01:00
namespace Nelmio\ApiDocBundle\DependencyInjection;
2016-07-12 00:33:55 +02:00
2016-08-04 22:36:20 +02:00
use FOS\RestBundle\Controller\Annotations\ParamInterface;
use Nelmio\ApiDocBundle\ModelDescriber\FormModelDescriber;
use Nelmio\ApiDocBundle\Routing\FilteredRouteCollectionBuilder;
2016-07-13 23:05:14 +02:00
use phpDocumentor\Reflection\DocBlockFactory;
2016-07-12 00:33:55 +02:00
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
2017-06-22 22:28:30 +02:00
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
2017-03-16 19:35:04 +01:00
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
2017-06-24 17:49:00 +02:00
use Symfony\Component\Form\FormInterface;
2016-07-15 00:04:07 +02:00
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
2017-06-22 22:28:30 +02:00
use Symfony\Component\Routing\RouteCollection;
2016-07-12 00:33:55 +02:00
final class NelmioApiDocExtension extends Extension implements PrependExtensionInterface
2016-07-12 00:33:55 +02:00
{
/**
* {@inheritdoc}
*/
public function prepend(ContainerBuilder $container)
{
$container->prependExtensionConfig('framework', ['property_info' => ['enabled' => true]]);
}
2016-07-12 00:33:55 +02:00
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container)
{
$config = $this->processConfiguration(new Configuration(), $configs);
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.xml');
2016-07-13 23:05:14 +02:00
2017-06-24 17:49:00 +02:00
if (interface_exists(FormInterface::class)) {
$container->register('nelmio_api_doc.model_describers.form', FormModelDescriber::class)
->setPublic(false)
->addArgument(new Reference('form.factory'))
->addTag('nelmio_api_doc.model_describer', ['priority' => 10]);
}
// Filter routes
2017-06-22 22:28:30 +02:00
$routesDefinition = (new Definition(RouteCollection::class))
->setFactory([new Reference('router'), 'getRouteCollection']);
if (0 === count($config['routes']['path_patterns'])) {
$container->setDefinition('nelmio_api_doc.routes', $routesDefinition)
->setPublic(false);
} else {
$container->register('nelmio_api_doc.routes', RouteCollection::class)
->setPublic(false)
->setFactory([
(new Definition(FilteredRouteCollectionBuilder::class))
->addArgument($config['routes']['path_patterns']),
'filter',
])
2017-06-22 22:28:30 +02:00
->addArgument($routesDefinition);
}
2016-08-04 22:27:10 +02:00
// Import services needed for each library
2017-01-23 19:46:38 +01:00
$loader->load('swagger_php.xml');
2016-08-04 22:27:10 +02:00
if (class_exists(DocBlockFactory::class)) {
$loader->load('php_doc.xml');
2016-07-28 10:20:59 +02:00
}
2016-08-04 22:36:20 +02:00
if (interface_exists(ParamInterface::class)) {
$loader->load('fos_rest.xml');
}
2016-07-29 10:22:40 +02:00
$bundles = $container->getParameter('kernel.bundles');
// ApiPlatform support
if (isset($bundles['ApiPlatformBundle']) && class_exists('ApiPlatform\Core\Documentation\Documentation')) {
$loader->load('api_platform.xml');
}
// Import the base configuration
$container->getDefinition('nelmio_api_doc.describers.config')->replaceArgument(0, $config['documentation']);
2016-07-12 00:33:55 +02:00
}
}