123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252 |
- <?php
- namespace Symfony\Framework\PropelBundle\Command;
- use Symfony\Components\Console\Command\Command;
- use Symfony\Components\Console\Input\InputArgument;
- use Symfony\Components\Console\Input\InputOption;
- use Symfony\Components\Console\Input\InputInterface;
- use Symfony\Components\Console\Output\OutputInterface;
- use Symfony\Components\Console\Output\Output;
- use Symfony\Framework\WebBundle\Util\Filesystem;
- use Symfony\Components\Finder\Finder;
- /*
- * This file is part of the Symfony framework.
- *
- * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
- *
- * This source file is subject to the MIT license that is bundled
- * with this source code in the file LICENSE.
- */
- /**
- * BuildCommand.
- *
- * @package Symfony
- * @subpackage Framework_PropelBundle
- * @author Fabien Potencier <fabien.potencier@symfony-project.com>
- */
- class BuildCommand extends Command
- {
- protected $additionalPhingArgs = array();
- /**
- * @see Command
- */
- protected function configure()
- {
- $this
- ->setDefinition(array(
- new InputOption('--classes', '', InputOption::PARAMETER_NONE, 'Build all classes'),
- ))
- ->setName('propel:build')
- ;
- }
- /**
- * @see Command
- *
- * @throws \InvalidArgumentException When the target directory does not exist
- */
- protected function execute(InputInterface $input, OutputInterface $output)
- {
- $this->callPhing('om');
- foreach ($this->application->getKernel()->getBundles() as $bundle) {
- $map = array();
- if (is_dir($dir = $bundle->getPath().'/Model/map')) {
- $finder = new Finder();
- $files = $finder->files()->name('*TableMap.php')->followLinks()->in($dir);
- foreach ($files as $file) {
- $class = substr($file->getBasename(), 0, -12);
- $map = array_merge($map, array(
- $class => 'Model/'.$class.'.php',
- $class.'Peer' => 'Model/'.$class.'Peer.php',
- $class.'Query' => 'Model/'.$class.'Query.php',
- 'Base'.$class => 'Model/om/Base'.$class.'.php',
- 'Base'.$class.'Peer' => 'Model/om/Base'.$class.'Peer.php',
- 'Base'.$class.'Query' => 'Model/om/Base'.$class.'Query.php',
- $class.'TableMap' => 'Model/map/'.$class.'TableMap.php',
- ));
- }
- }
- if (!is_dir($bundle->getPath().'/Resources/config'))
- {
- mkdir($bundle->getPath().'/Resources/config', null, true);
- }
- if ($map)
- {
- file_put_contents($bundle->getPath().'/Resources/config/classmap.php', '<?php return '.var_export($map, true).';');
- }
- }
- unlink($this->application->getKernel()->getCacheDir().'/propel_autoload.php');
- }
- 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) {
- $filesystem->copy((string) $schema, $file = $tmpDir.DIRECTORY_SEPARATOR.md5($schema).'_'.$schema->getBaseName());
- $content = file_get_contents($file);
- $content = preg_replace_callback('/package\s*=\s*"(.*?)"/', function ($matches) use ($prefix) {
- return sprintf('package="%s"', $prefix.'.'.$matches[1]);
- }, $content);
- file_put_contents($file, $content);
- }
- }
- }
- $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' => '/',
- ), $properties);
- foreach ($properties as $key => $value) {
- $args[] = "-D$key=$value";
- }
- // Build file
- $args[] = '-f';
- $args[] = realpath($kernel->getContainer()->getParameter('propel.generator.path').DIRECTORY_SEPARATOR.'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;
- }
- protected function getPhingPropertiesForConnection($databaseManager, $connection)
- {
- $database = $databaseManager->getDatabase($connection);
- return array(
- 'propel.database' => $database->getParameter('phptype'),
- 'propel.database.driver' => $database->getParameter('phptype'),
- 'propel.database.url' => $database->getParameter('dsn'),
- 'propel.database.user' => $database->getParameter('username'),
- 'propel.database.password' => $database->getParameter('password'),
- 'propel.database.encoding' => $database->getParameter('encoding'),
- );
- }
- protected function getProperties($file)
- {
- $properties = array();
- if (false === $lines = @file($file)) {
- throw new sfCommandException('Unable to parse contents of the "sqldb.map" file.');
- }
- foreach ($lines as $line) {
- $line = trim($line);
- if ('' == $line) {
- continue;
- }
- if (in_array($line[0], array('#', ';'))) {
- continue;
- }
- $pos = strpos($line, '=');
- $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
- }
- return $properties;
- }
- }
|