Skip to content

Commit

Permalink
Make AnnotationReader optional
Browse files Browse the repository at this point in the history
  • Loading branch information
MisatoTremor committed Feb 27, 2024
1 parent 6b01426 commit 1395f33
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
CHANGELOG
=========

1.1.x
-----

* Make AnnotationReader optional
* Update changelog

1.1.0
-----

Expand Down
4 changes: 1 addition & 3 deletions Tests/Util/FieldRetrieverAnnotationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Avro\CaseBundle\Util\CaseConverter;
use Avro\CsvBundle\Tests\AnnotationTestEntity;
use Avro\CsvBundle\Util\FieldRetriever;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use const PHP_VERSION_ID;

Expand All @@ -27,7 +26,6 @@ class FieldRetrieverAnnotationTest extends TestCase

public function setUp(): void
{
$annotationReader = new AnnotationReader();
$caseConverter = $this->createMock(CaseConverter::class);
$caseConverter
->method('convert')
Expand All @@ -47,7 +45,7 @@ public function setUp(): void
['custom', 'camel', 'custom'],
]
);
$this->fieldRetriever = new FieldRetriever($annotationReader, $caseConverter);
$this->fieldRetriever = new FieldRetriever($caseConverter);
$this->class = AnnotationTestEntity::class;
}

Expand Down
4 changes: 1 addition & 3 deletions Tests/Util/FieldRetrieverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Avro\CaseBundle\Util\CaseConverter;
use Avro\CsvBundle\Tests\TestEntity;
use Avro\CsvBundle\Util\FieldRetriever;
use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

Expand All @@ -31,7 +30,6 @@ class FieldRetrieverTest extends TestCase

public function setUp(): void
{
$annotationReader = new AnnotationReader();
$this->caseConverter = $this->createMock(CaseConverter::class);
$this->caseConverter
->method('convert')
Expand All @@ -51,7 +49,7 @@ public function setUp(): void
['custom', 'camel', 'custom'],
]
);
$this->fieldRetriever = new FieldRetriever($annotationReader, $this->caseConverter);
$this->fieldRetriever = new FieldRetriever($this->caseConverter);
$this->class = TestEntity::class;
}

Expand Down
25 changes: 14 additions & 11 deletions Util/FieldRetriever.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use Avro\CaseBundle\Util\CaseConverter;
use Avro\CsvBundle\Annotation\ImportExclude;
use Doctrine\Common\Annotations\Reader as AnnotationReader;
use Doctrine\Common\Annotations\AnnotationReader;

/**
* Retrieves the fields of a Doctrine entity/document that
Expand All @@ -23,21 +23,22 @@ class FieldRetriever
protected $caseConverter;

/**
* @param AnnotationReader $annotationReader The annotation reader service
* @param CaseConverter $caseConverter The caseConverter service
* @param CaseConverter $caseConverter The caseConverter service
*/
public function __construct(AnnotationReader $annotationReader, CaseConverter $caseConverter)
public function __construct(CaseConverter $caseConverter)
{
$this->annotationReader = $annotationReader;
if (class_exists(AnnotationReader::class)) {
$this->annotationReader = new AnnotationReader();
}
$this->caseConverter = $caseConverter;
}

/**
* Get the entity/documents field names.
*
* @param string $class The class name
* @param string $format The desired field case format
* @param bool $copyToKey Copy the field values to their respective key
* @param string $class The class name
* @param string $format The desired field case format
* @param bool $copyToKey Copy the field values to their respective key
*
* @return array $fields
*/
Expand All @@ -49,9 +50,11 @@ public function getFields($class, $format = 'title', $copyToKey = false)
$fields = [];
foreach ($properties as $property) {
$addField = true;
foreach ($this->annotationReader->getPropertyAnnotations($property) as $annotation) {
if ($annotation instanceof ImportExclude) {
$addField = false;
if ($this->annotationReader) {
foreach ($this->annotationReader->getPropertyAnnotations($property) as $annotation) {
if ($annotation instanceof ImportExclude) {
$addField = false;
}
}
}
if (PHP_VERSION_ID >= 80000) {
Expand Down
16 changes: 10 additions & 6 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@
"misatotremor/case-bundle": "^1.0.0"
},
"require-dev": {
"doctrine/doctrine-bundle": "^1.12|^2.0",
"doctrine/annotations": "^1.13 || ^2.0",
"doctrine/doctrine-bundle": "^1.12 || ^2.0",
"doctrine/orm": "^2.8",
"symfony/event-dispatcher": "^4.4|^5.0|^6.0",
"symfony/form": "^4.4|^5.0|^6.0",
"symfony/framework-bundle": "^4.4|^5.0|^6.0",
"symfony/routing": "^4.4|^5.0|^6.0",
"symfony/phpunit-bridge": "^4.4|^5.0|^6.0"
"symfony/event-dispatcher": "^4.4 || ^5.0 || ^6.0",
"symfony/form": "^4.4 || ^5.0 || ^6.0",
"symfony/framework-bundle": "^4.4 || ^5.0 || ^6.0",
"symfony/routing": "^4.4 || ^5.0 || ^6.0",
"symfony/phpunit-bridge": "^4.4 || ^5.0 || ^6.0"
},
"conflict": {
"doctrine/annotations": "<1.13 || >=3.0"
},
"autoload": {
"psr-4": { "Avro\\CsvBundle\\": "" }
Expand Down

0 comments on commit 1395f33

Please sign in to comment.