/**
  * {@inheritdoc}
  */
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $questionHelper = $this->getQuestionHelper();
     $questionHelper->writeSection($output, 'Welcome to the Kunstmaan default site generator');
     $inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $questionHelper, $this->getApplication()->getKernel(), $this->getContainer());
     $inputAssistant->askForNamespace(array('', 'This command helps you to generate tests to test the admin of the default site setup.', 'You must specify the namespace of the bundle where you want to generate the tests.', 'Use <comment>/</comment> instead of <comment>\\ </comment>for the namespace delimiter to avoid any problem.', ''));
 }
 /**
  * @param ContainerInterface $container   The container
  * @param Filesystem         $filesystem  The filesytem
  * @param string             $skeletonDir The skeleton directory
  */
 public function __construct(ContainerInterface $container, Filesystem $filesystem, $skeletonDir)
 {
     $this->container = $container;
     $this->filesystem = $filesystem;
     $this->skeletonDir = $skeletonDir;
     $this->fullSkeletonDir = GeneratorUtils::getFullSkeletonPath($skeletonDir);
 }
 /**
  * Executes the command.
  *
  * @param InputInterface $input An InputInterface instance
  * @param OutputInterface $output An OutputInterface instance
  *
  * @throws \RuntimeException
  * @return void
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $questionHelper = $this->getQuestionHelper();
     if ($input->isInteractive()) {
         $confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Do you confirm generation', 'yes', '?'), true);
         if (!$questionHelper->ask($input, $output, $confirmationQuestion)) {
             $output->writeln('<error>Command aborted</error>');
             return 1;
         }
     }
     GeneratorUtils::ensureOptionsProvided($input, array('namespace', 'dir'));
     $namespace = Validators::validateBundleNamespace($input->getOption('namespace'));
     if (!($bundle = $input->getOption('bundle-name'))) {
         $bundle = strtr($namespace, array('\\' => ''));
     }
     $bundle = Validators::validateBundleName($bundle);
     $dir = $this::validateTargetDir($input->getOption('dir'), $bundle, $namespace);
     $format = 'yml';
     $questionHelper->writeSection($output, 'Bundle generation');
     if (!$this->getContainer()->get('filesystem')->isAbsolutePath($dir)) {
         $dir = getcwd() . '/' . $dir;
     }
     $generator = $this->getGenerator($this->getApplication()->getKernel()->getBundle("KunstmaanGeneratorBundle"));
     $generator->generate($namespace, $bundle, $dir, $format);
     $output->writeln('Generating the bundle code: <info>OK</info>');
     $errors = array();
     $runner = $questionHelper->getRunner($output, $errors);
     // check that the namespace is already autoloaded
     $runner($this->checkAutoloader($output, $namespace, $bundle));
     // register the bundle in the Kernel class
     $runner($this->updateKernel($questionHelper, $input, $output, $this->getContainer()->get('kernel'), $namespace, $bundle));
     // routing
     $runner($this->updateRouting($questionHelper, $input, $output, $bundle, $format));
     $questionHelper->writeGeneratorSummary($output, $errors);
 }
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $questionHelper = $this->getQuestionHelper();
     $questionHelper->writeSection($output, 'Welcome to the Kunstmaan Article generator');
     $inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $questionHelper, $this->getApplication()->getKernel(), $this->getContainer());
     $inputAssistant->askForNamespace(array('', 'This command helps you to generate the Article classes.', 'You must specify the namespace of the bundle where you want to generate the classes in.', 'Use <comment>/</comment> instead of <comment>\\ </comment>for the namespace delimiter to avoid any problem.', ''));
     // entity
     $entity = $input->getOption('entity') ? $input->getOption('entity') : null;
     if (is_null($entity)) {
         $output->writeln(array('', 'You must specify a name for the collection of Article entities.', 'This name will be prefixed before every new entity.', 'For example entering <comment>News</comment> will result in:', '<comment>News</comment>OverviewPage, <comment>News</comment>Page and <comment>News</comment>Author', ''));
         $entityValidation = function ($entity) {
             if (empty($entity)) {
                 throw new \RuntimeException('You have to provide a entity name!');
             } elseif (!preg_match('/^[a-zA-Z][a-zA-Z_0-9]+$/', $entity)) {
                 throw new \InvalidArgumentException(sprintf("%s" . ' contains invalid characters', $entity));
             } else {
                 return $entity;
             }
         };
         $question = new Question($questionHelper->getQuestion('Name', $entity), $entity);
         $question->setValidator($entityValidation);
         $entity = $questionHelper->ask($input, $output, $question);
         $input->setOption('entity', $entity);
     }
     $inputAssistant->askForPrefix();
 }
 /**
  * @param Bundle          $bundle     The bundle
  * @param array           $parameters The template parameters
  * @param string          $rootDir    The root directory
  * @param OutputInterface $output
  */
 public function generateTemplates(Bundle $bundle, array $parameters, $rootDir, OutputInterface $output)
 {
     $dirPath = $bundle->getPath();
     $fullSkeletonDir = $this->skeletonDir . '/Resources/views';
     $this->filesystem->copy(__DIR__ . '/../Resources/SensioGeneratorBundle/skeleton' . $fullSkeletonDir . '/Pages/Search/SearchPage/view.html.twig', $dirPath . '/Resources/views/Pages/Search/SearchPage/view.html.twig', true);
     GeneratorUtils::prepend("{% extends '" . $bundle->getName() . ":Page:layout.html.twig' %}\n", $dirPath . '/Resources/views/Pages/Search/SearchPage/view.html.twig');
     $output->writeln('Generating Twig Templates : <info>OK</info>');
 }
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $dialog = $this->getDialogHelper();
     $dialog->writeSection($output, 'Welcome to the SearchPage generator');
     $inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $dialog, $this->getApplication()->getKernel(), $this->getContainer());
     $inputAssistant->askForNamespace(array('', 'This command helps you to generate a SearchPage.', 'You must specify the namespace of the bundle where you want to generate the SearchPage in.', 'Use <comment>/</comment> instead of <comment>\\ </comment>for the namespace delimiter to avoid any problem.', ''));
     $inputAssistant->askForPrefix();
 }
 /**
  * Generate the website.
  *
  * @param BundleInterface $bundle
  * @param string $prefix
  * @param string $rootDir
  * @param bool $demosite
  */
 public function generate(BundleInterface $bundle, $prefix, $rootDir, $demosite = false)
 {
     $this->bundle = $bundle;
     $this->prefix = GeneratorUtils::cleanPrefix($prefix);
     $this->rootDir = $rootDir;
     $this->demosite = $demosite;
     $parameters = array('namespace' => $this->bundle->getNamespace(), 'bundle' => $this->bundle, 'bundle_name' => $this->bundle->getName(), 'prefix' => $this->prefix, 'demosite' => $this->demosite, 'multilanguage' => $this->isMultiLangEnvironment());
     $this->generateControllers($parameters);
     $this->generateAdminLists($parameters);
     $this->generateEntities($parameters);
     $this->generateFormTypes($parameters);
     $this->generateTwigExtensions($parameters);
     $this->generateMenuAdaptors($parameters);
     $this->generateFixtures($parameters);
     $this->generatePagepartConfigs($parameters);
     $this->generatePagetemplateConfigs($parameters);
     $this->generateConfig();
     $this->generateRouting($parameters);
     $this->generateTemplates($parameters);
 }
 /**
  * @param Bundle          $bundle     The bundle
  * @param string          $entity     The name of the entity
  * @param array           $parameters The template parameters
  * @param OutputInterface $output
  */
 public function generateTemplates(Bundle $bundle, $entity, array $parameters, OutputInterface $output)
 {
     $dirPath = sprintf("%s/Resources/views", $bundle->getPath());
     $skeletonDir = sprintf("%s/Resources/views", $this->skeletonDir);
     $fullSkeletonDir = sprintf("%s/Resources/views", $this->fullSkeletonDir);
     $this->filesystem->copy($fullSkeletonDir . '/OverviewPage/view.html.twig', $dirPath . '/Pages/' . $entity . 'OverviewPage/view.html.twig', true);
     GeneratorUtils::prepend("{% extends '" . $bundle->getName() . ":Layout:layout.html.twig' %}\n", $dirPath . '/Pages/' . $entity . 'OverviewPage/view.html.twig');
     $this->filesystem->copy($fullSkeletonDir . '/OverviewPage/pagetemplate.html.twig', $dirPath . '/Pages/' . $entity . 'OverviewPage/pagetemplate.html.twig', true);
     $this->filesystem->copy($fullSkeletonDir . '/Page/view.html.twig', $dirPath . '/Pages/' . $entity . 'Page/view.html.twig', true);
     GeneratorUtils::prepend("{% extends '" . $bundle->getName() . ":Layout:layout.html.twig' %}\n", $dirPath . '/Pages/' . $entity . 'Page/view.html.twig');
     $this->filesystem->copy($fullSkeletonDir . '/Page/pagetemplate.html.twig', $dirPath . '/Pages/' . $entity . 'Page/pagetemplate.html.twig', true);
     $this->renderFile($skeletonDir . '/PageAdminList/list.html.twig', $dirPath . '/AdminList/' . '/' . $entity . 'PageAdminList/list.html.twig', $parameters);
     $output->writeln('Generating twig templates : <info>OK</info>');
 }
 /**
  * Install the default pagepart configuration.
  *
  * @param BundleInterface $bundle
  */
 protected function installDefaultPagePartConfiguration($bundle)
 {
     // Pagepart configuration
     $dirPath = sprintf('%s/Resources/config/pageparts/', $bundle->getPath());
     $skeletonDir = sprintf('%s/Resources/config/pageparts/', GeneratorUtils::getFullSkeletonPath('/common'));
     // Only copy when folder does not exist yet
     if (!$this->filesystem->exists($dirPath)) {
         $files = array('footer.yml', 'main.yml', 'left-sidebar.yml', 'right-sidebar.yml');
         foreach ($files as $file) {
             $this->filesystem->copy($skeletonDir . $file, $dirPath . $file, false);
         }
     }
 }
 /**
  * @covers Kunstmaan\GeneratorBundle\Helper\GeneratorUtils::cleanPrefix
  */
 public function testSpacesShouldCreateEmptyPrefix()
 {
     $response = GeneratorUtils::cleanPrefix('  ');
     $this->assertEquals(null, $response);
 }
 /**
 * KunstmaanTestBundle_TestEntity:
     resource: "@KunstmaanTestBundle/Controller/TestEntityAdminListController.php"
     type:     annotation
     prefix:   /{_locale}/admin/testentity/
     requirements:
     _locale: %requiredlocales%
 */
 protected function createGenerator()
 {
     return new AdminListGenerator($this->getContainer()->get('filesystem'), GeneratorUtils::getFullSkeletonPath('adminlist'));
 }
 /**
  * @param Filesystem $filesystem  The filesytem
  * @param string     $skeletonDir The skeleton directory
  */
 public function __construct(Filesystem $filesystem, $skeletonDir)
 {
     $this->filesystem = $filesystem;
     $this->skeletonDir = $skeletonDir;
     $this->fullSkeletonDir = GeneratorUtils::getFullSkeletonPath($skeletonDir);
 }
 /**
  * Ask for which bundle we need to generate something. It there is only one custom bundle
  * created by the user, we don't ask anything and just use that bundle. If the user provided
  * a namespace as input option, we try to get that bundle first.
  *
  * @param string      $objectName The thing we are going to create (pagepart, bundle, layout, ...)
  * @param string|null $namespace  The namespace provided as input option
  *
  * @return BundleInterface
  */
 protected function askForBundleName($objectName, $namespace = null)
 {
     $ownBundles = $this->getOwnBundles();
     if (count($ownBundles) <= 0) {
         $this->assistant->writeError("Looks like you don't have created any bundles for your project...", true);
     }
     // If the user provided the namespace as input option
     if (!is_null($namespace)) {
         foreach ($ownBundles as $key => $bundleInfo) {
             if (GeneratorUtils::fixNamespace($namespace) == GeneratorUtils::fixNamespace($bundleInfo['namespace'])) {
                 $bundleName = $bundleInfo['name'];
                 break;
             }
         }
         // When the provided namespace was not found, we show an error on the screen and ask the bundle again
         if (empty($bundleName)) {
             $this->assistant->writeError("The provided bundle namespace '{$namespace}' was not found...");
         }
     }
     if (empty($bundleName)) {
         // If we only have 1 bundle, we don't need to ask
         if (count($ownBundles) > 1) {
             $bundleSelect = array();
             foreach ($ownBundles as $key => $bundleInfo) {
                 $bundleSelect[$key] = $bundleInfo['name'];
             }
             $bundleId = $this->assistant->askSelect('In which bundle do you want to create the ' . $objectName, $bundleSelect);
             $bundleName = $ownBundles[$bundleId]['name'];
             $this->assistant->writeLine('');
         } else {
             $bundleName = $ownBundles[1]['name'];
             $this->assistant->writeLine(array("The {$objectName} will be created for the <comment>{$bundleName}</comment> bundle.\n"));
         }
     }
     $bundle = $this->assistant->getKernel()->getBundle($bundleName);
     return $bundle;
 }
 /**
  * Asks for the prefix and sets it on the InputInterface as the 'prefix' option, if this option is not set yet.
  * Will set the default to a snake_cased namespace when the namespace has been set on the InputInterface.
  *
  * @param array  $text What you want printed before the prefix is asked. If null is provided it'll write a default text.
  * @param string $namespace An optional namespace. If this is set it'll create the default based on this prefix.
  *  If it's not provided it'll check if the InputInterface already has the namespace option.
  *
  * @return string The prefix. But it's also been set on the InputInterface.
  */
 public function askForPrefix(array $text = null, $namespace = null)
 {
     $prefix = $this->input->hasOption('prefix') ? $this->input->getOption('prefix') : null;
     if (is_null($text)) {
         $text = array('', 'You can add a prefix to the table names of the generated entities for example: <comment>projectname_bundlename_</comment>', 'Enter an underscore \'_\' if you don\'t want a prefix.', '');
     }
     if (is_null($prefix)) {
         if (count($text) > 0) {
             $this->output->writeln($text);
         }
         if (is_null($namespace) || empty($namespace)) {
             $namespace = $this->input->hasOption('namespace') ? $this->input->getOption('namespace') : null;
         } else {
             $namespace = $this->fixNamespace($namespace);
         }
         $defaultPrefix = GeneratorUtils::cleanPrefix($this->convertNamespaceToSnakeCase($namespace));
         $prefix = $this->dialog->ask($this->output, $this->dialog->getQuestion('Tablename prefix', $defaultPrefix), $defaultPrefix);
         $prefix = GeneratorUtils::cleanPrefix($prefix);
         if ($this->input->hasOption('prefix')) {
             $this->input->setOption('prefix', $prefix);
         }
     }
     return $prefix;
 }
 /**
  * @param ClassMetadata $metadata
  *
  * @return string[]
  */
 private function getFieldsFromMetadata(ClassMetadata $metadata)
 {
     return GeneratorUtils::getFieldsFromMetadata($metadata);
 }
 /**
  * {@inheritdoc}
  */
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $questionHelper = $this->getQuestionHelper();
     $questionHelper->writeSection($output, 'Welcome to the Doctrine2 entity generator');
     // namespace
     $output->writeln(array('', 'This command helps you generate Doctrine2 entities.', '', 'First, you need to give the entity name you want to generate.', 'You must use the shortcut notation like <comment>AcmeBlogBundle:Post</comment>.', ''));
     $bundleNames = array_keys($this->getContainer()->get('kernel')->getBundles());
     /** @var $foundBundle Bundle */
     $foundBundle = $bundle = $entity = null;
     while (true) {
         $question = new Question($questionHelper->getQuestion('The Entity shortcut name', $input->getOption('entity')));
         $question->setValidator(array('Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateEntityName'));
         $question->setAutocompleterValues($bundleNames);
         $entity = $questionHelper->ask($input, $output, $question);
         list($bundle, $entity) = $this->parseShortcutNotation($entity);
         // check entity name
         if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $entity)) {
             $output->writeln(sprintf('<bg=red> "%s" is not a valid entity name.</>', $entity));
             continue;
         }
         // check reserved words
         if ($this->getGenerator()->isReservedKeyword($entity)) {
             $output->writeln(sprintf('<bg=red> "%s" is a reserved word</>.', $entity));
             continue;
         }
         try {
             $foundBundle = $this->getContainer()->get('kernel')->getBundle($bundle);
             if (!file_exists($foundBundle->getPath() . '/Entity/' . str_replace('\\', '/', $entity) . '.php')) {
                 break;
             }
             $output->writeln(sprintf('<bg=red>Entity "%s:%s" already exists</>.', $bundle, $entity));
         } catch (\Exception $e) {
             $output->writeln(sprintf('<bg=red>Bundle "%s" does not exist.</>', $bundle));
         }
     }
     $input->setOption('entity', $bundle . ':' . $entity);
     $inputAssistant = GeneratorUtils::getInputAssistant($input, $output, $questionHelper, $this->getApplication()->getKernel(), $this->getContainer());
     $inputAssistant->askForPrefix(null, $foundBundle->getNamespace());
     // fields
     $input->setOption('fields', $this->addFields($input, $output, $questionHelper));
     // repository?
     $output->writeln('');
     $confirmationQuestion = new ConfirmationQuestion($questionHelper->getQuestion('Do you want to generate an empty repository class', $input->getOption('with-repository') ? 'yes' : 'no', '?'), $input->getOption('with-repository'));
     $withRepository = $questionHelper->ask($input, $output, $confirmationQuestion);
     $input->setOption('with-repository', $withRepository);
     // summary
     $output->writeln(array('', $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), '', sprintf("You are going to generate a \"<info>%s:%s</info>\" Doctrine2 entity", $bundle, $entity), ''));
 }
 /**
  * KunstmaanTestBundle_TestEntity:
  * resource: "@KunstmaanTestBundle/Controller/TestEntityAdminListController.php"
  * type:     annotation
  * prefix:   /{_locale}/admin/testentity/
  * requirements:
  * _locale: %requiredlocales%
  */
 protected function createGenerator()
 {
     return new AdminListGenerator(GeneratorUtils::getFullSkeletonPath('adminlist'));
 }
 /**
  * Asks for the prefix and sets it on the InputInterface as the 'prefix' option, if this option is not set yet.
  * Will set the default to a snake_cased namespace when the namespace has been set on the InputInterface.
  *
  * @param array $text What you want printed before the prefix is asked. If null is provided it'll write a default text.
  * @param string $namespace An optional namespace. If this is set it'll create the default based on this prefix.
  *  If it's not provided it'll check if the InputInterface already has the namespace option.
  *
  * @return string The prefix. But it's also been set on the InputInterface.
  */
 public function askForPrefix(array $text = null, $namespace = null)
 {
     $prefix = $this->input->hasOption('prefix') ? $this->input->getOption('prefix') : null;
     if (is_null($text)) {
         $text = array('', 'You can add a prefix to the table names of the generated entities for example: <comment>projectname_bundlename_</comment>', 'Enter an underscore \'_\' if you don\'t want a prefix.', '');
     }
     while (is_null($prefix)) {
         if (count($text) > 0) {
             $this->output->writeln($text);
         }
         if (is_null($namespace) || empty($namespace)) {
             $namespace = $this->input->hasOption('namespace') ? $this->input->getOption('namespace') : null;
         } else {
             $namespace = $this->fixNamespace($namespace);
         }
         $defaultPrefix = GeneratorUtils::cleanPrefix($this->convertNamespaceToSnakeCase($namespace));
         $question = new Question($this->questionHelper->getQuestion('Tablename prefix', $defaultPrefix), $defaultPrefix);
         $prefix = $this->questionHelper->ask($this->input, $this->output, $question);
         $prefix = GeneratorUtils::cleanPrefix($prefix);
         if ($this->input->hasOption('prefix')) {
             $this->input->setOption('prefix', $prefix);
         }
         if ($prefix == '') {
             break;
         }
         if (!preg_match('/^[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*$/', $prefix)) {
             $this->output->writeln(sprintf('<bg=red> "%s" contains invalid characters</>', $prefix));
             $prefix = $text = null;
             continue;
         }
     }
     return $prefix;
 }
 /**
  * Copy and modify default config files for the pagetemplate and pageparts.
  */
 private function copyTemplateConfig()
 {
     $dirPath = $this->bundle->getPath();
     $pagepartFile = $dirPath . '/Resources/config/pageparts/' . $this->template . '.yml';
     $this->filesystem->copy($this->skeletonDir . '/Resources/config/pageparts/formpage.yml', $pagepartFile, false);
     GeneratorUtils::replace("~~~ENTITY~~~", $this->entity, $pagepartFile);
     $pagetemplateFile = $dirPath . '/Resources/config/pagetemplates/' . $this->template . '.yml';
     $this->filesystem->copy($this->skeletonDir . '/Resources/config/pagetemplates/formpage.yml', $pagetemplateFile, false);
     GeneratorUtils::replace("~~~BUNDLE~~~", $this->bundle->getName(), $pagetemplateFile);
     GeneratorUtils::replace("~~~ENTITY~~~", $this->entity, $pagetemplateFile);
 }