This commit is contained in:
Kevin Adametz 2020-03-07 19:46:02 +01:00
parent a37785b391
commit 33458b2ca3
9915 changed files with 1247019 additions and 0 deletions

View file

@ -0,0 +1,79 @@
<?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 Sensio\Bundle\GeneratorBundle\Model\Bundle;
use Symfony\Component\Filesystem\Filesystem;
/**
* Generates a bundle.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class BundleGenerator extends Generator
{
private $filesystem;
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function generateBundle(Bundle $bundle)
{
$dir = $bundle->getTargetDirectory();
if (file_exists($dir)) {
if (!is_dir($dir)) {
throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir)));
}
$files = scandir($dir);
if ($files != array('.', '..')) {
throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir)));
}
if (!is_writable($dir)) {
throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir)));
}
}
$parameters = array(
'namespace' => $bundle->getNamespace(),
'bundle' => $bundle->getName(),
'format' => $bundle->getConfigurationFormat(),
'bundle_basename' => $bundle->getBasename(),
'extension_alias' => $bundle->getExtensionAlias(),
);
$this->renderFile('bundle/Bundle.php.twig', $dir.'/'.$bundle->getName().'.php', $parameters);
if ($bundle->shouldGenerateDependencyInjectionDirectory()) {
$this->renderFile('bundle/Extension.php.twig', $dir.'/DependencyInjection/'.$bundle->getBasename().'Extension.php', $parameters);
$this->renderFile('bundle/Configuration.php.twig', $dir.'/DependencyInjection/Configuration.php', $parameters);
}
$this->renderFile('bundle/DefaultController.php.twig', $dir.'/Controller/DefaultController.php', $parameters);
$this->renderFile('bundle/DefaultControllerTest.php.twig', $bundle->getTestsDirectory().'/Controller/DefaultControllerTest.php', $parameters);
$this->renderFile('bundle/index.html.twig.twig', $dir.'/Resources/views/Default/index.html.twig', $parameters);
// render the services.yml/xml file
$servicesFilename = $bundle->getServicesConfigurationFilename();
$this->renderFile(
sprintf('bundle/%s.twig', $servicesFilename),
$dir.'/Resources/config/'.$servicesFilename, $parameters
);
if ($routingFilename = $bundle->getRoutingConfigurationFilename()) {
$this->renderFile(
sprintf('bundle/%s.twig', $routingFilename),
$dir.'/Resources/config/'.$routingFilename, $parameters
);
}
}
}

View file

@ -0,0 +1,64 @@
<?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;
/**
* Generates a Command inside a bundle.
*
* @author Javier Eguiluz <javier.eguiluz@gmail.com>
*/
class CommandGenerator extends Generator
{
private $filesystem;
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function generate(BundleInterface $bundle, $name)
{
$bundleDir = $bundle->getPath();
$commandDir = $bundleDir.'/Command';
self::mkdir($commandDir);
$commandClassName = $this->classify($name).'Command';
$commandFile = $commandDir.'/'.$commandClassName.'.php';
if ($this->filesystem->exists($commandFile)) {
throw new \RuntimeException(sprintf('Command "%s" already exists', $name));
}
$parameters = array(
'namespace' => $bundle->getNamespace(),
'class_name' => $commandClassName,
'name' => $name,
);
$this->renderFile('command/Command.php.twig', $commandFile, $parameters);
}
/**
* Transforms the given string to a new string valid as a PHP class name
* ('app:my-project' -> 'AppMyProject', 'app:namespace:name' -> 'AppNamespaceName').
*
* @param string $string
*
* @return string The string transformed to be a valid PHP class name
*/
public function classify($string)
{
return str_replace(' ', '', ucwords(strtr($string, '_-:', ' ')));
}
}

View file

