InitBundleCommand.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Symfony\Framework\WebBundle\Command;
  3. use Symfony\Components\Console\Input\InputArgument;
  4. use Symfony\Components\Console\Input\InputOption;
  5. use Symfony\Components\Console\Input\InputInterface;
  6. use Symfony\Components\Console\Output\OutputInterface;
  7. use Symfony\Components\Console\Output\Output;
  8. use Symfony\Framework\WebBundle\Util\Filesystem;
  9. use Symfony\Framework\WebBundle\Util\Mustache;
  10. /*
  11. * This file is part of the Symfony framework.
  12. *
  13. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  14. *
  15. * This source file is subject to the MIT license that is bundled
  16. * with this source code in the file LICENSE.
  17. */
  18. /**
  19. * Initializes a new bundle.
  20. *
  21. * @package Symfony
  22. * @subpackage Framework_WebBundle
  23. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  24. */
  25. class InitBundleCommand extends Command
  26. {
  27. /**
  28. * @see Command
  29. */
  30. protected function configure()
  31. {
  32. $this
  33. ->setDefinition(array(
  34. new InputArgument('namespace', InputArgument::REQUIRED, 'The namespace of the bundle to create'),
  35. ))
  36. ->setName('init:bundle')
  37. ;
  38. }
  39. /**
  40. * @see Command
  41. *
  42. * @throws \InvalidArgumentException When namespace doesn't end with Bundle
  43. * @throws \RuntimeException When bundle can't be executed
  44. */
  45. protected function execute(InputInterface $input, OutputInterface $output)
  46. {
  47. if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace')))
  48. {
  49. throw new \InvalidArgumentException('The namespace must end with Bundle.');
  50. }
  51. $dirs = $this->container->getKernelService()->getBundleDirs();
  52. $tmp = str_replace('\\', '/', $namespace);
  53. $namespace = str_replace('/', '\\', dirname($tmp));
  54. $bundle = basename($tmp);
  55. if (!isset($dirs[$namespace]))
  56. {
  57. throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle (%s not defined).', $namespace));
  58. }
  59. $dir = $dirs[$namespace];
  60. $output->writeln(sprintf('Initializing bundle "<info>%s</info>" in "<info>%s</info>"', $bundle, realpath($dir)));
  61. if (file_exists($targetDir = $dir.'/'.$bundle))
  62. {
  63. throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle));
  64. }
  65. $filesystem = new Filesystem();
  66. $filesystem->mirror(__DIR__.'/../Resources/skeleton/bundle', $targetDir);
  67. Mustache::renderDir($targetDir, array(
  68. 'namespace' => $namespace,
  69. 'bundle' => $bundle,
  70. ));
  71. }
  72. }