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

Auto-generated types for string/int/uuid backed objects #4

Merged
merged 6 commits into from
Nov 28, 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
9 changes: 3 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ jobs:
strategy:
matrix:
operating-system: [ 'ubuntu-22.04' ]
php: [ '8.1', '8.2', '8.3' ]
php: [ '8.2', '8.3' ]
symfony: ['6.4.*', '7.0.*']
exclude:
- php: '8.1'
symfony: '7.0.*'

steps:
- uses: actions/checkout@v4
Expand Down Expand Up @@ -53,15 +50,15 @@ jobs:

ecs:
name: Easy Coding Standard
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: ramsey/composer-install@v3
- run: vendor/bin/ecs

phpstan:
name: PHPStan
runs-on: ubuntu-latest
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v4
- uses: ramsey/composer-install@v3
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
composer.lock
src/_generated
var/
vendor/
66 changes: 66 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,74 @@ return [

## Features

- [Dynamically Create Types For Standard Objects](#dynamically-create-types-for-standard-objects)
- [Auto-Register Custom Doctrine Type Mappings](#auto-register-custom-doctrine-type-mappings)
- [Auto-Register Carbon Doctrine Type Mappings](#auto-register-carbon-datetime-type-mappings)

### Dynamically Create Types For Standard Objects

Applying the `#[DoctrineType]` attribute to a class will generate a Doctrine mapping type for the object and register
it in the application. To configure this behaviour, you must specify where to look for the classes to auto-register.

```yaml
headsnet_doctrine_tools:
root_namespace: Headsnet\DoctrineToolsBundle
custom_types:
scan_dirs:
- 'src/' # the default value
```

Currently the bundle supports the following standard types:

- `string` via `AbstractStringMappingType`
- `integer` via `AbstractIntegerMappingType`
- `uuid` via `AbstractUuidMappingType`

For example, we have a string backed value object:

```php
namespace App\Domain\Model;

use Headsnet\DoctrineToolsBundle\Attribute\DoctrineType;

#[DoctrineType(name: 'person_name', type: 'string')]
class PersonName
{
public function __construct(
private readonly string $value
) {
}

public static function create(string $value): self
{
return new self($value);
}

public function asString(): string
{
return $this->value;
}
}
```

This will cause a PHP file to be generated that provides the Doctrine Mapping Type for the class - something like:

```php
class PersonNameType extends \Headsnet\DoctrineToolsBundle\Types\StandardTypes\AbstractStringMappingType {
public function getName(): string
{
return 'person_name';
}

public function getClass(): string
{
return 'App\Domain\Model\PersonName';
}
}
```

This file is created in `src/_generated/` - you will want to add this directory to your `.gitignore` file.

### Auto-Register Custom Doctrine Type Mappings

The bundle can auto-register custom Doctrine DBAL types, eliminating the need to specify them all in
Expand All @@ -48,6 +113,7 @@ Then add the `#[DoctrineTypeMapping]` attribute to the custom type class:

```php
use Doctrine\DBAL\Types\Type;
use Headsnet\DoctrineToolsBundle\Attribute\DoctrineMappingType;

#[DoctrineTypeMapping]
final class ReservationIdType extends Type
Expand Down
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@
"issues": "https://github.com/headsnet/doctrine-tools-bundle/issues"
},
"require": {
"php": ">=8.1",
"php": ">=8.2",
"doctrine/dbal": "^3.0 || ^4.0",
"league/construct-finder": "^1.3",
"symfony/config": "^6.0 || ^7.0",
"symfony/dependency-injection": "^6.0 || ^7.0",
"symfony/http-kernel": "^6.0 || ^7.0",
"doctrine/doctrine-bundle": "^2.0"
"doctrine/doctrine-bundle": "^2.0",
"symfony/uid": "^7.0"
},
"require-dev": {
"phpunit/phpunit": "^10.0",
Expand Down
15 changes: 15 additions & 0 deletions config/services.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php
declare(strict_types=1);

namespace Symfony\Component\DependencyInjection\Loader\Configurator;

return function (ContainerConfigurator $configurator): void
{
$services = $configurator->services()
->defaults()
->autowire()
->autoconfigure()
;

$services->load('Headsnet\\DoctrineToolsBundle\\', '../src/*');
};
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
bootstrap="tests/bootstrap.php"
cacheDirectory="var/cache/.phpunit.cache"
executionOrder="depends,defects"
requireCoverageMetadata="true"
beStrictAboutCoverageMetadata="true"
beStrictAboutCoverageMetadata="false"
beStrictAboutOutputDuringTests="true"
displayDetailsOnTestsThatTriggerWarnings="true"
failOnRisky="true"
Expand Down
16 changes: 16 additions & 0 deletions src/Attribute/DoctrineType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\Attribute;

use Attribute;

#[Attribute(Attribute::TARGET_CLASS)]
final class DoctrineType
{
public function __construct(
public readonly string $name,
public readonly string $type
) {
}
}
20 changes: 20 additions & 0 deletions src/HeadsnetDoctrineToolsBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Headsnet\DoctrineToolsBundle\Mapping\CarbonTypeMappingsCompilerPass;
use Headsnet\DoctrineToolsBundle\Mapping\DoctrineTypeMappingsCompilerPass;
use Headsnet\DoctrineToolsBundle\Types\DoctrineTypesCompilerPass;
use Symfony\Component\Config\Definition\Configurator\DefinitionConfigurator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
Expand All @@ -15,6 +16,15 @@ public function configure(DefinitionConfigurator $definition): void
{
$definition->rootNode()
->children()
->scalarNode('root_namespace')->cannotBeEmpty()->end()
->arrayNode('custom_types')
->canBeDisabled()
->children()
->arrayNode('scan_dirs')
->defaultValue(['src/'])->scalarPrototype()->end()
->end()
->end()
->end() // End custom_types
->arrayNode('custom_mappings')
->children()
->arrayNode('scan_dirs')
Expand All @@ -33,13 +43,19 @@ public function configure(DefinitionConfigurator $definition): void

/**
* @param array{
* root_namespace: string,
* custom_types: array{scan_dirs: array<string>},
* custom_mappings: array{scan_dirs: array<string>},
* carbon_mappings: array{enabled: boolean, replace: boolean}
* } $config
*/
public function loadExtension(array $config, ContainerConfigurator $container, ContainerBuilder $builder): void
{
$container->import('../config/services.php');

$container->parameters()
->set('headsnet_doctrine_tools.root_namespace', $config['root_namespace'])
->set('headsnet_doctrine_tools.custom_types.scan_dirs', $config['custom_types']['scan_dirs'])
->set('headsnet_doctrine_tools.custom_mappings.scan_dirs', $config['custom_mappings']['scan_dirs'])
->set('headsnet_doctrine_tools.carbon_mappings.enabled', $config['carbon_mappings']['enabled'])
->set('headsnet_doctrine_tools.carbon_mappings.replace', $config['carbon_mappings']['replace'])
Expand All @@ -50,6 +66,10 @@ public function build(ContainerBuilder $container): void
{
parent::build($container);

$container->addCompilerPass(
new DoctrineTypesCompilerPass()
);

$container->addCompilerPass(
new DoctrineTypeMappingsCompilerPass()
);
Expand Down
30 changes: 30 additions & 0 deletions src/Types/CandidateType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);

namespace Headsnet\DoctrineToolsBundle\Types;

final class CandidateType
{
private string $baseTypeClass = '';

/**
* @param class-string $objectClass
*/
public function __construct(
public readonly string $typeName,
public readonly string $typeClass,
public readonly string $baseType,
public readonly string $objectClass,
) {
}

public function setBaseTypeClass(string $baseTypeClass): void
{
$this->baseTypeClass = $baseTypeClass;
}

public function getBaseTypeClass(): string
{
return $this->baseTypeClass;
}
}
Loading