PageRenderTime 65ms CodeModel.GetById 35ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/src/Symfony/Bundle/FrameworkBundle/Command/InitBundleCommand.php

https://github.com/casoetan/ServerGroveLiveChat
PHP | 103 lines | 76 code | 10 blank | 17 comment | 0 complexity | e4618fedbd3824027cd8aea4d75b0899 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, ISC, BSD-3-Clause
  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. } elseif (!preg_match('/Bundle$/', $bundle)) {
  67. throw new \InvalidArgumentException('The bundle name must end with Bundle.');
  68. }
  69. $dir = $input->getArgument('dir');
  70. // add trailing / if necessary
  71. $dir = '/' === substr($dir, -1, 1) ? $dir : $dir . '/';
  72. $targetDir = $dir.strtr($namespace, '\\', '/');
  73. $output->writeln(sprintf('Initializing bundle "<info>%s</info>" in "<info>%s</info>"', $bundle, $dir));
  74. if (file_exists($targetDir)) {
  75. throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle));
  76. }
  77. $filesystem = $this->container->get('filesystem');
  78. $filesystem->mirror(__DIR__.'/../Resources/skeleton/bundle', $targetDir);
  79. Mustache::renderDir($targetDir, array(
  80. 'namespace' => $namespace,
  81. 'bundle' => $bundle,
  82. ));
  83. rename($targetDir.'/Bundle.php', $targetDir.'/'.$bundle.'.php');
  84. }
  85. }