PageRenderTime 41ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/plugins/sfPropelPlugin/lib/task/sfPropelBaseTask.class.php

https://github.com/bheneka/gitta
PHP | 357 lines | 264 code | 63 blank | 30 comment | 31 complexity | f250453c01a3b7f82201a49225a11cb5 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * Base class for all symfony Propel tasks.
  11. *
  12. * @package symfony
  13. * @subpackage propel
  14. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  15. * @version SVN: $Id$
  16. */
  17. abstract class sfPropelBaseTask extends sfBaseTask
  18. {
  19. const CHECK_SCHEMA = true;
  20. const DO_NOT_CHECK_SCHEMA = false;
  21. static protected $done = false;
  22. protected $additionalPhingArgs = array();
  23. public function initialize(sfEventDispatcher $dispatcher, sfFormatter $formatter)
  24. {
  25. parent::initialize($dispatcher, $formatter);
  26. if (!self::$done)
  27. {
  28. sfToolkit::addIncludePath(array(
  29. sfConfig::get('sf_propel_runtime_path', realpath(dirname(__FILE__).'/../lib/vendor')),
  30. dirname(__FILE__),
  31. ));
  32. self::$done = true;
  33. }
  34. }
  35. protected function process(sfCommandManager $commandManager, $options)
  36. {
  37. parent::process($commandManager, $options);
  38. // capture phing-arg options
  39. if ($commandManager->getOptionSet()->hasOption('phing-arg'))
  40. {
  41. $this->additionalPhingArgs = $commandManager->getOptionValue('phing-arg');
  42. }
  43. }
  44. protected function schemaToYML($checkSchema = self::CHECK_SCHEMA, $prefix = '')
  45. {
  46. $finder = sfFinder::type('file')->name('*schema.xml')->prune('doctrine');
  47. $schemas = array_unique(array_merge($finder->in(sfConfig::get('sf_config_dir')), $finder->in($this->configuration->getPluginSubPaths('/config'))));
  48. if (self::CHECK_SCHEMA === $checkSchema && !count($schemas))
  49. {
  50. throw new sfCommandException('You must create a schema.xml file.');
  51. }
  52. $dbSchema = new sfPropelDatabaseSchema();
  53. foreach ($schemas as $schema)
  54. {
  55. $dbSchema->loadXML($schema);
  56. $this->logSection('schema', sprintf('converting "%s" to YML', $schema));
  57. $localprefix = $prefix;
  58. // change prefix for plugins
  59. if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
  60. {
  61. $localprefix = $prefix.$match[1].'-';
  62. }
  63. // save converted xml files in original directories
  64. $yml_file_name = str_replace('.xml', '.yml', basename($schema));
  65. $file = str_replace(basename($schema), $prefix.$yml_file_name, $schema);
  66. $this->logSection('schema', sprintf('putting %s', $file));
  67. file_put_contents($file, $dbSchema->asYAML());
  68. }
  69. }
  70. protected function schemaToXML($checkSchema = self::CHECK_SCHEMA, $prefix = '')
  71. {
  72. $finder = sfFinder::type('file')->name('*schema.yml')->prune('doctrine');
  73. $dirs = array_merge(array(sfConfig::get('sf_config_dir')), $this->configuration->getPluginSubPaths('/config'));
  74. $schemas = $finder->in($dirs);
  75. if (self::CHECK_SCHEMA === $checkSchema && !count($schemas))
  76. {
  77. throw new sfCommandException('You must create a schema.yml file.');
  78. }
  79. $dbSchema = new sfPropelDatabaseSchema();
  80. foreach ($schemas as $schema)
  81. {
  82. $schemaArray = sfYaml::load($schema);
  83. if (!is_array($schemaArray))
  84. {
  85. continue; // No defined schema here, skipping
  86. }
  87. if (!isset($schemaArray['classes']))
  88. {
  89. // Old schema syntax: we convert it
  90. $schemaArray = $dbSchema->convertOldToNewYaml($schemaArray);
  91. }
  92. $customSchemaFilename = str_replace(array(
  93. str_replace(DIRECTORY_SEPARATOR, '/', sfConfig::get('sf_root_dir')).'/',
  94. 'plugins/',
  95. 'config/',
  96. '/',
  97. 'schema.yml'
  98. ), array('', '', '', '_', 'schema.custom.yml'), $schema);
  99. $customSchemas = sfFinder::type('file')->name($customSchemaFilename)->in($dirs);
  100. foreach ($customSchemas as $customSchema)
  101. {
  102. $this->logSection('schema', sprintf('found custom schema %s', $customSchema));
  103. $customSchemaArray = sfYaml::load($customSchema);
  104. if (!isset($customSchemaArray['classes']))
  105. {
  106. // Old schema syntax: we convert it
  107. $customSchemaArray = $dbSchema->convertOldToNewYaml($customSchemaArray);
  108. }
  109. $schemaArray = sfToolkit::arrayDeepMerge($schemaArray, $customSchemaArray);
  110. }
  111. $dbSchema->loadArray($schemaArray);
  112. $this->logSection('schema', sprintf('converting "%s" to XML', $schema));
  113. $localprefix = $prefix;
  114. // change prefix for plugins
  115. if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
  116. {
  117. $localprefix = $prefix.$match[1].'-';
  118. }
  119. // save converted xml files in original directories
  120. $xml_file_name = str_replace('.yml', '.xml', basename($schema));
  121. $file = str_replace(basename($schema), $localprefix.$xml_file_name, $schema);
  122. $this->logSection('schema', sprintf('putting %s', $file));
  123. file_put_contents($file, $dbSchema->asXML());
  124. }
  125. }
  126. protected function copyXmlSchemaFromPlugins($prefix = '')
  127. {
  128. if (!$dirs = $this->configuration->getPluginSubPaths('/config'))
  129. {
  130. return;
  131. }
  132. $schemas = sfFinder::type('file')->name('*schema.xml')->prune('doctrine')->in($dirs);
  133. foreach ($schemas as $schema)
  134. {
  135. // reset local prefix
  136. $localprefix = '';
  137. // change prefix for plugins
  138. if (preg_match('#plugins[/\\\\]([^/\\\\]+)[/\\\\]#', $schema, $match))
  139. {
  140. // if the plugin name is not in the schema filename, add it
  141. if (!strstr(basename($schema), $match[1]))
  142. {
  143. $localprefix = $match[1].'-';
  144. }
  145. }
  146. // if the prefix is not in the schema filename, add it
  147. if (!strstr(basename($schema), $prefix))
  148. {
  149. $localprefix = $prefix.$localprefix;
  150. }
  151. $this->getFilesystem()->copy($schema, 'config'.DIRECTORY_SEPARATOR.$localprefix.basename($schema));
  152. if ('' === $localprefix)
  153. {
  154. $this->getFilesystem()->remove($schema);
  155. }
  156. }
  157. }
  158. protected function cleanup()
  159. {
  160. if (null === $this->commandApplication || !$this->commandApplication->withTrace())
  161. {
  162. $finder = sfFinder::type('file')->name('generated-*schema.xml')->name('*schema-transformed.xml');
  163. $this->getFilesystem()->remove($finder->in(array('config', 'plugins')));
  164. }
  165. }
  166. protected function callPhing($taskName, $checkSchema, $properties = array())
  167. {
  168. $schemas = sfFinder::type('file')->name('*schema.xml')->relative()->follow_link()->in(sfConfig::get('sf_config_dir'));
  169. if (self::CHECK_SCHEMA === $checkSchema && !$schemas)
  170. {
  171. throw new sfCommandException('You must create a schema.yml or schema.xml file.');
  172. }
  173. // Call phing targets
  174. sfToolkit::addIncludePath(array(
  175. sfConfig::get('sf_symfony_lib_dir'),
  176. sfConfig::get('sf_propel_generator_path', realpath(dirname(__FILE__).'/../vendor/propel-generator/classes')),
  177. ));
  178. $args = array();
  179. $bufferPhingOutput = null === $this->commandApplication || !$this->commandApplication->withTrace();
  180. $properties = array_merge(array(
  181. 'build.properties' => 'propel.ini',
  182. 'project.dir' => sfConfig::get('sf_config_dir'),
  183. 'propel.output.dir' => sfConfig::get('sf_root_dir'),
  184. ), $properties);
  185. foreach ($properties as $key => $value)
  186. {
  187. $args[] = "-D$key=$value";
  188. }
  189. // Build file
  190. $args[] = '-f';
  191. $args[] = realpath(dirname(__FILE__).DIRECTORY_SEPARATOR.'..'.DIRECTORY_SEPARATOR.'vendor'.DIRECTORY_SEPARATOR.'propel-generator'.DIRECTORY_SEPARATOR.'build.xml');
  192. // Logger
  193. if (DIRECTORY_SEPARATOR != '\\' && (function_exists('posix_isatty') && @posix_isatty(STDOUT)))
  194. {
  195. $args[] = '-logger';
  196. $args[] = 'phing.listener.AnsiColorLogger';
  197. }
  198. // Add our listener to detect errors
  199. $args[] = '-listener';
  200. $args[] = 'sfPhingListener';
  201. // Add any arbitrary arguments last
  202. foreach ($this->additionalPhingArgs as $arg)
  203. {
  204. if (in_array($arg, array('verbose', 'debug')))
  205. {
  206. $bufferPhingOutput = false;
  207. }
  208. $args[] = '-'.$arg;
  209. }
  210. $args[] = $taskName;
  211. // filter arguments through the event dispatcher
  212. $args = $this->dispatcher->filter(new sfEvent($this, 'propel.filter_phing_args'), $args)->getReturnValue();
  213. require_once dirname(__FILE__).'/sfPhing.class.php';
  214. // enable output buffering
  215. Phing::setOutputStream(new OutputStream(fopen('php://output', 'w')));
  216. Phing::startup();
  217. Phing::setProperty('phing.home', getenv('PHING_HOME'));
  218. $this->logSection('propel', 'Running "'.$taskName.'" phing task');
  219. if ($bufferPhingOutput)
  220. {
  221. ob_start();
  222. }
  223. $m = new sfPhing();
  224. $m->execute($args);
  225. $m->runBuild();
  226. if ($bufferPhingOutput)
  227. {
  228. ob_end_clean();
  229. }
  230. chdir(sfConfig::get('sf_root_dir'));
  231. // any errors?
  232. $ret = true;
  233. if (sfPhingListener::hasErrors())
  234. {
  235. $messages = array('Some problems occurred when executing the task:');
  236. foreach (sfPhingListener::getExceptions() as $exception)
  237. {
  238. $messages[] = '';
  239. $messages[] = preg_replace('/^.*build\-propel\.xml/', 'build-propel.xml', $exception->getMessage());
  240. $messages[] = '';
  241. }
  242. if (count(sfPhingListener::getErrors()))
  243. {
  244. $messages[] = 'If the exception message is not clear enough, read the output of the task for';
  245. $messages[] = 'more information';
  246. }
  247. $this->logBlock($messages, 'ERROR_LARGE');
  248. $ret = false;
  249. }
  250. return $ret;
  251. }
  252. protected function getPhingPropertiesForConnection($databaseManager, $connection)
  253. {
  254. $database = $databaseManager->getDatabase($connection);
  255. return array(
  256. 'propel.database' => $database->getParameter('phptype'),
  257. 'propel.database.driver' => $database->getParameter('phptype'),
  258. 'propel.database.url' => $database->getParameter('dsn'),
  259. 'propel.database.user' => $database->getParameter('username'),
  260. 'propel.database.password' => $database->getParameter('password'),
  261. 'propel.database.encoding' => $database->getParameter('encoding'),
  262. );
  263. }
  264. protected function getProperties($file)
  265. {
  266. $properties = array();
  267. if (false === $lines = @file($file))
  268. {
  269. throw new sfCommandException('Unable to parse contents of the "sqldb.map" file.');
  270. }
  271. foreach ($lines as $line)
  272. {
  273. $line = trim($line);
  274. if ('' == $line)
  275. {
  276. continue;
  277. }
  278. if (in_array($line[0], array('#', ';')))
  279. {
  280. continue;
  281. }
  282. $pos = strpos($line, '=');
  283. $properties[trim(substr($line, 0, $pos))] = trim(substr($line, $pos + 1));
  284. }
  285. return $properties;
  286. }
  287. }