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
|
||||
*/
|
||||
private $inputClass = null;
|
||||
private $input = null;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
@ -70,8 +70,8 @@ class ApiDoc
|
||||
|
||||
public function __construct(array $data)
|
||||
{
|
||||
if (isset($data['inputClass'])) {
|
||||
$this->inputClass = $data['inputClass'];
|
||||
if (isset($data['input'])) {
|
||||
$this->input = $data['input'];
|
||||
} elseif (isset($data['filters'])) {
|
||||
foreach ($data['filters'] as $filter) {
|
||||
if (!isset($filter['name'])) {
|
||||
@ -121,9 +121,9 @@ class ApiDoc
|
||||
/**
|
||||
* @return string|null
|
||||
*/
|
||||
public function getInputClass()
|
||||
public function getInput()
|
||||
{
|
||||
return $this->inputClass;
|
||||
return $this->input;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -227,9 +227,9 @@ class ApiDoc
|
||||
if ($requirements = $this->requirements) {
|
||||
$data['requirements'] = $requirements;
|
||||
}
|
||||
|
||||
if ($inputClass = $this->inputClass) {
|
||||
$data['inputClass'] = $inputClass;
|
||||
|
||||
if ($input = $this->input) {
|
||||
$data['input'] = $input;
|
||||
}
|
||||
|
||||
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 $array;
|
||||
}
|
||||
|
||||
@ -177,14 +175,13 @@ class ApiDocExtractor
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Registers a class parser to use for parsing input class metadata
|
||||
*
|
||||
* @param ParserInterface $parser
|
||||
* @return void
|
||||
* @param ParserInterface $parser
|
||||
*/
|
||||
public function registerParser(ParserInterface $parser)
|
||||
public function addParser(ParserInterface $parser)
|
||||
{
|
||||
$this->parsers[] = $parser;
|
||||
}
|
||||
@ -225,13 +222,13 @@ class ApiDocExtractor
|
||||
// doc
|
||||
$annotation->setDocumentation($this->getDocCommentText($method));
|
||||
|
||||
// inputClass
|
||||
if (null !== $inputClass = $annotation->getInputClass()) {
|
||||
// input
|
||||
if (null !== $input = $annotation->getInput()) {
|
||||
$parameters = array();
|
||||
|
||||
foreach ($this->parsers as $parser) {
|
||||
if ($parser->supportsClass($inputClass)) {
|
||||
$parameters = $parser->parse($inputClass);
|
||||
if ($parser->supports($input)) {
|
||||
$parameters = $parser->parse($input);
|
||||
}
|
||||
}
|
||||
|
||||
@ -296,7 +293,7 @@ class ApiDocExtractor
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Reflector $reflected
|
||||
* @param Reflector $reflected
|
||||
* @return string
|
||||
*/
|
||||
protected function getDocComment(\Reflector $reflected)
|
||||
@ -316,7 +313,7 @@ class ApiDocExtractor
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Reflector $reflected
|
||||
* @param Reflector $reflected
|
||||
* @return string
|
||||
*/
|
||||
protected function getDocCommentText(\Reflector $reflected)
|
||||
|
@ -26,7 +26,7 @@ interface FormatterInterface
|
||||
/**
|
||||
* Format documentation data for one route.
|
||||
*
|
||||
* @param ApiDoc $annotation
|
||||
* @param ApiDoc $annotation
|
||||
* return string|array
|
||||
*/
|
||||
public function formatOne(ApiDoc $annotation);
|
||||
|
@ -4,7 +4,7 @@ namespace Nelmio\ApiDocBundle;
|
||||
|
||||
use Symfony\Component\HttpKernel\Bundle\Bundle;
|
||||
use Symfony\Component\DependencyInjection\ContainerBuilder;
|
||||
use Nelmio\ApiDocBundle\DependencyInjection\RegisterExtractorClassParsersPass;
|
||||
use Nelmio\ApiDocBundle\DependencyInjection\RegisterExtractorParsersPass;
|
||||
|
||||
class NelmioApiDocBundle extends Bundle
|
||||
{
|
||||
@ -12,6 +12,6 @@ class NelmioApiDocBundle extends Bundle
|
||||
{
|
||||
parent::build($container);
|
||||
|
||||
$container->addCompilerPass(new RegisterExtractorClassParsersPass());
|
||||
$container->addCompilerPass(new RegisterExtractorParsersPass());
|
||||
}
|
||||
}
|
||||
|
@ -11,8 +11,8 @@
|
||||
|
||||
namespace Nelmio\ApiDocBundle\Parser;
|
||||
|
||||
use Symfony\Component\Form\FormBuilder;
|
||||
use Symfony\Component\Form\FormFactoryInterface;
|
||||
use Symfony\Component\Form\Exception\FormException;
|
||||
|
||||
class FormTypeParser implements ParserInterface
|
||||
{
|
||||
@ -39,18 +39,23 @@ class FormTypeParser implements ParserInterface
|
||||
{
|
||||
$this->formFactory = $formFactory;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function supportsClass($class)
|
||||
public function supports($item)
|
||||
{
|
||||
if (is_string($class) && class_exists($class)) {
|
||||
$ref = new \ReflectionClass($class);
|
||||
return ($ref->implementsInterface('Symfony\Component\Form\FormTypeInterface'));
|
||||
try {
|
||||
if (is_string($item) && class_exists($item)) {
|
||||
$item = new $item();
|
||||
}
|
||||
|
||||
$form = $this->formFactory->create($item);
|
||||
} catch (FormException $e) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -19,11 +19,11 @@ interface ParserInterface
|
||||
/**
|
||||
* 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
|
||||
*/
|
||||
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
|
||||
* an array of data with the following keys:
|
||||
@ -32,9 +32,9 @@ interface ParserInterface
|
||||
* - description string
|
||||
* - 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
|
||||
*/
|
||||
public function parse($className);
|
||||
|
||||
}
|
||||
public function parse($item);
|
||||
|
||||
}
|
||||
|
@ -14,7 +14,7 @@
|
||||
<services>
|
||||
<service id="nelmio_api_doc.parser.form_type_parser" class="%nelmio_api_doc.parser.form_type_parser.class%">
|
||||
<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 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%"
|
||||
|
@ -27,7 +27,7 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertFalse($annot->isResource());
|
||||
$this->assertFalse(isset($array['description']));
|
||||
$this->assertNull($annot->getInputClass());
|
||||
$this->assertNull($annot->getInput());
|
||||
}
|
||||
|
||||
public function testConstructWithInvalidData()
|
||||
@ -44,7 +44,7 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertFalse($annot->isResource());
|
||||
$this->assertFalse(isset($array['description']));
|
||||
$this->assertNull($annot->getInputClass());
|
||||
$this->assertNull($annot->getInput());
|
||||
}
|
||||
|
||||
public function testConstruct()
|
||||
@ -60,14 +60,14 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertFalse($annot->isResource());
|
||||
$this->assertEquals($data['description'], $array['description']);
|
||||
$this->assertNull($annot->getInputClass());
|
||||
$this->assertNull($annot->getInput());
|
||||
}
|
||||
|
||||
public function testConstructDefinesAFormType()
|
||||
{
|
||||
$data = array(
|
||||
'description' => 'Heya',
|
||||
'inputClass' => 'My\Form\Type',
|
||||
'input' => 'My\Form\Type',
|
||||
);
|
||||
|
||||
$annot = new ApiDoc($data);
|
||||
@ -77,7 +77,7 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertFalse($annot->isResource());
|
||||
$this->assertEquals($data['description'], $array['description']);
|
||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
||||
$this->assertEquals($data['input'], $annot->getInput());
|
||||
}
|
||||
|
||||
public function testConstructMethodIsResource()
|
||||
@ -85,7 +85,7 @@ class ApiDocTest extends TestCase
|
||||
$data = array(
|
||||
'resource' => true,
|
||||
'description' => 'Heya',
|
||||
'inputClass' => 'My\Form\Type',
|
||||
'input' => 'My\Form\Type',
|
||||
);
|
||||
|
||||
$annot = new ApiDoc($data);
|
||||
@ -95,7 +95,7 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertTrue($annot->isResource());
|
||||
$this->assertEquals($data['description'], $array['description']);
|
||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
||||
$this->assertEquals($data['input'], $annot->getInput());
|
||||
}
|
||||
|
||||
public function testConstructMethodResourceIsFalse()
|
||||
@ -103,7 +103,7 @@ class ApiDocTest extends TestCase
|
||||
$data = array(
|
||||
'resource' => false,
|
||||
'description' => 'Heya',
|
||||
'inputClass' => 'My\Form\Type',
|
||||
'input' => 'My\Form\Type',
|
||||
);
|
||||
|
||||
$annot = new ApiDoc($data);
|
||||
@ -113,7 +113,7 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertFalse($annot->isResource());
|
||||
$this->assertEquals($data['description'], $array['description']);
|
||||
$this->assertEquals($data['inputClass'], $annot->getInputClass());
|
||||
$this->assertEquals($data['input'], $annot->getInput());
|
||||
}
|
||||
|
||||
public function testConstructMethodHasFilters()
|
||||
@ -135,7 +135,7 @@ class ApiDocTest extends TestCase
|
||||
$this->assertEquals(array('a-filter' => array()), $array['filters']);
|
||||
$this->assertTrue($annot->isResource());
|
||||
$this->assertEquals($data['description'], $array['description']);
|
||||
$this->assertNull($annot->getInputClass());
|
||||
$this->assertNull($annot->getInput());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -158,7 +158,7 @@ class ApiDocTest extends TestCase
|
||||
$data = array(
|
||||
'resource' => true,
|
||||
'description' => 'Heya',
|
||||
'inputClass' => 'My\Form\Type',
|
||||
'input' => 'My\Form\Type',
|
||||
'filters' => array(
|
||||
array('name' => 'a-filter'),
|
||||
),
|
||||
@ -171,6 +171,6 @@ class ApiDocTest extends TestCase
|
||||
$this->assertFalse(isset($array['filters']));
|
||||
$this->assertTrue($annot->isResource());
|
||||
$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->assertEquals('index action', $a1->getDescription());
|
||||
$this->assertTrue(is_array($array1['filters']));
|
||||
$this->assertNull($a1->getInputClass());
|
||||
$this->assertNull($a1->getInput());
|
||||
|
||||
$a1 = $data[1]['annotation'];
|
||||
$array1 = $a1->toArray();
|
||||
$this->assertTrue($a1->isResource());
|
||||
$this->assertEquals('index action', $a1->getDescription());
|
||||
$this->assertTrue(is_array($array1['filters']));
|
||||
$this->assertNull($a1->getInputClass());
|
||||
$this->assertNull($a1->getInput());
|
||||
|
||||
$a2 = $data[2]['annotation'];
|
||||
$array2 = $a2->toArray();
|
||||
$this->assertFalse($a2->isResource());
|
||||
$this->assertEquals('create test', $a2->getDescription());
|
||||
$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'];
|
||||
$array2 = $a2->toArray();
|
||||
$this->assertFalse($a2->isResource());
|
||||
$this->assertEquals('create test', $a2->getDescription());
|
||||
$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()
|
||||
@ -76,7 +76,7 @@ class ApiDocExtractorTest extends WebTestCase
|
||||
|
||||
$array = $annotation->toArray();
|
||||
$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->getRoute()
|
||||
|
Loading…
x
Reference in New Issue
Block a user