|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Symfony\Framework\PropelBundle\Command; |
| 4 | + |
| 5 | +use Symfony\Components\Console\Command\Command; |
| 6 | +use Symfony\Components\Console\Input\InputArgument; |
| 7 | +use Symfony\Components\Console\Input\InputOption; |
| 8 | +use Symfony\Components\Console\Input\InputInterface; |
| 9 | +use Symfony\Components\Console\Output\OutputInterface; |
| 10 | +use Symfony\Components\Console\Output\Output; |
| 11 | +use Symfony\Framework\WebBundle\Util\Filesystem; |
| 12 | +use Symfony\Components\Finder\Finder; |
| 13 | + |
| 14 | +/* |
| 15 | + * This file is part of the Symfony framework. |
| 16 | + * |
| 17 | + * (c) Fabien Potencier <fabien.potencier@symfony-project.com> |
| 18 | + * |
| 19 | + * This source file is subject to the MIT license that is bundled |
| 20 | + * with this source code in the file LICENSE. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * BuildCommand. |
| 25 | + * |
| 26 | + * @package Symfony |
| 27 | + * @subpackage Framework_PropelBundle |
| 28 | + * @author Fabien Potencier <fabien.potencier@symfony-project.com> |
| 29 | + */ |
| 30 | +class BuildCommand extends Command |
| 31 | +{ |
| 32 | + protected $additionalPhingArgs = array(); |
| 33 | + |
| 34 | + /** |
| 35 | + * @see Command |
| 36 | + */ |
| 37 | + protected function configure() |
| 38 | + { |
| 39 | + $this |
| 40 | + ->setDefinition(array( |
| 41 | + new InputOption('--classes', '', InputOption::PARAMETER_NONE, 'Build all classes'), |
| 42 | + )) |
| 43 | + ->setName('propel:build') |
| 44 | + ; |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * @see Command |
| 49 | + * |
| 50 | + * @throws \InvalidArgumentException When the target directory does not exist |
| 51 | + */ |
| 52 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 53 | + { |
| 54 | + return $this->callPhing('om'); |
| 55 | + |
| 56 | + if (!is_dir($input->getArgument('target'))) { |
| 57 | + throw new \InvalidArgumentException(sprintf('The target directory "%s" does not exist.', $input->getArgument('target'))); |
| 58 | + } |
| 59 | + |
| 60 | + $filesystem = new Filesystem(); |
| 61 | + |
| 62 | + $dirs = $this->container->getKernelService()->getBundleDirs(); |
| 63 | + foreach ($this->container->getKernelService()->getBundles() as $bundle) { |
| 64 | + $tmp = dirname(str_replace('\\', '/', get_class($bundle))); |
| 65 | + $namespace = str_replace('/', '\\', dirname($tmp)); |
| 66 | + $class = basename($tmp); |
| 67 | + |
| 68 | + if (isset($dirs[$namespace]) && is_dir($originDir = $dirs[$namespace].'/'.$class.'/Resources/public')) { |
| 69 | + $output->writeln(sprintf('Installing assets for <comment>%s\\%s</comment>', $namespace, $class)); |
| 70 | + |
| 71 | + $targetDir = $input->getArgument('target').'/bundles/'.preg_replace('/bundle$/', '', strtolower($class)); |
| 72 | + |
| 73 | + $filesystem->remove($targetDir); |
| 74 | + mkdir($targetDir, 0755, true); |
| 75 | + $filesystem->mirror($originDir, $targetDir); |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + protected function callPhing($taskName, $properties = array()) |
| 81 | + { |
| 82 | + $kernel = $this->application->getKernel(); |
| 83 | + |
| 84 | + $tmpDir = sys_get_temp_dir().'/propel-gen'; |
| 85 | + $filesystem = new Filesystem(); |
| 86 | + $filesystem->remove($tmpDir); |
| 87 | + $filesystem->mkdirs($tmpDir); |
| 88 | + |
| 89 | + $bundleDirs = $kernel->getBundleDirs(); |
| 90 | + foreach ($kernel->getBundles() as $bundle) { |
| 91 | + $tmp = dirname(str_replace('\\', '/', get_class($bundle))); |
| 92 | + $namespace = str_replace('/', '\\', dirname($tmp)); |
| 93 | + $class = basename($tmp); |
| 94 | + |
| 95 | + if (isset($bundleDirs[$namespace]) && is_dir($dir = $bundleDirs[$namespace].'/'.$class.'/Resources/config')) { |
| 96 | + $finder = new Finder(); |
| 97 | + $schemas = $finder->files()->name('*schema.xml')->followLinks()->in($dir); |
| 98 | + |
| 99 | + $parts = explode(DIRECTORY_SEPARATOR, realpath($bundleDirs[$namespace])); |
| 100 | + $prefix = implode('.', array_slice($parts, 1, -1)); |
| 101 | + |
| 102 | + foreach ($schemas as $schema) { |
| 103 | + $filesystem->copy((string) $schema, $file = $tmpDir.DIRECTORY_SEPARATOR.md5($schema).'_'.$schema->getBaseName()); |
| 104 | + |
| 105 | + $content = file_get_contents($file); |
| 106 | + $content = preg_replace_callback('/package\s*=\s*"(.*?)"/', function ($matches) use ($prefix) { |
| 107 | + return sprintf('package="%s"', $prefix.'.'.$matches[1]); |
| 108 | + }, $content); |
| 109 | + |
| 110 | + file_put_contents($file, $content); |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + $filesystem->touch($tmpDir.'/build.properties'); |
| 116 | + |
| 117 | + $args = array(); |
| 118 | +// $bufferPhingOutput = !$this->commandApplication->withTrace(); |
| 119 | + |
| 120 | + $properties = array_merge(array( |
| 121 | + 'propel.database' => 'mysql', |
| 122 | + 'project.dir' => $tmpDir, |
| 123 | + 'propel.output.dir' => $kernel->getRootDir().'/propel', |
| 124 | + 'propel.php.dir' => '/', |
| 125 | + ), $properties); |
| 126 | + foreach ($properties as $key => $value) { |
| 127 | + $args[] = "-D$key=$value"; |
| 128 | + } |
| 129 | + |
| 130 | + // Build file |
| 131 | + $args[] = '-f'; |
| 132 | + $args[] = realpath($kernel->getContainer()->getParameter('propel.generator.path').DIRECTORY_SEPARATOR.'build.xml'); |
| 133 | + |
| 134 | +/* |
| 135 | + // Logger |
| 136 | + if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT))) { |
| 137 | + $args[] = '-logger'; |
| 138 | + $args[] = 'phing.listener.AnsiColorLogger'; |
| 139 | + } |
| 140 | +
|
| 141 | + // Add our listener to detect errors |
| 142 | + $args[] = '-listener'; |
| 143 | + $args[] = 'sfPhingListener'; |
| 144 | +*/ |
| 145 | + // Add any arbitrary arguments last |
| 146 | + foreach ($this->additionalPhingArgs as $arg) { |
| 147 | + if (in_array($arg, array('verbose', 'debug'))) { |
| 148 | + $bufferPhingOutput = false; |
| 149 | + } |
| 150 | + |
| 151 | + $args[] = '-'.$arg; |
| 152 | + } |
| 153 | + |
| 154 | + $args[] = $taskName; |
| 155 | + |
| 156 | + // enable output buffering |
| 157 | + Phing::setOutputStream(new \OutputStream(fopen('php://output', 'w'))); |
| 158 | + Phing::startup(); |
| 159 | + Phing::setProperty('phing.home', getenv('PHING_HOME')); |
| 160 | + |
| 161 | +// $this->logSection('propel', 'Running "'.$taskName.'" phing task'); |
| 162 | + |
| 163 | + $bufferPhingOutput = false; |
| 164 | + if ($bufferPhingOutput) { |
| 165 | + ob_start(); |
| 166 | + } |
| 167 | + |
| 168 | + $m = new Phing(); |
| 169 | + $m->execute($args); |
| 170 | + $m->runBuild(); |
| 171 | + |
| 172 | + if ($bufferPhingOutput) { |
| 173 | + ob_end_clean(); |
| 174 | + } |
| 175 | + print $bufferPhingOutput; |
| 176 | + chdir($kernel->getRootDir()); |
| 177 | +/* |
| 178 | + // any errors? |
| 179 | + $ret = true; |
| 180 | + if (sfPhingListener::hasErrors()) |
| 181 | + { |
| 182 | + $messages = array('Some problems occurred when executing the task:'); |
| 183 | +
|
| 184 | + foreach (sfPhingListener::getExceptions() as $exception) |
| 185 | + { |
| 186 | + $messages[] = ''; |
| 187 | + $messages[] = preg_replace('/^.*build\-propel\.xml/', 'build-propel.xml', $exception->getMessage()); |
| 188 | + $messages[] = ''; |
| 189 | + } |
| 190 | +
|
| 191 | + if (count(sfPhingListener::getErrors())) |
| 192 | + { |
| 193 | + $messages[] = 'If the exception message is not clear enough, read the output of the task for'; |
| 194 | + $messages[] = 'more information'; |
| 195 | + } |
| 196 | +
|
| 197 | + $this->logBlock($messages, 'ERROR_LARGE'); |
| 198 | +
|
| 199 | + $ret = false; |
| 200 | + } |
| 201 | +*/ |
| 202 | + |
| 203 | + $ret = true; |
| 204 | + return $ret; |
| 205 | + } |
| 206 | + |
| 207 | + protected function getPhingPropertiesForConnection($databaseManager, $connection) |
| 208 | + { |
| 209 | + $database = $databaseManager->getDatabase($connection); |
| 210 | + |
| 211 | + return array( |
| 212 | + 'propel.database' => $database->getParameter('phptype'), |
| 213 | + 'propel.database.driver' => $database->getParameter('phptype'), |
| 214 | + 'propel.database.url' => $database->getParameter('dsn'), |
| 215 | + 'propel.database.user' => $database->getParameter('username'), |
| 216 | + 'propel.database.password' => $database->getParameter('password'), |
| 217 | + 'propel.database.encoding' => $database->getParameter('encoding'), |
| 218 | + ); |
| 219 | + } |
| 220 | + |
| 221 | + protected function getProperties($file) |
| 222 | + { |
| 223 | + $properties = array(); |
| 224 | + |
| 225 | + if (false === $lines = @file($file)) { |
| 226 | + throw new sfCommandException('Unable to parse contents of the "sqldb.map" file.'); |
| 227 | + } |
| 228 | + |
| 229 | + foreach ($lines as $line) { |
| 230 | + $line = trim($line); |
| 231 | + |
| 232 | + if ('' == $line) { |
| 233 | + continue; |
| 234 | + } |
| 235 | + |
| 236 | + if (in_array($line[0], array('#', ';'))) { |
| 237 | + continue; |
| 238 | + } |
| 239 | + |
| 240 | + $pos = strpos($line, '='); |
| 241 | + $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1)); |
| 242 | + } |
| 243 | + |
| 244 | + return $properties; |
| 245 | + } |
| 246 | +} |
0 commit comments