public function getImportCode(Bundle $bundle)
    {
        return sprintf(<<<EOF
    - { resource: "@%s/Resources/config/%s" }
EOF
, $bundle->getName(), $bundle->getServicesConfigurationFilename());
    }
 /**
  * @dataProvider getNonInteractiveCommandData
  */
 public function testNonInteractiveCommand($options, $expected)
 {
     list($namespace, $bundleName, $dir, $format, $shared) = $expected;
     $bundle = new Bundle($namespace, $bundleName, $dir, $format, $shared);
     $container = $this->getContainer();
     // not shared? the tests should be at the root of the project
     if (!$shared) {
         $bundle->setTestsDirectory($container->getParameter('kernel.root_dir') . '/../tests/' . $bundleName);
     }
     $generator = $this->getGenerator();
     $generator->expects($this->once())->method('generateBundle')->with($bundle);
     $tester = new CommandTester($this->getCommand($generator, '', $container));
     $tester->execute($options, array('interactive' => false));
 }
 /**
  * Creates the Bundle object based on the user's (non-interactive) input.
  *
  * @param InputInterface $input
  *
  * @return Bundle
  */
 protected function createBundleObject(InputInterface $input)
 {
     foreach (array('namespace', 'dir') as $option) {
         if (null === $input->getOption($option)) {
             throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
         }
     }
     $shared = $input->getOption('shared');
     $namespace = Validators::validateBundleNamespace($input->getOption('namespace'), $shared);
     if (!($bundleName = $input->getOption('bundle-name'))) {
         $bundleName = strtr($namespace, array('\\' => ''));
     }
     $bundleName = Validators::validateBundleName($bundleName);
     $dir = $input->getOption('dir');
     if (null === $input->getOption('format')) {
         $input->setOption('format', 'annotation');
     }
     $format = Validators::validateFormat($input->getOption('format'));
     // an assumption that the kernel root dir is in a directory (like app/)
     $projectRootDirectory = $this->getContainer()->getParameter('kernel.root_dir') . '/..';
     if (!$this->getContainer()->get('filesystem')->isAbsolutePath($dir)) {
         $dir = $projectRootDirectory . '/' . $dir;
     }
     // add trailing / if necessary
     $dir = '/' === substr($dir, -1, 1) ? $dir : $dir . '/';
     $bundle = new Bundle($namespace, $bundleName, $dir, $format, $shared);
     // not shared - put the tests in the root
     if (!$shared) {
         $testsDir = $projectRootDirectory . '/tests/' . $bundleName;
         $bundle->setTestsDirectory($testsDir);
     }
     return $bundle;
 }
 public function generateBundle(Bundle $bundle)
 {
     $dir = $bundle->getTargetDirectory();
     if (file_exists($dir)) {
         if (!is_dir($dir)) {
             throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" exists but is a file.', realpath($dir)));
         }
         $files = scandir($dir);
         if ($files != array('.', '..')) {
             throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not empty.', realpath($dir)));
         }
         if (!is_writable($dir)) {
             throw new \RuntimeException(sprintf('Unable to generate the bundle as the target directory "%s" is not writable.', realpath($dir)));
         }
     }
     $parameters = array('namespace' => $bundle->getNamespace(), 'bundle' => $bundle->getName(), 'format' => $bundle->getConfigurationFormat(), 'bundle_basename' => $bundle->getBasename(), 'extension_alias' => $bundle->getExtensionAlias());
     $this->renderFile('bundle/Bundle.php.twig', $dir . '/' . $bundle->getName() . '.php', $parameters);
     if ($bundle->shouldGenerateDependencyInjectionDirectory()) {
         $this->renderFile('bundle/Extension.php.twig', $dir . '/DependencyInjection/' . $bundle->getBasename() . 'Extension.php', $parameters);
         $this->renderFile('bundle/Configuration.php.twig', $dir . '/DependencyInjection/Configuration.php', $parameters);
     }
     $this->renderFile('bundle/DefaultController.php.twig', $dir . '/Controller/DefaultController.php', $parameters);
     $this->renderFile('bundle/DefaultControllerTest.php.twig', $dir . '/Tests/Controller/DefaultControllerTest.php', $parameters);
     $this->renderFile('bundle/index.html.twig.twig', $dir . '/Resources/views/Default/index.html.twig', $parameters);
     // render the services.yml/xml file
     $servicesFilename = $bundle->getServicesConfigurationFilename();
     $this->renderFile(sprintf('bundle/%s.twig', $servicesFilename), $dir . '/Resources/config/' . $servicesFilename, $parameters);
     if ($routingFilename = $bundle->getRoutingConfigurationFilename()) {
         $this->renderFile(sprintf('bundle/%s.twig', $routingFilename), $dir . '/Resources/config/' . $routingFilename, $parameters);
     }
 }
Example #5
0
 public function testAlternateTestsDirectory()
 {
     $bundle = new Bundle('Foo\\BarBundle', 'FooBarBundle', $this->tmpDir, 'xml', true);
     $bundle->setTestsDirectory($this->tmpDir . '/other/path/tests');
     $this->getGenerator()->generateBundle($bundle);
     $this->assertTrue(file_exists($this->tmpDir . '/other/path/tests/Controller/DefaultControllerTest.php'));
 }
 protected function updateConfiguration(OutputInterface $output, Bundle $bundle)
 {
     $targetConfigurationPath = $this->getContainer()->getParameter('kernel.root_dir') . '/config/config.yml';
     $output->write(sprintf('> Importing the bundle\'s %s from the <info>%s</info> file: ', $bundle->getServicesConfigurationFilename(), $this->makePathRelative($targetConfigurationPath)));
     $manipulator = new ConfigurationManipulator($targetConfigurationPath);
     try {
         $manipulator->addResource($bundle);
     } catch (\RuntimeException $e) {
         return array('- Import the bundle\'s %s resource in the app\'s main configuration file:', '', $manipulator->getImportCode($bundle), '');
     }
 }
 protected function updateRouting(OutputInterface $output, Bundle $bundle)
 {
     $targetRoutingPath = $this->getContainer()->getParameter('kernel.root_dir') . '/config/routing.yml';
     $output->write(sprintf('> Importing the bundle\'s routes from the <info>%s</info> file: ', $this->makePathRelative($targetRoutingPath)));
     $routing = new RoutingManipulator($targetRoutingPath);
     try {
         $ret = $routing->addResource($bundle->getName(), $bundle->getConfigurationFormat(), '/admin');
         if (!$ret) {
             $help = sprintf("        <comment>resource: \"@%s/Resources/config/routing.%s\"</comment>\n", $bundle->getName(), $bundle->getConfigurationFormat());
             $help .= "        <comment>prefix:   /admin</comment>\n";
             return array('- Import the bundle\'s routing resource in the app\'s main routing file:', '', sprintf('    <comment>%s:</comment>', $bundle->getName()), $help, '');
         }
     } catch (\RuntimeException $e) {
         return array(sprintf('Bundle <comment>%s</comment> is already imported.', $bundle->getName()), '');
     }
 }