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

v3: Migrate from supporting annotations to attributes #20

Merged
merged 2 commits into from
Dec 10, 2024
Merged
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## v3.0.0 (2024-12-09)

* [BREAKING] Switch to using attributes instead of annotations for mapping
- Removes the `ExplicitCallslistAnnotationDriver` class
- Removes the `doctrine.config.metadata.reader` service definition
- Adds `ExplicitClasslistAttributeDriver` as the default mapping driver
- Drops composer dependency on doctrine/annotations
* Narrow supported dependency versions to the current latest minor of the suppoerted
major version.
* Drop support for PHP < 8.2
Expand Down
1 change: 0 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"php": "~8.2.0 || ~8.3.0",
"ext-pdo": "*",
"composer/installers": "^1.12 || ^2",
"doctrine/annotations": "^1.14",
"doctrine/cache": "^1.13",
"doctrine/common": "^3.4",
"doctrine/dbal": "^3.9",
Expand Down
34 changes: 2 additions & 32 deletions src/Dependency/DoctrineFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,13 @@
namespace Ingenerator\KohanaDoctrine\Dependency;


use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
use Doctrine\Common\EventManager;
use Doctrine\DBAL\Types\Type;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\Persistence\Mapping\Driver\MappingDriver;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAnnotationDriver;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver;

class DoctrineFactory
{
Expand Down Expand Up @@ -53,21 +49,13 @@ public static function definitions()
'metadata' => [
'driver' => [
'_settings' => [
'class' => ExplicitClasslistAnnotationDriver::class,
'class' => ExplicitClasslistAttributeDriver::class,
'arguments' => [
'%doctrine.config.metadata.reader%',
'@doctrine.entity_classes@',
],
'shared' => TRUE,
],
],
'reader' => [
'_settings' => [
'class' => static::class,
'constructor' => 'buildMetadataReader',
'shared' => TRUE,
],
],
],
'orm_config' => [
'_settings' => [
Expand Down Expand Up @@ -196,24 +184,6 @@ public static function buildEntityManager(
);
}

/**
* @return CachedReader
*/
public static function buildMetadataReader()
{
// Register the Doctrine annotations - this replaces the older method of loading a specific file, instead
// just allows the autoloader to do its work. Future doctrine releases are expected to drop this outright
// in favour of just always using autoloading
AnnotationRegistry::registerLoader('class_exists');

$reader = new SimpleAnnotationReader();
$reader->addNamespace('Doctrine\ORM\Mapping');

// The cache used here is largely irrelevant, the compiled metadata is cached by the metadata cache and if
// so the reader is never used at all.
return new CachedReader($reader, new ArrayCache);
}

/**
* Creates and configures the ORM config
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,20 @@
namespace Ingenerator\KohanaDoctrine;


use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\MappingException;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

class ExplicitClasslistAnnotationDriver extends AnnotationDriver
class ExplicitClasslistAttributeDriver extends AttributeDriver
{
/**
* ExplicitClasslistAnnotationDriver constructor.
*
* @param Reader $reader
* @param array|NULL $entity_classes The list of entity class names
* [NB] it is not expected to be valid for this to be empty at runtime, but
* allowing a null value allows us to create an instance in development / test
* environments without full config.
* @param list<class-string>|null $entity_classes The list of entity class names. It is not expected to be nullable
* at runtime, but can take null to support testing when config is
* not fully defined.
*/
public function __construct(Reader $reader, array $entity_classes = NULL)
public function __construct(?array $entity_classes = NULL)
{
parent::__construct($reader, []);
$this->classNames = $entity_classes ?: [];
parent::__construct([], reportFieldsWhereDeclared: TRUE);
$this->classNames = $entity_classes ?? [];
}

public function getAllClassNames()
Expand Down
19 changes: 8 additions & 11 deletions test/integration/Dependency/DoctrineFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
namespace test\integration\Ingenerator\KohanaDoctrine\Dependency;


use Doctrine\Common\Annotations\CachedReader;
use Doctrine\Common\Cache\ApcuCache;
use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\Cache;
Expand All @@ -16,9 +15,12 @@
use Doctrine\ORM\Configuration;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Events;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Ingenerator\KohanaDoctrine\Dependency\ConnectionConfigProvider;
use Ingenerator\KohanaDoctrine\Dependency\DoctrineFactory;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAnnotationDriver;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver;
use PHPUnit\Framework\TestCase;

class DoctrineFactoryTest extends TestCase
Expand Down Expand Up @@ -50,8 +52,7 @@ public function provider_expected_services()
['doctrine.cache.data_cache', Cache::class, TRUE],
['doctrine.cache.compiler_cache', Cache::class, TRUE],
['doctrine.config.connection_config', ConnectionConfigProvider::class, FALSE],
['doctrine.config.metadata.driver', ExplicitClasslistAnnotationDriver::class, TRUE],
['doctrine.config.metadata.reader', CachedReader::class, TRUE],
['doctrine.config.metadata.driver', ExplicitClasslistAttributeDriver::class, TRUE],
['doctrine.config.orm_config', Configuration::class, TRUE],
['doctrine.entity_manager', EntityManager::class, TRUE],
['doctrine.event_manager', EventManager::class, TRUE],
Expand Down Expand Up @@ -339,16 +340,12 @@ protected function newContainer(array $definitions)

}

/**
* @Entity
*/
#[Entity]
class SomeEntity
{

/**
* @Id
* @Column(nullable=true)
*/
#[Id]
#[Column(nullable: true)]
protected $whatever;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,20 @@


use BadMethodCallException;
use Doctrine\Common\Annotations\Reader;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
use Doctrine\ORM\Mapping\Driver\AttributeDriver;
use Doctrine\Persistence\Mapping\MappingException;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAnnotationDriver;
use Ingenerator\KohanaDoctrine\ExplicitClasslistAttributeDriver;
use PHPUnit\Framework\TestCase;

class ExplicitClasslistAnnotationDriverTest extends TestCase
class ExplicitClasslistAttributeDriverTest extends TestCase
{
/**
* @var array
*/
protected $classes = [];

/**
* @var
*/
protected $reader;
protected array $classes = [];

public function test_is_initialisable_annotation_driver()
{
$subject = $this->newSubject();
$this->assertInstanceOf(ExplicitClasslistAnnotationDriver::class, $subject);
$this->assertInstanceOf(AnnotationDriver::class, $subject);
$this->assertInstanceOf(ExplicitClasslistAttributeDriver::class, $subject);
$this->assertInstanceOf(AttributeDriver::class, $subject);
}

public function test_it_returns_injected_list_of_class_names()
Expand Down Expand Up @@ -60,15 +51,9 @@ public function test_it_throws_from_get_paths()
$this->newSubject()->getPaths();
}

protected function setUp(): void
{
parent::setUp();
$this->reader = $this->getMockBuilder(Reader::class)->getMock();
}

protected function newSubject()
{
return new ExplicitClasslistAnnotationDriver($this->reader, $this->classes);
return new ExplicitClasslistAttributeDriver($this->classes);
}

}
Expand Down
Loading