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