Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basic SF4 Support #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
94 changes: 67 additions & 27 deletions src/Command/LoadDataFixturesCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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();
}

/**
Expand All @@ -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')];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could probably be made it in configuration. What do you think?


/** @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)
{
Expand All @@ -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)
{
Expand All @@ -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(' <comment>></comment> <info>%s</info>', $message));
}
);
$executor->execute($fixtures, $this->getTypeOfFixtures($input));
$this->dataFixturesExecutor->execute($fixtures, $this->getTypeOfFixtures($input));
}

/**
* @param InputInterface $input
*
* @return string
*/
protected function getTypeOfFixtures(InputInterface $input)
Expand All @@ -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);
}
Expand Down
10 changes: 10 additions & 0 deletions src/Resources/config/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will not works with SF < 4.1. A new parameter_bag service was added only since 4.1

tags:
- { name: console.command }