Sensio\Bundle\GeneratorBundle\Manipulator\RoutingManipulator::addResource PHP Method

addResource() public method

Adds a routing resource at the top of the existing ones.
public addResource ( string $bundle, string $format, string $prefix = '/', string $path = 'routing' ) : boolean
$bundle string
$format string
$prefix string
$path string
return boolean true if it worked, false otherwise
    public function addResource($bundle, $format, $prefix = '/', $path = 'routing')
    {
        $code = sprintf("%s:\n", $bundle.('/' !== $prefix ? '_'.str_replace('/', '_', substr($prefix, 1)) : ''));
        if ('annotation' == $format) {
            $code .= sprintf("    resource: \"@%s/Controller/\"\n    type:     annotation\n", $bundle);
        } else {
            $code .= sprintf("    resource: \"@%s/Resources/config/%s.%s\"\n", $bundle, $path, $format);
        }
        $code .= sprintf("    prefix:   %s\n", $prefix);

        $code .= "\n";

        if (file_exists($this->file)) {
            $code .= file_get_contents($this->file);
        } elseif (!is_dir($dir = dirname($this->file))) {
            mkdir($dir, 0777, true);
        }

        // Don't add same bundle twice
        if (false !== strpos($code, $bundle)) {
            throw new \RuntimeException(sprintf('Bundle "%s" is already imported.', $bundle));
        }

        if (false === file_put_contents($this->file, $code)) {
            return false;
        }

        return true;
    }

Usage Example

 protected function updateRouting(InputInterface $input, OutputInterface $output, $bundle, $format = "yml")
 {
     $refClass = new \ReflectionClass($bundle);
     $bundleRoutingFile = sprintf("%s/Resources/config/routing.%s", dirname($refClass->getFileName()), $format);
     if (is_file($bundleRoutingFile)) {
         $routing = new RoutingManipulator($this->getContainer()->getParameter('kernel.root_dir') . '/config/routing.yml');
         $bundleName = substr($bundle, 1 + strrpos($bundle, "\\"));
         try {
             $ret = $routing->addResource($bundleName, $format);
             if (!$ret) {
                 if ('annotation' === $format) {
                     $help = sprintf("        <comment>resource: \"@%s/Controller/\"</comment>\n        <comment>type:     annotation</comment>\n", $bundle);
                 } else {
                     $help = sprintf("        <comment>resource: \"@%s/Resources/config/routing.%s\"</comment>\n", $bundle, $format);
                 }
                 $help .= "        <comment>prefix:   /</comment>\n";
                 $output->writeln("- Import the bundle\\'s routing resource in the app main routing file:\n");
                 $output->writeln(sprintf("    <comment>%s:</comment>\n", $bundle));
                 $output->writeln($help);
             }
         } catch (\RuntimeException $e) {
             $output->writeln(sprintf("Bundle <comment>%s</comment> is already imported.", $bundle));
             throw $e;
         }
     }
 }
All Usage Examples Of Sensio\Bundle\GeneratorBundle\Manipulator\RoutingManipulator::addResource
RoutingManipulator