public function save()
{
if (!$this->addBundles && !$this->removeBundles) {
return false;
}
$appKernelPath = new \ReflectionClass('AppKernel');
$file = $appKernelPath->getFileName();
$this->script = file_get_contents($file);
$inBundles = false;
$inRegisterBundles = false;
$inBlockComment = false;
$inComment = false;
$bundleShortArrayVersion = false;
$bundles = $this->bundles;
foreach ($this->addBundles as $bundle) {
$bundles[] = $bundle;
}
foreach ($this->removeBundles as $bundle) {
$pos = array_search($bundle, $bundles);
unset($bundles[$pos]);
}
$this->length = strlen($this->script);
for ($this->position = 0; $this->position < $this->length; $this->position++) {
if ($inComment || $inBlockComment) {
//search for comment ends
if (!$inBlockComment && $inComment && $this->expect("\n")) {
$inComment = false;
}
if (!$inComment && $inBlockComment && $this->expect('*/')) {
$inBlockComment = false;
}
continue;
} else {
//search for comment starts
if (!$inBlockComment && $this->expect('//')) {
$inComment = true;
continue;
}
if (!$inComment && $this->expect('/*')) {
$inBlockComment = true;
continue;
}
}
if (!$inBundles) {
if (!$inRegisterBundles) {
if ($this->expect('function registerBundles')) {
$inRegisterBundles = true;
}
} else {
if ($this->expect('$bundles = array(')) {
$inBundles = true;
}
if ($this->expect('$bundles = [')) {
$inBundles = true;
$bundleShortArrayVersion = true;
}
}
}
if ($inBundles) {
if ($bundleShortArrayVersion && $this->expect('];') || !$bundleShortArrayVersion && $this->expect(');')) {
$this->remove(2);
$code = "\$bundles = array(\n";
foreach ($bundles as $bundle) {
$code .= " new {$bundle}(),\n";
}
$code .= " );";
$this->write($code);
break;
} else {
$this->remove(1);
}
}
}
return !!file_put_contents($file, $this->script);
}