Try to get stuff working

This commit is contained in:
brabli
2025-11-27 14:37:29 +00:00
parent d48fb65010
commit 91e21013ec
9 changed files with 118 additions and 96 deletions

View File

@@ -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": {

View File

@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
/**
* This file defines the layout of the configuration, type constraints
* and any default argument values.
*/
return static function (DefinitionConfigurator $definition): void {
$definition->rootNode()
->children()
->scalarNode('name')->defaultValue('Mr. NoName')->cannotBeEmpty()->end()
->end()
;
};

View File

@@ -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 }

View File

@@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Pcm\MetadataBundle\Exception;
final class MissingKeyException extends \RuntimeException
{
}

View File

@@ -1,18 +0,0 @@
<?php
declare(strict_types=1);
namespace Pcm\MetadataBundle;
final class Greeting
{
public function __construct(private string $name)
{
}
public function greetPerson(): string
{
return sprintf("Hello there %s! Hope you're well.", $this->name);
}
}

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace Pcm\MetadataBundle\Interface;
use Pcm\MetadataBundle\Exception\MissingKeyException;
interface MetadataInterface
{
/**
* Retrieve a value by it's key.
*
* @return scalar|scalar[]
*
* @throws MissingKeyException when the key is not set
*/
public function get(string $key): mixed;
/**
* Sets a value against the given key. This overwrites
* any existing value.
*
* @param scalar|scalar[] $value
*/
public function set(string $key, mixed $value): static;
/**
* Whether a key has been set on the metadata.
*/
public function has(string $key): bool;
}

43
src/Model/Metadata.php Normal file
View File

@@ -0,0 +1,43 @@
<?php
declare(strict_types=1);
namespace Pcm\MetadataBundle\Model;
use App\Exception\Dto\Metadata\MissingKeyException;
use App\Interface\Dto\MetadataInterface;
/**
* Store scalar metadata on an entity.
*/
final class Metadata implements MetadataInterface
{
/**
* @param array<string, scalar|scalar[]> &$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);
}
}

View File

@@ -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<string, mixed> $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');
}
}

View File

@@ -0,0 +1,27 @@
<?php
declare(strict_types=1);
namespace Pcm\MetadataBundle\Trait;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping as ORM;
use Pcm\MetadataBundle\Model\Metadata;
trait MetadataTrait
{
/**
* @var array<string, scalar>|array<string, scalar[]>|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);
}
}
}