Skip to content

Commit

Permalink
Merge pull request #1357 from garak/fix-attribute-annotation
Browse files Browse the repository at this point in the history
do not rely on annotations to use attributes
  • Loading branch information
garak committed Feb 27, 2023
2 parents 6f884a2 + a179ffe commit 6ee998d
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ jobs:
with:
php-version: ${{ matrix.php }}
extensions: mongodb-stable, pdo_sqlite
- run: composer require --no-update doctrine/annotations:^1.14
- run: |
composer global config --no-plugins allow-plugins.symfony/flex true
composer global require --no-interaction --no-progress symfony/flex:^2.2
Expand Down
1 change: 0 additions & 1 deletion UPGRADE.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,3 @@
* the new default type for mapping is "attribute". You can still use annotations, but you need an explicit definition (set "annotation" as value for "vich_uploader.metadata.type" config key)
* the service "vich_uploader.current_date_time_helper" has been removed. The `DateTimeHelper` interface has been
removed as well.
* if your project use `doctrine-bundle` >= 2.8 version, you must require `doctrine/annotations` in order to use annotations or attributes for mapping
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
"symfony/yaml": "^5.4 || ^6.0"
},
"suggest": {
"doctrine/annotations": "If your project use doctrine-bundle:>=2.8, this package is required to use annotations or attributes",
"doctrine/annotations": "If you use doctrine/doctrine-bundle >2.7, this package is required to use annotations",
"doctrine/doctrine-bundle": "For integration with Doctrine",
"doctrine/mongodb-odm-bundle": "For integration with Doctrine ODM",
"doctrine/orm": "For integration with Doctrine ORM",
Expand Down
22 changes: 11 additions & 11 deletions src/DependencyInjection/Compiler/RegisterMappingDriversPass.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ public function process(ContainerBuilder $container): void
new Reference('vich_uploader.metadata_driver.xml'),
];

if ($container->has('annotation_reader')) {
$managers = [];
if ($container->hasDefinition('doctrine_mongodb')) {
$managers[] = new Reference('doctrine_mongodb');
}
if ($container->hasDefinition('doctrine')) {
$managers[] = new Reference('doctrine');
}
if ($container->hasDefinition('doctrine_phpcr')) {
$managers[] = new Reference('doctrine_phpcr');
}
$managers = [];
if ($container->hasDefinition('doctrine_mongodb')) {
$managers[] = new Reference('doctrine_mongodb');
}
if ($container->hasDefinition('doctrine')) {
$managers[] = new Reference('doctrine');
}
if ($container->hasDefinition('doctrine_phpcr')) {
$managers[] = new Reference('doctrine_phpcr');
}

if (count($managers) > 0) {
$drivers[] = $container->getDefinition('vich_uploader.metadata_driver.annotation')
->replaceArgument('$managerRegistryList', $managers);
}
Expand Down
13 changes: 10 additions & 3 deletions src/DependencyInjection/VichUploaderExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Vich\UploaderBundle\Exception\MissingPackageException;
use Vich\UploaderBundle\Metadata\CacheWarmer;
use Vich\UploaderBundle\Storage\StorageInterface;

Expand Down Expand Up @@ -138,16 +139,22 @@ protected function registerAnnotationStrategy(ContainerBuilder $container, array
}

switch ($config['metadata']['type']) {
case 'attribute':
case 'annotation':
if (!class_exists(AnnotationReader::class) || !$container::willBeAvailable('doctrine/annotations', AnnotationReader::class, [])) {
$msg = 'Annotations support missing. Try running "composer require doctrine/annotations".';
throw new MissingPackageException($msg);
}

$container->setDefinition(
'vich_uploader.metadata.reader',
$container->getDefinition('vich_uploader.metadata.attribute_reader')
new Definition(AnnotationReader::class)
);
break;

default:
$container->setDefinition(
'vich_uploader.metadata.reader',
new Definition(AnnotationReader::class)
$container->getDefinition('vich_uploader.metadata.attribute_reader')
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class AnnotationDriver implements AdvancedDriverInterface
* @param \Doctrine\Persistence\ManagerRegistry[] $managerRegistryList
*/
public function __construct(
protected readonly AnnotationReader $reader,
protected readonly AnnotationReader|AttributeReader $reader,
private readonly array $managerRegistryList
) {
}
Expand Down
9 changes: 4 additions & 5 deletions src/Metadata/Driver/AttributeReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Vich\UploaderBundle\Metadata\Driver;

use Doctrine\Common\Annotations\Reader;
use ReflectionAttribute;
use ReflectionClass;
use ReflectionMethod;
Expand All @@ -12,15 +11,15 @@
/**
* @internal
*/
final class AttributeReader implements Reader
final class AttributeReader
{
/** @return AnnotationInterface[] */
public function getClassAnnotations(ReflectionClass $class): array
{
return $this->convertToAttributeInstances($class->getAttributes());
}

public function getClassAnnotation(ReflectionClass $class, $annotationName): ?AnnotationInterface
public function getClassAnnotation(ReflectionClass $class, string $annotationName): ?AnnotationInterface
{
return $this->getClassAnnotations($class)[$annotationName] ?? null;
}
Expand All @@ -31,7 +30,7 @@ public function getMethodAnnotations(ReflectionMethod $method): array
return $this->convertToAttributeInstances($method->getAttributes());
}

public function getMethodAnnotation(ReflectionMethod $method, $annotationName): ?AnnotationInterface
public function getMethodAnnotation(ReflectionMethod $method, string $annotationName): ?AnnotationInterface
{
return $this->getMethodAnnotations($method)[$annotationName] ?? null;
}
Expand All @@ -42,7 +41,7 @@ public function getPropertyAnnotations(ReflectionProperty $property): array
return $this->convertToAttributeInstances($property->getAttributes());
}

public function getPropertyAnnotation(ReflectionProperty $property, $annotationName): ?AnnotationInterface
public function getPropertyAnnotation(ReflectionProperty $property, string $annotationName): ?AnnotationInterface
{
return $this->getPropertyAnnotations($property)[$annotationName] ?? null;
}
Expand Down
4 changes: 2 additions & 2 deletions tests/Command/MappingDebugCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@

final class MappingDebugCommandTest extends AbstractCommandTestCase
{
public function testNotExistantMapping(): void
public function testNotExistentMapping(): void
{
$mappings = [];
$command = new MappingDebugCommand($mappings);
$this->expectException(MappingNotFoundException::class);
$this->executeCommand('vich:mapping:debug', $command, ['mapping' => 'foo']);
}

public function testExistantMapping(): void
public function testExistentMapping(): void
{
$mappings = ['image_mapping' => []];
$command = new MappingDebugCommand($mappings);
Expand Down

0 comments on commit 6ee998d

Please sign in to comment.