mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-03-10 09:36:10 +03:00
changed inputClass to input, refactored method and class names accordingly, fixed cs in several places
This commit is contained in:
parent
b2a2426f76
commit
b9e8d61082
@ -26,7 +26,7 @@ class ApiDoc
|
|||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
private $inputClass = null;
|
private $input = null;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var string
|
* @var string
|
||||||
@ -70,8 +70,8 @@ class ApiDoc
|
|||||||
|
|
||||||
public function __construct(array $data)
|
public function __construct(array $data)
|
||||||
{
|
{
|
||||||
if (isset($data['inputClass'])) {
|
if (isset($data['input'])) {
|
||||||
$this->inputClass = $data['inputClass'];
|
$this->input = $data['input'];
|
||||||
} elseif (isset($data['filters'])) {
|
} elseif (isset($data['filters'])) {
|
||||||
foreach ($data['filters'] as $filter) {
|
foreach ($data['filters'] as $filter) {
|
||||||
if (!isset($filter['name'])) {
|
if (!isset($filter['name'])) {
|
||||||
@ -121,9 +121,9 @@ class ApiDoc
|
|||||||
/**
|
/**
|
||||||
* @return string|null
|
* @return string|null
|
||||||
*/
|
*/
|
||||||
public function getInputClass()
|
public function getInput()
|
||||||
{
|
{
|
||||||
return $this->inputClass;
|
return $this->input;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -228,8 +228,8 @@ class ApiDoc
|
|||||||
$data['requirements'] = $requirements;
|
$data['requirements'] = $requirements;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($inputClass = $this->inputClass) {
|
if ($input = $this->input) {
|
||||||
$data['inputClass'] = $inputClass;
|
$data['input'] = $input;
|
||||||
}
|
}
|
||||||
|
|
||||||
return $data;
|
return $data;
|
||||||
|
@ -1,23 +0,0 @@
|
|||||||
<?php
|
|
||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\DependencyInjection;
|
|
||||||
|
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
|
||||||
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
|
||||||
use Symfony\Component\DependencyInjection\Reference;
|
|
||||||
|
|
||||||
class RegisterExtractorClassParsersPass implements CompilerPassInterface
|
|
||||||
{
|
|
||||||
public function process(ContainerBuilder $container)
|
|
||||||
{
|
|
||||||
if (false === $container->hasDefinition('nelmio_api_doc.extractor.api_doc_extractor')) {
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$definition = $container->getDefinition('nelmio_api_doc.extractor.api_doc_extractor');
|
|
||||||
|
|
||||||
foreach ($container->findTaggedServiceIds('nelmio_api_doc.extractor.class_parser') as $id => $attributes) {
|
|
||||||
$definition->addMethodCall('registerParser', array(new Reference($id)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
37
DependencyInjection/RegisterExtractorParsersPass.php
Normal file
37
DependencyInjection/RegisterExtractorParsersPass.php
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Nelmio\ApiDocBundle\DependencyInjection;
|
||||||
|
|
||||||
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
|
||||||
|
use Symfony\Component\DependencyInjection\Reference;
|
||||||
|
|
||||||
|
class RegisterExtractorParsersPass implements CompilerPassInterface
|
||||||
|
{
|
||||||
|
public function process(ContainerBuilder $container)
|
||||||
|
{
|
||||||
|
if (false === $container->hasDefinition('nelmio_api_doc.extractor.api_doc_extractor')) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$definition = $container->getDefinition('nelmio_api_doc.extractor.api_doc_extractor');
|
||||||
|
|
||||||
|
//find registered parsers and sort by priority
|
||||||
|
$sortedParsers = array();
|
||||||
|
foreach ($container->findTaggedServiceIds('nelmio_api_doc.extractor.parser') as $id => $attributes) {
|
||||||
|
$priority = isset($attributes['priority']) ? $attributes['priority'] : 0;
|
||||||
|
$sortedParsers[$priority][] = $id;
|
||||||
|
}
|
||||||
|
|
||||||
|
//add parsers if any
|
||||||
|
if (!empty($sortedParsers)) {
|
||||||
|
krsort($sortedParsers);
|
||||||
|
$sortedParsers = call_user_func_array('array_merge', $sortedParsers);
|
||||||
|
|
||||||
|
//add method call for each registered parsers
|
||||||
|
foreach ($sortedParsers as $id) {
|
||||||
|
$definition->addMethodCall('addParser', array(new Reference($id)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -121,8 +121,6 @@ class ApiDocExtractor
|
|||||||
return strcmp($a['resource'], $b['resource']);
|
return strcmp($a['resource'], $b['resource']);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
return $array;
|
return $array;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -182,9 +180,8 @@ class ApiDocExtractor
|
|||||||
* Registers a class parser to use for parsing input class metadata
|
* Registers a class parser to use for parsing input class metadata
|
||||||
*
|
*
|
||||||
* @param ParserInterface $parser
|
* @param ParserInterface $parser
|
||||||
* @return void
|
|
||||||
*/
|
*/
|
||||||
public function registerParser(ParserInterface $parser)
|
public function addParser(ParserInterface $parser)
|
||||||
{
|
{
|
||||||
$this->parsers[] = $parser;
|
$this->parsers[] = $parser;
|
||||||
}
|
}
|
||||||
@ -225,13 +222,13 @@ class ApiDocExtractor
|
|||||||
// doc
|
// doc
|
||||||
$annotation->setDocumentation($this->getDocCommentText($method));
|
$annotation->setDocumentation($this->getDocCommentText($method));
|
||||||
|
|
||||||
// inputClass
|
// input
|
||||||
if (null !== $inputClass = $annotation->getInputClass()) {
|
if (null !== $input = $annotation->getInput()) {
|
||||||
$parameters = array();
|
$parameters = array();
|
||||||
|
|
||||||
foreach ($this->parsers as $parser) {
|
foreach ($this->parsers as $parser) {
|
||||||
if ($parser->supportsClass($inputClass)) {
|
if ($parser->supports($input)) {
|
||||||
$parameters = $parser->parse($inputClass);
|
$parameters = $parser->parse($input);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@ namespace Nelmio\ApiDocBundle;
|
|||||||
|
|
||||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||||
use Nelmio\ApiDocBundle\DependencyInjection\RegisterExtractorClassParsersPass;
|
use Nelmio\ApiDocBundle\DependencyInjection\RegisterExtractorParsersPass;
|
||||||
|
|
||||||
class NelmioApiDocBundle extends Bundle
|
class NelmioApiDocBundle extends Bundle
|
||||||
{
|
{
|
||||||
@ -12,6 +12,6 @@ class NelmioApiDocBundle extends Bundle
|
|||||||
{
|
{
|
||||||
parent::build($container);
|
parent::build($container);
|
||||||
|
|
||||||
$container->addCompilerPass(new RegisterExtractorClassParsersPass());
|
$container->addCompilerPass(new RegisterExtractorParsersPass());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,8 +11,8 @@
|
|||||||
|
|
||||||
namespace Nelmio\ApiDocBundle\Parser;
|
namespace Nelmio\ApiDocBundle\Parser;
|
||||||
|
|
||||||
use Symfony\Component\Form\FormBuilder;
|
|
||||||
use Symfony\Component\Form\FormFactoryInterface;
|
use Symfony\Component\Form\FormFactoryInterface;
|
||||||
|
use Symfony\Component\Form\Exception\FormException;
|
||||||
|
|
||||||
class FormTypeParser implements ParserInterface
|
class FormTypeParser implements ParserInterface
|
||||||
{
|
{
|
||||||
@ -43,16 +43,21 @@ class FormTypeParser implements ParserInterface
|
|||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
public function supportsClass($class)
|
public function supports($item)
|
||||||
{
|
{
|
||||||
if (is_string($class) && class_exists($class)) {
|
try {
|
||||||
$ref = new \ReflectionClass($class);
|
if (is_string($item) && class_exists($item)) {
|
||||||
return ($ref->implementsInterface('Symfony\Component\Form\FormTypeInterface'));
|
$item = new $item();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$form = $this->formFactory->create($item);
|
||||||
|
} catch (FormException $e) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritdoc}
|
* {@inheritdoc}
|
||||||
*/
|
*/
|
||||||
|
@ -19,10 +19,10 @@ interface ParserInterface
|
|||||||
/**
|
/**
|
||||||
* Return true/false whether this class supports parsing the given class.
|
* Return true/false whether this class supports parsing the given class.
|
||||||
*
|
*
|
||||||
* @param string $item The string name of the class to parse.
|
* @param string $item The string type of input to parse.
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function supportsClass($className);
|
public function supports($item);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns an array of class property metadata where each item is a key (the property name) and
|
* Returns an array of class property metadata where each item is a key (the property name) and
|
||||||
@ -32,9 +32,9 @@ interface ParserInterface
|
|||||||
* - description string
|
* - description string
|
||||||
* - readonly boolean
|
* - readonly boolean
|
||||||
*
|
*
|
||||||
* @param string $class The string name of the class to parse.
|
* @param string $item The string type of input to parse.
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public function parse($className);
|
public function parse($item);
|
||||||
|
|
||||||
}
|
}
|
@ -14,7 +14,7 @@
|
|||||||
<services>
|
<services>
|
||||||
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
||||||
<argument type="service" id="form.factory" />
|
<argument type="service" id="form.factory" />
|
||||||
<tag name="nelmio_api_doc.extractor.class_parser" />
|
<tag name="nelmio_api_doc.extractor.parser" priority="0" />
|
||||||
</service>
|
</service>
|
||||||
<service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" />
|
<service id="nelmio_api_doc.formatter.abstract_formatter" class="%nelmio_api_doc.formatter.abstract_formatter.class%" />
|
||||||
<service id="nelmio_api_doc.formatter.markdown_formatter" class="%nelmio_api_doc.formatter.markdown_formatter.class%"
|
<service id="nelmio_api_doc.formatter.markdown_formatter" class="%nelmio_api_doc.formatter.markdown_formatter.class%"
|
||||||
|
@ -27,7 +27,7 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertFalse($annot->isResource());
|
$this->assertFalse($annot->isResource());
|
||||||
$this->assertFalse(isset($array['description']));
|
$this->assertFalse(isset($array['description']));
|
||||||
$this->assertNull($annot->getInputClass());
|
$this->assertNull($annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructWithInvalidData()
|
public function testConstructWithInvalidData()
|
||||||
@ -44,7 +44,7 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertFalse($annot->isResource());
|
$this->assertFalse($annot->isResource());
|
||||||
$this->assertFalse(isset($array['description']));
|
$this->assertFalse(isset($array['description']));
|
||||||
$this->assertNull($annot->getInputClass());
|
$this->assertNull($annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstruct()
|
public function testConstruct()
|
||||||
@ -60,14 +60,14 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertFalse($annot->isResource());
|
$this->assertFalse($annot->isResource());
|
||||||
$this->assertEquals($data['description'], $array['description']);
|
$this->assertEquals($data['description'], $array['description']);
|
||||||
$this->assertNull($annot->getInputClass());
|
$this->assertNull($annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructDefinesAFormType()
|
public function testConstructDefinesAFormType()
|
||||||
{
|
{
|
||||||
$data = array(
|
$data = array(
|
||||||
'description' => 'Heya',
|
'description' => 'Heya',
|
||||||
'inputClass' => 'My\Form\Type',
|
'input' => 'My\Form\Type',
|
||||||
);
|
);
|
||||||
|
|
||||||
$annot = new ApiDoc($data);
|
$annot = new ApiDoc($data);
|
||||||
@ -77,7 +77,7 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertFalse($annot->isResource());
|
$this->assertFalse($annot->isResource());
|
||||||
$this->assertEquals($data['description'], $array['description']);
|
$this->assertEquals($data['description'], $array['description']);
|
||||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
$this->assertEquals($data['input'], $annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructMethodIsResource()
|
public function testConstructMethodIsResource()
|
||||||
@ -85,7 +85,7 @@ class ApiDocTest extends TestCase
|
|||||||
$data = array(
|
$data = array(
|
||||||
'resource' => true,
|
'resource' => true,
|
||||||
'description' => 'Heya',
|
'description' => 'Heya',
|
||||||
'inputClass' => 'My\Form\Type',
|
'input' => 'My\Form\Type',
|
||||||
);
|
);
|
||||||
|
|
||||||
$annot = new ApiDoc($data);
|
$annot = new ApiDoc($data);
|
||||||
@ -95,7 +95,7 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertTrue($annot->isResource());
|
$this->assertTrue($annot->isResource());
|
||||||
$this->assertEquals($data['description'], $array['description']);
|
$this->assertEquals($data['description'], $array['description']);
|
||||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
$this->assertEquals($data['input'], $annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructMethodResourceIsFalse()
|
public function testConstructMethodResourceIsFalse()
|
||||||
@ -103,7 +103,7 @@ class ApiDocTest extends TestCase
|
|||||||
$data = array(
|
$data = array(
|
||||||
'resource' => false,
|
'resource' => false,
|
||||||
'description' => 'Heya',
|
'description' => 'Heya',
|
||||||
'inputClass' => 'My\Form\Type',
|
'input' => 'My\Form\Type',
|
||||||
);
|
);
|
||||||
|
|
||||||
$annot = new ApiDoc($data);
|
$annot = new ApiDoc($data);
|
||||||
@ -113,7 +113,7 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertFalse($annot->isResource());
|
$this->assertFalse($annot->isResource());
|
||||||
$this->assertEquals($data['description'], $array['description']);
|
$this->assertEquals($data['description'], $array['description']);
|
||||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
$this->assertEquals($data['input'], $annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testConstructMethodHasFilters()
|
public function testConstructMethodHasFilters()
|
||||||
@ -135,7 +135,7 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertEquals(array('a-filter' => array()), $array['filters']);
|
$this->assertEquals(array('a-filter' => array()), $array['filters']);
|
||||||
$this->assertTrue($annot->isResource());
|
$this->assertTrue($annot->isResource());
|
||||||
$this->assertEquals($data['description'], $array['description']);
|
$this->assertEquals($data['description'], $array['description']);
|
||||||
$this->assertNull($annot->getInputClass());
|
$this->assertNull($annot->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -158,7 +158,7 @@ class ApiDocTest extends TestCase
|
|||||||
$data = array(
|
$data = array(
|
||||||
'resource' => true,
|
'resource' => true,
|
||||||
'description' => 'Heya',
|
'description' => 'Heya',
|
||||||
'inputClass' => 'My\Form\Type',
|
'input' => 'My\Form\Type',
|
||||||
'filters' => array(
|
'filters' => array(
|
||||||
array('name' => 'a-filter'),
|
array('name' => 'a-filter'),
|
||||||
),
|
),
|
||||||
@ -171,6 +171,6 @@ class ApiDocTest extends TestCase
|
|||||||
$this->assertFalse(isset($array['filters']));
|
$this->assertFalse(isset($array['filters']));
|
||||||
$this->assertTrue($annot->isResource());
|
$this->assertTrue($annot->isResource());
|
||||||
$this->assertEquals($data['description'], $array['description']);
|
$this->assertEquals($data['description'], $array['description']);
|
||||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
$this->assertEquals($data['input'], $annot->getInput());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -39,28 +39,28 @@ class ApiDocExtractorTest extends WebTestCase
|
|||||||
$this->assertTrue($a1->isResource());
|
$this->assertTrue($a1->isResource());
|
||||||
$this->assertEquals('index action', $a1->getDescription());
|
$this->assertEquals('index action', $a1->getDescription());
|
||||||
$this->assertTrue(is_array($array1['filters']));
|
$this->assertTrue(is_array($array1['filters']));
|
||||||
$this->assertNull($a1->getInputClass());
|
$this->assertNull($a1->getInput());
|
||||||
|
|
||||||
$a1 = $data[1]['annotation'];
|
$a1 = $data[1]['annotation'];
|
||||||
$array1 = $a1->toArray();
|
$array1 = $a1->toArray();
|
||||||
$this->assertTrue($a1->isResource());
|
$this->assertTrue($a1->isResource());
|
||||||
$this->assertEquals('index action', $a1->getDescription());
|
$this->assertEquals('index action', $a1->getDescription());
|
||||||
$this->assertTrue(is_array($array1['filters']));
|
$this->assertTrue(is_array($array1['filters']));
|
||||||
$this->assertNull($a1->getInputClass());
|
$this->assertNull($a1->getInput());
|
||||||
|
|
||||||
$a2 = $data[2]['annotation'];
|
$a2 = $data[2]['annotation'];
|
||||||
$array2 = $a2->toArray();
|
$array2 = $a2->toArray();
|
||||||
$this->assertFalse($a2->isResource());
|
$this->assertFalse($a2->isResource());
|
||||||
$this->assertEquals('create test', $a2->getDescription());
|
$this->assertEquals('create test', $a2->getDescription());
|
||||||
$this->assertFalse(isset($array2['filters']));
|
$this->assertFalse(isset($array2['filters']));
|
||||||
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInputClass());
|
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInput());
|
||||||
|
|
||||||
$a2 = $data[3]['annotation'];
|
$a2 = $data[3]['annotation'];
|
||||||
$array2 = $a2->toArray();
|
$array2 = $a2->toArray();
|
||||||
$this->assertFalse($a2->isResource());
|
$this->assertFalse($a2->isResource());
|
||||||
$this->assertEquals('create test', $a2->getDescription());
|
$this->assertEquals('create test', $a2->getDescription());
|
||||||
$this->assertFalse(isset($array2['filters']));
|
$this->assertFalse(isset($array2['filters']));
|
||||||
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInputClass());
|
$this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInput());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGet()
|
public function testGet()
|
||||||
@ -76,7 +76,7 @@ class ApiDocExtractorTest extends WebTestCase
|
|||||||
|
|
||||||
$array = $annotation->toArray();
|
$array = $annotation->toArray();
|
||||||
$this->assertTrue(is_array($array['filters']));
|
$this->assertTrue(is_array($array['filters']));
|
||||||
$this->assertNull($annotation->getInputClass());
|
$this->assertNull($annotation->getInput());
|
||||||
|
|
||||||
$annotation2 = $extractor->get('nemlio.test.controller:indexAction', 'test_service_route_1');
|
$annotation2 = $extractor->get('nemlio.test.controller:indexAction', 'test_service_route_1');
|
||||||
$annotation2->getRoute()
|
$annotation2->getRoute()
|
||||||
|
Loading…
x
Reference in New Issue
Block a user