Development

/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineDeleteModelFilesTask.class.php

You must first sign up to be able to contribute.

root/branches/1.3/lib/plugins/sfDoctrinePlugin/lib/task/sfDoctrineDeleteModelFilesTask.class.php

Revision 29677, 4.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 files associated with a Doctrine model. Forms, filters, etc.
15  *
16  * @package    symfony
17  * @subpackage doctrine
18  * @author     Jonathan H. Wage <jonwage@gmail.com>
19  * @version    SVN: $Id$
20  */
21 class sfDoctrineDeleteModelFilesTask extends sfDoctrineBaseTask
22 {
23   protected function configure()
24   {
25     $this->addArguments(array(
26       new sfCommandArgument('name', sfCommandArgument::REQUIRED | sfCommandArgument::IS_ARRAY, 'The name of the model you wish to delete all related files for.'),
27     ));
28
29     $this->addOptions(array(
30       new sfCommandOption('no-confirmation', null, sfCommandOption::PARAMETER_NONE, 'Do not ask for confirmation'),
31       new sfCommandOption('prefix', null, sfCommandOption::PARAMETER_REQUIRED | sfCommandOption::IS_ARRAY, 'Class prefix to remove'),
32       new sfCommandOption('suffix', null, sfCommandOption::PARAMETER_REQUIRED | sfCommandOption::IS_ARRAY, 'Class suffix to remove'),
33       new sfCommandOption('extension', null, sfCommandOption::PARAMETER_REQUIRED | sfCommandOption::IS_ARRAY, 'Filename extension to remove'),
34     ));
35
36     $this->aliases = array();
37     $this->namespace = 'doctrine';
38     $this->name = 'delete-model-files';
39     $this->briefDescription = 'Delete all the related auto generated files for a given model name.';
40
41     $this->detailedDescription = <<<EOF
42 The [doctrine:delete-model-files|INFO] task deletes all files associated with certain
43 models:
44
45   [./symfony doctrine:delete-model-files Article Author|INFO]
46 EOF;
47   }
48
49   /**
50    * @see sfTask
51    */
52   protected function execute($arguments = array(), $options = array())
53   {
54     $paths = array_merge(
55       array(
56         sfConfig::get('sf_lib_dir').'/model/doctrine',
57         sfConfig::get('sf_lib_dir').'/form/doctrine',
58         sfConfig::get('sf_lib_dir').'/filter/doctrine',
59       ),
60       $this->configuration->getPluginSubPaths('/lib/model/doctrine'),
61       $this->configuration->getPluginSubPaths('/lib/form/doctrine'),
62       $this->configuration->getPluginSubPaths('/lib/filter/doctrine')
63     );
64
65     $prefixPattern    = $this->valuesToRegex($options['prefix'] ? $options['prefix'] : array('', 'Base', 'Plugin'));
66     $suffixPattern    = $this->valuesToRegex($options['suffix'] ? $options['suffix'] : array('', 'Table', 'Form', 'FormFilter'));
67     $extensionPattern = $this->valuesToRegex($options['extension'] ? $options['extension'] : array('.php', '.class.php'));
68
69     $total = 0;
70
71     foreach ($arguments['name'] as $modelName)
72     {
73       $finder = sfFinder::type('file')->name('/^'.$prefixPattern.$modelName.$suffixPattern.$extensionPattern.'$/');
74       $files = $finder->in($paths);
75
76       if ($files)
77       {
78         if (!$options['no-confirmation'] && !$this->askConfirmation(array_merge(
79           array('The following '.$modelName.' files will be deleted:', ''),
80           array_map(create_function('$v', 'return \' - \'.sfDebug::shortenFilePath($v);'), $files),
81           array('', 'Continue? (y/N)')
82         ), 'QUESTION_LARGE', false))
83         {
84           $this->logSection('doctrine', 'Aborting delete of "'.$modelName.'" files');
85           continue;
86         }
87
88         $this->logSection('doctrine', 'Deleting "'.$modelName.'" files');
89         $this->getFilesystem()->remove($files);
90
91         $total += count($files);
92       }
93       else
94       {
95         $this->logSection('doctrine', 'No files found for the model named "'.$modelName.'"');
96       }
97     }
98
99     $this->logSection('doctrine', 'Deleted a total of '.$total.' file(s)');
100   }
101
102   /**
103    * Converts an array of values to a regular expression pattern fragment.
104    *
105    * @param array  $values    An array of values for the pattern
106    * @param string $delimiter The regular expression delimiter
107    *
108    * @return string A regular expression fragment
109    */
110   protected function valuesToRegex($values, $delimiter = '/')
111   {
112     if (false !== $pos = array_search('', $values))
113     {
114       $required = false;
115       unset($values[$pos]);
116     }
117     else
118     {
119       $required = true;
120     }
121
122     if (count($values))
123     {
124       $regex = '(';
125       foreach ($values as $i => $value)
126       {
127         $regex .= preg_quote($value, $delimiter);
128         if (isset($values[$i + 1]))
129         {
130           $regex .= '|';
131         }
132       }
133       $regex .= ')';
134
135       if (!$required)
136       {
137         $regex .= '?';
138       }
139
140       return $regex;
141     }
142   }
143 }
144
Note: See TracBrowser for help on using the browser.