Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: metadata factory namespace #31

Open
wants to merge 7 commits into
base: chore/move-last-bits
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Action/EntrypointAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
namespace ApiPlatform\Action;

use ApiPlatform\Api\Entrypoint;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;

/**
* Generates the API entrypoint.
Expand Down
2 changes: 1 addition & 1 deletion src/Api/Entrypoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Api;

use ApiPlatform\Core\Metadata\Resource\ResourceNameCollection;
use ApiPlatform\Metadata\Resource\ResourceNameCollection;

/**
* The first path you will see in the API.
Expand Down
2 changes: 1 addition & 1 deletion src/Api/ResourceClassResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@

namespace ApiPlatform\Api;

use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Exception\InvalidArgumentException;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Util\ClassInfoTrait;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Doctrine\Orm\Metadata\Property;

use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\Persistence\ManagerRegistry;

/**
* Use Doctrine metadata to populate the identifier property.
*
* @author Kévin Dunglas <[email protected]>
*/
final class DoctrineOrmPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
private $decorated;
private $managerRegistry;

public function __construct(ManagerRegistry $managerRegistry, PropertyMetadataFactoryInterface $decorated)
{
$this->managerRegistry = $managerRegistry;
$this->decorated = $decorated;
}

/**
* {@inheritdoc}
*/
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
{
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options);

if (null !== $propertyMetadata->isIdentifier()) {
return $propertyMetadata;
}

$manager = $this->managerRegistry->getManagerForClass($resourceClass);
if (!$manager) {
return $propertyMetadata;
}
$doctrineClassMetadata = $manager->getClassMetadata($resourceClass);

$identifiers = $doctrineClassMetadata->getIdentifier();
foreach ($identifiers as $identifier) {
if ($identifier === $property) {
$propertyMetadata = $propertyMetadata->withIdentifier(true);

if (null !== $propertyMetadata->isWritable()) {
break;
}

if ($doctrineClassMetadata instanceof ClassMetadataInfo) {
$writable = $doctrineClassMetadata->isIdentifierNatural();
} else {
$writable = false;
}

$propertyMetadata = $propertyMetadata->withWritable($writable);

break;
}
}

if (null === $propertyMetadata->isIdentifier()) {
$propertyMetadata = $propertyMetadata->withIdentifier(false);
}

if ($doctrineClassMetadata instanceof ClassMetadataInfo && \in_array($property, $doctrineClassMetadata->getFieldNames(), true)) {
/** @var mixed[] */
$fieldMapping = $doctrineClassMetadata->getFieldMapping($property);
$propertyMetadata = $propertyMetadata->withDefault($fieldMapping['options']['default'] ?? $propertyMetadata->getDefault());
}

return $propertyMetadata;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
use ApiPlatform\Core\Bridge\NelmioApiDoc\Parser\ApiPlatformParser;
use ApiPlatform\Core\Bridge\Symfony\Routing\OperationMethodResolverInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Documentation\Documentation;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use Nelmio\ApiDocBundle\Annotation\ApiDoc;
use Nelmio\ApiDocBundle\Extractor\AnnotationsProviderInterface;
use Psr\Container\ContainerInterface;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@

use ApiPlatform\Core\Api\FormatsProviderInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\ResourceMetadata;
use ApiPlatform\Documentation\Documentation;
use ApiPlatform\Exception\RuntimeException;
use ApiPlatform\Metadata\Resource\Factory\ResourceMetadataCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Symfony\Bundle\SwaggerUi\SwaggerUiAction as OpenApiSwaggerUiAction;
use ApiPlatform\Util\RequestAttributesExtractor;
use Symfony\Component\HttpFoundation\Request;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Symfony/Bundle/Command/RectorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
use ApiPlatform\Core\Bridge\Rector\Service\SubresourceTransformer;
use ApiPlatform\Core\Bridge\Rector\Set\ApiPlatformSetList;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Operation\Factory\SubresourceOperationFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use PhpParser\Lexer\Emulative;
use PhpParser\NodeTraverser;
use PhpParser\Parser\Php7;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Symfony/Bundle/Command/SwaggerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Command;

use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Core\Swagger\Serializer\DocumentationNormalizer;
use ApiPlatform\Documentation\Documentation;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\OpenApi\Serializer\ApiGatewayNormalizer;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidOptionException;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Symfony/Maker/MakeDataPersister.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Core\Bridge\Symfony\Maker;

use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Generator;
Expand Down
2 changes: 1 addition & 1 deletion src/Core/Bridge/Symfony/Maker/MakeDataProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

namespace ApiPlatform\Core\Bridge\Symfony\Maker;

use ApiPlatform\Core\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use ApiPlatform\Metadata\Resource\Factory\ResourceNameCollectionFactoryInterface;
use Symfony\Bundle\MakerBundle\ConsoleStyle;
use Symfony\Bundle\MakerBundle\DependencyBuilder;
use Symfony\Bundle\MakerBundle\Exception\RuntimeCommandException;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Core\Bridge\Symfony\PropertyInfo\Metadata\Property;

use ApiPlatform\Core\Exception\PropertyNotFoundException;
use ApiPlatform\Core\Metadata\Property\Factory\PropertyMetadataFactoryInterface;
use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
use Symfony\Component\PropertyInfo\PropertyInfoExtractorInterface;

/**
* PropertyInfo metadata loader decorator.
*
* @author Kévin Dunglas <[email protected]>
*/
final class PropertyInfoPropertyMetadataFactory implements PropertyMetadataFactoryInterface
{
private $propertyInfo;
private $decorated;

public function __construct(PropertyInfoExtractorInterface $propertyInfo, PropertyMetadataFactoryInterface $decorated = null)
{
$this->propertyInfo = $propertyInfo;
$this->decorated = $decorated;
}

/**
* {@inheritdoc}
*/
public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
{
if (null === $this->decorated) {
$propertyMetadata = new PropertyMetadata();
} else {
try {
$propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
} catch (PropertyNotFoundException $propertyNotFoundException) {
$propertyMetadata = new PropertyMetadata();
}
}

if (null === $propertyMetadata->getType()) {
$types = $this->propertyInfo->getTypes($resourceClass, $property, $options);
if (isset($types[0])) {
$propertyMetadata = $propertyMetadata->withType($types[0]);
}
}

if (null === $propertyMetadata->getDescription() && null !== $description = $this->propertyInfo->getShortDescription($resourceClass, $property, $options)) {
$propertyMetadata = $propertyMetadata->withDescription($description);
}

if (null === $propertyMetadata->isReadable() && null !== $readable = $this->propertyInfo->isReadable($resourceClass, $property, $options)) {
$propertyMetadata = $propertyMetadata->withReadable($readable);
}

if (null === $propertyMetadata->isWritable() && null !== $writable = $this->propertyInfo->isWritable($resourceClass, $property, $options)) {
$propertyMetadata = $propertyMetadata->withWritable($writable);
}

if (method_exists($this->propertyInfo, 'isInitializable')) {
if (null === $propertyMetadata->isInitializable() && null !== $initializable = $this->propertyInfo->isInitializable($resourceClass, $property, $options)) {
$propertyMetadata = $propertyMetadata->withInitializable($initializable);
}
} else {
// BC layer for Symfony < 4.2
$ref = new \ReflectionClass($resourceClass);
if ($ref->isInstantiable() && $constructor = $ref->getConstructor()) {
foreach ($constructor->getParameters() as $constructorParameter) {
if ($constructorParameter->name === $property && null === $propertyMetadata->isInitializable()) {
$propertyMetadata = $propertyMetadata->withInitializable(true);
}
}
}
}

return $propertyMetadata;
}
}
Loading