Sulu\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')
    {
        $current = '';
        if (file_exists($this->file)) {
            $current = file_get_contents($this->file);
            // Don't add same bundle twice
            if (false !== strpos($current, $bundle)) {
                throw new \RuntimeException(sprintf('Bundle "%s" is already imported.', $bundle));
            }
        } elseif (!is_dir($dir = dirname($this->file))) {
            mkdir($dir, 0777, true);
        }
        $bundleName = substr($bundle, 0, -6);
        $prefixName = '/' !== $prefix ? '_' . str_replace('/', '_', substr($prefix, 1)) : '';
        $prefixName = strpos($bundleName, $prefixName) !== false ? $prefixName : '';
        $code = sprintf("%s:\n", Container::underscore($bundleName) . $prefixName);
        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";
        $code .= $current;
        if (false === file_put_contents($this->file, $code)) {
            return false;
        }
        return true;
    }

Usage Example

Example #1
0
 protected function updateRouting(DialogHelper $dialog, InputInterface $input, OutputInterface $output, $bundle, $format = 'yml')
 {
     $auto = true;
     $this->route = '/';
     if ($input->isInteractive()) {
         $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of the Routing', 'yes', '?'), true);
         if ($auto) {
             $this->route = $dialog->askAndValidate($output, 'Route prefix [length > 3]: ', function ($x) {
                 if (strlen($x) < 3) {
                     return false;
                 }
                 if ($x[0] != '/') {
                     $x = $x = '/' . $x;
                 }
                 return $x;
             });
         }
     }
     $output->write('Importing the bundle routing resource: ');
     $routing = new RoutingManipulator($this->getContainer()->getParameter('kernel.root_dir') . '/config/routing.yml');
     try {
         $ret = $auto ? $routing->addResource($bundle, $format, $this->route) : false;
         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";
             return ['- Import the bundle\'s routing resource in the app main routing file:', '', sprintf('    <comment>%s:</comment>', $bundle), $help, ''];
         }
     } catch (\RuntimeException $e) {
         return [sprintf('Bundle <comment>%s</comment> is already imported.', $bundle), ''];
     }
 }
RoutingManipulator