* * This source file is subject to the MIT license that is bundled * with this source code in the file LICENSE. */ /** * Initializes a new bundle. * * @package Symfony * @subpackage Framework_WebBundle * @author Fabien Potencier */ class InitBundleCommand extends Command { /** * @see Command */ protected function configure() { $this ->setDefinition(array( new InputArgument('namespace', InputArgument::REQUIRED, 'The namespace of the bundle to create'), )) ->setName('init:bundle') ; } /** * @see Command * * @throws \InvalidArgumentException When namespace doesn't end with Bundle * @throws \RuntimeException When bundle can't be executed */ protected function execute(InputInterface $input, OutputInterface $output) { if (!preg_match('/Bundle$/', $namespace = $input->getArgument('namespace'))) { throw new \InvalidArgumentException('The namespace must end with Bundle.'); } $dirs = $this->container->getKernelService()->getBundleDirs(); $tmp = str_replace('\\', '/', $namespace); $namespace = str_replace('/', '\\', dirname($tmp)); $bundle = basename($tmp); if (!isset($dirs[$namespace])) { throw new \InvalidArgumentException(sprintf('Unable to initialize the bundle (%s not defined).', $namespace)); } $dir = $dirs[$namespace]; $output->writeln(sprintf('Initializing bundle "%s" in "%s"', $bundle, realpath($dir))); if (file_exists($targetDir = $dir.'/'.$bundle)) { throw new \RuntimeException(sprintf('Bundle "%s" already exists.', $bundle)); } $filesystem = new Filesystem(); $filesystem->mirror(__DIR__.'/../Resources/skeleton/bundle', $targetDir); Mustache::renderDir($targetDir, array( 'namespace' => $namespace, 'bundle' => $bundle, )); } }