/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineCleanModelFilesTask.class.php

https://github.com/bheneka/gitta · PHP · 119 lines · 68 code · 16 blank · 35 comment · 8 complexity · 0b4352a0d88f680f8e5c50d7de3ea850 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Jonathan H. Wage <jonwage@gmail.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. require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
  10. /**
  11. * Delete all generated model classes for models which no longer exist in your YAML schema
  12. *
  13. * @package symfony
  14. * @subpackage doctrine
  15. * @author Jonathan H. Wage <jonwage@gmail.com>
  16. * @version SVN: $Id$
  17. */
  18. class sfDoctrineCleanModelFilesTask extends sfDoctrineBaseTask
  19. {
  20. protected function configure()
  21. {
  22. $this->addOptions(array(
  23. new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Do not ask for confirmation'),
  24. ));
  25. $this->aliases = array('doctrine:clean');
  26. $this->namespace = 'doctrine';
  27. $this->name = 'clean-model-files';
  28. $this->briefDescription = 'Delete all generated model classes for models which no longer exist in your YAML schema';
  29. $this->detailedDescription = <<<EOF
  30. The [doctrine:clean-model-files|INFO] task deletes model classes that are not
  31. represented in project or plugin schema.yml files:
  32. [./symfony doctrine:clean-model-files|INFO]
  33. EOF;
  34. }
  35. /**
  36. * @see sfTask
  37. */
  38. protected function execute($arguments = array(), $options = array())
  39. {
  40. $config = $this->getCliConfig();
  41. $changed = false;
  42. $deleteModelFiles = new sfDoctrineDeleteModelFilesTask($this->dispatcher, $this->formatter);
  43. $deleteModelFiles->setCommandApplication($this->commandApplication);
  44. $deleteModelFiles->setConfiguration($this->configuration);
  45. $yamlSchema = $this->getYamlSchema($config['yaml_schema_path']);
  46. // remove any models present in the filesystem but not in the yaml schema
  47. if ($modelsToRemove = array_diff($this->getFileModels($config['models_path']), array_keys($yamlSchema)))
  48. {
  49. $deleteModelFiles->run($modelsToRemove, array('no-confirmation' => $options['no-confirmation']));
  50. $changed = true;
  51. }
  52. // remove form classes whose generation is disabled
  53. foreach ($yamlSchema as $model => $definition)
  54. {
  55. if (isset($definition['options']['symfony']['form']) && !$definition['options']['symfony']['form'] && class_exists($model.'Form'))
  56. {
  57. $deleteModelFiles->run(array($model), array('suffix' => array('Form'), 'no-confirmation' => $options['no-confirmation']));
  58. $changed = true;
  59. }
  60. if (isset($definition['options']['symfony']['filter']) && !$definition['options']['symfony']['filter'] && class_exists($model.'FormFilter'))
  61. {
  62. $deleteModelFiles->run(array($model), array('suffix' => array('FormFilter'), 'no-confirmation' => $options['no-confirmation']));
  63. $changed = true;
  64. }
  65. }
  66. if ($changed)
  67. {
  68. $this->reloadAutoload();
  69. }
  70. else
  71. {
  72. $this->logSection('doctrine', 'Could not find any files that need to be removed');
  73. }
  74. }
  75. /**
  76. * Returns models defined in YAML.
  77. *
  78. * @return array
  79. */
  80. protected function getYamlModels($yamlSchemaPath)
  81. {
  82. return array_keys($this->getYamlSchema($yamlSchemaPath));
  83. }
  84. /**
  85. * Returns the schema as defined in YAML.
  86. *
  87. * @return array
  88. */
  89. protected function getYamlSchema($yamlSchemaPath)
  90. {
  91. return (array) sfYaml::load($this->prepareSchemaFile($yamlSchemaPath));
  92. }
  93. /**
  94. * Returns models that have class files.
  95. *
  96. * @return array
  97. */
  98. protected function getFileModels($modelsPath)
  99. {
  100. Doctrine_Core::loadModels($modelsPath);
  101. return Doctrine_Core::getLoadedModels();
  102. }
  103. }