welcome back to dyb-tech

This commit is contained in:
Daniel Guzman
2024-05-18 02:28:01 +02:00
parent 9513cdba09
commit 9f30bc98c7
6149 changed files with 668407 additions and 0 deletions
@@ -0,0 +1,45 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
class ArrayPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface, PropertyDescriberAwareInterface
{
use ModelRegistryAwareTrait;
use PropertyDescriberAwareTrait;
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->type = 'array';
$property = Util::getChild($property, OA\Items::class);
foreach ($types[0]->getCollectionValueTypes() as $type) {
// Handle list pseudo type
// https://symfony.com/doc/current/components/property_info.html#type-getcollectionkeytypes-type-getcollectionvaluetypes
if ($this->supports([$type]) && empty($type->getCollectionValueTypes())) {
continue;
}
$this->propertyDescriber->describe([$type], $property, $groups, $schema, $context);
}
}
public function supports(array $types): bool
{
return 1 === count($types)
&& $types[0]->isCollection();
}
}
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class BooleanPropertyDescriber implements PropertyDescriberInterface
{
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->type = 'boolean';
}
public function supports(array $types): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_BOOL === $types[0]->getBuiltinType();
}
}
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
class CompoundPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface, PropertyDescriberAwareInterface
{
use ModelRegistryAwareTrait;
use PropertyDescriberAwareTrait;
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->oneOf = Generator::UNDEFINED !== $property->oneOf ? $property->oneOf : [];
foreach ($types as $type) {
$property->oneOf[] = $schema = Util::createChild($property, OA\Schema::class, []);
$this->propertyDescriber->describe([$type], $schema, $groups, $schema, $context);
}
}
public function supports(array $types): bool
{
return count($types) >= 2;
}
}
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class DateTimePropertyDescriber implements PropertyDescriberInterface
{
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->type = 'string';
$property->format = 'date-time';
}
public function supports(array $types): bool
{
return 1 === count($types)
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType()
&& is_a($types[0]->getClassName(), \DateTimeInterface::class, true);
}
}
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class FloatPropertyDescriber implements PropertyDescriberInterface
{
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->type = 'number';
$property->format = 'float';
}
public function supports(array $types): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_FLOAT === $types[0]->getBuiltinType();
}
}
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class IntegerPropertyDescriber implements PropertyDescriberInterface
{
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->type = 'integer';
}
public function supports(array $types): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_INT === $types[0]->getBuiltinType();
}
}
@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
final class NullablePropertyDescriber implements PropertyDescriberInterface, PropertyDescriberAwareInterface
{
use PropertyDescriberAwareTrait;
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
if (Generator::UNDEFINED === $property->nullable) {
$property->nullable = true;
}
$this->propertyDescriber->describe($types, $property, $groups, $schema, $context);
}
public function supports(array $types): bool
{
foreach ($types as $type) {
if ($type->isNullable()) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,55 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
use Symfony\Component\PropertyInfo\Type;
/**
* @deprecated Since 4.17, {@see NullablePropertyDescriber} instead.
*/
trait NullablePropertyTrait
{
protected function setNullableProperty(Type $type, OA\Schema $property, ?OA\Schema $schema, array $context = []): void
{
if (Generator::UNDEFINED !== $property->nullable) {
if (!$property->nullable) {
// if already false mark it as undefined (so it does not show up as `nullable: false`)
$property->nullable = Generator::UNDEFINED;
}
return;
}
if ($type->isNullable()) {
$property->nullable = true;
}
if (!$type->isNullable() && Generator::UNDEFINED !== $property->default) {
return;
}
if (!$type->isNullable() && null !== $schema) {
$propertyName = Util::getSchemaPropertyName($schema, $property);
if (null === $propertyName) {
return;
}
$existingRequiredFields = Generator::UNDEFINED !== $schema->required ? $schema->required : [];
$existingRequiredFields[] = $propertyName;
$schema->required = array_values(array_unique($existingRequiredFields));
}
}
}
@@ -0,0 +1,60 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use Nelmio\ApiDocBundle\Model\Model;
use Nelmio\ApiDocBundle\OpenApiPhp\Util;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class ObjectPropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
{
use ModelRegistryAwareTrait;
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$type = new Type(
$types[0]->getBuiltinType(),
false,
$types[0]->getClassName(),
$types[0]->isCollection(),
// BC layer for symfony < 5.3
method_exists($types[0], 'getCollectionKeyTypes') ? $types[0]->getCollectionKeyTypes() : $types[0]->getCollectionKeyType(),
method_exists($types[0], 'getCollectionValueTypes') ?
($types[0]->getCollectionValueTypes()[0] ?? null) :
$types[0]->getCollectionValueType()
); // ignore nullable field
if ($types[0]->isNullable()) {
$weakContext = Util::createWeakContext($property->_context);
$schemas = [new OA\Schema(['ref' => $this->modelRegistry->register(new Model($type, $groups, null, $context)), '_context' => $weakContext])];
if (function_exists('enum_exists') && enum_exists($type->getClassName())) {
$property->allOf = $schemas;
} else {
$property->oneOf = $schemas;
}
return;
}
$property->ref = $this->modelRegistry->register(new Model($type, $groups, null, $context));
}
public function supports(array $types): bool
{
return 1 === count($types)
&& Type::BUILTIN_TYPE_OBJECT === $types[0]->getBuiltinType();
}
}
@@ -0,0 +1,78 @@
<?php
declare(strict_types=1);
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareInterface;
use Nelmio\ApiDocBundle\Describer\ModelRegistryAwareTrait;
use OpenApi\Annotations as OA;
final class PropertyDescriber implements PropertyDescriberInterface, ModelRegistryAwareInterface
{
use ModelRegistryAwareTrait;
/** @var array<string, PropertyDescriberInterface[]> Recursion helper */
private $called = [];
/** @var PropertyDescriberInterface[] */
private $propertyDescribers;
public function __construct(
iterable $propertyDescribers
) {
$this->propertyDescribers = $propertyDescribers;
}
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = []): void
{
if (!$propertyDescriber = $this->getPropertyDescriber($types)) {
return;
}
$this->called[$this->getHash($types)][] = $propertyDescriber;
$propertyDescriber->describe($types, $property, $groups, $schema, $context);
$this->called = []; // Reset recursion helper
}
public function supports(array $types): bool
{
return null !== $this->getPropertyDescriber($types);
}
private function getHash(array $types): string
{
return md5(serialize($types));
}
private function getPropertyDescriber(array $types): ?PropertyDescriberInterface
{
foreach ($this->propertyDescribers as $propertyDescriber) {
/* BC layer for Symfony < 6.3 @see https://symfony.com/doc/6.3/service_container/tags.html#reference-tagged-services */
if ($propertyDescriber instanceof self) {
continue;
}
// Prevent infinite recursion
if (key_exists($this->getHash($types), $this->called)) {
if (in_array($propertyDescriber, $this->called[$this->getHash($types)], true)) {
continue;
}
}
if ($propertyDescriber instanceof ModelRegistryAwareInterface) {
$propertyDescriber->setModelRegistry($this->modelRegistry);
}
if ($propertyDescriber instanceof PropertyDescriberAwareInterface) {
$propertyDescriber->setPropertyDescriber($this);
}
if ($propertyDescriber->supports($types)) {
return $propertyDescriber;
}
}
return null;
}
}
@@ -0,0 +1,17 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
interface PropertyDescriberAwareInterface
{
public function setPropertyDescriber(PropertyDescriberInterface $propertyDescriber): void;
}
@@ -0,0 +1,25 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
trait PropertyDescriberAwareTrait
{
/**
* @var PropertyDescriberInterface
*/
protected $propertyDescriber;
public function setPropertyDescriber(PropertyDescriberInterface $propertyDescriber): void
{
$this->propertyDescriber = $propertyDescriber;
}
}
@@ -0,0 +1,31 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations\Schema;
use Symfony\Component\PropertyInfo\Type;
interface PropertyDescriberInterface
{
/**
* @param Type[] $types
* @param string[]|null $groups Deprecated use $context['groups'] instead
* @param Schema $schema Allows to make changes inside of the schema (e.g. adding required fields)
* @param array<string, mixed> $context Context options for describing the property
*/
public function describe(array $types, Schema $property, array $groups = null /* , ?Schema $schema = null */ /* , array $context = [] */);
/**
* @param Type[] $types
*/
public function supports(array $types): bool;
}
@@ -0,0 +1,50 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use OpenApi\Generator;
/**
* Mark a property as required if it is not nullable.
*/
final class RequiredPropertyDescriber implements PropertyDescriberInterface, PropertyDescriberAwareInterface
{
use PropertyDescriberAwareTrait;
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$this->propertyDescriber->describe($types, $property, $groups, $schema, $context);
if (!$property instanceof OA\Property) {
return;
}
if (null === $schema) {
return;
}
if (true === $property->nullable || !Generator::isDefault($property->default)) {
return;
}
$existingRequiredFields = Generator::UNDEFINED !== $schema->required ? $schema->required : [];
$existingRequiredFields[] = $property->property;
$schema->required = array_values(array_unique($existingRequiredFields));
}
public function supports(array $types): bool
{
return true;
}
}
@@ -0,0 +1,28 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\PropertyDescriber;
use OpenApi\Annotations as OA;
use Symfony\Component\PropertyInfo\Type;
class StringPropertyDescriber implements PropertyDescriberInterface
{
public function describe(array $types, OA\Schema $property, array $groups = null, ?OA\Schema $schema = null, array $context = [])
{
$property->type = 'string';
}
public function supports(array $types): bool
{
return 1 === count($types) && Type::BUILTIN_TYPE_STRING === $types[0]->getBuiltinType();
}
}