Sulu\Bundle\GeneratorBundle\Command\Validators::validateBundleNamespace PHP Method

validateBundleNamespace() public static method

public static validateBundleNamespace ( $namespace )
    public static function validateBundleNamespace($namespace)
    {
        if (!preg_match('/Bundle$/', $namespace)) {
            throw new \InvalidArgumentException('The namespace must end with Bundle.');
        }
        $namespace = strtr($namespace, '/', '\\');
        if (!preg_match('/^(?:[a-zA-Z_\\x7f-\\xff][a-zA-Z0-9_\\x7f-\\xff]*\\\\?)+$/', $namespace)) {
            throw new \InvalidArgumentException('The namespace contains invalid characters.');
        }
        // validate reserved keywords
        $reserved = self::getReservedWords();
        foreach (explode('\\', $namespace) as $word) {
            if (in_array(strtolower($word), $reserved)) {
                throw new \InvalidArgumentException(sprintf('The namespace cannot contain PHP reserved words ("%s").', $word));
            }
        }
        // validate that the namespace is at least one level deep
        if (false === strpos($namespace, '\\')) {
            $msg = [];
            $msg[] = sprintf('The namespace must contain a vendor namespace (e.g. "VendorName\\%s" instead of simply "%s").', $namespace, $namespace);
            $msg[] = 'If you\'ve specified a vendor namespace, did you forget to surround it with quotes (init:bundle "Acme\\BlogBundle")?';
            throw new \InvalidArgumentException(implode("\n\n", $msg));
        }
        return $namespace;
    }

Usage Example

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'), '']);
 }