/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $metadatas = $em->getMetadataFactory()->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); // Process destination directory if (($destPath = $input->getArgument('dest-path')) === null) { $destPath = $em->getConfiguration()->getProxyDir(); } if (!is_dir($destPath)) { mkdir($destPath, 0777, true); } $destPath = realpath($destPath); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Proxies destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Proxies destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } if (count($metadatas)) { foreach ($metadatas as $metadata) { $output->write(sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL); } // Generating Proxies $em->getProxyFactory()->generateProxyClasses($metadatas, $destPath); // Outputting information message $output->write(PHP_EOL . sprintf('Proxy classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $metadatas = $em->getMetadataFactory()->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); // Process destination directory $destPath = realpath($input->getArgument('dest-path')); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } if (count($metadatas)) { $numRepositories = 0; $generator = new EntityRepositoryGenerator(); foreach ($metadatas as $metadata) { if ($metadata->customRepositoryClassName) { $output->write(sprintf('Processing repository "<info>%s</info>"', $metadata->customRepositoryClassName) . PHP_EOL); $generator->writeEntityRepositoryClass($metadata->customRepositoryClassName, $destPath); $numRepositories++; } } if ($numRepositories) { // Outputting information message $output->write(PHP_EOL . sprintf('Repository classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); } else { $output->write('No Repository classes were found to be processed.' . PHP_EOL); } } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getDropSchemaSql($metadatas); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { $output->write('Dropping database schema...' . PHP_EOL); $schemaTool->dropSchema($metadatas); $output->write('Database schema dropped successfully!' . PHP_EOL); } }
protected function executeSchemaCommand(InputInterface $input, OutputInterface $output, SchemaTool $schemaTool, array $metadatas) { // Defining if update is complete or not (--complete not defined means $saveMode = true) $saveMode = $input->getOption('complete') === true; if ($input->getOption('dump-sql') === true) { $sqls = $schemaTool->getUpdateSchemaSql($metadatas, $saveMode); $output->write(implode(';' . PHP_EOL, $sqls) . PHP_EOL); } else { $output->write('Updating database schema...' . PHP_EOL); $schemaTool->updateSchema($metadatas, $saveMode); $output->write('Database schema updated successfully!' . PHP_EOL); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $conn = $this->getHelper('db')->getConnection(); if (($fileNames = $input->getArgument('file')) !== null) { foreach ((array) $fileNames as $fileName) { $fileName = realpath($fileName); if (!file_exists($fileName)) { throw new \InvalidArgumentException(sprintf("SQL file '<info>%s</info>' does not exist.", $fileName)); } else { if (!is_readable($fileName)) { throw new \InvalidArgumentException(sprintf("SQL file '<info>%s</info>' does not have read permissions.", $fileName)); } } $output->write(sprintf("Processing file '<info>%s</info>'... ", $fileName)); $sql = file_get_contents($fileName); if ($conn instanceof \Doctrine\DBAL\Driver\PDOConnection) { // PDO Drivers try { $lines = 0; $stmt = $conn->prepare($sql); $stmt->execute(); do { // Required due to "MySQL has gone away!" issue $stmt->fetch(); $stmt->closeCursor(); $lines++; } while ($stmt->nextRowset()); $output->write(sprintf('%d statements executed!', $lines) . PHP_EOL); } catch (\PDOException $e) { $output->write('error!' . PHP_EOL); throw new \RuntimeException($e->getMessage(), $e->getCode(), $e); } } else { // Non-PDO Drivers (ie. OCI8 driver) $stmt = $conn->prepare($sql); $rs = $stmt->execute(); if ($rs) { $printer->writeln('OK!'); } else { $error = $stmt->errorInfo(); $output->write('error!' . PHP_EOL); throw new \RuntimeException($error[2], $error[0]); } $stmt->closeCursor(); } } } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $cacheDriver = $em->getConfiguration()->getQueryCacheImpl(); if (!$cacheDriver) { throw new \InvalidArgumentException('No Query cache driver is configured on given EntityManager.'); } $output->write('Clearing ALL Query cache entries' . PHP_EOL); $cacheIds = $cacheDriver->deleteAll(); if ($cacheIds) { foreach ($cacheIds as $cacheId) { $output->write(' - ' . $cacheId . PHP_EOL); } } else { $output->write('No entries to be deleted.' . PHP_EOL); } }
/** * @param InputInterface $input * @param OutputInterface $output */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $emHelper = $this->getHelper('em'); /* @var $em \Doctrine\ORM\EntityManager */ $em = $emHelper->getEntityManager(); $metadatas = $em->getMetadataFactory()->getAllMetadata(); if (!empty($metadatas)) { // Create SchemaTool $tool = new \Doctrine\ORM\Tools\SchemaTool($em); $updateSql = $tool->getUpdateSchemaSql($metadatas, false); if (count($updateSql) == 0) { $output->write("[Database] OK - Metadata schema exactly matches the database schema."); } else { $output->write("[Database] FAIL - There are differences between metadata and database schema."); } } else { $output->write("No metadata mappings found"); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $cacheDriver = $em->getConfiguration()->getMetadataCacheImpl(); if (!$cacheDriver) { throw new \InvalidArgumentException('No Metadata cache driver is configured on given EntityManager.'); } if ($cacheDriver instanceof \Doctrine\Common\Cache\ApcCache) { throw new \LogicException("Cannot clear APC Cache from Console, its shared in the Webserver memory and not accessible from the CLI."); } $output->write('Clearing ALL Metadata cache entries' . PHP_EOL); $cacheIds = $cacheDriver->deleteAll(); if ($cacheIds) { foreach ($cacheIds as $cacheId) { $output->write(' - ' . $cacheId . PHP_EOL); } } else { $output->write('No entries to be deleted.' . PHP_EOL); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $validator = new \Doctrine\ORM\Tools\SchemaValidator($em); $errors = $validator->validateMapping(); $exit = 0; if ($errors) { foreach ($errors as $className => $errorMessages) { $output->write("<error>[Mapping] FAIL - The entity-class '" . $className . "' mapping is invalid:</error>\n"); foreach ($errorMessages as $errorMessage) { $output->write('* ' . $errorMessage . "\n"); } $output->write("\n"); } $exit += 1; } else { $output->write('<info>[Mapping] OK - The mapping files are correct.</info>' . "\n"); } if (!$validator->schemaInSyncWithMetadata()) { $output->write('<error>[Database] FAIL - The database schema is not in sync with the current mapping file.</error>' . "\n"); $exit += 2; } else { $output->write('<info>[Database] OK - The database schema is in sync with the mapping files.</info>' . "\n"); } exit($exit); }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $cmf = new DisconnectedClassMetadataFactory($em); $metadatas = $cmf->getAllMetadata(); $metadatas = MetadataFilter::filter($metadatas, $input->getOption('filter')); // Process destination directory $destPath = realpath($input->getArgument('dest-path')); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Entities destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } if (count($metadatas)) { // Create EntityGenerator $entityGenerator = new EntityGenerator(); $entityGenerator->setGenerateAnnotations($input->getOption('generate-annotations')); $entityGenerator->setGenerateStubMethods($input->getOption('generate-methods')); $entityGenerator->setRegenerateEntityIfExists($input->getOption('regenerate-entities')); $entityGenerator->setUpdateEntityIfExists($input->getOption('update-entities')); $entityGenerator->setNumSpaces($input->getOption('num-spaces')); if (($extend = $input->getOption('extend')) !== null) { $entityGenerator->setClassToExtend($extend); } foreach ($metadatas as $metadata) { $output->write(sprintf('Processing entity "<info>%s</info>"', $metadata->name) . PHP_EOL); } // Generating Entities $entityGenerator->generate($metadatas, $destPath); // Outputting information message $output->write(PHP_EOL . sprintf('Entity classes generated to "<info>%s</INFO>"', $destPath) . PHP_EOL); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
/** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $kernel = $this->container->getKernelService(); $container = new Builder($kernel->getDefaultParameters()); $configuration = new BuilderConfiguration(); foreach ($kernel->getBundles() as $bundle) { $configuration->merge($bundle->buildContainer($container)); } $configuration->merge($kernel->registerContainerConfiguration()); $container->merge($configuration); $kernel->optimizeContainer($container); $container->setService('kernel', $kernel); $dumper = new GraphvizDumper($container); $output->write($dumper->dump(), Output::OUTPUT_RAW); }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); $error = false; try { $em->getConfiguration()->ensureProductionSettings(); if ($input->getOption('complete') !== null) { $em->getConnection()->connect(); } } catch (\Exception $e) { $error = true; $output->writeln('<error>' . $e->getMessage() . '</error>'); } if ($error === false) { $output->write('<info>Environment is correctly configured for production.</info>' . PHP_EOL); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); if ($input->getOption('from-database') === true) { $em->getConfiguration()->setMetadataDriverImpl(new \Doctrine\ORM\Mapping\Driver\DatabaseDriver($em->getConnection()->getSchemaManager())); } $cmf = new DisconnectedClassMetadataFactory($em); $metadata = $cmf->getAllMetadata(); $metadata = MetadataFilter::filter($metadata, $input->getOption('filter')); // Process destination directory if (!is_dir($destPath = $input->getArgument('dest-path'))) { mkdir($destPath, 0777, true); } $destPath = realpath($destPath); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Mapping destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } $toType = strtolower($input->getArgument('to-type')); $cme = new ClassMetadataExporter(); $exporter = $cme->getExporter($toType, $destPath); if ($toType == 'annotation') { $entityGenerator = new EntityGenerator(); $exporter->setEntityGenerator($entityGenerator); $entityGenerator->setNumSpaces($input->getOption('num-spaces')); if (($extend = $input->getOption('extend')) !== null) { $entityGenerator->setClassToExtend($extend); } } if (count($metadata)) { foreach ($metadata as $class) { $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL); } $exporter->setMetadata($metadata); $exporter->export(); $output->write(PHP_EOL . sprintf('Exporting "<info>%s</info>" mapping information to "<info>%s</info>"' . PHP_EOL, $toType, $destPath)); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
/** * @see Console\Command\Command */ protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output) { $em = $this->getHelper('em')->getEntityManager(); // Process source directories $fromPaths = array_merge(array($input->getArgument('from-path')), $input->getOption('from')); foreach ($fromPaths as &$dirName) { $dirName = realpath($dirName); if (!file_exists($dirName)) { throw new \InvalidArgumentException(sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)); } else { if (!is_readable($dirName)) { throw new \InvalidArgumentException(sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)); } } } // Process destination directory $destPath = realpath($input->getArgument('dest-path')); if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } $toType = $input->getArgument('to-type'); $cme = new ClassMetadataExporter(); $exporter = $cme->getExporter($toType, $destPath); if (strtolower($toType) === 'annotation') { $entityGenerator = new EntityGenerator(); $exporter->setEntityGenerator($entityGenerator); $entityGenerator->setNumSpaces($input->getOption('num-spaces')); if (($extend = $input->getOption('extend')) !== null) { $entityGenerator->setClassToExtend($extend); } } $converter = new ConvertDoctrine1Schema($fromPaths); $metadata = $converter->getMetadata(); if ($metadata) { $output->write(PHP_EOL); foreach ($metadata as $class) { $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL); } $exporter->setMetadata($metadata); $exporter->export(); $output->write(PHP_EOL . sprintf('Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath)); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
private function _printDeleted(Console\Output\OutputInterface $output, array $items) { if ($items) { foreach ($items as $item) { $output->write(' - ' . $item . PHP_EOL); } } else { $output->write('No entries to be deleted.' . PHP_EOL); } }
/** * @throws \InvalidArgumentException When route does not exist */ protected function outputRoute(OutputInterface $output, $routes, $name) { $output->writeln($this->getHelper('formatter')->formatSection('router', sprintf('Route "%s"', $name))); if (!isset($routes[$name])) { throw new \InvalidArgumentException(sprintf('The route "%s" does not exist.', $name)); } $route = $routes[$name]; $output->writeln(sprintf('<comment>Name</comment> %s', $name)); $output->writeln(sprintf('<comment>Pattern</comment> %s', $route->getPattern())); $output->writeln(sprintf('<comment>Class</comment> %s', get_class($route))); $defaults = ''; $d = $route->getDefaults(); ksort($d); foreach ($d as $name => $value) { $defaults .= ($defaults ? "\n" . str_repeat(' ', 13) : '') . $name . ': ' . $this->formatValue($value); } $output->writeln(sprintf('<comment>Defaults</comment> %s', $defaults)); $requirements = ''; $r = $route->getRequirements(); ksort($r); foreach ($r as $name => $value) { $requirements .= ($requirements ? "\n" . str_repeat(' ', 13) : '') . $name . ': ' . $this->formatValue($value); } $output->writeln(sprintf('<comment>Requirements</comment> %s', $requirements)); $options = ''; $o = $route->getOptions(); ksort($o); foreach ($o as $name => $value) { $options .= ($options ? "\n" . str_repeat(' ', 13) : '') . $name . ': ' . $this->formatValue($value); } $output->writeln(sprintf('<comment>Options</comment> %s', $options)); $output->write('<comment>Regex</comment> '); $output->writeln(preg_replace('/^ /', '', preg_replace('/^/m', ' ', $route->getRegex())), Output::OUTPUT_RAW); $tokens = ''; foreach ($route->getTokens() as $token) { if (!$tokens) { $tokens = $this->displayToken($token); } else { $tokens .= "\n" . str_repeat(' ', 13) . $this->displayToken($token); } } $output->writeln(sprintf('<comment>Tokens</comment> %s', $tokens)); }
/** * @param \Doctrine\ORM\EntityManager $em * @param array $fromPaths * @param string $destPath * @param string $toType * @param int $numSpaces * @param string|null $extend * @param Console\Output\OutputInterface $output */ public function convertDoctrine1Schema($em, $fromPaths, $destPath, $toType, $numSpaces, $extend, $output) { foreach ($fromPaths as &$dirName) { $dirName = realpath($dirName); if (!file_exists($dirName)) { throw new \InvalidArgumentException(sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not exist.", $dirName)); } else { if (!is_readable($dirName)) { throw new \InvalidArgumentException(sprintf("Doctrine 1.X schema directory '<info>%s</info>' does not have read permissions.", $dirName)); } } } if (!file_exists($destPath)) { throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not exist.", $destPath)); } else { if (!is_writable($destPath)) { throw new \InvalidArgumentException(sprintf("Doctrine 2.X mapping destination directory '<info>%s</info>' does not have write permissions.", $destPath)); } } $cme = $this->getMetadataExporter(); $exporter = $cme->getExporter($toType, $destPath); if (strtolower($toType) === 'annotation') { $entityGenerator = $this->getEntityGenerator(); $exporter->setEntityGenerator($entityGenerator); $entityGenerator->setNumSpaces($numSpaces); if ($extend !== null) { $entityGenerator->setClassToExtend($extend); } } $converter = new ConvertDoctrine1Schema($fromPaths); $metadata = $converter->getMetadata(); if ($metadata) { $output->write(PHP_EOL); foreach ($metadata as $class) { $output->write(sprintf('Processing entity "<info>%s</info>"', $class->name) . PHP_EOL); } $exporter->setMetadata($metadata); $exporter->export(); $output->write(PHP_EOL . sprintf('Converting Doctrine 1.X schema to "<info>%s</info>" mapping type in "<info>%s</info>"', $toType, $destPath)); } else { $output->write('No Metadata Classes to process.' . PHP_EOL); } }
/** * Renders a catched exception. * * @param Exception $e An exception instance * @param OutputInterface $output An OutputInterface instance */ public function renderException($e, $output) { $strlen = function ($string) { return function_exists('mb_strlen') ? mb_strlen($string) : strlen($string); }; $title = sprintf(' [%s] ', get_class($e)); $len = $strlen($title); $lines = array(); foreach (explode("\n", $e->getMessage()) as $line) { $lines[] = sprintf(' %s ', $line); $len = max($strlen($line) + 4, $len); } $messages = array(str_repeat(' ', $len), $title . str_repeat(' ', $len - $strlen($title))); foreach ($lines as $line) { $messages[] = $line . str_repeat(' ', $len - $strlen($line)); } $messages[] = str_repeat(' ', $len); if (null !== $this->runningCommand) { // $output->writeln(sprintf('<info>%s</info>', sprintf($this->runningCommand->getSynopsis(), $this->getName()))); // $output->writeln("\n"); $output->write($this->runningCommand->asText()); } else { $output->writeln("\n"); foreach ($messages as $message) { $output->writeln('<error>' . $message . '</error>'); } $output->writeln("\n"); } if (Output::VERBOSITY_VERBOSE === $output->getVerbosity()) { $output->writeln('</comment>Exception trace:</comment>'); // exception related properties $trace = $e->getTrace(); array_unshift($trace, array('function' => '', 'file' => $e->getFile() != null ? $e->getFile() : 'n/a', 'line' => $e->getLine() != null ? $e->getLine() : 'n/a', 'args' => array())); for ($i = 0, $count = count($trace); $i < $count; $i++) { $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : ''; $function = $trace[$i]['function']; $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a'; $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a'; $output->writeln(sprintf(' %s%s%s() at <info>%s:%s</info>', $class, $type, $function, $file, $line)); } $output->writeln("\n"); } }