Sulu\Bundle\GeneratorBundle\Manipulator\KernelManipulator::addBundle PHP Метод

addBundle() публичный Метод

Adds a bundle at the end of the existing ones.
public addBundle ( string $bundle ) : boolean
$bundle string The bundle class name
Результат 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 - 2), [rtrim(rtrim($src[$this->line - 2]), ',') . ",\n"], [sprintf("            new %s(),\n", $bundle)], array_slice($src, $this->line - 1));
                file_put_contents($this->reflected->getFilename(), implode('', $lines));
                return true;
            }
        }
    }

Usage Example

Пример #1
0
 protected function updateKernel(DialogHelper $dialog, InputInterface $input, OutputInterface $output, KernelInterface $kernel, $namespace, $bundle)
 {
     $auto = true;
     if ($input->isInteractive()) {
         $auto = $dialog->askConfirmation($output, $dialog->getQuestion('Confirm automatic update of your Kernel', 'yes', '?'), true);
     }
     $output->write('Enabling the bundle inside the Kernel: ');
     $manip = new KernelManipulator($kernel);
     try {
         $ret = $auto ? $manip->addBundle($namespace . '\\' . $bundle) : false;
         if (!$ret) {
             $reflected = new \ReflectionObject($kernel);
             return [sprintf('- Edit <comment>%s</comment>', $reflected->getFilename()), '  and add the following bundle in the <comment>AppKernel::registerBundles()</comment> method:', '', sprintf('    <comment>new %s(),</comment>', $namespace . '\\' . $bundle), ''];
         }
     } catch (\RuntimeException $e) {
         return [sprintf('Bundle <comment>%s</comment> is already defined in <comment>AppKernel::registerBundles()</comment>.', $namespace . '\\' . $bundle), ''];
     }
 }
KernelManipulator