Skip to content

Commit

Permalink
Initial project (#1)
Browse files Browse the repository at this point in the history
Initial project
  • Loading branch information
GwendolenLynch authored Oct 6, 2019
2 parents 274c19c + 871defa commit 8257f44
Show file tree
Hide file tree
Showing 21 changed files with 811 additions and 0 deletions.
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/.env.local
/.env.local.php
/.env.*.local
/var/
/vendor/
.phpunit
.phpunit.result.cache
phpunit.xml
.php_cs
.php_cs.cache
composer.lock
symfony.lock
27 changes: 27 additions & 0 deletions .php_cs.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

return Camelot\CsFixer\Config::create()
->addRules(
Camelot\CsFixer\Rules::create()
->risky()
->php71()
)
->addRules([
'@PhpCsFixer:risky' => true,
'@PHP73Migration' => true,
'@PHPUnit60Migration:risky' => true,
'@PHPUnit75Migration:risky' => true,
'declare_strict_types' => true,
'native_function_invocation' => [
'include' => ['@compiler_optimized'],
],
'no_superfluous_phpdoc_tags' => true,
'ordered_class_elements' => true,
'php_unit_strict' => false,
'comment_to_phpdoc' => false,
])
->in('src')
->in('tests')
;
35 changes: 35 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
language: php

matrix:
include:
- php: 7.4snapshot
- php: 7.4snapshot
env: COMPOSER_FLAGS="--prefer-lowest"
- php: 7.4snapshot
env: PHPUNIT_FLAGS="--coverage-text --coverage-html coverage/"
- php: nightly
fast_finish: true
allow_failures:
- php: nightly

before_script:
- |
if [[ "$COVERAGE" = true ]] ; then
echo > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/xdebug.ini
git clone --single-branch --branch=v1.0.6 --depth=1 https://github.com/krakjoe/pcov
cd pcov
phpize
./configure
make clean install
echo "extension=pcov.so" > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/pcov.ini
cd $TRAVIS_BUILD_DIR
fi
- travis_retry composer self-update
- travis_retry composer update --no-interaction --prefer-dist $COMPOSER_FLAGS

script:
- vendor/bin/phpunit $PHPUNIT_FLAGS

cache:
directories:
- $COMPOSER_CACHE_DIR
117 changes: 117 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
Camelot Doctrine Inheritance Mapping
====================================

Installation
------------

Open a command console, enter your project directory and execute:

```console
$ composer require camelot/doctrine-inheritance-mapping
```

This command requires you to have Composer installed globally, as explained
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
of the Composer documentation.

### Standalone Configuration

```php
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorMapLoader;
use Doctrine\Common\Annotations\AnnotationReader;
use Doctrine\Common\Annotations\DocParser;
use Doctrine\ORM\Configuration;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;

// Annotation reader & driver
$reader = new AnnotationReader(new DocParser());
$driver = new AnnotationDriver($reader);
$driver->addPaths(['/path/to/entities']);

// Doctrine configuration
$config = new Configuration();
$config->setMetadataDriverImpl($driver);

$classMetadata = new ClassMetadata(YourEntityName::class);

$loader = new DiscriminatorMapLoader($reader, $config);
$loader->loadClassMetadata($classMetadata);
```

### Framework Configuration

#### Symfony Bundle

If using the Symfony Framework, you can enable the bundle by adding it to the
list of registered bundles in the `config/bundles.php` file of your project:

```php
// config/bundles.php

return [
// ...
Camelot\DoctrineInheritanceMapping\Bridge\Symfony\DoctrineInheritanceMappingBundle::class => ['all' => true],
];
```

Usage
-----

### Single Table Inheritance

#### `@DiscriminatorMapItem` Annotation

Doctrine's [Single Table Inheritance][single-table-inheritance] is an
inheritance mapping strategy where all classes of a hierarchy are mapped to a
single database table.

The mapping is handled by a "discriminator" column, defined in the mapping
definition of the parent class. This column value defines the entity class to
use, based on the inheritance hierarchy. This binds the parent to the children
and mixes responsibilities in the process.

To separate these concerns, this library provides the `@DiscriminatorMapItem`
annotation for use in *each* entity in a hierarchy, replacing the parent class
use of Doctrine's `@DiscriminatorMap`, thus eliminating the need to update the
parent for each subclass.

##### Example

###### Parent Class

```php
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorEntry;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @ORM\InheritanceType("SINGLE_TABLE")
* @DiscriminatorMapItem(value="SingleTable")
*/
class SingleTable
{
// ...
}

```

**NOTE:** Using `@DiscriminatorColumn` along with `@DiscriminatorMapItem` is
optional, and has been omitted above for clarity.

###### Child(ren) Class

```php
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorEntry;
use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity()
* @DiscriminatorMapItem(value="SingleTableChild")
*/
class SingleTableChild extends SingleTable
{
// ...
}
```
[single-table-inheritance]: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#single-table-inheritance
40 changes: 40 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "camelot/doctrine-inheritance-mapping",
"description": "Doctrine inheritance mapping library",
"type": "library",
"license": "MIT",
"keywords": ["doctrine", "orm", "inheritance-mapping", "symfony", "symfony-bundle"],
"require": {
"php": "^7.1.3",
"doctrine/annotations": "^1.7",
"doctrine/orm": "^2.5.11"
},
"require-dev": {
"camelot/coding-style": "^2.0",
"doctrine/doctrine-bundle": "^1.6.10",
"friendsofphp/php-cs-fixer": "^2.15",
"phpunit/phpunit": "^8.4",
"ramsey/uuid-doctrine": "^1.5",
"symfony/config": "^4.3",
"symfony/debug-bundle": "^4.3",
"symfony/framework-bundle": "^4.3",
"symfony/http-kernel": "^4.3",
"symfony/phpunit-bridge": "^4.3.4",
"symfony/var-dumper": "^4.3",
"symfony/yaml": "^4.3"
},
"autoload": {
"psr-4": {
"Camelot\\DoctrineInheritanceMapping\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Camelot\\DoctrineInheritanceMapping\\Tests\\": "tests/"
}
},
"scripts": {
"lint": "vendor/bin/php-cs-fixer fix --show-progress=dots",
"test": "vendor/bin/phpunit --coverage-text"
}
}
26 changes: 26 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>

<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.4/phpunit.xsd"
backupGlobals="false"
colors="true"
bootstrap="vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<server name="SHELL_VERBOSITY" value="-1" />
</php>

<testsuites>
<testsuite name="Project Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>src</directory>
</whitelist>
</filter>
</phpunit>
34 changes: 34 additions & 0 deletions src/Annotation/DiscriminatorMapItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Camelot\DoctrineInheritanceMapping\Annotation;

use BadMethodCallException;

/**
* @Annotation
*/
final class DiscriminatorMapItem
{
/** @var string|null */
private $value;

public function __construct(array $data)
{
static::assertValid($data);
$this->value = $data['value'];
}

public function getValue(): ?string
{
return $this->value;
}

private static function assertValid(array $data): void
{
if (!($data['value'] ?? false)) {
throw new BadMethodCallException(sprintf('Value for annotation DiscriminatorMapItem is missing or empty.'));
}
}
}
Loading

0 comments on commit 8257f44

Please sign in to comment.