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;
|
2017-06-26 10:34:42 +02:00
|
|
|
use Nelmio\ApiDocBundle\ModelDescriber\FormModelDescriber;
|
2017-06-25 15:40:07 +02:00
|
|
|
use Nelmio\ApiDocBundle\ModelDescriber\JMSModelDescriber;
|
2017-06-26 10:34:42 +02:00
|
|
|
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;
|
2017-01-25 19:38:56 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Extension\PrependExtensionInterface;
|
2017-03-16 19:35:04 +01:00
|
|
|
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
|
2017-06-26 10:34:42 +02:00
|
|
|
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
|
|
|
|
2017-01-25 19:38:56 +01:00
|
|
|
final class NelmioApiDocExtension extends Extension implements PrependExtensionInterface
|
2016-07-12 00:33:55 +02:00
|
|
|
{
|
2017-01-25 19:38:56 +01:00
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*/
|
|
|
|
public function prepend(ContainerBuilder $container)
|
|
|
|
{
|
|
|
|
$container->prependExtensionConfig('framework', ['property_info' => ['enabled' => true]]);
|
2017-06-25 15:40:07 +02:00
|
|
|
|
|
|
|
// JMS Serializer support
|
|
|
|
$bundles = $container->getParameter('kernel.bundles');
|
|
|
|
if (isset($bundles['JMSSerializerBundle'])) {
|
|
|
|
$container->prependExtensionConfig('nelmio_api_doc', ['models' => ['use_jms' => true]]);
|
|
|
|
}
|
2017-01-25 19:38:56 +01:00
|
|
|
}
|
|
|
|
|
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'))
|
2017-06-25 15:40:07 +02:00
|
|
|
->addTag('nelmio_api_doc.model_describer', ['priority' => 100]);
|
2017-06-24 17:49:00 +02:00
|
|
|
}
|
|
|
|
|
2016-11-30 14:08:10 +01:00
|
|
|
// 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']),
|
2017-06-26 10:34:42 +02:00
|
|
|
'filter',
|
|
|
|
])
|
2017-06-22 22:28:30 +02:00
|
|
|
->addArgument($routesDefinition);
|
|
|
|
}
|
2016-11-30 14:08:10 +01:00
|
|
|
|
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
|
|
|
|
|
|
|
// ApiPlatform support
|
2017-06-25 15:40:07 +02:00
|
|
|
$bundles = $container->getParameter('kernel.bundles');
|
2016-07-29 10:22:40 +02:00
|
|
|
if (isset($bundles['ApiPlatformBundle']) && class_exists('ApiPlatform\Core\Documentation\Documentation')) {
|
|
|
|
$loader->load('api_platform.xml');
|
|
|
|
}
|
2017-01-25 18:53:19 +01:00
|
|
|
|
2017-06-25 15:40:07 +02:00
|
|
|
// JMS metadata support
|
|
|
|
if ($config['models']['use_jms']) {
|
|
|
|
$container->register('nelmio_api_doc.model_describers.jms', JMSModelDescriber::class)
|
|
|
|
->setPublic(false)
|
|
|
|
->setArguments([new Reference('jms_serializer.metadata_factory'), new Reference('jms_serializer.naming_strategy')])
|
|
|
|
->addTag('nelmio_api_doc.model_describer', ['priority' => 50]);
|
|
|
|
}
|
|
|
|
|
2017-01-25 18:53:19 +01:00
|
|
|
// Import the base configuration
|
|
|
|
$container->getDefinition('nelmio_api_doc.describers.config')->replaceArgument(0, $config['documentation']);
|
2016-07-12 00:33:55 +02:00
|
|
|
}
|
|
|
|
}
|