protected function callPhing($taskName, $properties = array()) { $kernel = $this->application->getKernel(); $tmpDir = sys_get_temp_dir() . '/propel-gen'; $filesystem = new Filesystem(); $filesystem->remove($tmpDir); $filesystem->mkdirs($tmpDir); foreach ($kernel->getBundles() as $bundle) { if (is_dir($dir = $bundle->getPath() . '/Resources/config')) { $finder = new Finder(); $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir); $parts = explode(DIRECTORY_SEPARATOR, realpath($bundle->getPath())); $prefix = implode('.', array_slice($parts, 1, -2)); foreach ($schemas as $schema) { $tempSchema = md5($schema) . '_' . $schema->getBaseName(); $this->tempSchemas[$tempSchema] = array('bundle' => $bundle->getName(), 'basename' => $schema->getBaseName(), 'path' => $schema->getPathname()); $file = $tmpDir . DIRECTORY_SEPARATOR . $tempSchema; $filesystem->copy((string) $schema, $file); // the package needs to be set absolute // besides, the automated namespace to package conversion has not taken place yet // so it needs to be done manually $database = simplexml_load_file($file); if (isset($database['package'])) { $database['package'] = $prefix . '.' . $database['package']; } elseif (isset($database['namespace'])) { $database['package'] = $prefix . '.' . str_replace('\\', '.', $database['namespace']); } foreach ($database->table as $table) { if (isset($table['package'])) { $table['package'] = $prefix . '.' . $table['package']; } elseif (isset($table['namespace'])) { $table['package'] = $prefix . '.' . str_replace('\\', '.', $table['namespace']); } } file_put_contents($file, $database->asXML()); } } } $filesystem->touch($tmpDir . '/build.properties'); $args = array(); // $bufferPhingOutput = !$this->commandApplication->withTrace(); $properties = array_merge(array('propel.database' => 'mysql', 'project.dir' => $tmpDir, 'propel.output.dir' => $kernel->getRootDir() . '/propel', 'propel.php.dir' => '/', 'propel.packageObjectModel' => true), $properties); foreach ($properties as $key => $value) { $args[] = "-D{$key}={$value}"; } // Build file $args[] = '-f'; $args[] = realpath($kernel->getContainer()->getParameter('propel.path') . '/generator/build.xml'); /* // Logger if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT))) { $args[] = '-logger'; $args[] = 'phing.listener.AnsiColorLogger'; } // Add our listener to detect errors $args[] = '-listener'; $args[] = 'sfPhingListener'; */ // Add any arbitrary arguments last foreach ($this->additionalPhingArgs as $arg) { if (in_array($arg, array('verbose', 'debug'))) { $bufferPhingOutput = false; } $args[] = '-' . $arg; } $args[] = $taskName; // enable output buffering Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w'))); Phing::startup(); Phing::setProperty('phing.home', getenv('PHING_HOME')); // $this->logSection('propel', 'Running "'.$taskName.'" phing task'); $bufferPhingOutput = false; if ($bufferPhingOutput) { ob_start(); } $m = new Phing(); $m->execute($args); $m->runBuild(); if ($bufferPhingOutput) { ob_end_clean(); } print $bufferPhingOutput; chdir($kernel->getRootDir()); /* // any errors? $ret = true; if (sfPhingListener::hasErrors()) { $messages = array('Some problems occurred when executing the task:'); foreach (sfPhingListener::getExceptions() as $exception) { $messages[] = ''; $messages[] = preg_replace('/^.*build\-propel\.xml/', 'build-propel.xml', $exception->getMessage()); $messages[] = ''; } if (count(sfPhingListener::getErrors())) { $messages[] = 'If the exception message is not clear enough, read the output of the task for'; $messages[] = 'more information'; } $this->logBlock($messages, 'ERROR_LARGE'); $ret = false; } */ $ret = true; return $ret; }
/** * @see Command */ protected function execute(InputInterface $input, OutputInterface $output) { $bundleClass = null; $bundleDirs = $this->container->getKernelService()->getBundleDirs(); foreach ($this->container->getKernelService()->getBundles() as $bundle) { if (strpos(get_class($bundle), $input->getArgument('bundle')) !== false) { $tmp = dirname(str_replace('\\', '/', get_class($bundle))); $namespace = str_replace('/', '\\', dirname($tmp)); $class = basename($tmp); if (isset($bundleDirs[$namespace])) { $destPath = realpath($bundleDirs[$namespace]) . '/' . $class; $bundleClass = $class; break; } } } $type = $input->getArgument('mapping-type') ? $input->getArgument('mapping-type') : 'xml'; if ($type === 'annotation') { $destPath .= '/Entities'; } else { $destPath .= '/Resources/config/doctrine/metadata'; } $cme = new ClassMetadataExporter(); $exporter = $cme->getExporter($type); if ($type === 'annotation') { $entityGenerator = $this->getEntityGenerator(); $exporter->setEntityGenerator($entityGenerator); } $converter = new ConvertDoctrine1Schema($input->getArgument('d1-schema')); $metadata = $converter->getMetadata(); if ($metadata) { $output->writeln(sprintf('Converting Doctrine 1 schema "<info>%s</info>"', $input->getArgument('d1-schema'))); foreach ($metadata as $class) { $className = $class->name; $class->name = $namespace . '\\' . $bundleClass . '\\Entities\\' . $className; if ($type === 'annotation') { $path = $destPath . '/' . $className . '.php'; } else { $path = $destPath . '/' . str_replace('\\', '.', $class->name) . '.dcm.xml'; } $output->writeln(sprintf(' > writing <comment>%s</comment>', $path)); $code = $exporter->exportClassMetadata($class); if (!is_dir($dir = dirname($path))) { $filesystem = new Filesystem(); $filesystem->mkdirs($dir); } file_put_contents($path, $code); } } else { $output->writeln('Database does not have any mapping information.' . PHP_EOL, 'ERROR'); } }
/** * @throws \InvalidArgumentException When the bundle doesn't end with Bundle (Example: "Bundle\MySampleBundle") */ protected function execute(InputInterface $input, OutputInterface $output) { if (!preg_match('/Bundle$/', $bundle = $input->getArgument('bundle'))) { throw new \InvalidArgumentException('The bundle name must end with Bundle. Example: "Bundle\\MySampleBundle".'); } $dirs = $this->container->getKernelService()->getBundleDirs(); $tmp = str_replace('\\', '/', $bundle); $namespace = str_replace('/', '\\', dirname($tmp)); $bundle = basename($tmp); if (!isset($dirs[$namespace])) { throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle entity (%s not defined).', $namespace)); } $entity = $input->getArgument('entity'); $entityNamespace = $namespace . '\\' . $bundle . '\\Entities'; $fullEntityClassName = $entityNamespace . '\\' . $entity; $tmp = str_replace('\\', '/', $fullEntityClassName); $tmp = str_replace('/', '\\', dirname($tmp)); $className = basename($tmp); $mappingType = $input->getOption('mapping-type'); $mappingType = $mappingType ? $mappingType : 'xml'; $class = new ClassMetadataInfo($fullEntityClassName); $class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true)); $class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO); // Map the specified fields $fields = $input->getOption('fields'); $e = explode(' ', $fields); foreach ($e as $value) { $e = explode(':', $value); $name = $e[0]; $type = isset($e[1]) ? $e[1] : 'string'; preg_match_all('/(.*)\\((.*)\\)/', $type, $matches); $type = isset($matches[1][0]) ? $matches[1][0] : 'string'; $length = isset($matches[2][0]) ? $matches[2][0] : null; $class->mapField(array('fieldName' => $name, 'type' => $type, 'length' => $length)); } // Setup a new exporter for the mapping type specified $cme = new ClassMetadataExporter(); $exporter = $cme->getExporter($mappingType); if ($mappingType === 'annotation') { $path = $dirs[$namespace] . '/' . $bundle . '/Entities/' . str_replace($entityNamespace . '\\', null, $fullEntityClassName) . '.php'; $exporter->setEntityGenerator($this->getEntityGenerator()); } else { $path = $dirs[$namespace] . '/' . $bundle . '/Resources/config/doctrine/metadata/' . str_replace('\\', '.', $fullEntityClassName) . '.dcm.xml'; } $code = $exporter->exportClassMetadata($class); if (!is_dir($dir = dirname($path))) { $filesystem = new Filesystem(); $filesystem->mkdirs($dir); } $output->writeln(sprintf('Generating entity for "<info>%s</info>"', $bundle)); $output->writeln(sprintf(' > generating <comment>%s</comment>', $fullEntityClassName)); file_put_contents($path, $code); }