/** * Load SQL fixtures * * @param \Symfony\Component\Console\Input\InputInterface $input * @param \Symfony\Component\Console\Output\OutputInterface $output * @return void */ protected function loadSqlFixtures(InputInterface $input, OutputInterface $output) { $finder = new Finder(); $tmpdir = $this->getApplication()->getKernel()->getRootDir() . '/cache/propel'; $datas = $finder->name('*.sql')->in($this->absoluteFixturesPath); $this->prepareCache($tmpdir); list($name, $defaultConfig) = $this->getConnection($input, $output); // Create a "sqldb.map" file $sqldbContent = ''; foreach ($datas as $data) { $output->writeln(sprintf('<info>Loading SQL fixtures from</info> <comment>%s</comment>.', $data)); $sqldbContent .= $data->getFilename() . '=' . $name . PHP_EOL; $this->filesystem->copy($data, $tmpdir . '/fixtures/' . $data->getFilename(), true); } if ('' === $sqldbContent) { return -1; } $sqldbFile = $tmpdir . '/fixtures/sqldb.map'; file_put_contents($sqldbFile, $sqldbContent); if (!$this->insertSql($defaultConfig, $tmpdir . '/fixtures', $tmpdir, $output)) { return -1; } $this->filesystem->remove($tmpdir); return 0; }
/** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { list($name, $defaultConfig) = $this->getConnection($input, $output); $this->callPhing('reverse', array('propel.project' => $name, 'propel.database.url' => $defaultConfig['connection']['dsn'], 'propel.database.database' => $defaultConfig['adapter'], 'propel.database.user' => $defaultConfig['connection']['user'], 'propel.database.password' => $defaultConfig['connection']['password'])); $filesystem = new Filesystem(); $dest = $this->getApplication()->getKernel()->getRootDir() . '/propel/' . $name . '_reversed_schema.xml'; $filesystem->copy($this->getTmpDir() . '/schema.xml', $dest); $output->writeln(sprintf('New generated schema is <comment>%s</comment>.', $dest)); }
/** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { list($name, $defaultConfig) = $this->getConnection($input, $output); $this->callPhing('datadump', array('propel.database.url' => $defaultConfig['connection']['dsn'], 'propel.database.database' => $defaultConfig['adapter'], 'propel.database.user' => $defaultConfig['connection']['user'], 'propel.database.password' => $defaultConfig['connection']['password'], 'propel.schema.dir' => $this->getApplication()->getKernel()->getRootDir() . '/propel/schema/')); $finder = new Finder(); $filesystem = new Filesystem(); $datas = $finder->name('*_data.xml')->in($this->getTmpDir()); foreach ($datas as $data) { $dest = $this->getApplication()->getKernel()->getRootDir() . self::$destPath . '/xml/' . $data->getFilename(); $filesystem->copy((string) $data, $dest); $filesystem->remove($data); $output->writeln(sprintf('Wrote dumped data in "<info>%s</info>".', $dest)); } if (count($datas) <= 0) { $output->writeln('No new dumped files.'); } }
/** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { $container = $this->getApplication()->getKernel()->getContainer(); $propelConfiguration = $container->get('propel.configuration'); $name = $input->getOption('connection') ? $input->getOption('connection') : $container->getParameter('propel.dbal.default_connection'); if (isset($propelConfiguration['datasources'][$name])) { $defaultConfig = $propelConfiguration['datasources'][$name]; } else { throw new \InvalidArgumentException(sprintf('Connection named %s doesn\'t exist', $name)); } $output->writeln(sprintf('<info>Generate XML schema from connection named <comment>%s</comment></info>', $name)); $this->callPhing('reverse', array('propel.project' => $name, 'propel.database.url' => $defaultConfig['connection']['dsn'], 'propel.database.database' => $defaultConfig['adapter'], 'propel.database.user' => $defaultConfig['connection']['user'], 'propel.database.password' => $defaultConfig['connection']['password'])); $filesystem = new Filesystem(); $dest = $this->getApplication()->getKernel()->getRootDir() . '/propel/' . $name . '_reversed_schema.xml'; $filesystem->copy($this->getTmpDir() . '/schema.xml', $dest); $output->writeln(sprintf('New generated schema is "<info>%s</info>".', $dest)); }
/** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { $rootDir = $this->getApplication()->getKernel()->getRootDir(); $schemaDir = $rootDir . '/propel/schema/'; $sqlDir = $rootDir . '/propel/sql/'; $xmlDumpDir = $rootDir . '/propel/dump/xml/'; $filesystem = new Filesystem(); $finder = new Finder(); foreach ($finder->name('*_data.xml')->in($xmlDumpDir) as $data) { $filesystem->copy((string) $data, $schemaDir . $data->getFilename()); } $this->callPhing('datasql', array('propel.sql.dir' => $sqlDir, 'propel.schema.dir' => $schemaDir)); $finder = new Finder(); foreach ($finder->name('*_data.xml')->in($schemaDir) as $data) { $filesystem->remove($data); } $this->summary($output, 'propel-data-sql'); $output->writeln(sprintf('SQL from XML data dump file is in <comment>%s</comment>.', $sqlDir)); }
/** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { $this->writeSection($output, '[Propel] You are running the command: propel:reverse'); if ($input->getOption('verbose')) { $this->additionalPhingArgs[] = 'verbose'; } list($name, $defaultConfig) = $this->getConnection($input, $output); $ret = $this->callPhing('reverse', array('propel.project' => $name, 'propel.database.url' => $defaultConfig['connection']['dsn'], 'propel.database.database' => $defaultConfig['adapter'], 'propel.database.user' => $defaultConfig['connection']['user'], 'propel.database.password' => isset($defaultConfig['connection']['password']) ? $defaultConfig['connection']['password'] : '')); if (true === $ret) { $filesystem = new Filesystem(); $generated = $this->getCacheDir() . '/schema.xml'; $filename = $name . '_reversed_schema.xml'; $destFile = $this->getApplication()->getKernel()->getRootDir() . '/propel/generated-schemas/' . $filename; if (file_exists($generated)) { $filesystem->copy($generated, $destFile); $output->writeln(array('', sprintf('>> <info>File+</info> %s', $destFile))); } else { $output->writeln(array('', 'No generated files.')); } } else { $this->writeTaskError($output, 'reverse'); } }
/** * @see Command * * @throws \InvalidArgumentException When the target directory does not exist */ protected function execute(InputInterface $input, OutputInterface $output) { $container = $this->getApplication()->getKernel()->getContainer(); $propelConfiguration = $container->get('propel.configuration'); $name = $input->getOption('connection') ? $input->getOption('connection') : $container->getParameter('propel.dbal.default_connection'); if (isset($propelConfiguration['datasources'][$name])) { $defaultConfig = $propelConfiguration['datasources'][$name]; } else { throw new \InvalidArgumentException(sprintf('Connection named %s doesn\'t exist', $name)); } $output->writeln(sprintf('<info>Generate XML schema from connection named <comment>%s</comment></info>', $name)); $this->callPhing('datadump', array('propel.database.url' => $defaultConfig['connection']['dsn'], 'propel.database.database' => $defaultConfig['adapter'], 'propel.database.user' => $defaultConfig['connection']['user'], 'propel.database.password' => $defaultConfig['connection']['password'], 'propel.schema.dir' => $this->getApplication()->getKernel()->getRootDir() . '/propel/schema/')); $finder = new Finder(); $filesystem = new Filesystem(); $datas = $finder->name('*_data.xml')->in($this->getTmpDir()); foreach ($datas as $data) { $dest = $this->getApplication()->getKernel()->getRootDir() . self::$destPath . '/xml/' . $data->getFilename(); $filesystem->copy((string) $data, $dest); $output->writeln(sprintf('Wrote dumped data in "<info>%s</info>".', $dest)); } if (count($datas) <= 0) { $output->writeln('No new dumped files.'); } }
/** * Call a Phing task. * * @param string $taskName A Propel task name. * @param array $properties An array of properties to pass to Phing. */ protected function callPhing($taskName, $properties = array()) { $kernel = $this->getApplication()->getKernel(); $filesystem = new Filesystem(); if (isset($properties['propel.schema.dir'])) { $this->tmpDir = $properties['propel.schema.dir']; } else { $this->tmpDir = sys_get_temp_dir() . '/propel-gen'; $filesystem->remove($this->tmpDir); $filesystem->mkdir($this->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())); $length = count(explode('\\', $bundle->getNamespace())) * -1; $prefix = implode('.', array_slice($parts, 1, $length)); foreach ($schemas as $schema) { $tempSchema = md5($schema) . '_' . $schema->getBaseName(); $this->tempSchemas[$tempSchema] = array('bundle' => $bundle->getName(), 'basename' => $schema->getBaseName(), 'path' => $schema->getPathname()); $file = $this->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']); } else { throw new \RuntimeException(sprintf('Please define a `package` attribute or a `namespace` attribute for schema `%s`', $schema->getBaseName())); } 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']); } else { $table['package'] = $database['package']; } } file_put_contents($file, $database->asXML()); } } } // build.properties $this->buildPropertiesFile = $kernel->getRootDir() . '/config/propel.ini'; $filesystem->touch($this->buildPropertiesFile); $filesystem->copy($this->buildPropertiesFile, $this->tmpDir . '/build.properties'); // Required by the Phing task $this->createBuildTimeFile($this->tmpDir . '/buildtime-conf.xml'); $args = array(); $properties = array_merge(array('propel.database' => 'mysql', 'project.dir' => $this->tmpDir, 'propel.output.dir' => $kernel->getRootDir() . '/propel', 'propel.php.dir' => '/', 'propel.packageObjectModel' => true), $properties); $properties = array_merge($properties, $this->getContainer()->get('propel.build_properties')->getProperties()); foreach ($properties as $key => $value) { $args[] = "-D{$key}={$value}"; } // Build file $args[] = '-f'; $args[] = realpath($this->getContainer()->getParameter('propel.path') . '/generator/build.xml'); $bufferPhingOutput = !$this->getContainer()->getParameter('kernel.debug'); // 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::setErrorStream(new \OutputStream(fopen('php://output', 'w'))); Phing::startup(); Phing::setProperty('phing.home', getenv('PHING_HOME')); ob_start(); $phing = new Phing(); $returnStatus = true; // optimistic way try { $phing->execute($args); $phing->runBuild(); $this->buffer = ob_get_contents(); $returnStatus = false !== preg_match('#failed. Aborting.#', $this->buffer); } catch (Exception $e) { $returnStatus = false; } if ($bufferPhingOutput) { ob_end_clean(); } else { ob_end_flush(); } chdir($kernel->getRootDir()); return $returnStatus; }
/** * Create a 'build.properties' file. * * @param KernelInterface $kernel The application kernel. * @param string $file Should be 'build.properties'. */ protected function createBuildPropertiesFile(KernelInterface $kernel, $file) { $filesystem = new Filesystem(); $buildPropertiesFile = $kernel->getRootDir() . '/config/propel.ini'; if (file_exists($buildPropertiesFile)) { $filesystem->copy($buildPropertiesFile, $file); } else { $filesystem->touch($file); } }