diff --git a/composer.json b/composer.json index a5f06bd..b182e4d 100644 --- a/composer.json +++ b/composer.json @@ -33,7 +33,9 @@ "require": { "symfony/dependency-injection": "^7.1", "symfony/framework-bundle": "^7.1", - "symfony/yaml": "^7.1" + "symfony/yaml": "^7.1", + "doctrine/doctrine-bundle": "^2.18", + "doctrine/orm": "^3.5" }, "require-dev": { diff --git a/config/definition.php b/config/definition.php deleted file mode 100644 index 8c6eb92..0000000 --- a/config/definition.php +++ /dev/null @@ -1,18 +0,0 @@ -rootNode() - ->children() - ->scalarNode('name')->defaultValue('Mr. NoName')->cannotBeEmpty()->end() - ->end() - ; -}; - diff --git a/config/services.yaml b/config/services.yaml index 1dd317f..2dda648 100644 --- a/config/services.yaml +++ b/config/services.yaml @@ -2,27 +2,3 @@ services: _defaults: autowire: true autoconfigure: true - - # Hide the fully qualified Greeting class name from public view - # but give the class an alias. This alias will be made public instead. - # - # Read the documentation to see why we do this: - # https://symfony.com/doc/current/service_container/autowiring.html#service-autowiring-alias - # - Pcm\MetadataBundle\Greeting: - public: false - alias: pcm_example.greeting - - - # Mark the alias we created as public. - pcm_example.greeting: - public: true - class: Pcm\MetadataBundle\Greeting - - - # If we were defining a twig extension, we'd want to add the twig.runtime tag - # so it loads correctly. - # - # Pcm\MetadataBundle\SomeTwigRuntime - # tags: - # - { name: twig.runtime } diff --git a/src/Exception/MissingKeyException.php b/src/Exception/MissingKeyException.php new file mode 100644 index 0000000..25529c2 --- /dev/null +++ b/src/Exception/MissingKeyException.php @@ -0,0 +1,10 @@ +name); - } -} - diff --git a/src/Interface/MetadataInterface.php b/src/Interface/MetadataInterface.php new file mode 100644 index 0000000..45cb73a --- /dev/null +++ b/src/Interface/MetadataInterface.php @@ -0,0 +1,35 @@ + &$metadata + */ + public function __construct(private array &$metadata) + { + } + + public function get(string $key): mixed + { + if (!$this->has($key)) { + throw new MissingKeyException($key); + } + + return $this->metadata[$key]; + } + + public function set(string $key, mixed $value): static + { + $this->metadata[$key] = $value; + + return $this; + } + + public function has(string $key): bool + { + return array_key_exists($key, $this->metadata); + } +} + diff --git a/src/PcmMetadataBundle.php b/src/PcmMetadataBundle.php index 99ffdf6..b03d868 100644 --- a/src/PcmMetadataBundle.php +++ b/src/PcmMetadataBundle.php @@ -4,9 +4,6 @@ declare(strict_types=1); namespace Pcm\MetadataBundle; -use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator; -use Symfony\Component\DependencyInjection\ContainerBuilder; -use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator; use Symfony\Component\HttpKernel\Bundle\AbstractBundle; /** @@ -14,37 +11,5 @@ use Symfony\Component\HttpKernel\Bundle\AbstractBundle; */ final class PcmMetadataBundle extends AbstractBundle { - /** - * @param array $config - */ - public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void - { - /** - * Load the services defined in services.yaml into the container. - */ - $container->import('../config/services.yaml'); - - /** - * The "$config" variable contains an array representing the - * configuration and it's values. - * - * We can use it to configure the service container, for example - * by passing in arguments to any services we have defined. - * - * (see services.yaml) - */ - $container->services() - ->get('pcm_example.greeting') - ->arg('$name', $config['name']) - ; - } - - public function configure(DefinitionConfigurator $definition): void - { - /** - * Import the config definition (see definition.php). - */ - $definition->import('../config/definition.php'); - } } diff --git a/src/Trait/MetadataTrait.php b/src/Trait/MetadataTrait.php new file mode 100644 index 0000000..6b609bf --- /dev/null +++ b/src/Trait/MetadataTrait.php @@ -0,0 +1,27 @@ +|array|null $rawMetadata + */ + #[ORM\Column(type: Types::JSON, nullable: true, name: 'metadata')] + private ?array $rawMetadata = []; + + public Metadata $metadata { + get { + $this->rawMetadata ??= []; + + return new Metadata($this->rawMetadata); + } + } +} +