Skip to content

Commit

Permalink
Update & Migrations support
Browse files Browse the repository at this point in the history
- added support for `nettrine/migrations`
- added extension interface `MigrationsDirectoriesProviderInterface` and DI DTO `MigrationsDirectory`
- updated `nettrine` packages to `^0.8`
- updated php-cs-fixer and added new configuration
  • Loading branch information
tg666 committed Apr 19, 2022
1 parent b00848e commit f74d50c
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 31 deletions.
6 changes: 3 additions & 3 deletions .php_cs.dist → .php-cs-fixer.dist.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
$finder = PhpCsFixer\Finder::create()
->in(__DIR__ . '/src')
->in(__DIR__ . '/tests')
->exclude('temp')
->exclude(__DIR__ . '/tests/temp')
;

return PhpCsFixer\Config::create()
return (new PhpCsFixer\Config)
->setUsingCache(FALSE)
->setIndent("\t")
->setRules([
'@PSR2' => TRUE,
'array_syntax' => ['syntax' => 'short'],
'trailing_comma_in_multiline_array' => true,
'trailing_comma_in_multiline' => TRUE,
'constant_case' => [
'case' => 'upper',
],
Expand Down
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,19 @@
}
],
"require": {
"php": "^7.3",
"php": "^7.4",
"nette/di": "^3.0.3"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.4",
"friendsofphp/php-cs-fixer": "^2.0",
"friendsofphp/php-cs-fixer": "^3.0",
"nette/bootstrap": "^3.0",
"nette/tester": "^2.3.4",
"roave/security-advisories": "dev-master"
"roave/security-advisories": "dev-latest"
},
"suggest": {
"nettrine/orm": "For integration with nettrine/orm"
"nettrine/orm": "For integration with nettrine/orm",
"nettrine/migrations": "For integration with nettrine/migrations"
},
"autoload": {
"psr-4": {
Expand Down
46 changes: 43 additions & 3 deletions src/Bridge/Nettrine/DI/DoctrineBridgeExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@
use Nette\DI\Definitions\Reference;
use Nettrine\DBAL\DI\DbalExtension;
use Nettrine\ORM\DI\Helpers\MappingHelper;
use Nette\DI\Definitions\ServiceDefinition;
use Nettrine\Migrations\DI\MigrationsExtension;
use Doctrine\ORM\Tools\ResolveTargetEntityListener;
use Doctrine\Migrations\Configuration\Configuration;
use SixtyEightPublishers\DoctrineBridge\DI\EntityMapping;
use SixtyEightPublishers\DoctrineBridge\DI\DatabaseTypeProviderInterface;
use SixtyEightPublishers\DoctrineBridge\DI\TargetEntityProviderInterface;
use SixtyEightPublishers\DoctrineBridge\Type\ContainerAwareTypeInterface;
use SixtyEightPublishers\DoctrineBridge\DI\EntityMappingProviderInterface;
use SixtyEightPublishers\DoctrineBridge\DI\MigrationsDirectoriesProviderInterface;

final class DoctrineBridgeExtension extends CompilerExtension
{
Expand All @@ -24,7 +28,7 @@ final class DoctrineBridgeExtension extends CompilerExtension
*/
public function loadConfiguration(): void
{
if (0 >= count($this->compiler->getExtensions(TargetEntityProviderInterface::class))) {
if (!$this->isExtensionLoaded(TargetEntityProviderInterface::class)) {
return;
}

Expand All @@ -41,6 +45,7 @@ public function beforeCompile(): void
$this->processDatabaseTypeProviders();
$this->processEntityMappings();
$this->processTargetEntities();
$this->processMigrationsDirectories();
}

/**
Expand All @@ -57,8 +62,10 @@ private function processDatabaseTypeProviders(): void
$dbalExtensionName = key($dbalExtensions);
$builder = $this->getContainerBuilder();

/** @var \Nette\DI\Definitions\ServiceDefinition $connectionFactory */
$connectionFactory = $builder->getDefinition($dbalExtensionName . '.connectionFactory');

assert($connectionFactory instanceof ServiceDefinition);

$factory = $connectionFactory->getFactory();
[$types, $typesMapping] = $factory->arguments;
$contexts = [];
Expand All @@ -82,9 +89,10 @@ private function processDatabaseTypeProviders(): void
$factory->arguments[0] = $types;
$factory->arguments[1] = $typesMapping;

/** @var \Nette\DI\Definitions\ServiceDefinition $connection */
$connection = $builder->getDefinition($dbalExtensionName . '.connection');

assert($connection instanceof ServiceDefinition);

foreach ($types as $typeName => $typeOptions) {
$typeClassName = $typeOptions['class'];

Expand Down Expand Up @@ -149,4 +157,36 @@ private function processTargetEntities(): void
}
}
}

/**
* @return void
*/
private function processMigrationsDirectories(): void
{
if (!$this->isExtensionLoaded(MigrationsExtension::class)) {
return;
}

$builder = $this->getContainerBuilder();
$configuration = $builder->getDefinitionByType(Configuration::class);

assert($configuration instanceof ServiceDefinition);

/** @var \SixtyEightPublishers\DoctrineBridge\DI\MigrationsDirectoriesProviderInterface $extension */
foreach ($this->compiler->getExtensions(MigrationsDirectoriesProviderInterface::class) as $extension) {
foreach ($extension->getMigrationsDirectories() as $migrationsDirectory) {
$configuration->addSetup('addMigrationsDirectory', [$migrationsDirectory->namespace, $migrationsDirectory->directory]);
}
}
}

/**
* @param string $classname
*
* @return bool
*/
private function isExtensionLoaded(string $classname): bool
{
return 0 < count($this->compiler->getExtensions($classname));
}
}
12 changes: 4 additions & 8 deletions src/DI/DatabaseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@

final class DatabaseType
{
/** @var string */
public $name;
public string $name;

/** @var string */
public $class;
public string $class;

/** @var string|NULL */
public $mappingType;
public ?string $mappingType;

/** @var array */
public $context;
public array $context;

/**
* @param string $name
Expand Down
9 changes: 3 additions & 6 deletions src/DI/EntityMapping.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,11 @@ final class EntityMapping
public const DRIVER_YAML = 'yaml';
public const DRIVER_XML = 'xml';

/** @var string */
public $driver;
public string $driver;

/** @var string */
public $namespace;
public string $namespace;

/** @var string */
public $path;
public string $path;

/**
* @param string $driver
Expand Down
13 changes: 13 additions & 0 deletions src/DI/MigrationsDirectoriesProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\DoctrineBridge\DI;

interface MigrationsDirectoriesProviderInterface
{
/**
* @return \SixtyEightPublishers\DoctrineBridge\DI\MigrationsDirectory[]
*/
public function getMigrationsDirectories(): array;
}
22 changes: 22 additions & 0 deletions src/DI/MigrationsDirectory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

declare(strict_types=1);

namespace SixtyEightPublishers\DoctrineBridge\DI;

final class MigrationsDirectory
{
public string $namespace;

public string $directory;

/**
* @param string $namespace
* @param string $directory
*/
public function __construct(string $namespace, string $directory)
{
$this->namespace = $namespace;
$this->directory = $directory;
}
}
9 changes: 3 additions & 6 deletions src/DI/TargetEntity.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,11 @@

final class TargetEntity
{
/** @var string */
public $originalEntity;
public string $originalEntity;

/** @var string */
public $newEntity;
public string $newEntity;

/** @var array */
public $mapping;
public array$mapping;

/**
* @param string $originalEntity
Expand Down
3 changes: 2 additions & 1 deletion vendor-bin/nettrine/composer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"require": {
"theofidry/composer-inheritance-plugin": "^1.0",
"nettrine/orm": "^0.7"
"nettrine/orm": "^0.8",
"nettrine/migrations": "^0.8"
},
"config": {
"bin-dir": "bin",
Expand Down

0 comments on commit f74d50c

Please sign in to comment.