Skip to content

Commit 8257f44

Browse files
Initial project (#1)
Initial project
2 parents 274c19c + 871defa commit 8257f44

21 files changed

+811
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/.env.local
2+
/.env.local.php
3+
/.env.*.local
4+
/var/
5+
/vendor/
6+
.phpunit
7+
.phpunit.result.cache
8+
phpunit.xml
9+
.php_cs
10+
.php_cs.cache
11+
composer.lock
12+
symfony.lock

.php_cs.dist

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
return Camelot\CsFixer\Config::create()
6+
->addRules(
7+
Camelot\CsFixer\Rules::create()
8+
->risky()
9+
->php71()
10+
)
11+
->addRules([
12+
'@PhpCsFixer:risky' => true,
13+
'@PHP73Migration' => true,
14+
'@PHPUnit60Migration:risky' => true,
15+
'@PHPUnit75Migration:risky' => true,
16+
'declare_strict_types' => true,
17+
'native_function_invocation' => [
18+
'include' => ['@compiler_optimized'],
19+
],
20+
'no_superfluous_phpdoc_tags' => true,
21+
'ordered_class_elements' => true,
22+
'php_unit_strict' => false,
23+
'comment_to_phpdoc' => false,
24+
])
25+
->in('src')
26+
->in('tests')
27+
;

.travis.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
language: php
2+
3+
matrix:
4+
include:
5+
- php: 7.4snapshot
6+
- php: 7.4snapshot
7+
env: COMPOSER_FLAGS="--prefer-lowest"
8+
- php: 7.4snapshot
9+
env: PHPUNIT_FLAGS="--coverage-text --coverage-html coverage/"
10+
- php: nightly
11+
fast_finish: true
12+
allow_failures:
13+
- php: nightly
14+
15+
before_script:
16+
- |
17+
if [[ "$COVERAGE" = true ]] ; then
18+
echo > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/xdebug.ini
19+
git clone --single-branch --branch=v1.0.6 --depth=1 https://github.com/krakjoe/pcov
20+
cd pcov
21+
phpize
22+
./configure
23+
make clean install
24+
echo "extension=pcov.so" > $HOME/.phpenv/versions/$TRAVIS_PHP_VERSION/etc/conf.d/pcov.ini
25+
cd $TRAVIS_BUILD_DIR
26+
fi
27+
- travis_retry composer self-update
28+
- travis_retry composer update --no-interaction --prefer-dist $COMPOSER_FLAGS
29+
30+
script:
31+
- vendor/bin/phpunit $PHPUNIT_FLAGS
32+
33+
cache:
34+
directories:
35+
- $COMPOSER_CACHE_DIR

README.md

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Camelot Doctrine Inheritance Mapping
2+
====================================
3+
4+
Installation
5+
------------
6+
7+
Open a command console, enter your project directory and execute:
8+
9+
```console
10+
$ composer require camelot/doctrine-inheritance-mapping
11+
```
12+
13+
This command requires you to have Composer installed globally, as explained
14+
in the [installation chapter](https://getcomposer.org/doc/00-intro.md)
15+
of the Composer documentation.
16+
17+
### Standalone Configuration
18+
19+
```php
20+
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorMapLoader;
21+
use Doctrine\Common\Annotations\AnnotationReader;
22+
use Doctrine\Common\Annotations\DocParser;
23+
use Doctrine\ORM\Configuration;
24+
use Doctrine\ORM\Mapping\ClassMetadata;
25+
use Doctrine\ORM\Mapping\Driver\AnnotationDriver;
26+
27+
// Annotation reader & driver
28+
$reader = new AnnotationReader(new DocParser());
29+
$driver = new AnnotationDriver($reader);
30+
$driver->addPaths(['/path/to/entities']);
31+
32+
// Doctrine configuration
33+
$config = new Configuration();
34+
$config->setMetadataDriverImpl($driver);
35+
36+
$classMetadata = new ClassMetadata(YourEntityName::class);
37+
38+
$loader = new DiscriminatorMapLoader($reader, $config);
39+
$loader->loadClassMetadata($classMetadata);
40+
```
41+
42+
### Framework Configuration
43+
44+
#### Symfony Bundle
45+
46+
If using the Symfony Framework, you can enable the bundle by adding it to the
47+
list of registered bundles in the `config/bundles.php` file of your project:
48+
49+
```php
50+
// config/bundles.php
51+
52+
return [
53+
// ...
54+
Camelot\DoctrineInheritanceMapping\Bridge\Symfony\DoctrineInheritanceMappingBundle::class => ['all' => true],
55+
];
56+
```
57+
58+
Usage
59+
-----
60+
61+
### Single Table Inheritance
62+
63+
#### `@DiscriminatorMapItem` Annotation
64+
65+
Doctrine's [Single Table Inheritance][single-table-inheritance] is an
66+
inheritance mapping strategy where all classes of a hierarchy are mapped to a
67+
single database table.
68+
69+
The mapping is handled by a "discriminator" column, defined in the mapping
70+
definition of the parent class. This column value defines the entity class to
71+
use, based on the inheritance hierarchy. This binds the parent to the children
72+
and mixes responsibilities in the process.
73+
74+
To separate these concerns, this library provides the `@DiscriminatorMapItem`
75+
annotation for use in *each* entity in a hierarchy, replacing the parent class
76+
use of Doctrine's `@DiscriminatorMap`, thus eliminating the need to update the
77+
parent for each subclass.
78+
79+
##### Example
80+
81+
###### Parent Class
82+
83+
```php
84+
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorEntry;
85+
use Doctrine\ORM\Mapping as ORM;
86+
87+
/**
88+
* @ORM\Entity()
89+
* @ORM\InheritanceType("SINGLE_TABLE")
90+
* @DiscriminatorMapItem(value="SingleTable")
91+
*/
92+
class SingleTable
93+
{
94+
// ...
95+
}
96+
97+
```
98+
99+
**NOTE:** Using `@DiscriminatorColumn` along with `@DiscriminatorMapItem` is
100+
optional, and has been omitted above for clarity.
101+
102+
###### Child(ren) Class
103+
104+
```php
105+
use Camelot\DoctrineInheritanceMapping\Annotation\DiscriminatorEntry;
106+
use Doctrine\ORM\Mapping as ORM;
107+
108+
/**
109+
* @ORM\Entity()
110+
* @DiscriminatorMapItem(value="SingleTableChild")
111+
*/
112+
class SingleTableChild extends SingleTable
113+
{
114+
// ...
115+
}
116+
```
117+
[single-table-inheritance]: https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/reference/inheritance-mapping.html#single-table-inheritance

composer.json

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
{
2+
"name": "camelot/doctrine-inheritance-mapping",
3+
"description": "Doctrine inheritance mapping library",
4+
"type": "library",
5+
"license": "MIT",
6+
"keywords": ["doctrine", "orm", "inheritance-mapping", "symfony", "symfony-bundle"],
7+
"require": {
8+
"php": "^7.1.3",
9+
"doctrine/annotations": "^1.7",
10+
"doctrine/orm": "^2.5.11"
11+
},
12+
"require-dev": {
13+
"camelot/coding-style": "^2.0",
14+
"doctrine/doctrine-bundle": "^1.6.10",
15+
"friendsofphp/php-cs-fixer": "^2.15",
16+
"phpunit/phpunit": "^8.4",
17+
"ramsey/uuid-doctrine": "^1.5",
18+
"symfony/config": "^4.3",
19+
"symfony/debug-bundle": "^4.3",
20+
"symfony/framework-bundle": "^4.3",
21+
"symfony/http-kernel": "^4.3",
22+
"symfony/phpunit-bridge": "^4.3.4",
23+
"symfony/var-dumper": "^4.3",
24+
"symfony/yaml": "^4.3"
25+
},
26+
"autoload": {
27+
"psr-4": {
28+
"Camelot\\DoctrineInheritanceMapping\\": "src/"
29+
}
30+
},
31+
"autoload-dev": {
32+
"psr-4": {
33+
"Camelot\\DoctrineInheritanceMapping\\Tests\\": "tests/"
34+
}
35+
},
36+
"scripts": {
37+
"lint": "vendor/bin/php-cs-fixer fix --show-progress=dots",
38+
"test": "vendor/bin/phpunit --coverage-text"
39+
}
40+
}

phpunit.xml.dist

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!-- https://phpunit.readthedocs.io/en/latest/configuration.html -->
4+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/8.4/phpunit.xsd"
6+
backupGlobals="false"
7+
colors="true"
8+
bootstrap="vendor/autoload.php"
9+
>
10+
<php>
11+
<ini name="error_reporting" value="-1" />
12+
<server name="SHELL_VERBOSITY" value="-1" />
13+
</php>
14+
15+
<testsuites>
16+
<testsuite name="Project Test Suite">
17+
<directory>tests</directory>
18+
</testsuite>
19+
</testsuites>
20+
21+
<filter>
22+
<whitelist>
23+
<directory>src</directory>
24+
</whitelist>
25+
</filter>
26+
</phpunit>
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Camelot\DoctrineInheritanceMapping\Annotation;
6+
7+
use BadMethodCallException;
8+
9+
/**
10+
* @Annotation
11+
*/
12+
final class DiscriminatorMapItem
13+
{
14+
/** @var string|null */
15+
private $value;
16+
17+
public function __construct(array $data)
18+
{
19+
static::assertValid($data);
20+
$this->value = $data['value'];
21+
}
22+
23+
public function getValue(): ?string
24+
{
25+
return $this->value;
26+
}
27+
28+
private static function assertValid(array $data): void
29+
{
30+
if (!($data['value'] ?? false)) {
31+
throw new BadMethodCallException(sprintf('Value for annotation DiscriminatorMapItem is missing or empty.'));
32+
}
33+
}
34+
}

0 commit comments

Comments
 (0)