| 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 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->namespace = 'doctrine'; |
|---|
| 37 |
$this->name = 'delete-model-files'; |
|---|
| 38 |
$this->briefDescription = 'Delete all the related auto generated files for a given model name.'; |
|---|
| 39 |
|
|---|
| 40 |
$this->detailedDescription = <<<EOF |
|---|
| 41 |
The [doctrine:delete-model-files|INFO] task deletes all files associated with certain |
|---|
| 42 |
models: |
|---|
| 43 |
|
|---|
| 44 |
[./symfony doctrine:delete-model-files Article Author|INFO] |
|---|
| 45 |
EOF; |
|---|
| 46 |
} |
|---|
| 47 |
|
|---|
| 48 |
|
|---|
| 49 |
* @see sfTask |
|---|
| 50 |
*/ |
|---|
| 51 |
protected function execute($arguments = array(), $options = array()) |
|---|
| 52 |
{ |
|---|
| 53 |
$paths = array_merge( |
|---|
| 54 |
array( |
|---|
| 55 |
sfConfig::get('sf_lib_dir').'/model/doctrine', |
|---|
| 56 |
sfConfig::get('sf_lib_dir').'/form/doctrine', |
|---|
| 57 |
sfConfig::get('sf_lib_dir').'/filter/doctrine', |
|---|
| 58 |
), |
|---|
| 59 |
$this->configuration->getPluginSubPaths('/lib/model/doctrine'), |
|---|
| 60 |
$this->configuration->getPluginSubPaths('/lib/form/doctrine'), |
|---|
| 61 |
$this->configuration->getPluginSubPaths('/lib/filter/doctrine') |
|---|
| 62 |
); |
|---|
| 63 |
|
|---|
| 64 |
$prefixPattern = $this->valuesToRegex($options['prefix'] ? $options['prefix'] : array('', 'Base', 'Plugin')); |
|---|
| 65 |
$suffixPattern = $this->valuesToRegex($options['suffix'] ? $options['suffix'] : array('', 'Table', 'Form', 'FormFilter')); |
|---|
| 66 |
$extensionPattern = $this->valuesToRegex($options['extension'] ? $options['extension'] : array('.php', '.class.php')); |
|---|
| 67 |
|
|---|
| 68 |
$total = 0; |
|---|
| 69 |
|
|---|
| 70 |
foreach ($arguments['name'] as $modelName) |
|---|
| 71 |
{ |
|---|
| 72 |
$finder = sfFinder::type('file')->name('/^'.$prefixPattern.$modelName.$suffixPattern.$extensionPattern.'$/'); |
|---|
| 73 |
$files = $finder->in($paths); |
|---|
| 74 |
|
|---|
| 75 |
if ($files) |
|---|
| 76 |
{ |
|---|
| 77 |
if (!$options['no-confirmation'] && !$this->askConfirmation(array_merge( |
|---|
| 78 |
array('The following '.$modelName.' files will be deleted:', ''), |
|---|
| 79 |
array_map(create_function('$v', 'return \' - \'.sfDebug::shortenFilePath($v);'), $files), |
|---|
| 80 |
array('', 'Continue? (y/N)') |
|---|
| 81 |
), 'QUESTION_LARGE', false)) |
|---|
| 82 |
{ |
|---|
| 83 |
$this->logSection('doctrine', 'Aborting delete of "'.$modelName.'" files'); |
|---|
| 84 |
continue; |
|---|
| 85 |
} |
|---|
| 86 |
|
|---|
| 87 |
$this->logSection('doctrine', 'Deleting "'.$modelName.'" files'); |
|---|
| 88 |
$this->getFilesystem()->remove($files); |
|---|
| 89 |
|
|---|
| 90 |
$total += count($files); |
|---|
| 91 |
} |
|---|
| 92 |
else |
|---|
| 93 |
{ |
|---|
| 94 |
$this->logSection('doctrine', 'No files found for the model named "'.$modelName.'"'); |
|---|
| 95 |
} |
|---|
| 96 |
} |
|---|
| 97 |
|
|---|
| 98 |
$this->logSection('doctrine', 'Deleted a total of '.$total.' file(s)'); |
|---|
| 99 |
} |
|---|
| 100 |
|
|---|
| 101 |
|
|---|
| 102 |
* Converts an array of values to a regular expression pattern fragment. |
|---|
| 103 |
* |
|---|
| 104 |
* @param array $values An array of values for the pattern |
|---|
| 105 |
* @param string $delimiter The regular expression delimiter |
|---|
| 106 |
* |
|---|
| 107 |
* @return string A regular expression fragment |
|---|
| 108 |
*/ |
|---|
| 109 |
protected function valuesToRegex($values, $delimiter = '/') |
|---|
| 110 |
{ |
|---|
| 111 |
if (false !== $pos = array_search('', $values)) |
|---|
| 112 |
{ |
|---|
| 113 |
$required = false; |
|---|
| 114 |
unset($values[$pos]); |
|---|
| 115 |
} |
|---|
| 116 |
else |
|---|
| 117 |
{ |
|---|
| 118 |
$required = true; |
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
if (count($values)) |
|---|
| 122 |
{ |
|---|
| 123 |
$regex = '('; |
|---|
| 124 |
foreach ($values as $i => $value) |
|---|
| 125 |
{ |
|---|
| 126 |
$regex .= preg_quote($value, $delimiter); |
|---|
| 127 |
if (isset($values[$i + 1])) |
|---|
| 128 |
{ |
|---|
| 129 |
$regex .= '|'; |
|---|
| 130 |
} |
|---|
| 131 |
} |
|---|
| 132 |
$regex .= ')'; |
|---|
| 133 |
|
|---|
| 134 |
if (!$required) |
|---|
| 135 |
{ |
|---|
| 136 |
$regex .= '?'; |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
return $regex; |
|---|
| 140 |
} |
|---|
| 141 |
} |
|---|
| 142 |
} |
|---|
| 143 |
|
|---|