@ -0,0 +1,196 @@
<?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;
/**
* Generates a Controller inside a bundle.
*
* @author Wouter J <wouter@wouterj.nl>
*/
class ControllerGenerator extends Generator
{
private $filesystem;
public function __construct(Filesystem $filesystem)
{
$this->filesystem = $filesystem;
}
public function generate(BundleInterface $bundle, $controller, $routeFormat, $templateFormat, array $actions = array())
{
$dir = $bundle->getPath();
$controllerFile = $dir.'/Controller/'.$controller.'Controller.php';
if (file_exists($controllerFile)) {
throw new \RuntimeException(sprintf('Controller "%s" already exists', $controller));
}
$parameters = array(
'namespace' => $bundle->getNamespace(),
'bundle' => $bundle->getName(),
'format' => array(
'routing' => $routeFormat,
'templating' => $templateFormat,
),
'controller' => $controller,
);
foreach ($actions as $i => $action) {
// get the action name without the suffix Action (for the template logical name)
$actions[$i]['basename'] = substr($action['name'], 0, -6);
$params = $parameters;
$params['action'] = $actions[$i];
// create a template
$template = $actions[$i]['template'];
if ('default' == $template) {
@trigger_error('The use of the "default" keyword is deprecated. Use the real template name instead.', E_USER_DEPRECATED);
$template = $bundle->getName().':'.$controller.':'.
strtolower(preg_replace(array('/([A-Z]+)([A-Z][a-z])/', '/([a-z\d])([A-Z])/'), array('\\1_\\2', '\\1_\\2'), strtr(substr($action['name'], 0, -6), '_', '.')))
.'.html.'.$templateFormat;
}
if ('twig' == $templateFormat) {
$this->renderFile('controller/Template.html.twig.twig', $dir.'/Resources/views/'.$this->parseTemplatePath($template), $params);
} else {
$this->renderFile('controller/Template.html.php.twig', $dir.'/Resources/views/'.$this->parseTemplatePath($template), $params);
}
$this->generateRouting($bundle, $controller, $actions[$i], $routeFormat);
}
$parameters['actions'] = $actions;
$this->renderFile('controller/Controller.php.twig', $controllerFile, $parameters);
$this->renderFile('controller/ControllerTest.php.twig', $dir.'/Tests/Controller/'.$controller.'ControllerTest.php', $parameters);
}
public function generateRouting(BundleInterface $bundle, $controller, array $action, $format)
{
// annotation is generated in the templates
if ('annotation' == $format) {
return true;
}
$file = $bundle->getPath().'/Resources/config/routing.'.$format;
if (file_exists($file)) {
$content = file_get_contents($file);
} elseif (!is_dir($dir = $bundle->getPath().'/Resources/config')) {
self::mkdir($dir);
}
$controller = $bundle->getName().':'.$controller.':'.$action['basename'];
$name = strtolower(preg_replace('/([A-Z])/', '_\\1', $action['basename']));
if ('yml' == $format) {
// yaml
if (!isset($content)) {
$content = '';
}
$content .= sprintf(
"\n%s:\n path: %s\n defaults: { _controller: %s }\n",
$name,
$action['route'],
$controller
);
} elseif ('xml' == $format) {
// xml
if (!isset($content)) {
// new file
$content = <<<EOT
<?xml version="1.0" encoding="UTF-8" ?>
<routes xmlns="http://symfony.com/schema/routing"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/routing http://symfony.com/schema/routing/routing-1.0.xsd">
</routes>
EOT;
}
$sxe = simplexml_load_string($content);
$route = $sxe->addChild('route');
$route->addAttribute('id', $name);
$route->addAttribute('path', $action['route']);
$default = $route->addChild('default', $controller);
$default->addAttribute('key', '_controller');
$dom = new \DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML($sxe->asXML());
$content = $dom->saveXML();
} elseif ('php' == $format) {
// php
if (isset($content)) {
// edit current file
$pointer = strpos($content, 'return');
if (!preg_match('/(\$[^ ]*).*?new RouteCollection\(\)/', $content, $collection) || false === $pointer) {
throw new \RuntimeException('Routing.php file is not correct, please initialize RouteCollection.');
}
$content = substr($content, 0, $pointer);
$content .= sprintf("%s->add('%s', new Route('%s', array(", $collection[1], $name, $action['route']);
$content .= sprintf("\n '_controller' => '%s',", $controller);
$content .= "\n)));\n\nreturn ".$collection[1].';';
} else {
// new file
$content = <<<EOT
<?php
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
\$collection = new RouteCollection();
EOT;
$content .= sprintf("\n\$collection->add('%s', new Route('%s', array(", $name, $action['route']);
$content .= sprintf("\n '_controller' => '%s',", $controller);
$content .= "\n)));\n\nreturn \$collection;";
}
}
$flink = fopen($file, 'w');
if ($flink) {
$write = fwrite($flink, $content);
if ($write) {
fclose($flink);
} else {
throw new \RuntimeException(sprintf('We cannot write into file "%s", has that file the correct access level?', $file));
}
} else {
throw new \RuntimeException(sprintf('Problems with generating file "%s", did you gave write access to that directory?', $file));
}
}
protected function parseTemplatePath($template)
{
$data = $this->parseLogicalTemplateName($template);
return $data['controller'].'/'.$data['template'];
}
protected function parseLogicalTemplateName($logicalName, $part = '')
{
if (2 !== substr_count($logicalName, ':')) {
throw new \RuntimeException(sprintf('The given template name ("%s") is not correct (it must contain two colons).', $logicalName));
}
$data = array();
list($data['bundle'], $data['controller'], $data['template']) = explode(':', $logicalName);
return $part ? $data[$part] : $data;
}
}

