Skip to content

Commit

Permalink
Merge pull request #22 from okvpn/symfony-4
Browse files Browse the repository at this point in the history
Fix support symfony 4
  • Loading branch information
vtsykun authored Feb 1, 2019
2 parents 59fdd30 + 342da4f commit 3123b71
Show file tree
Hide file tree
Showing 11 changed files with 258 additions and 75 deletions.
50 changes: 41 additions & 9 deletions src/Command/DiffMigrationsCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\Question;

class DiffMigrationsCommand extends ContainerAwareCommand
{
Expand All @@ -29,6 +30,11 @@ class DiffMigrationsCommand extends ContainerAwareCommand
*/
protected $extendedFieldOptions = [];

/**
* @var string
*/
protected $migrationPath;

/**
* @var string
*/
Expand All @@ -44,10 +50,14 @@ class DiffMigrationsCommand extends ContainerAwareCommand
*/
protected $version;

/** @var SchemaProviderInterface */
/**
* @var SchemaProviderInterface
*/
protected $schemaProvider;

/** @var SchemaProviderInterface */
/**
* @var SchemaProviderInterface
*/
protected $okvpnSchemaProvider;

/**
Expand All @@ -73,6 +83,29 @@ protected function configure()
->setDescription('Compare current existing database structure with orm structure');
}

/**
* {@inheritdoc}
*/
protected function interact(InputInterface $input, OutputInterface $output)
{
if (!$input->getOption('bundle')) {
$helper = $this->getHelper('question');
$question = new Question("<info>Please select your package name:</info>\n > ");
$bundleNames = array_keys($this->getContainer()->get('okvpn_migration.migrations.loader')->getBundleList());
$question->setAutocompleterValues($bundleNames);
$question->setValidator(function ($answer) use ($bundleNames) {
if (!in_array($answer, $bundleNames)) {
throw new \RuntimeException(sprintf('Package "%s" does not exist.', $answer));
}
return $answer;
});

$question->setMaxAttempts(3);
$bundle = $helper->ask($input, $output, $question);
$input->setOption('bundle', $bundle);
}
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -137,14 +170,16 @@ protected function getOkvpnSchemaProvider()
protected function initializeBundleRestrictions($bundle)
{
if ($bundle) {
$bundles = $this->getContainer()->getParameter('kernel.bundles');
$bundles = $this->getContainer()->get('okvpn_migration.migrations.loader')->getBundleList();
if (!array_key_exists($bundle, $bundles)) {
throw new \InvalidArgumentException(
sprintf('Bundle "%s" is not a known bundle', $bundle)
);
}
$this->namespace = str_replace($bundle, 'Entity', $bundles[$bundle]);

$this->migrationPath = $bundles[$bundle]['dir_name'];
$this->className = $bundle . 'Installer';
$this->namespace = $bundles[$bundle]['namespace'];
}
}

Expand All @@ -159,19 +194,16 @@ protected function initializeMetadataInformation()
array_walk(
$allMetadata,
function (ClassMetadata $entityMetadata) {
if ($this->namespace) {
if ($entityMetadata->namespace == $this->namespace) {
if ($this->migrationPath) {
if (strpos($entityMetadata->getReflectionClass()->getFileName(), $this->migrationPath) === 0) {
$this->allowedTables[$entityMetadata->getTableName()] = true;
foreach ($entityMetadata->getAssociationMappings() as $associationMappingInfo) {
if (!empty($associationMappingInfo['joinTable'])) {
$joinTableName = $associationMappingInfo['joinTable']['name'];
$this->allowedTables[$joinTableName] = true;
}
}
//$this->initializeExtendedFieldsOptions($entityMetadata);
}
} else {
//$this->initializeExtendedFieldsOptions($entityMetadata);
}
}
);
Expand Down
4 changes: 3 additions & 1 deletion src/DependencyInjection/Compiler/MigrationExtensionPass.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
<?php

declare(strict_types=1);

namespace Okvpn\Bundle\MigrationBundle\DependencyInjection\Compiler;

use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;

class MigrationExtensionPass implements CompilerPassInterface
final class MigrationExtensionPass implements CompilerPassInterface
{
const MANAGER_SERVICE_KEY = 'okvpn_migration.migrations.extension_manager';
const TAG = 'okvpn_migration.extension';
Expand Down
32 changes: 29 additions & 3 deletions src/DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<?php

declare(strict_types=1);

namespace Okvpn\Bundle\MigrationBundle\DependencyInjection;

use Symfony\Component\Config\Definition\Builder\TreeBuilder;
use Symfony\Component\Config\Definition\ConfigurationInterface;

class Configuration implements ConfigurationInterface
final class Configuration implements ConfigurationInterface
{
/**
* {@inheritdoc}
Expand All @@ -17,9 +19,33 @@ public function getConfigTreeBuilder()

$rootNode
->children()
->scalarNode('migrations_path')->end()
->scalarNode('migrations_table')->end()
->scalarNode('migrations_path')
->info('Deprecated use "dir_prefix"')
->end()
->scalarNode('migrations_table')
->info('Deprecated use "dir_prefix"')
->end()
->scalarNode('table_name')
->info('By default "okvpn_migrations"')
->end()
->scalarNode('dir_prefix')
->info('By default "Migrations/Schema"')
->end()
->arrayNode('migrations_paths')
->info('Lookup migrations directories')
->useAttributeAsKey('name')
->prototype('array')
->children()
->scalarNode('dir_name')->defaultNull()->end()
->scalarNode('namespace')
->info('Namespace is arbitrary but should be different from App\Migrations\Schema as migrations classes should NOT be autoloaded')
->defaultNull()
->end()
->end()
->end()
->end()
->end();

return $treeBuilder;
}
}
32 changes: 29 additions & 3 deletions src/DependencyInjection/OkvpnMigrationExtension.php
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
<?php

declare(strict_types=1);

namespace Okvpn\Bundle\MigrationBundle\DependencyInjection;

use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\HttpKernel\Kernel;

class OkvpnMigrationExtension extends Extension
final class OkvpnMigrationExtension extends Extension
{
/**
* {@inheritdoc}
Expand All @@ -20,12 +23,35 @@ public function load(array $configs, ContainerBuilder $container)
$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');

if (isset($config['migrations_path']) && null !== $config['migrations_path']) {
if (isset($config['dir_prefix']) && null !== $config['dir_prefix']) {
$container->setParameter('okvpn.migrations_path', $config['dir_prefix']);
} elseif (isset($config['migrations_path']) && null !== $config['migrations_path']) {
$container->setParameter('okvpn.migrations_path', $config['migrations_path']);
}

if (isset($config['migrations_table']) && null !== $config['migrations_table']) {
if (isset($config['table_name']) && null !== $config['table_name']) {
$container->setParameter('okvpn.migrations_table', $config['table_name']);
} elseif (isset($config['migrations_table']) && null !== $config['migrations_table']) {
$container->setParameter('okvpn.migrations_table', $config['migrations_table']);
}

$migrationsPaths = [];
foreach ($container->getParameter('kernel.bundles_metadata') as $bundleName => $bundle) {
$migrationsPaths[$bundleName] = [
'dir_name' => $bundle['path'],
'namespace' => $bundle['namespace'],
];
}

// Add root_dir to migration paths
if (empty($config['migrations_paths']) && Kernel::MAJOR_VERSION >= 4) {
$config['migrations_paths']['App'] = [
'dir_name' => $container->getParameter('kernel.root_dir'),
'namespace' => 'App',
];
}

$container->getDefinition('okvpn_migration.migrations.loader')
->replaceArgument(6, array_merge($migrationsPaths, $config['migrations_paths'] ?? []));
}
}
34 changes: 34 additions & 0 deletions src/EventListener/DoctrineSchemaChangeListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

declare(strict_types=1);

namespace Okvpn\Bundle\MigrationBundle\EventListener;

use Doctrine\DBAL\Event\SchemaAlterTableChangeColumnEventArgs;

/**
* In order to disable update migration table.
*/
final class DoctrineSchemaChangeListener
{
/** @var string */
private $migrationTable;

/**
* @param string $migrationTable
*/
public function __construct(string $migrationTable)
{
$this->migrationTable = $migrationTable;
}

/**
* @param SchemaAlterTableChangeColumnEventArgs $args
*/
public function onSchemaAlterTableChangeColumn(SchemaAlterTableChangeColumnEventArgs $args)
{
if ($args->getTableDiff()->name === $this->migrationTable) {
$args->preventDefault();
}
}
}
Loading

0 comments on commit 3123b71

Please sign in to comment.