validateTargetDir() public static method

public static validateTargetDir ( $dir, $bundle, $namespace )
Example #1
0
 protected function interact(InputInterface $input, OutputInterface $output)
 {
     $dialog = $this->getDialogHelper();
     $dialog->writeSection($output, 'Welcome to the Sulu bundle generator');
     // namespace
     $namespace = null;
     try {
         $namespace = $input->getOption('namespace') ? Validators::validateBundleNamespace($input->getOption('namespace')) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $namespace) {
         $output->writeln(['', 'Your application code must be written in <comment>bundles</comment>. This command helps', 'you generate them easily.', '', 'Each bundle is hosted under a namespace (like <comment>Acme/Bundle/BlogBundle</comment>).', 'The namespace should begin with a "vendor" name like your company name, your', 'project name, or your client name, followed by one or more optional category', 'sub-namespaces, and it should end with the bundle name itself', '(which must have <comment>Bundle</comment> as a suffix).', '', 'See http://symfony.com/doc/current/cookbook/bundles/best_practices.html#index-1 for more', 'details on bundle naming conventions.', '', 'Use <comment>/</comment> instead of <comment>\\ </comment> for the namespace delimiter to avoid any problem.', '']);
         $namespace = $dialog->askAndValidate($output, $dialog->getQuestion('Bundle namespace', $input->getOption('namespace')), ['Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateBundleNamespace'], false, $input->getOption('namespace'));
         $input->setOption('namespace', $namespace);
     }
     // bundle name
     $bundle = null;
     try {
         $bundle = $input->getOption('bundle-name') ? Validators::validateBundleName($input->getOption('bundle-name')) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $bundle) {
         $bundle = strtr($namespace, ['\\Bundle\\' => '', '\\' => '']);
         $output->writeln(['', 'In your code, a bundle is often referenced by its name. It can be the', 'concatenation of all namespace parts but it\'s really up to you to come', 'up with a unique name (a good practice is to start with the vendor name).', 'Based on the namespace, we suggest <comment>' . $bundle . '</comment>.', '']);
         $bundle = $dialog->askAndValidate($output, $dialog->getQuestion('Bundle name', $bundle), ['Sensio\\Bundle\\GeneratorBundle\\Command\\Validators', 'validateBundleName'], false, $bundle);
         $input->setOption('bundle-name', $bundle);
     }
     // target dir
     $dir = null;
     try {
         $dir = $input->getOption('dir') ? Validators::validateTargetDir($input->getOption('dir'), $bundle, $namespace) : null;
     } catch (\Exception $error) {
         $output->writeln($dialog->getHelperSet()->get('formatter')->formatBlock($error->getMessage(), 'error'));
     }
     if (null === $dir) {
         $d = strtolower($namespace);
         $d = str_replace('\\', '/', $d);
         $d = str_replace('/bundle/', '/', $d);
         $d = str_replace('bundle', '', $d);
         $dir = dirname($this->getContainer()->getParameter('kernel.root_dir')) . '/vendor/' . $d . '-bundle/';
         $output->writeln(['', 'The bundle can be generated anywhere. The suggested default directory uses', 'the standard conventions.', '']);
         $dir = $dialog->askAndValidate($output, $dialog->getQuestion('Target directory', $dir), function ($dir) use($bundle, $namespace) {
             return Validators::validateTargetDir($dir, $bundle, $namespace);
         }, false, $dir);
         $input->setOption('dir', $dir);
     }
     // optional files to generate
     $output->writeln(['', 'To help you get started faster, the command can generate some', 'code snippets for you.', '']);
     $structure = $input->getOption('structure');
     if (!$structure && $dialog->askConfirmation($output, $dialog->getQuestion('Do you want to generate the whole directory structure', 'yes', '?'), true)) {
         $structure = true;
     }
     $input->setOption('structure', $structure);
     // summary
     $output->writeln(['', $this->getHelper('formatter')->formatBlock('Summary before generation', 'bg=blue;fg=white', true), '', sprintf("You are going to generate a \"<info>%s\\%s</info>\" bundle\nin \"<info>%s</info>\" using the \"<info>%s</info>\" format.", $namespace, $bundle, $dir, 'yml'), '']);
 }