Skip to content
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
12 changes: 12 additions & 0 deletions .claude/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"enabledPlugins": {
"code-review@claude-plugins-official": true,
"github@claude-plugins-official": true,
"feature-dev@claude-plugins-official": true,
"code-simplifier@claude-plugins-official": true,
"ralph-loop@claude-plugins-official": true,
"pr-review-toolkit@claude-plugins-official": true,
"claude-md-management@claude-plugins-official": true,
"php-lsp@claude-plugins-official": true
}
}
96 changes: 96 additions & 0 deletions src/Command/MetadataDumpCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ChamberOrchestra package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ChamberOrchestra\MetadataBundle\Command;

use ChamberOrchestra\MetadataBundle\Mapping\MetadataReader;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Style\SymfonyStyle;

#[AsCommand(
name: 'chamber-orchestra:metadata:dump',
description: 'Dump extension metadata for a given entity class',
)]
class MetadataDumpCommand extends Command
{
public function __construct(
private readonly MetadataReader $metadataReader,
private readonly EntityManagerInterface $entityManager,
) {
parent::__construct();
}

protected function configure(): void
{
$this->addArgument('entity-class', InputArgument::REQUIRED, 'The fully-qualified entity class name');
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$io = new SymfonyStyle($input, $output);

/** @var string $entityClass */
$entityClass = $input->getArgument('entity-class');

if (!\class_exists($entityClass)) {
$io->error(\sprintf('Class "%s" does not exist.', $entityClass));

return Command::FAILURE;
}

$metadata = $this->metadataReader->getExtensionMetadata($this->entityManager, $entityClass);

$io->title(\sprintf('Extension Metadata for %s', $entityClass));

$configurations = $metadata->getConfigurations();

if ([] === $configurations) {
$io->info('No metadata configurations found for this entity.');

return Command::SUCCESS;
}

foreach ($configurations as $configClass => $configuration) {
$io->section($configClass);

$mappings = $configuration->getMappings();
if ([] === $mappings) {
$io->text('No field mappings.');
continue;
}

$rows = [];
foreach ($mappings as $fieldName => $mapping) {
$rows[] = [$fieldName, \json_encode($mapping, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT)];
}

$io->table(['Field', 'Mapping'], $rows);
}

$embeddedMetadata = $metadata->getEmbeddedMetadata();
if ([] !== $embeddedMetadata) {
$io->section('Embedded Metadata');
foreach ($embeddedMetadata as $fieldName => $embeddedMeta) {
$io->text(\sprintf(' %s -> %s', $fieldName, $embeddedMeta->getName()));
foreach ($embeddedMeta->getConfigurations() as $configClass => $configuration) {
$io->text(\sprintf(' Configuration: %s (%d fields)', $configClass, \count($configuration->getFieldNames())));
}
}
}

return Command::SUCCESS;
}
}
63 changes: 63 additions & 0 deletions src/DataCollector/MetadataDataCollector.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ChamberOrchestra package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ChamberOrchestra\MetadataBundle\DataCollector;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\DataCollector\DataCollector;

class MetadataDataCollector extends DataCollector
{
/**
* @var list<array{driver: string, entity: string, supported: bool, loaded: bool}>
*/
private array $invocations = [];

public function recordDriverInvocation(string $driverClass, string $entityClass, bool $supported, bool $loaded): void
{
$this->invocations[] = [
'driver' => $driverClass,
'entity' => $entityClass,
'supported' => $supported,
'loaded' => $loaded,
];
}

public function collect(Request $request, Response $response, ?\Throwable $exception = null): void
{
$this->data = [
'invocations' => $this->invocations,
];
}

/**
* @return list<array{driver: string, entity: string, supported: bool, loaded: bool}>
*/
public function getDriverStats(): array
{
/** @var list<array{driver: string, entity: string, supported: bool, loaded: bool}> $stats */
$stats = $this->data['invocations'] ?? [];

return $stats;
}

public function getName(): string
{
return 'chamber_orchestra_metadata';
}

public function reset(): void
{
$this->data = [];
$this->invocations = [];
}
}
5 changes: 5 additions & 0 deletions src/DependencyInjection/ChamberOrchestraMetadataExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace ChamberOrchestra\MetadataBundle\DependencyInjection;

use ChamberOrchestra\MetadataBundle\DataCollector\MetadataDataCollector;
use ChamberOrchestra\MetadataBundle\Mapping\Driver\MappingDriverInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand All @@ -26,5 +27,9 @@ public function load(array $configs, ContainerBuilder $container): void

$container->registerForAutoconfiguration(MappingDriverInterface::class)
->addTag('chamber_orchestra_metadata.mapping.driver');

