ninestat/src/ninestat-1.0/src/Cadoles/CoreBundle/Command/InitDataCommand.php

79 lines
2.5 KiB
PHP

<?php
namespace Cadoles\CoreBundle\Command;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use Symfony\Component\HttpKernel\KernelInterface;
use Doctrine\DBAL\Connection as DBALConnection;
use Doctrine\ORM\EntityManager;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\Exception\UnsatisfiedDependencyException;
use Doctrine\ORM\Mapping\ClassMetadata;
use Doctrine\ORM\Id\AssignedGenerator;
use Cadoles\CoreBundle\Entity\Etab;
class InitDataCommand extends ContainerAwareCommand
{
protected function configure()
{
$this
// the name of the command (the part after "bin/console")
->setName('Core:InitData')
// the short description shown while running "php bin/console list"
->setDescription('Init Data for Core')
// the full command description shown when running the command with
// the "--help" option
->setHelp('This command Init Data for Core')
;
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('');
$output->writeln('CORE = Default Data');
$em = $this->getContainer()->get('doctrine')->getManager();
$finder = new Finder();
$finder->in('src/Cadoles/CoreBundle/Command/data');
// Init 01 = ce qui templetisé
$finder->name('core-init-01.sql');
foreach( $finder as $file ){
$content = $file->getContents();
$stmt = $em->getConnection()->prepare($content);
$stmt->execute();
}
$stmt->closeCursor();
// afin de forcer les ID sur certaines entités
$metadata = $em->getClassMetaData('CadolesCoreBundle:Etab');
$metadata->setIdGeneratorType(ClassMetadata::GENERATOR_TYPE_NONE);
$metadata->setIdGenerator(new AssignedGenerator());
// Etab inconnu
$entity = $em->getRepository('CadolesCoreBundle:Etab')->find(-100);
if(!$entity) {
$entity = new Etab;
$entity->setId(-100);
}
$entity->setLatitude(50);
$entity->setLongitude(-30);
$em->persist($entity);
$em->flush();
}
protected static function determineKernelRootDir(Event $event) {
$extra = $event->getComposer()->getPackage()->getExtra();
$rootdir = rtrim(getcwd(), '/');
return $rootdir . '/' . trim($extra['symfony-app-dir'], '/');
}
}