InitBundleCommand.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.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 Symfony\Bundle\FrameworkBundle\Command;
  11. use Symfony\Component\Console\Input\InputArgument;
  12. use Symfony\Component\Console\Input\InputOption;
  13. use Symfony\Component\Console\Input\InputInterface;
  14. use Symfony\Component\Console\Output\OutputInterface;
  15. use Symfony\Component\Console\Output\Output;
  16. use Symfony\Bundle\FrameworkBundle\Util\Mustache;
  17. /**
  18. * Initializes a new bundle.
  19. *
  20. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  21. */
  22. class InitBundleCommand extends Command
  23. {
  24. /**
  25. * @see Command
  26. */
  27. protected function configure()
  28. {
  29. $this
  30. ->setDefinition(array(
  31. new InputArgument('namespace', InputArgument::REQUIRED, 'The namespace of the bundle to create'),
  32. new InputArgument('dir', InputArgument::REQUIRED, 'The directory where to create the bundle'),
  33. new InputArgument('bundleName', InputArgument::OPTIONAL, 'The optional bundle name'),
  34. ))
  35. ->setHelp(<<<EOT
  36. The <info>init:bundle</info> command generates a new bundle with a basic skeleton.
  37. <info>./app/console init:bundle "Vendor\HelloBundle" src [bundleName]</info>
  38. The bundle namespace must end with "Bundle" (e.g. <comment>Vendor\HelloBundle</comment>)
  39. and can be placed in any directory (e.g. <comment>src</comment>).
  40. If you don't specify a bundle name (e.g. <comment>HelloBundle</comment>), the bundle name will
  41. be the concatenation of the namespace segments (e.g. <comment>VendorHelloBundle</comment>).
  42. EOT
  43. )
  44. ->setName('init:bundle')
  45. ;
  46. }
  47. /**
  48. * @see Command
  49. *
  50. * @throws \InvalidArgumentException When namespace doesn't end with Bundle
  51. * @throws \RuntimeException When bundle can't be executed
  52. */
  53. protected function execute(InputInterface $input, OutputInterface $output)
  54. {
  55. if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) {
  56. throw new \InvalidArgumentException('The namespace must end with Bundle.');
  57. }
  58. // validate namespace
  59. if (preg_match('/[^A-Za-z0-9_\\\-]/', $namespace)) {
  60. throw new \InvalidArgumentException('The namespace contains invalid characters.');
  61. }
  62. // user specified bundle name?
  63. $bundle = $input->getArgument('bundleName');
  64. if (!$bundle) {
  65. $bundle = strtr($namespace, array('\\' => ''));
  66. }
  67. if (!preg_match('/Bundle$/', $bundle)) {
  68. throw new \InvalidArgumentException('The bundle name must end with Bundle.');
  69. }
  70. $dir = $input->getArgument('dir');
  71. // add trailing / if necessary
  72. $dir = '/' === substr($dir, -1, 1) ? $dir : $dir.'/';
  73. $targetDir = $dir.strtr($namespace, '\\', '/');
  74. $output->writeln(sprintf('Initializing bundle "<info>%s</info>" in "<info>%s</info>"', $bundle, $dir));
  75. if (file_exists($targetDir)) {
  76. throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle));
  77. }
  78. $filesystem = $this->container->get('filesystem');
  79. $filesystem->mirror(__DIR__.'/../Resources/skeleton/bundle', $targetDir);
  80. Mustache::renderDir($targetDir, array(
  81. 'namespace' => $namespace,
  82. 'bundle' => $bundle,
  83. ));
  84. rename($targetDir.'/Bundle.php', $targetDir.'/'.$bundle.'.php');
  85. }
  86. }