InitBundleCommand.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. ))
  34. ->setName('init:bundle')
  35. ;
  36. }
  37. /**
  38. * @see Command
  39. *
  40. * @throws \InvalidArgumentException When namespace doesn't end with Bundle
  41. * @throws \RuntimeException When bundle can't be executed
  42. */
  43. protected function execute(InputInterface $input, OutputInterface $output)
  44. {
  45. if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) {
  46. throw new \InvalidArgumentException('The namespace must end with Bundle.');
  47. }
  48. $pos = strrpos($namespace, '\\');
  49. $bundle = substr($namespace, $pos ? $pos + 1 : 0);
  50. $dir = $input->getArgument('dir');
  51. $output->writeln(sprintf('Initializing bundle "<info>%s</info>" in "<info>%s</info>"', $bundle, $dir));
  52. if (file_exists($targetDir = $dir.'/'.$bundle)) {
  53. throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle));
  54. }
  55. $filesystem = $this->container->get('filesystem');
  56. $filesystem->mirror(__DIR__.'/../Resources/skeleton/bundle', $targetDir);
  57. Mustache::renderDir($targetDir, array(
  58. 'namespace' => $namespace,
  59. 'bundle' => $bundle,
  60. ));
  61. rename($targetDir.'/Bundle.php', $targetDir.'/'.$bundle.'.php');
  62. }
  63. }