Source of file DoctrineFormGenerator.php
Size: 3,622 Bytes - Last Modified: 2013-07-04T14:20:59+02:00
/home/theseer/Downloads/Symfony/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Generator/DoctrineFormGenerator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113 | <?php /* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ namespace Sensio\Bundle\GeneratorBundle\Generator; use Symfony\Component\Filesystem\Filesystem; use Symfony\Component\HttpKernel\Bundle\BundleInterface; use Doctrine\ORM\Mapping\ClassMetadataInfo; /** * Generates a form class based on a Doctrine entity. * * @author Fabien Potencier <fabien@symfony.com> * @author Hugo Hamon <hugo.hamon@sensio.com> */ class DoctrineFormGenerator extends Generator { private $filesystem; private $className; private $classPath; /** * Constructor. * * @param Filesystem $filesystem A Filesystem instance */ public function __construct(Filesystem $filesystem) { $this->filesystem = $filesystem; } public function getClassName() { return $this->className; } public function getClassPath() { return $this->classPath; } /** * Generates the entity form class if it does not exist. * * @param BundleInterface $bundle The bundle in which to create the class * @param string $entity The entity relative class name * @param ClassMetadataInfo $metadata The entity metadata class */ public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata) { $parts = explode('\\', $entity); $entityClass = array_pop($parts); $this->className = $entityClass.'Type'; $dirPath = $bundle->getPath().'/Form'; $this->classPath = $dirPath.'/'.str_replace('\\', '/', $entity).'Type.php'; if (file_exists($this->classPath)) { throw new \RuntimeException(sprintf('Unable to generate the %s form class as it already exists under the %s file', $this->className, $this->classPath)); } if (count($metadata->identifier) > 1) { throw new \RuntimeException('The form generator does not support entity classes with multiple primary keys.'); } $parts = explode('\\', $entity); array_pop($parts); $this->renderFile('form/FormType.php.twig', $this->classPath, array( 'fields' => $this->getFieldsFromMetadata($metadata), 'namespace' => $bundle->getNamespace(), 'entity_namespace' => implode('\\', $parts), 'entity_class' => $entityClass, 'bundle' => $bundle->getName(), 'form_class' => $this->className, 'form_type_name' => strtolower(str_replace('\\', '_', $bundle->getNamespace()).($parts ? '_' : '').implode('_', $parts).'_'.$this->className), )); } /** * Returns an array of fields. Fields can be both column fields and * association fields. * * @param ClassMetadataInfo $metadata * @return array $fields */ private function getFieldsFromMetadata(ClassMetadataInfo $metadata) { $fields = (array) $metadata->fieldNames; // Remove the primary key field if it's not managed manually if (!$metadata->isIdentifierNatural()) { $fields = array_diff($fields, $metadata->identifier); } foreach ($metadata->associationMappings as $fieldName => $relation) { if ($relation['type'] !== ClassMetadataInfo::ONE_TO_MANY) { $fields[] = $fieldName; } } return $fields; } } |