PageRenderTime 47ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/Group-I/jobeet/lib/vendor/symfony/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineBaseTask.class.php

https://bitbucket.org/hosseinzolfi/db-lab-spring-2011/
PHP | 286 lines | 159 code | 34 blank | 93 comment | 17 complexity | b89b4bcc2b9724e8053621cd5232f01d MD5 | raw file
Possible License(s): ISC, AGPL-3.0, LGPL-2.1, BSD-3-Clause, LGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) Jonathan H. Wage <jonwage@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * Base class for all symfony Doctrine tasks.
  12. *
  13. * @package symfony
  14. * @subpackage doctrine
  15. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  16. * @author Jonathan H. Wage <jonwage@gmail.com>
  17. * @version SVN: $Id: sfDoctrineBaseTask.class.php 28976 2010-04-05 00:27:39Z Kris.Wallsmith $
  18. */
  19. abstract class sfDoctrineBaseTask extends sfBaseTask
  20. {
  21. /**
  22. * Returns an array of configuration variables for the Doctrine CLI.
  23. *
  24. * @return array $config
  25. *
  26. * @see sfDoctrinePluginConfiguration::getCliConfig()
  27. */
  28. public function getCliConfig()
  29. {
  30. return $this->configuration->getPluginConfiguration('sfDoctrinePlugin')->getCliConfig();
  31. }
  32. /**
  33. * Calls a Doctrine CLI command.
  34. *
  35. * @param string $task Name of the Doctrine task to call
  36. * @param array $args Arguments for the task
  37. *
  38. * @see sfDoctrineCli
  39. */
  40. public function callDoctrineCli($task, $args = array())
  41. {
  42. $config = $this->getCliConfig();
  43. $arguments = array('./symfony', $task);
  44. foreach ($args as $key => $arg)
  45. {
  46. if (isset($config[$key]))
  47. {
  48. $config[$key] = $arg;
  49. }
  50. else
  51. {
  52. $arguments[] = $arg;
  53. }
  54. }
  55. $cli = new sfDoctrineCli($config);
  56. $cli->setSymfonyDispatcher($this->dispatcher);
  57. $cli->setSymfonyFormatter($this->formatter);
  58. $cli->run($arguments);
  59. }
  60. /**
  61. * Returns Doctrine databases from the supplied database manager.
  62. *
  63. * @param sfDatabaseManager $databaseManager
  64. * @param array|null $names An array of names or NULL for all databases
  65. *
  66. * @return array An associative array of {@link sfDoctrineDatabase} objects and their names
  67. *
  68. * @throws InvalidArgumentException If a requested database is not a Doctrine database
  69. */
  70. protected function getDoctrineDatabases(sfDatabaseManager $databaseManager, array $names = null)
  71. {
  72. $databases = array();
  73. if (null === $names)
  74. {
  75. foreach ($databaseManager->getNames() as $name)
  76. {
  77. $database = $databaseManager->getDatabase($name);
  78. if ($database instanceof sfDoctrineDatabase)
  79. {
  80. $databases[$name] = $database;
  81. }
  82. }
  83. }
  84. else
  85. {
  86. foreach ($names as $name)
  87. {
  88. $database = $databaseManager->getDatabase($name);
  89. if (!$database instanceof sfDoctrineDatabase)
  90. {
  91. throw new InvalidArgumentException(sprintf('The database "%s" is not a Doctrine database.', $name));
  92. }
  93. $databases[$name] = $database;
  94. }
  95. }
  96. return $databases;
  97. }
  98. /**
  99. * Merges all project and plugin schema files into one.
  100. *
  101. * Schema files are merged similar to how other configuration files are in
  102. * symfony, utilizing a configuration cascade. Files later in the cascade
  103. * can change values from earlier in the cascade.
  104. *
  105. * The order in which schema files are processed is like so:
  106. *
  107. * 1. Plugin schema files
  108. * * Plugins are processed in the order which they were enabled in ProjectConfiguration
  109. * * Each plugin's schema files are processed in alphabetical order
  110. * 2. Project schema files
  111. * * Project schema files are processed in alphabetical order
  112. *
  113. * A schema file is any file saved in a plugin or project's config/doctrine/
  114. * directory that matches the "*.yml" glob.
  115. *
  116. * @return string Absolute path to the consolidated schema file
  117. */
  118. protected function prepareSchemaFile($yamlSchemaPath)
  119. {
  120. $models = array();
  121. $finder = sfFinder::type('file')->name('*.yml')->sort_by_name()->follow_link();
  122. // plugin models
  123. foreach ($this->configuration->getPlugins() as $name)
  124. {
  125. $plugin = $this->configuration->getPluginConfiguration($name);
  126. foreach ($finder->in($plugin->getRootDir().'/config/doctrine') as $schema)
  127. {
  128. $pluginModels = (array) sfYaml::load($schema);
  129. $globals = $this->filterSchemaGlobals($pluginModels);
  130. foreach ($pluginModels as $model => $definition)
  131. {
  132. // canonicalize this definition
  133. $definition = $this->canonicalizeModelDefinition($model, $definition);
  134. // merge in the globals
  135. $definition = array_merge($globals, $definition);
  136. // merge this model into the schema
  137. $models[$model] = isset($models[$model]) ? sfToolkit::arrayDeepMerge($models[$model], $definition) : $definition;
  138. // the first plugin to define this model gets the package
  139. if (!isset($models[$model]['package']))
  140. {
  141. $models[$model]['package'] = $plugin->getName().'.lib.model.doctrine';
  142. }
  143. if (!isset($models[$model]['package_custom_path']) && 0 === strpos($models[$model]['package'], $plugin->getName()))
  144. {
  145. $models[$model]['package_custom_path'] = $plugin->getRootDir().'/lib/model/doctrine';
  146. }
  147. }
  148. }
  149. }
  150. // project models
  151. foreach ($finder->in($yamlSchemaPath) as $schema)
  152. {
  153. $projectModels = (array) sfYaml::load($schema);
  154. $globals = $this->filterSchemaGlobals($projectModels);
  155. foreach ($projectModels as $model => $definition)
  156. {
  157. // canonicalize this definition
  158. $definition = $this->canonicalizeModelDefinition($model, $definition);
  159. // merge in the globals
  160. $definition = array_merge($globals, $definition);
  161. // merge this model into the schema
  162. $models[$model] = isset($models[$model]) ? sfToolkit::arrayDeepMerge($models[$model], $definition) : $definition;
  163. }
  164. }
  165. // create one consolidated schema file
  166. $file = realpath(sys_get_temp_dir()).'/doctrine_schema_'.rand(11111, 99999).'.yml';
  167. $this->logSection('file+', $file);
  168. file_put_contents($file, sfYaml::dump($models, 4));
  169. return $file;
  170. }
  171. /**
  172. * Removes and returns globals from the supplied array of models.
  173. *
  174. * @param array $models An array of model definitions
  175. *
  176. * @return array An array of globals
  177. *
  178. * @see Doctrine_Import_Schema::getGlobalDefinitionKeys()
  179. */
  180. protected function filterSchemaGlobals(& $models)
  181. {
  182. $globals = array();
  183. $globalKeys = Doctrine_Import_Schema::getGlobalDefinitionKeys();
  184. foreach ($models as $key => $value)
  185. {
  186. if (in_array($key, $globalKeys))
  187. {
  188. $globals[$key] = $value;
  189. unset($models[$key]);
  190. }
  191. }
  192. return $globals;
  193. }
  194. /**
  195. * Canonicalizes a model definition in preparation for merging.
  196. *
  197. * @param string $model The model name
  198. * @param array $definition The model definition
  199. *
  200. * @return array The canonicalized model definition
  201. */
  202. protected function canonicalizeModelDefinition($model, $definition)
  203. {
  204. // expand short "type" syntax
  205. if (isset($definition['columns']))
  206. {
  207. foreach ($definition['columns'] as $key => $value)
  208. {
  209. if (!is_array($value))
  210. {
  211. $definition['columns'][$key] = array('type' => $value);
  212. $value = $definition['columns'][$key];
  213. }
  214. // expand short type(length, scale) syntax
  215. if (isset($value['type']) && preg_match('/ *(\w+) *\( *(\d+)(?: *, *(\d+))? *\)/', $value['type'], $match))
  216. {
  217. $definition['columns'][$key]['type'] = $match[1];
  218. $definition['columns'][$key]['length'] = $match[2];
  219. if (isset($match[3]))
  220. {
  221. $definition['columns'][$key]['scale'] = $match[3];
  222. }
  223. }
  224. }
  225. }
  226. // expand short "actAs" syntax
  227. if (isset($definition['actAs']))
  228. {
  229. foreach ($definition['actAs'] as $key => $value)
  230. {
  231. if (is_numeric($key))
  232. {
  233. $definition['actAs'][$value] = array();
  234. unset($definition['actAs'][$key]);
  235. }
  236. }
  237. }
  238. // expand short "listeners" syntax
  239. if (isset($definition['listeners']))
  240. {
  241. foreach ($definition['listeners'] as $key => $value)
  242. {
  243. if (is_numeric($key))
  244. {
  245. $definition['listeners'][$value] = array();
  246. unset($definition['listeners'][$key]);
  247. }
  248. }
  249. }
  250. return $definition;
  251. }
  252. }