Development

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

You must first sign up to be able to contribute.

root/branches/1.4/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineCleanModelFilesTask.class.php

Revision 29677, 3.5 kB (checked in by Kris.Wallsmith, 4 years ago)

[1.3, 1.4] updated doctrine:clean task to delete form classes when generation has been disabled (closes #7777)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1 <?php
2
3 /*
4  * This file is part of the symfony package.
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 require_once(dirname(__FILE__).'/sfDoctrineBaseTask.class.php');
12
13 /**
14  * Delete all generated model classes for models which no longer exist in your YAML schema
15  *
16  * @package    symfony
17  * @subpackage doctrine
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id$
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     // remove any models present in the filesystem but not in the yaml schema
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     // remove form classes whose generation is disabled
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
Note: See TracBrowser for help on using the browser.