Source of file DoctrineCrudGenerator.php
Size: 9,808 Bytes - Last Modified: 2013-07-04T14:20:59+02:00
/home/theseer/Downloads/Symfony/vendor/sensio/generator-bundle/Sensio/Bundle/GeneratorBundle/Generator/DoctrineCrudGenerator.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288 | <?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 CRUD controller. * * @author Fabien Potencier <fabien@symfony.com> */ class DoctrineCrudGenerator extends Generator { protected $filesystem; protected $routePrefix; protected $routeNamePrefix; protected $bundle; protected $entity; protected $metadata; protected $format; protected $actions; /** * Constructor. * * @param Filesystem $filesystem A Filesystem instance */ public function __construct(Filesystem $filesystem) { $this->filesystem = $filesystem; } /** * Generate the CRUD controller. * * @param BundleInterface $bundle A bundle object * @param string $entity The entity relative class name * @param ClassMetadataInfo $metadata The entity class metadata * @param string $format The configuration format (xml, yaml, annotation) * @param string $routePrefix The route name prefix * @param array $needWriteActions Wether or not to generate write actions * * @throws \RuntimeException */ public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $format, $routePrefix, $needWriteActions, $forceOverwrite) { $this->routePrefix = $routePrefix; $this->routeNamePrefix = str_replace('/', '_', $routePrefix); $this->actions = $needWriteActions ? array('index', 'show', 'new', 'edit', 'delete') : array('index', 'show'); if (count($metadata->identifier) > 1) { throw new \RuntimeException('The CRUD generator does not support entity classes with multiple primary keys.'); } if (!in_array('id', $metadata->identifier)) { throw new \RuntimeException('The CRUD generator expects the entity object has a primary key field named "id" with a getId() method.'); } $this->entity = $entity; $this->bundle = $bundle; $this->metadata = $metadata; $this->setFormat($format); $this->generateControllerClass($forceOverwrite); $dir = sprintf('%s/Resources/views/%s', $this->bundle->getPath(), str_replace('\\', '/', $this->entity)); if (!file_exists($dir)) { $this->filesystem->mkdir($dir, 0777); } $this->generateIndexView($dir); if (in_array('show', $this->actions)) { $this->generateShowView($dir); } if (in_array('new', $this->actions)) { $this->generateNewView($dir); } if (in_array('edit', $this->actions)) { $this->generateEditView($dir); } $this->generateTestClass(); $this->generateConfiguration(); } /** * Sets the configuration format. * * @param string $format The configuration format */ private function setFormat($format) { switch ($format) { case 'yml': case 'xml': case 'php': case 'annotation': $this->format = $format; break; default: $this->format = 'yml'; break; } } /** * Generates the routing configuration. * */ protected function generateConfiguration() { if (!in_array($this->format, array('yml', 'xml', 'php'))) { return; } $target = sprintf( '%s/Resources/config/routing/%s.%s', $this->bundle->getPath(), strtolower(str_replace('\\', '_', $this->entity)), $this->format ); $this->renderFile('crud/config/routing.'.$this->format.'.twig', $target, array( 'actions' => $this->actions, 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, 'bundle' => $this->bundle->getName(), 'entity' => $this->entity, )); } /** * Generates the controller class only. * */ protected function generateControllerClass($forceOverwrite) { $dir = $this->bundle->getPath(); $parts = explode('\\', $this->entity); $entityClass = array_pop($parts); $entityNamespace = implode('\\', $parts); $target = sprintf( '%s/Controller/%s/%sController.php', $dir, str_replace('\\', '/', $entityNamespace), $entityClass ); if (!$forceOverwrite && file_exists($target)) { throw new \RuntimeException('Unable to generate the controller as it already exists.'); } $this->renderFile('crud/controller.php.twig', $target, array( 'actions' => $this->actions, 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, 'bundle' => $this->bundle->getName(), 'entity' => $this->entity, 'entity_class' => $entityClass, 'namespace' => $this->bundle->getNamespace(), 'entity_namespace' => $entityNamespace, 'format' => $this->format, )); } /** * Generates the functional test class only. * */ protected function generateTestClass() { $parts = explode('\\', $this->entity); $entityClass = array_pop($parts); $entityNamespace = implode('\\', $parts); $dir = $this->bundle->getPath() .'/Tests/Controller'; $target = $dir .'/'. str_replace('\\', '/', $entityNamespace).'/'. $entityClass .'ControllerTest.php'; $this->renderFile('crud/tests/test.php.twig', $target, array( 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, 'entity' => $this->entity, 'bundle' => $this->bundle->getName(), 'entity_class' => $entityClass, 'namespace' => $this->bundle->getNamespace(), 'entity_namespace' => $entityNamespace, 'actions' => $this->actions, 'form_type_name' => strtolower(str_replace('\\', '_', $this->bundle->getNamespace()).($parts ? '_' : '').implode('_', $parts).'_'.$entityClass.'Type'), )); } /** * Generates the index.html.twig template in the final bundle. * * @param string $dir The path to the folder that hosts templates in the bundle */ protected function generateIndexView($dir) { $this->renderFile('crud/views/index.html.twig.twig', $dir.'/index.html.twig', array( 'bundle' => $this->bundle->getName(), 'entity' => $this->entity, 'fields' => $this->metadata->fieldMappings, 'actions' => $this->actions, 'record_actions' => $this->getRecordActions(), 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, )); } /** * Generates the show.html.twig template in the final bundle. * * @param string $dir The path to the folder that hosts templates in the bundle */ protected function generateShowView($dir) { $this->renderFile('crud/views/show.html.twig.twig', $dir.'/show.html.twig', array( 'bundle' => $this->bundle->getName(), 'entity' => $this->entity, 'fields' => $this->metadata->fieldMappings, 'actions' => $this->actions, 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, )); } /** * Generates the new.html.twig template in the final bundle. * * @param string $dir The path to the folder that hosts templates in the bundle */ protected function generateNewView($dir) { $this->renderFile('crud/views/new.html.twig.twig', $dir.'/new.html.twig', array( 'bundle' => $this->bundle->getName(), 'entity' => $this->entity, 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, 'actions' => $this->actions, )); } /** * Generates the edit.html.twig template in the final bundle. * * @param string $dir The path to the folder that hosts templates in the bundle */ protected function generateEditView($dir) { $this->renderFile('crud/views/edit.html.twig.twig', $dir.'/edit.html.twig', array( 'route_prefix' => $this->routePrefix, 'route_name_prefix' => $this->routeNamePrefix, 'entity' => $this->entity, 'bundle' => $this->bundle->getName(), 'actions' => $this->actions, )); } /** * Returns an array of record actions to generate (edit, show). * * @return array */ protected function getRecordActions() { return array_filter($this->actions, function($item) { return in_array($item, array('show', 'edit')); }); } } |