View file

@ -0,0 +1,316 @@
<?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;
use Doctrine\Common\Inflector\Inflector;
/**
* Generates a CRUD controller.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class DoctrineCrudGenerator extends Generator
{
protected $filesystem;
protected $rootDir;
protected $routePrefix;
protected $routeNamePrefix;
protected $bundle;
protected $entity;
protected $entitySingularized;
protected $entityPluralized;
protected $metadata;
protected $format;
protected $actions;
/**
* @param Filesystem $filesystem
* @param string $rootDir
*/
public function __construct(Filesystem $filesystem, $rootDir)
{
$this->filesystem = $filesystem;
$this->rootDir = $rootDir;
}
/**
* 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 bool $needWriteActions Whether or not to generate write actions
* @param bool $forceOverwrite Whether or not to overwrite the controller
*
* @throws \RuntimeException
*/
public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $format, $routePrefix, $needWriteActions, $forceOverwrite)
{
$this->routePrefix = $routePrefix;
$this->routeNamePrefix = self::getRouteNamePrefix($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 or no primary keys.');
}
$this->entity = $entity;
$entity = str_replace('\\', '/', $entity);
$entityParts = explode('/', $entity);
$entityName = end($entityParts);
$this->entitySingularized = lcfirst(Inflector::singularize($entityName));
$this->entityPluralized = lcfirst(Inflector::pluralize($entityName));
$this->bundle = $bundle;
$this->metadata = $metadata;
$this->setFormat($format);
$this->generateControllerClass($forceOverwrite);
$dir = sprintf('%s/Resources/views/%s', $this->rootDir, strtolower($entity));
if (!file_exists($dir)) {
self::mkdir($dir);
}
$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
*/
protected 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,
'identifier' => $this->metadata->identifier[0],
));
}
/**
* 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_singularized' => $this->entitySingularized,
'entity_pluralized' => $this->entityPluralized,
'identifier' => $this->metadata->identifier[0],
'entity_class' => $entityClass,
'namespace' => $this->bundle->getNamespace(),
'entity_namespace' => $entityNamespace,
'format' => $this->format,
// BC with Symfony 2.7
'use_form_type_instance' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
));
}
/**
* 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),
));
}
/**
* 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,
'entity_pluralized' => $this->entityPluralized,
'entity_singularized' => $this->entitySingularized,
'identifier' => $this->metadata->identifier[0],
'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,
'entity_singularized' => $this->entitySingularized,
'identifier' => $this->metadata->identifier[0],
'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,
'entity_singularized' => $this->entitySingularized,
'route_prefix' => $this->routePrefix,
'route_name_prefix' => $this->routeNamePrefix,
'actions' => $this->actions,
'fields' => $this->metadata->fieldMappings,
));
}
/**
* 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,
'identifier' => $this->metadata->identifier[0],
'entity' => $this->entity,
'entity_singularized' => $this->entitySingularized,
'fields' => $this->metadata->fieldMappings,
'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'));
});
}
public static function getRouteNamePrefix($prefix)
{
$prefix = preg_replace('/{(.*?)}/', '', $prefix); // {foo}_bar -> _bar
$prefix = str_replace('/', '_', $prefix);
$prefix = preg_replace('/_+/', '_', $prefix); // foo__bar -> foo_bar
$prefix = trim($prefix, '_');
return $prefix;
}
}

View file

@ -0,0 +1,150 @@
<?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 Sensio\Bundle\GeneratorBundle\Model\EntityGeneratorResult;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Doctrine\ORM\Mapping\ClassMetadataInfo;
use Doctrine\ORM\Tools\EntityGenerator;
use Doctrine\ORM\Tools\EntityRepositoryGenerator;
use Doctrine\ORM\Tools\Export\ClassMetadataExporter;
use Doctrine\Common\Util\Inflector;
/**
* Generates a Doctrine entity class based on its name, fields and format.
*
* @author Fabien Potencier <fabien@symfony.com>
* @author Jonathan H. Wage <jonwage@gmail.com>
*/
class DoctrineEntityGenerator extends Generator
{
private $filesystem;
private $registry;
public function __construct(Filesystem $filesystem, RegistryInterface $registry)
{
$this->filesystem = $filesystem;
$this->registry = $registry;
}
/**
* @param BundleInterface $bundle
* @param string $entity
* @param string $format
* @param array $fields
*
* @return EntityGeneratorResult
*
* @throws \Doctrine\ORM\Tools\Export\ExportException
*/
public function generate(BundleInterface $bundle, $entity, $format, array $fields)
{
// configure the bundle (needed if the bundle does not contain any Entities yet)
$config = $this->registry->getManager(null)->getConfiguration();
$config->setEntityNamespaces(array_merge(
array($bundle->getName() => $bundle->getNamespace().'\\Entity'),
$config->getEntityNamespaces()
));
$entityClass = $this->registry->getAliasNamespace($bundle->getName()).'\\'.$entity;
$entityPath = $bundle->getPath().'/Entity/'.str_replace('\\', '/', $entity).'.php';
if (file_exists($entityPath)) {
throw new \RuntimeException(sprintf('Entity "%s" already exists.', $entityClass));
}
$class = new ClassMetadataInfo($entityClass, $config->getNamingStrategy());
$class->customRepositoryClassName = str_replace('\\Entity\\', '\\Repository\\', $entityClass).'Repository';
$class->mapField(array('fieldName' => 'id', 'type' => 'integer', 'id' => true));
$class->setIdGeneratorType(ClassMetadataInfo::GENERATOR_TYPE_AUTO);
foreach ($fields as $field) {
$class->mapField($field);
}
$entityGenerator = $this->getEntityGenerator();
if ('annotation' === $format) {
$entityGenerator->setGenerateAnnotations(true);
$class->setPrimaryTable(array('name' => Inflector::tableize(str_replace('\\', '', $entity))));
$entityCode = $entityGenerator->generateEntityClass($class);
$mappingPath = $mappingCode = false;
} else {
$cme = new ClassMetadataExporter();
$exporter = $cme->getExporter('yml' == $format ? 'yaml' : $format);
$mappingPath = $bundle->getPath().'/Resources/config/doctrine/'.str_replace('\\', '.', $entity).'.orm.'.$format;
if (file_exists($mappingPath)) {
throw new \RuntimeException(sprintf('Cannot generate entity when mapping "%s" already exists.', $mappingPath));
}
$mappingCode = $exporter->exportClassMetadata($class);
$entityGenerator->setGenerateAnnotations(false);
$entityCode = $entityGenerator->generateEntityClass($class);
}
$entityCode = str_replace(
array("@var integer\n", "@var boolean\n", "@param integer\n", "@param boolean\n", "@return integer\n", "@return boolean\n"),
array("@var int\n", "@var bool\n", "@param int\n", "@param bool\n", "@return int\n", "@return bool\n"),
$entityCode
);
self::mkdir(dirname($entityPath));
self::dump($entityPath, $entityCode);
if ($mappingPath) {
self::mkdir(dirname($mappingPath));
self::dump($mappingPath, $mappingCode);
}
$path = $bundle->getPath().str_repeat('/..', substr_count(get_class($bundle), '\\'));
$this->getRepositoryGenerator()->writeEntityRepositoryClass($class->customRepositoryClassName, $path);
$repositoryPath = $path.DIRECTORY_SEPARATOR.str_replace('\\', DIRECTORY_SEPARATOR, $class->customRepositoryClassName).'.php';
return new EntityGeneratorResult($entityPath, $repositoryPath, $mappingPath);
}
public function isReservedKeyword($keyword)
{
return $this->registry->getConnection()->getDatabasePlatform()->getReservedKeywordsList()->isKeyword($keyword);
}
protected function getEntityGenerator()
{
$entityGenerator = new EntityGenerator();
$entityGenerator->setGenerateAnnotations(false);
$entityGenerator->setGenerateStubMethods(true);
$entityGenerator->setRegenerateEntityIfExists(false);
$entityGenerator->setUpdateEntityIfExists(true);
$entityGenerator->setNumSpaces(4);
$entityGenerator->setAnnotationPrefix('ORM\\');
return $entityGenerator;
}
protected function getRepositoryGenerator()
{
return new EntityRepositoryGenerator();
}
/**
* Checks if the given name is a valid PHP variable name.
*
* @see http://php.net/manual/en/language.variables.basics.php
*
* @param $name string
*
* @return bool
*/
public function isValidPhpVariableName($name)
{
return (bool) preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*$/', $name, $matches);
}
}

