public function run() { $entityManager = null; if (is_array($_SERVER['argv'])) { foreach ($_SERVER['argv'] as $key => $value) { if (substr($value, 0, 5) === '--em=') { $entityManager = substr($value, 5); unset($_SERVER['argv'][$key]); if (is_int($_SERVER['argc'])) { $_SERVER['argc']--; } break; } } } $commands = $this->doctrine->getCommands(); $helperSet = $this->doctrine->getHelperSet($entityManager); if (!$helperSet instanceof HelperSet) { foreach ($GLOBALS as $helperSetCandidate) { if ($helperSetCandidate instanceof HelperSet) { $helperSet = $helperSetCandidate; break; } } } ConsoleRunner::run($helperSet, $commands); }
/** * @return EntityManagerInterface */ public function getEntityManager() { if (null === $this->entityManager) { $doctrine = new Doctrine(); $doctrine->setConfig($this->config); $this->entityManager = $doctrine->getEntityManager(); } return $this->entityManager; }
/** * @param InputInterface $input * @param OutputInterface $output * @return void * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output) { /** @var \Doctrine\DBAL\Driver\PDOConnection $conn */ $conn = $this->doctrine->getEntityManager()->getConnection()->getWrappedConnection(); $query = "SHOW TABLES"; foreach ($conn->query($query) as $row) { $command = new GenerateEntityCommand($this->doctrine); $command->setTable($row[0]); $command->run(new ArrayInput([]), new ConsoleOutput()); } }
public function setUp() { $doctrine = new Doctrine(); $doctrine->setConfig((new Config(['host' => getenv('MYSQL_HOST'), 'port' => getenv('MYSQL_PORT'), 'dbname' => getenv('MYSQL_DATABASE'), 'user' => getenv('MYSQL_USER'), 'password' => getenv('MYSQL_PASSWORD')]))->setIsDev(true)); $this->em = $doctrine->getEntityManager(); }
/** * @param InputInterface $input * @param OutputInterface $output * @return void * @throws \Exception */ protected function execute(InputInterface $input, OutputInterface $output) { if (!($table = $this->getTable())) { $question = new Question('Table name: '); $table = strtolower($this->question->ask($input, $output, $question)); } $words = explode('_', $table); $entity = ''; foreach ($words as $word) { if (substr($word, -1) === 's') { $word = substr($word, 0, -1); } $entity .= ucfirst($word); } $entity = $entity . '/' . $entity . 'Entity'; $directory = trim(dirname($entity), '/'); $entity = str_replace('.php', null, basename($entity)); $dirPath = $this->srcDir . DIRECTORY_SEPARATOR . $directory; if (file_exists($dirPath . DIRECTORY_SEPARATOR . $entity . '.php')) { $output->write('[ <fg=red>Failed: Entity ' . $directory . DIRECTORY_SEPARATOR . $entity . ' already exists</fg=red> ]', true); return; } if (!is_dir($dirPath)) { mkdir($dirPath, 0777, true); } // Execute doctrine command $fileListBefore = scandir($dirPath); $filter = str_replace(' ', '', ucwords(str_replace('_', ' ', $table))); $command = new ConvertMappingCommand(); $helperSet = new HelperSet(array('db' => new ConnectionHelper($this->doctrine->getEntityManager()->getConnection()), 'em' => new EntityManagerHelper($this->doctrine->getEntityManager()))); $command->setHelperSet($helperSet); $this->doctrine->getEntityManager()->getConfiguration()->setFilterSchemaAssetsExpression('/^' . $table . '$/i'); $command->run(new ArrayInput(['to-type' => 'annotation', 'dest-path' => $this->srcDir . "/{$directory}", '--from-database' => true, '--filter' => $filter]), new ConsoleOutput()); $fileListAfter = scandir($dirPath); // Move file to right location $file = current(array_diff($fileListAfter, $fileListBefore)); if (!$file) { throw new \Exception('No entity generated'); } $filePath = $dirPath . DIRECTORY_SEPARATOR . $file; rename($filePath, $dirPath . DIRECTORY_SEPARATOR . $entity . '.php'); $filePath = $dirPath . DIRECTORY_SEPARATOR . $entity . '.php'; $fileContent = file_get_contents($filePath); // Fix classes and namespace $fileContent = str_replace('<?php', '', $fileContent); $fileContent = str_replace('class ' . str_replace('.php', '', $file), 'class ' . $entity, $fileContent); $fileContent = str_replace(' * ' . str_replace('.php', '', $file) . PHP_EOL . ' *' . PHP_EOL, '', $fileContent); $fileContent = str_replace('\\DateTime', 'DateTime', $fileContent); $fileContent = str_replace('@var boolean', '@var bool', $fileContent); $fileContent = str_replace('@var integer', '@var int', $fileContent); $fileContent = str_replace(', nullable=false', '', $fileContent); $fileContent = str_replace("\n\n}", "}", $fileContent); // Fix table and entity inversion $fileContent = preg_replace('/(.*)@ORM\\\\Table(.*)\\n(.*)@ORM\\\\Entity(.*)\\n/im', "\$3@ORM\\Entity\$4\n\$1@ORM\\Table\$2\n", $fileContent); // Fix ids docblock $fileContent = str_replace('GeneratedValue(strategy="IDENTITY")', 'GeneratedValue', $fileContent); $fileContent = preg_replace('/(.*)@var(.*)\\n(.*)\\n(.*)@ORM\\\\Column(.*)\\n(.*)@ORM\\\\Id\\n(.*)@ORM\\\\GeneratedValue(.*)\\n/im', "\$6@ORM\\Id\n\$4@ORM\\Column\$5\n\$7@ORM\\GeneratedValue\$8\n\$1@var\$2\n", $fileContent); // Fix docblock $fileContent = preg_replace('/(.*)@var(.*)\\n(.*)\\n(.*)@ORM\\\\Column(.*)\\n/im', "\$4@ORM\\Column\$5\n\$1@var\$2\n", $fileContent); // Fix booleans $fileContent = preg_replace('/(.*)@var bool(.*)\\n(.*)\\n(.*)private(.*)\'0\';/im', "\$1@var bool\$2\n\$3\n\$4private\$5false;", $fileContent); $fileContent = preg_replace('/(.*)@var bool(.*)\\n(.*)\\n(.*)private(.*)\'1\';/im', "\$1@var bool\$2\n\$3\n\$4private\$5true;", $fileContent); // Fix int $fileContent = preg_replace('/(.*)@var int(.*)\\n(.*)\\n(.*)private(.*)\'(\\d*)\';/im', "\$1@var int\$2\n\$3\n\$4private\$5\$6;", $fileContent); // Append file namespace $fileContent = '<?php' . PHP_EOL . 'namespace ' . trim($this->getNamespace(), '\\/') . '\\' . str_replace('/', '\\', trim($directory, '\\/')) . ';' . PHP_EOL . 'use DateTime;' . PHP_EOL . PHP_EOL . trim($fileContent); // Fix enums /** @var \Doctrine\DBAL\Driver\PDOConnection $conn */ $conn = $this->doctrine->getEntityManager()->getConnection()->getWrappedConnection(); $query = "SHOW COLUMNS FROM `{$table}`"; foreach ($conn->query($query) as $row) { if (substr($row['Type'], 0, 4) === 'enum') { $definition = 'columnDefinition="ENUM' . substr($row['Type'], 4) . '"'; $fileContent = str_replace('name="' . $row['Field'] . '", type="string"', 'name="' . $row['Field'] . '", type="string", ' . $definition, $fileContent); } } // Save changes file_put_contents($filePath, $fileContent . PHP_EOL); $output->write('[ <fg=green>Success: Created entity ' . $directory . DIRECTORY_SEPARATOR . $entity . '</fg=green> ]', true); }