Sensio\Bundle\GeneratorBundle\Manipulator\KernelManipulator::addBundle PHP Method

addBundle() public method

Adds a bundle at the end of the existing ones.
public addBundle ( string $bundle ) : boolean
$bundle string The bundle class name
return boolean true if it worked, false otherwise
    public function addBundle($bundle)
    {
        if (!$this->reflected->getFilename()) {
            return false;
        }

        $src = file($this->reflected->getFilename());
        $method = $this->reflected->getMethod('registerBundles');
        $lines = array_slice($src, $method->getStartLine() - 1, $method->getEndLine() - $method->getStartLine() + 1);

        // Don't add same bundle twice
        if (false !== strpos(implode('', $lines), $bundle)) {
            throw new \RuntimeException(sprintf('Bundle "%s" is already defined in "AppKernel::registerBundles()".', $bundle));
        }

        $this->setCode(token_get_all('<?php '.implode('', $lines)), $method->getStartLine());
        while ($token = $this->next()) {
            // $bundles
            if (T_VARIABLE !== $token[0] || '$bundles' !== $token[1]) {
                continue;
            }

            // =
            $this->next();

            // array
            $token = $this->next();
            if (T_ARRAY !== $token[0]) {
                return false;
            }

            // add the bundle at the end of the array
            while ($token = $this->next()) {
                // look for );
                if (')' !== $this->value($token)) {
                    continue;
                }

                if (';' !== $this->value($this->peek())) {
                    continue;
                }

                // ;
                $this->next();

                $lines = array_merge(
                    array_slice($src, 0, $this->line - 1),
                    array(sprintf("            new %s(),\n", $bundle)),
                    array_slice($src, $this->line - 1)
                );

                file_put_contents($this->reflected->getFilename(), implode('', $lines));

                return true;
            }
        }
    }

Usage Example

Esempio n. 1
0
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     /** @var Kernel $kernel */
     $kernel = $this->getContainer()->get('kernel');
     $components = $this->getContainer()->getParameter('components');
     $kernelManipulator = new KernelManipulator($kernel);
     foreach ($components as $component) {
         $classArr = explode('\\', $component['class']);
         $bundleName = $classArr[0] . $classArr[1];
         $bundleClassName = $classArr[0] . '\\' . $classArr[1] . '\\' . $bundleName;
         if (!class_exists($bundleClassName)) {
             $bundleName = $classArr[0] . $classArr[1] . $classArr[2];
             $bundleClassName = $classArr[0] . '\\' . $classArr[1] . '\\' . $classArr[2] . '\\' . $bundleName;
             if (!class_exists($bundleClassName)) {
                 throw new \Exception("Bundle class not found");
             }
         }
         echo "Checking if " . $bundleName . " is in Kernel" . PHP_EOL;
         try {
             $kernel->getBundle($bundleName);
         } catch (\InvalidArgumentException $e) {
             // register bundle to kernel
             echo "Registering bundle " . $bundleName . PHP_EOL;
             try {
                 $kernelManipulator->addBundle($bundleClassName);
             } catch (\RuntimeException $e) {
                 return array(sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $bundleClassName), '');
             }
             $output->writeln($bundleName . " succesfully registered to Kernel." . PHP_EOL);
         }
     }
 }
All Usage Examples Of Sensio\Bundle\GeneratorBundle\Manipulator\KernelManipulator::addBundle
KernelManipulator