BundleGenerator.php 3.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sensio\Bundle\GeneratorBundle\Generator;
  11. use Symfony\Component\Filesystem\Filesystem;
  12. use Symfony\Component\DependencyInjection\Container;
  13. /**
  14. * Generates a bundle.
  15. *
  16. * @author Fabien Potencier <fabien@symfony.com>
  17. */
  18. class BundleGenerator extends Generator
  19. {
  20. private $filesystem;
  21. private $skeletonDir;
  22. public function __construct(Filesystem $filesystem, $skeletonDir)
  23. {
  24. $this->filesystem = $filesystem;
  25. $this->skeletonDir = $skeletonDir;
  26. }
  27. public function generate($namespace, $bundle, $dir, $format, $structure)
  28. {
  29. $dir .= '/'.strtr($namespace, '\\', '/');
  30. if (file_exists($dir)) {
  31. throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir)));
  32. }
  33. $basename = substr($bundle, 0, -6);
  34. $parameters = array(
  35. 'namespace' => $namespace,
  36. 'bundle' => $bundle,
  37. 'format' => $format,
  38. 'bundle_basename' => $basename,
  39. 'extension_alias' => Container::underscore($basename),
  40. );
  41. $this->renderFile($this->skeletonDir, 'Bundle.php', $dir.'/'.$bundle.'.php', $parameters);
  42. $this->renderFile($this->skeletonDir, 'Extension.php', $dir.'/DependencyInjection/'.$basename.'Extension.php', $parameters);
  43. $this->renderFile($this->skeletonDir, 'Configuration.php', $dir.'/DependencyInjection/Configuration.php', $parameters);
  44. $this->renderFile($this->skeletonDir, 'DefaultController.php', $dir.'/Controller/DefaultController.php', $parameters);
  45. $this->renderFile($this->skeletonDir, 'DefaultControllerTest.php', $dir.'/Tests/Controller/DefaultControllerTest.php', $parameters);
  46. $this->renderFile($this->skeletonDir, 'index.html.twig', $dir.'/Resources/views/Default/index.html.twig', $parameters);
  47. if ('xml' === $format || 'annotation' === $format) {
  48. $this->renderFile($this->skeletonDir, 'services.xml', $dir.'/Resources/config/services.xml', $parameters);
  49. } else {
  50. $this->renderFile($this->skeletonDir, 'services.'.$format, $dir.'/Resources/config/services.'.$format, $parameters);
  51. }
  52. if ('annotation' != $format) {
  53. $this->renderFile($this->skeletonDir, 'routing.'.$format, $dir.'/Resources/config/routing.'.$format, $parameters);
  54. }
  55. if ($structure) {
  56. $this->filesystem->mkdir($dir.'/Resources/doc');
  57. $this->filesystem->touch($dir.'/Resources/doc/index.rst');
  58. $this->filesystem->mkdir($dir.'/Resources/translations');
  59. $this->filesystem->copy($this->skeletonDir.'/messages.fr.xliff', $dir.'/Resources/translations/messages.fr.xliff');
  60. $this->filesystem->mkdir($dir.'/Resources/public/css');
  61. $this->filesystem->mkdir($dir.'/Resources/public/images');
  62. $this->filesystem->mkdir($dir.'/Resources/public/js');
  63. }
  64. }
  65. }