View file

@ -0,0 +1,116 @@
<?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.
*
* @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
* @param bool $forceOverwrite If true, remove any existing form class before generating it again
*/
public function generate(BundleInterface $bundle, $entity, ClassMetadataInfo $metadata, $forceOverwrite = false)
{
$parts = explode('\\', $entity);
$entityClass = array_pop($parts);
$this->className = $entityClass.'Type';
$dirPath = $bundle->getPath().'/Form';
$this->classPath = $dirPath.'/'.str_replace('\\', '/', $entity).'Type.php';
if (!$forceOverwrite && 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).'_'.substr($this->className, 0, -4)),
// BC with Symfony 2.7
'get_name_required' => !method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix'),
));
}
/**
* 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;
}
}

View file

@ -0,0 +1,108 @@
<?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\Console\Output\ConsoleOutput;
/**
* Generator is the base class for all generators.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Generator
{
private $skeletonDirs;
private static $output;
/**
* Sets an array of directories to look for templates.
*
* The directories must be sorted from the most specific to the most
* directory.
*
* @param array $skeletonDirs An array of skeleton dirs
*/
public function setSkeletonDirs($skeletonDirs)
{
$this->skeletonDirs = is_array($skeletonDirs) ? $skeletonDirs : array($skeletonDirs);
}
protected function render($template, $parameters)
{
$twig = $this->getTwigEnvironment();
return $twig->render($template, $parameters);
}
/**
* Gets the twig environment that will render skeletons.
*
* @return \Twig_Environment
*/
protected function getTwigEnvironment()
{
return new \Twig_Environment(new \Twig_Loader_Filesystem($this->skeletonDirs), array(
'debug' => true,
'cache' => false,
'strict_variables' => true,
'autoescape' => false,
));
}
protected function renderFile($template, $target, $parameters)
{
self::mkdir(dirname($target));
return self::dump($target, $this->render($template, $parameters));
}
/**
* @internal
*/
public static function mkdir($dir, $mode = 0777, $recursive = true)
{
if (!is_dir($dir)) {
mkdir($dir, $mode, $recursive);
self::writeln(sprintf(' <fg=green>created</> %s', self::relativizePath($dir)));
}
}
/**
* @internal
*/
public static function dump($filename, $content)
{
if (file_exists($filename)) {
self::writeln(sprintf(' <fg=yellow>updated</> %s', self::relativizePath($filename)));
} else {
self::writeln(sprintf(' <fg=green>created</> %s', self::relativizePath($filename)));
}
return file_put_contents($filename, $content);
}
private static function writeln($message)
{
if (null === self::$output) {
self::$output = new ConsoleOutput();
}
self::$output->writeln($message);
}
private static function relativizePath($absolutePath)
{
$relativePath = str_replace(getcwd(), '.', $absolutePath);
return is_dir($absolutePath) ? rtrim($relativePath, '/').'/' : $relativePath;
}
}