Newscoop\PluginGeneratorBundle\Command\Validators::validateBundleNamespace PHP Method

validateBundleNamespace() public static method

public static validateBundleNamespace ( $namespace, $requireVendorNamespace = true )
    public static function validateBundleNamespace($namespace, $requireVendorNamespace = true)
    {
        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 ($requireVendorNamespace && false === strpos($namespace, '\\')) {
            // language is (almost) duplicated in GenerateBundleCommand
            $msg = array();
            $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

 /**
  * @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)
 {
     $dialog = $this->getDialogHelper();
     if ($input->isInteractive()) {
         if (!$dialog->askConfirmation($output, $dialog->getQuestion('Do you confirm generation', 'yes', '?'), true)) {
             $output->writeln('<error>Command aborted</error>');
             return 1;
         }
     }
     foreach (array('plugin-name', 'namespace', 'dir') as $option) {
         if (null === $input->getOption($option)) {
             throw new \RuntimeException(sprintf('The "%s" option must be provided.', $option));
         }
     }
     // validate the namespace
     $vendor = Validators::validateVendor($input->getOption('vendor'));
     $pluginName = Validators::validatePluginName($input->getOption('plugin-name'));
     $namespace = Validators::validateBundleNamespace($input->getOption('namespace'), false);
     if (!($bundle = $input->getOption('bundle-name'))) {
         $bundle = strtr($namespace, array('\\' => ''));
     }
     $bundle = Validators::validateBundleName($bundle);
     $dir = Validators::validateTargetDir($input->getOption('dir'));
     if (null === $input->getOption('format')) {
         $input->setOption('format', 'annotation');
     }
     $format = Validators::validateFormat($input->getOption('format'));
     $structure = $input->getOption('structure');
     $admin = $input->getOption('admin');
     $zip = $input->getOption('zip');
     $dialog->writeSection($output, 'Bundle generation');
     if (!$this->getContainer()->get('filesystem')->isAbsolutePath($dir)) {
         $dir = getcwd() . '/' . $dir;
     }
     $generator = $this->getGenerator();
     $generator->generate($vendor, $pluginName, $namespace, $bundle, $dir, $format, $structure, $admin, $zip);
     $output->writeln('Generating the bundle code: <info>OK</info>');
     $errors = array();
     $dialog->writeGeneratorSummary($output, $errors);
     $output->writeln('You must run the plugin install command manually: <comment>php application/console plugins:install ' . strtolower($vendor) . '/' . strtolower($pluginName) . '-plugin-bundle</comment>');
 }