2018-05-04 22:04:30 +00:00
|
|
|
<?php
|
2020-10-14 22:19:31 +00:00
|
|
|
|
2021-07-19 05:53:45 +00:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-08-04 22:05:14 +00:00
|
|
|
namespace App\Console\Command;
|
2018-05-04 22:04:30 +00:00
|
|
|
|
2020-12-03 04:18:06 +00:00
|
|
|
use App\Environment;
|
2018-05-04 22:04:30 +00:00
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor;
|
|
|
|
use Doctrine\Common\DataFixtures\Loader;
|
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
|
2020-06-26 20:22:53 +00:00
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
2019-09-29 21:45:05 +00:00
|
|
|
use Psr\Container\ContainerInterface;
|
2019-12-07 00:57:50 +00:00
|
|
|
use RecursiveDirectoryIterator;
|
|
|
|
use RecursiveIteratorIterator;
|
2019-09-10 23:10:57 +00:00
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle;
|
2018-05-04 22:04:30 +00:00
|
|
|
|
2019-09-10 23:10:57 +00:00
|
|
|
class SetupFixturesCommand extends CommandAbstract
|
2018-05-04 22:04:30 +00:00
|
|
|
{
|
2019-09-10 23:10:57 +00:00
|
|
|
public function __invoke(
|
|
|
|
SymfonyStyle $io,
|
2020-06-26 20:22:53 +00:00
|
|
|
EntityManagerInterface $em,
|
2019-09-29 21:45:05 +00:00
|
|
|
ContainerInterface $di,
|
2020-12-03 04:18:06 +00:00
|
|
|
Environment $environment
|
2020-10-14 22:19:31 +00:00
|
|
|
): int {
|
2018-05-04 22:04:30 +00:00
|
|
|
$loader = new Loader();
|
2019-09-29 21:45:05 +00:00
|
|
|
|
|
|
|
// Dependency-inject the fixtures and load them.
|
2020-12-06 11:57:39 +00:00
|
|
|
$fixturesDir = $environment->getBaseDirectory() . '/src/Entity/Fixture';
|
2019-09-29 21:45:05 +00:00
|
|
|
|
2019-12-07 00:57:50 +00:00
|
|
|
$iterator = new RecursiveIteratorIterator(
|
|
|
|
new RecursiveDirectoryIterator($fixturesDir),
|
|
|
|
RecursiveIteratorIterator::LEAVES_ONLY
|
2019-09-29 21:45:05 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
foreach ($iterator as $file) {
|
|
|
|
// Skip dotfiles
|
|
|
|
if (($fileName = $file->getBasename('.php')) == $file->getBasename()) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$className = 'App\\Entity\\Fixture\\' . $fileName;
|
|
|
|
$fixture = $di->get($className);
|
|
|
|
|
|
|
|
$loader->addFixture($fixture);
|
|
|
|
}
|
2018-05-04 22:04:30 +00:00
|
|
|
|
|
|
|
$purger = new ORMPurger($em);
|
|
|
|
$executor = new ORMExecutor($em, $purger);
|
|
|
|
$executor->execute($loader->getFixtures());
|
|
|
|
|
2020-10-07 23:50:30 +00:00
|
|
|
$io->success(__('Fixtures loaded.'));
|
2018-05-07 01:57:06 +00:00
|
|
|
|
|
|
|
return 0;
|
2018-05-04 22:04:30 +00:00
|
|
|
}
|
2018-08-04 22:05:14 +00:00
|
|
|
}
|