diff --git a/src/Symfony/Bundle/FrameworkBundle/Command/InitBundleCommand.php b/src/Symfony/Bundle/FrameworkBundle/Command/InitBundleCommand.php index 3dc63d1a42..c3c2b5ef5e 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Command/InitBundleCommand.php +++ b/src/Symfony/Bundle/FrameworkBundle/Command/InitBundleCommand.php @@ -34,7 +34,19 @@ class InitBundleCommand extends Command ->setDefinition(array( new InputArgument('namespace', InputArgument::REQUIRED, 'The namespace of the bundle to create'), new InputArgument('dir', InputArgument::REQUIRED, 'The directory where to create the bundle'), + new InputArgument('bundleName', InputArgument::OPTIONAL, 'The optional bundle name'), )) + ->setHelp(<<init:bundle command generates a new bundle with a basic skeleton. + +./app/console init:bundle "Application\HelloBundle" src [bundleName] + +The bundle namespace must end with "Bundle" (e.g. Application\HelloBundle) +and can be placed in any directory (e.g. src). +If you don't specify a bundle name (e.g. HelloBundle the bundle name will +be a concatenation of namespace (e.g. ApplicationHelloBundle). +EOT + ) ->setName('init:bundle') ; } @@ -50,11 +62,28 @@ class InitBundleCommand extends Command if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) { throw new \InvalidArgumentException('The namespace must end with Bundle.'); } - - $bundle = strtr($namespace, array('\\' => '')); + + //validate namespace + if (preg_match('/[^A-Za-z0-9_\-\\\]/', $namespace)) { + throw new \InvalidArgumentException('The namespace contains invalid characters.'); + } + + //user specified bundle name? + if ('' == ($bundle = $input->getArgument('bundleName'))) { + $bundle = strtr($namespace, array('\\' => '')); + } else { + if (!preg_match('/Bundle$/', $bundle)) { + throw new \InvalidArgumentException('The bundleName must end with Bundle.'); + } + } $dir = $input->getArgument('dir'); + + //add trailing / if necessary + $dir = ('/' == substr($dir, -1, 1)) ? $dir : $dir . '/'; + $targetDir = $dir . strtr($namespace, '\\', '/'); + $output->writeln(sprintf('Initializing bundle "%s" in "%s"', $bundle, $dir)); if (file_exists($targetDir)) {