if ($container->hasParameter('kernel.debug') && !$container->getParameter('kernel.debug')) {
$container->removeDefinition(MetadataDataCollector::class);
}
}
}
8 changes: 8 additions & 0 deletions src/EventSubscriber/AbstractDoctrineListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ protected function getScheduledEntityDeletions(EntityManagerInterface $em, strin
return $this->collectArgs($em->getUnitOfWork()->getScheduledEntityDeletions(), $em, $class);
}

/**
* @return array<string, array{mixed, mixed}|\Doctrine\ORM\PersistentCollection<array-key, mixed>>
*/
protected function getEntityChangeSet(EntityManagerInterface $em, MetadataArgs $args): array
{
return $em->getUnitOfWork()->getEntityChangeSet($args->entity);
}

/**
* @param array<object> $entities
*
Expand Down
16 changes: 16 additions & 0 deletions src/Exception/DuplicateMappingException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ChamberOrchestra package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ChamberOrchestra\MetadataBundle\Exception;

class DuplicateMappingException extends MappingException
{
}
16 changes: 16 additions & 0 deletions src/Exception/InvalidMappingAttributeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

/*
* This file is part of the ChamberOrchestra package.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace ChamberOrchestra\MetadataBundle\Exception;

class InvalidMappingAttributeException extends MappingException
{
}
8 changes: 4 additions & 4 deletions src/Exception/MappingException.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@

class MappingException extends \RuntimeException implements ExceptionInterface
{
public static function duplicateFieldMapping(string $entity, string $fieldName): self
public static function duplicateFieldMapping(string $entity, string $fieldName): DuplicateMappingException
{
return new self('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once');
return new DuplicateMappingException('Property "'.$fieldName.'" in "'.$entity.'" was already declared, but it must be declared only once');
}

public static function missingProperty(string $className, string $property, string $originProperty): self
Expand All @@ -28,9 +28,9 @@ public static function missingProperty(string $className, string $property, stri
));
}

public static function missingAttribute(string $className, string $field, string $attributeClass): self
public static function missingAttribute(string $className, string $field, string $attributeClass): InvalidMappingAttributeException
{
return new self(\sprintf(
return new InvalidMappingAttributeException(\sprintf(
'Class "%s" has no required attribute "%s" at field "%s".',
$className,
$attributeClass,
Expand Down
23 changes: 22 additions & 1 deletion src/Mapping/AbstractExtensionMetadata.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use ChamberOrchestra\MetadataBundle\Exception\LogicException;
use ChamberOrchestra\MetadataBundle\Mapping\ORM\MetadataConfigurationInterface;
use ChamberOrchestra\MetadataBundle\Mapping\ORM\WakeupAwareConfigurationInterface;
use Doctrine\ORM\Mapping\ClassMetadata as OrmClassMetadata;
use Doctrine\ORM\Mapping\ReflectionEmbeddedProperty;
use Doctrine\Persistence\Mapping\ClassMetadata;
Expand Down Expand Up @@ -54,14 +55,24 @@ public function getName(): string

public function addConfiguration(MetadataConfigurationInterface $configuration): void
{
$this->configurations[\get_class($configuration)] = $configuration;
$this->configurations[$configuration::class] = $configuration;
}

public function getConfiguration(string $class): ?MetadataConfigurationInterface
{
return $this->configurations[$class] ?? null;
}

public function hasConfiguration(string $class): bool
{
return isset($this->configurations[$class]);
}

public function getConfigurations(): array
{
return $this->configurations;
}

/**
* @param ClassMetadata<object> $metadata
*/
Expand All @@ -86,6 +97,10 @@ public function wakeup(ClassMetadata $metadata, ReflectionService $reflectionSer
}
}
if ($metadata->fieldMappings) {
$shadowed = \array_intersect_key($mappings, $metadata->fieldMappings);
foreach ($shadowed as $field => $mapping) {
@\trigger_error(\sprintf('Extension metadata field "%s" in class "%s" is shadowed by a Doctrine field mapping and will be skipped.', $field, $this->name), \E_USER_NOTICE);
}
$mappings = \array_diff_key($mappings, $metadata->fieldMappings);
}

Expand Down Expand Up @@ -121,6 +136,12 @@ public function wakeup(ClassMetadata $metadata, ReflectionService $reflectionSer
$this->reflectionFields[$field] = $reflectionService->getAccessibleProperty($this->name, $field)
?? throw new LogicException(\sprintf('Unable to resolve reflection property "%s" on class "%s".', $field, $this->name));
}

foreach ($this->configurations as $configuration) {
if ($configuration instanceof WakeupAwareConfigurationInterface) {
$configuration->wakeup();
}
}
}

public function getOriginMetadata(): ClassMetadata
Expand Down
Loading