From 6a0dc72b5fce54a2c5c3b9b73f03740017b0d007 Mon Sep 17 00:00:00 2001 From: Nikita Makarov Date: Sun, 27 Oct 2019 21:13:51 +0300 Subject: [PATCH] Basic SF4 Support --- composer.json | 6 +- src/Command/LoadDataFixturesCommand.php | 94 ++++++++++++++++++------- src/Resources/config/services.yml | 10 +++ 3 files changed, 80 insertions(+), 30 deletions(-) diff --git a/composer.json b/composer.json index 159ed46..5e6a194 100644 --- a/composer.json +++ b/composer.json @@ -11,9 +11,9 @@ } ], "require": { - "php": ">=5.5.9", - "symfony/framework-bundle": "~2.7|~3.0", - "symfony/doctrine-bridge": "~2.7|~3.0", + "php": ">=7.0", + "symfony/framework-bundle": "~2.7|~3.0|~4.0", + "symfony/doctrine-bridge": "~2.7|~3.0|~4.0", "doctrine/data-fixtures": "^1.0", "doctrine/dbal": "^2.1", "doctrine/orm": "^2.1" diff --git a/src/Command/LoadDataFixturesCommand.php b/src/Command/LoadDataFixturesCommand.php index e90aeb1..32b4156 100644 --- a/src/Command/LoadDataFixturesCommand.php +++ b/src/Command/LoadDataFixturesCommand.php @@ -2,27 +2,58 @@ namespace Okvpn\Bundle\FixtureBundle\Command; +use Doctrine\Common\Persistence\ManagerRegistry; use Doctrine\DBAL\Connection; +use Okvpn\Bundle\FixtureBundle\Migration\DataFixturesExecutor; +use Okvpn\Bundle\FixtureBundle\Migration\DataFixturesExecutorInterface; +use Okvpn\Bundle\FixtureBundle\Migration\Loader\DataFixturesLoader; use Okvpn\Bundle\FixtureBundle\Tools\FixtureDatabaseChecker; -use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; +use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface; use Symfony\Component\HttpKernel\Bundle\BundleInterface; -use Okvpn\Bundle\FixtureBundle\Migration\DataFixturesExecutorInterface; - -class LoadDataFixturesCommand extends ContainerAwareCommand +class LoadDataFixturesCommand extends Command { const COMMAND_NAME = 'okvpn:fixtures:data:load'; - const MAIN_FIXTURES_TYPE = DataFixturesExecutorInterface::MAIN_FIXTURES; const DEMO_FIXTURES_TYPE = DataFixturesExecutorInterface::DEMO_FIXTURES; + /** @var Connection */ + protected $connection; + + /** @var ManagerRegistry */ + private $registry; + + /** @var DataFixturesLoader */ + private $dataFixturesLoader; + + /** @var DataFixturesExecutor */ + private $dataFixturesExecutor; + + /** @var ParameterBagInterface */ + private $parameterBag; + /** - * @var Connection + * @param ManagerRegistry $registry + * @param DataFixturesLoader $dataFixturesLoader + * @param DataFixturesExecutor $dataFixturesExecutor */ - protected $connection; + public function __construct( + ManagerRegistry $registry, + DataFixturesLoader $dataFixturesLoader, + DataFixturesExecutor $dataFixturesExecutor, + ParameterBagInterface $parameterBag + ) { + parent::__construct(self::COMMAND_NAME); + + $this->registry = $registry; + $this->dataFixturesLoader = $dataFixturesLoader; + $this->dataFixturesExecutor = $dataFixturesExecutor; + $this->parameterBag = $parameterBag; + } /** * {@inheritdoc} @@ -64,7 +95,7 @@ protected function configure() */ protected function initialize(InputInterface $input, OutputInterface $output) { - $this->connection = $this->getContainer()->get('doctrine')->getConnection(); + $this->connection = $this->registry->getConnection(); } /** @@ -90,45 +121,53 @@ protected function execute(InputInterface $input, OutputInterface $output) $this->processFixtures($input, $output, $fixtures); } } + return 0; } /** - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output + * * @return array * @throws \RuntimeException if loading of data fixtures should be terminated */ protected function getFixtures(InputInterface $input, OutputInterface $output) { - $loader = $this->getContainer()->get('okvpn_fixture.data.loader'); - $bundles = $input->getOption('bundles'); + $expectedBundles = $input->getOption('bundles'); $excludeBundles = $input->getOption('exclude'); $fixtureRelativePath = $this->getFixtureRelativePath($input); + $currentBundles = array_map(function (BundleInterface $bundle) { + return ['name' => $bundle->getName(), 'path' => $bundle->getPath()]; + }, $this->getApplication()->getKernel()->getBundles()); + + // Add root_dir to fixtures paths + $currentBundles[] = ['name' => 'App', 'path' => $this->parameterBag->get('kernel.root_dir')]; + /** @var BundleInterface $bundle */ - foreach ($this->getApplication()->getKernel()->getBundles() as $bundle) { - if (!empty($bundles) && !in_array($bundle->getName(), $bundles)) { + foreach ($currentBundles as $bundle) { + if (!empty($expectedBundles) && !in_array($bundle['name'], $expectedBundles)) { continue; } - if (!empty($excludeBundles) && in_array($bundle->getName(), $excludeBundles)) { + if (!empty($excludeBundles) && in_array($bundle['name'], $excludeBundles)) { continue; } - $path = $bundle->getPath() . $fixtureRelativePath; + $path = $bundle['path'] . $fixtureRelativePath; if (is_dir($path)) { - $loader->loadFromDirectory($path); + $this->dataFixturesLoader->loadFromDirectory($path); } } - return $loader->getFixtures(); + return $this->dataFixturesLoader->getFixtures(); } /** * Output list of fixtures * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output - * @param array $fixtures + * @param array $fixtures */ protected function outputFixtures(InputInterface $input, OutputInterface $output, $fixtures) { @@ -146,9 +185,9 @@ protected function outputFixtures(InputInterface $input, OutputInterface $output /** * Process fixtures * - * @param InputInterface $input + * @param InputInterface $input * @param OutputInterface $output - * @param array $fixtures + * @param array $fixtures */ protected function processFixtures(InputInterface $input, OutputInterface $output, $fixtures) { @@ -159,17 +198,17 @@ protected function processFixtures(InputInterface $input, OutputInterface $outpu ) ); - $executor = $this->getContainer()->get('okvpn_fixture.data.executor'); - $executor->setLogger( + $this->dataFixturesExecutor->setLogger( function ($message) use ($output) { $output->writeln(sprintf(' > %s', $message)); } ); - $executor->execute($fixtures, $this->getTypeOfFixtures($input)); + $this->dataFixturesExecutor->execute($fixtures, $this->getTypeOfFixtures($input)); } /** * @param InputInterface $input + * * @return string */ protected function getTypeOfFixtures(InputInterface $input) @@ -179,20 +218,21 @@ protected function getTypeOfFixtures(InputInterface $input) /** * @param InputInterface $input + * * @return string */ protected function getFixtureRelativePath(InputInterface $input) { $fixtureRelativePath = $this->getTypeOfFixtures($input) === self::DEMO_FIXTURES_TYPE - ? $this->getContainer()->getParameter('okvpn_fixture.path_data_demo') - : $this->getContainer()->getParameter('okvpn_fixture.path_data_main'); + ? $this->parameterBag->get('okvpn_fixture.path_data_demo') + : $this->parameterBag->get('okvpn_fixture.path_data_main'); return str_replace('/', DIRECTORY_SEPARATOR, '/' . $fixtureRelativePath); } protected function ensureTableExist() { - $table = $this->getContainer()->getParameter('okvpn_fixture.table'); + $table = $this->parameterBag->get('okvpn_fixture.table'); if (!FixtureDatabaseChecker::tablesExist($this->connection, $table)) { FixtureDatabaseChecker::declareTable($this->connection, $table); } diff --git a/src/Resources/config/services.yml b/src/Resources/config/services.yml index c2ef9b2..72a12d3 100644 --- a/src/Resources/config/services.yml +++ b/src/Resources/config/services.yml @@ -21,3 +21,13 @@ services: arguments: ['%okvpn_fixture.table%'] tags: - { name: doctrine.event_listener, event: loadClassMetadata } + + okvpn_fixture.command.load_data_fixtures: + class: Okvpn\Bundle\FixtureBundle\Command\LoadDataFixturesCommand + arguments: + - '@doctrine' + - '@okvpn_fixture.data.loader' + - '@okvpn_fixture.data.executor' + - '@parameter_bag' + tags: + - { name: console.command }