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;
}
}
}