Skip to content

Commit

Permalink
add class metadata finalizer method
Browse files Browse the repository at this point in the history
  • Loading branch information
alekitto committed Oct 15, 2019
1 parent 10f09c0 commit 81ea7e2
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/ClassMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,11 @@ public function getName(): string
{
return $this->getReflectionClass()->name;
}

/**
* {@inheritdoc}
*/
public function finalize(): void
{
}
}
6 changes: 6 additions & 0 deletions lib/ClassMetadataInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,10 @@ public function getAttributesMetadata(): array;
* @param MetadataInterface $metadata
*/
public function addAttributeMetadata(MetadataInterface $metadata): void;

/**
* Called after all attributes metadata has been loaded and this metadata instance
* is merged with the parents.
*/
// public function finalize(): void;
}
4 changes: 4 additions & 0 deletions lib/Factory/AbstractMetadataFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ public function getMetadataFor($value): ClassMetadataInterface
}

$this->mergeSuperclasses($classMetadata);
if (\method_exists($classMetadata, 'finalize')) {
$classMetadata->finalize();
}

$this->validate($classMetadata);

$this->dispatchClassMetadataLoadedEvent($classMetadata);
Expand Down
20 changes: 20 additions & 0 deletions tests/Factory/MetadataFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ protected function createMetadata(\ReflectionClass $class): ClassMetadataInterfa

class FakeClassMetadata extends ClassMetadata
{
public $finalized = false;

public function finalize(): void
{
$this->finalized = true;
}
}

class FakeClassNoMetadata
Expand Down Expand Up @@ -242,6 +248,20 @@ public function set_metadata_class_should_create_specified_object()
self::assertInstanceOf(FakeClassMetadata::class, $factory->getMetadataFor($this));
}

/**
* @test
*/
public function get_metadata_for_should_call_metadata_finalizer_method()
{
$this->loader->loadClassMetadata(Argument::type(ClassMetadataInterface::class))->willReturn(true);

$factory = new MetadataFactory($this->loader->reveal());
$factory->setMetadataClass(FakeClassMetadata::class);
$metadata = $factory->getMetadataFor($this);

self::assertTrue($metadata->finalized);
}

/**
* @test
*/
Expand Down

0 comments on commit 81ea7e2

Please sign in to comment.