Created
July 7, 2010 20:27
-
-
Save kriswallsmith/467224 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* A work in progress for dumping a form class into HTML and PHP markup | |
* suitable for a template file. | |
* | |
* @author Kris Wallsmith <kris.wallsmith@symfony-project.com> | |
*/ | |
class sfGenerateFormScaffoldingTask extends sfTaskExtraGeneratorBaseTask | |
{ | |
protected function configure() | |
{ | |
$this->addArguments(array( | |
new sfCommandArgument('form'), | |
)); | |
$this->addOptions(array( | |
new sfCommandOption('var', null, sfCommandOption::PARAMETER_REQUIRED, 'The name of the form variable', 'form'), | |
new sfCommandOption('using', null, sfCommandOption::PARAMETER_REQUIRED, 'The name of the form formatter to use'), | |
new sfCommandOption('exclude', null, sfCommandOption::PARAMETER_REQUIRED | sfCommandOption::IS_ARRAY, 'Parts to exclude'), | |
new sfCommandOption('succinct', null, sfCommandOption::PARAMETER_NONE, 'Call the more succinct renderRow()'), | |
new sfCommandOption('verbose', null, sfCommandOption::PARAMETER_NONE, 'Include placeholder vars in method calls'), | |
)); | |
$this->namespace = 'generate'; | |
$this->name = 'form-scaffolding'; | |
} | |
protected function execute($arguments = array(), $options = array()) | |
{ | |
if (!class_exists($arguments['form'])) | |
{ | |
throw new InvalidArgumentException(sprintf('The class "%s" does not exist.', $arguments['form'])); | |
} | |
if (!is_subclass_of($arguments['form'], 'sfForm')) | |
{ | |
throw new InvalidArgumentException(sprintf('The class "%s" does not extend sfForm.', $arguments['form'])); | |
} | |
$form = new $arguments['form'](); | |
$this->log($this->generateScaffolding($form->getFormFieldSchema(), '', $arguments, $options)); | |
} | |
protected function generateScaffolding(sfFormFieldSchema $fields, $prefix, $arguments = array(), $options = array()) | |
{ | |
$scaffolding = ''; | |
if ($options['using']) | |
{ | |
$fields->getWidget()->setFormFormatterName($options['using']); | |
} | |
foreach ($fields as $name => $field) | |
{ | |
$localPrefix = "{$prefix}['{$name}']"; | |
if ($field instanceof sfFormFieldSchema) | |
{ | |
if ($field->getWidget() instanceof sfWidgetFormSchemaDecorator) | |
{ | |
// todo: use decorator | |
$scaffolding .= $this->generateScaffolding($field, $localPrefix, $arguments, $options); | |
} | |
else | |
{ | |
$scaffolding .= $this->generateScaffolding($field, $localPrefix, $arguments, $options); | |
} | |
} | |
else if ($options['succinct']) | |
{ | |
$scaffolding .= sprintf('<?php echo $%s%s->renderRow(%s) ?>', $options['var'], $localPrefix, $options['verbose'] ? 'array(), null, null' : '')."\n"; | |
} | |
else | |
{ | |
$scaffolding .= strtr($field->getParent()->getWidget()->getFormFormatter()->getRowFormat(), array( | |
'%error%' => $this->getErrorPhp($field, $localPrefix, $arguments, $options), | |
'%label%' => $this->getLabelPhp($field, $localPrefix, $arguments, $options), | |
'%field%' => $this->getFieldPhp($field, $localPrefix, $arguments, $options), | |
'%help%' => $this->getHelpPhp($field, $localPrefix, $arguments, $options), | |
'%hidden_fields%' => $this->getHiddenFieldsPhp($field, $localPrefix, $arguments, $options), | |
)); | |
} | |
} | |
return $scaffolding; | |
} | |
protected function getErrorPhp(sfFormField $field, $prefix, $arguments = array(), $options = array()) | |
{ | |
if (!in_array('error', $options['exclude'])) | |
{ | |
} | |
} | |
protected function getLabelPhp(sfFormField $field, $prefix, $arguments = array(), $options = array()) | |
{ | |
if (!in_array('label', $options['exclude'])) | |
{ | |
return sprintf('<?php echo $%s%s->renderLabel(%s) ?>', $options['var'], $prefix, $options['verbose'] ? 'null, array()' : ''); | |
} | |
} | |
protected function getFieldPhp(sfFormField $field, $prefix, $arguments = array(), $options = array()) | |
{ | |
if (!in_array('field', $options['exclude'])) | |
{ | |
return sprintf('<?php echo $%s%s%s ?>', $options['var'], $prefix, $options['verbose'] ? '->render(array())' : ''); | |
} | |
} | |
protected function getHelpPhp(sfFormField $field, $prefix, $arguments = array(), $options = array()) | |
{ | |
if (!in_array('help', $options['exclude'])) | |
{ | |
$format = $field->getParent()->getWidget()->getFormFormatter()->getHelpFormat(); | |
return '<?php ($help = $'.$options['var'].$prefix.'->renderHelp()) && print strtr('.var_export($format, true).', array(\'%help%\' => $help)) ?>'; | |
} | |
} | |
protected function getHiddenFieldsPhp(sfFormField $field, $prefix, $arguments = array(), $options = array()) | |
{ | |
if (!in_array('hidden_fields', $options['exclude'])) | |
{ | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment