Skip to content

Mitigate dependency upon ConfigCache #7753

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 9 commits into from
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@
namespace Symfony\Bundle\FrameworkBundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Filesystem\Filesystem;

class CompilerDebugDumpPass implements CompilerPassInterface
{
public function process(ContainerBuilder $container)
{
$cache = new ConfigCache($this->getCompilerLogFilename($container), false);
$cache->write(implode("\n", $container->getCompiler()->getLog()));
$filesystem = new Filesystem();
$filesystem->dumpFile(
$this->getCompilerLogFilename($container),
implode("\n", $container->getCompiler()->getLog()),
0666 & ~umask()
);
}

public static function getCompilerLogFilename(ContainerInterface $container)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Dumper\XmlDumper;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\Config\ConfigCache;
use Symfony\Component\Filesystem\Filesystem;

/**
* Dumps the ContainerBuilder to a cache file so that it can be used by
Expand All @@ -28,7 +28,11 @@ class ContainerBuilderDebugDumpPass implements CompilerPassInterface
public function process(ContainerBuilder $container)
{
$dumper = new XmlDumper($container);
$cache = new ConfigCache($container->getParameter('debug.container.dump'), false);
$cache->write($dumper->dump());
$filesystem = new Filesystem();
$filesystem->dumpFile(
$container->getParameter('debug.container.dump'),
$dumper->dump(),
0666 & ~umask()
);
}
}
38 changes: 17 additions & 21 deletions src/Symfony/Component/Config/ConfigCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Config;

use Symfony\Component\Config\Resource\ResourceInterface;
use Symfony\Component\Filesystem\Filesystem;

/**
* ConfigCache manages PHP cache files.
Expand Down Expand Up @@ -66,7 +67,7 @@ public function isFresh()
return true;
}

$metadata = $this->file.'.meta';
$metadata = $this->getMetaFile();
if (!is_file($metadata)) {
return false;
}
Expand All @@ -92,28 +93,23 @@ public function isFresh()
*/
public function write($content, array $metadata = null)
{
$dir = dirname($this->file);
if (!is_dir($dir)) {
if (false === @mkdir($dir, 0777, true)) {
throw new \RuntimeException(sprintf('Unable to create the %s directory', $dir));
}
} elseif (!is_writable($dir)) {
throw new \RuntimeException(sprintf('Unable to write in the %s directory', $dir));
}

$tmpFile = tempnam($dir, basename($this->file));
if (false !== @file_put_contents($tmpFile, $content) && @rename($tmpFile, $this->file)) {
@chmod($this->file, 0666 & ~umask());
} else {
throw new \RuntimeException(sprintf('Failed to write cache file "%s".', $this->file));
}
$mode = 0666 & ~umask();
$filesystem = new Filesystem();
$filesystem->dumpFile($this->file, $content, $mode);

if (null !== $metadata && true === $this->debug) {
$file = $this->file.'.meta';
$tmpFile = tempnam($dir, basename($file));
if (false !== @file_put_contents($tmpFile, serialize($metadata)) && @rename($tmpFile, $file)) {
@chmod($file, 0666 & ~umask());
}
$filesystem->dumpFile($this->getMetaFile(), serialize($metadata), $mode);
}
}

/**
* Gets the meta file path.
*
* @return string The meta file path
*/
private function getMetaFile()
{
return $this->file.'.meta';
}

}
5 changes: 5 additions & 0 deletions src/Symfony/Component/Filesystem/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
CHANGELOG
=========

2.3.0
-----

* added the dumpFile() method to atomically write files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo automatically

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, thats "atomically" (sic) - writing the file in a way that other user see either the old or the new one, but never a partially-written state.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, thanks! I've learned a new word :)
Could you please review symfony/symfony-docs#2592 , after your comment I'm not sure if it's correct.


2.2.0
-----

Expand Down
28 changes: 28 additions & 0 deletions src/Symfony/Component/Filesystem/Filesystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -430,4 +430,32 @@ private function toIterator($files)

return $files;
}

/**
* Atomically dumps content into a file.
*
* @param string $filename The file to be written to.
* @param string $content The data to write into the file.
* @param integer $mode The file mode (octal).
* @throws IOException If the file cannot be written to.
*/
public function dumpFile($filename, $content, $mode = 0666)
{
$dir = dirname($filename);

if (!is_dir($dir)) {
$this->mkdir($dir);
} elseif (!is_writable($dir)) {
throw new IOException(sprintf('Unable to write in the %s directory\n', $dir));
}

$tmpFile = tempnam($dir, basename($filename));

if (false === @file_put_contents($tmpFile, $content)) {
throw new IOException(sprintf('Failed to write file "%s".', $filename));
}

$this->rename($tmpFile, $filename);
$this->chmod($filename, $mode);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is how it was before, but wouldn't it be better to chmod() first before moving the file in place?

}
}