merged branch yanoosh/ticket_4719 (PR #4779)

Commits
-------

74aa502 [HttpKernel] Fix #4719. Added condition which verify existence of profiler dump file. If file does not exists script inserts record to csv file.

Discussion
----------

[HttpKernel] Ticket #4719. Each call of FileProfilerStorage->write method adds row to index file.

Bug fix: yes
Feature addition: no
Backwards compatibility break: no
Symfony2 tests pass: yes
License of the code: MIT

Each call of FileProfilerStorage->write method adds row to index file even if profiler dump is only updated. Event dispatcher in kernel call several times write method to save fresh profiler info. I add condition which checks if profiler file already exist then ignore add row to index file.

---------------------------------------------------------------------------

by fabpot at 2012-07-08T08:04:33Z

Can you squash your commits before I merge? Thanks.

---------------------------------------------------------------------------

by yanoosh at 2012-07-08T10:08:48Z

Ready to merge.
This commit is contained in:
Fabien Potencier 2012-07-08 12:11:25 +02:00
commit dd4235ad38
2 changed files with 47 additions and 17 deletions

View File

@ -129,10 +129,13 @@ class FileProfilerStorage implements ProfilerStorageInterface
{
$file = $this->getFilename($profile->getToken());
// Create directory
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
$profileIndexed = is_file($file);
if (!$profileIndexed) {
// Create directory
$dir = dirname($file);
if (!is_dir($dir)) {
mkdir($dir, 0777, true);
}
}
// Store profile
@ -151,20 +154,22 @@ class FileProfilerStorage implements ProfilerStorageInterface
return false;
}
// Add to index
if (false === $file = fopen($this->getIndexFilename(), 'a')) {
return false;
}
if (!$profileIndexed) {
// Add to index
if (false === $file = fopen($this->getIndexFilename(), 'a')) {
return false;
}
fputcsv($file, array(
$profile->getToken(),
$profile->getIp(),
$profile->getMethod(),
$profile->getUrl(),
$profile->getTime(),
$profile->getParentToken(),
));
fclose($file);
fputcsv($file, array(
$profile->getToken(),
$profile->getIp(),
$profile->getMethod(),
$profile->getUrl(),
$profile->getTime(),
$profile->getParentToken(),
));
fclose($file);
}
return true;
}

View File

@ -12,6 +12,7 @@
namespace Symfony\Component\HttpKernel\Tests\Profiler;
use Symfony\Component\HttpKernel\Profiler\FileProfilerStorage;
use Symfony\Component\HttpKernel\Profiler\Profile;
class FileProfilerStorageTest extends AbstractProfilerStorageTest
{
@ -57,4 +58,28 @@ class FileProfilerStorageTest extends AbstractProfilerStorageTest
{
return self::$storage;
}
public function testMultiRowIndexFile()
{
$iteration = 3;
for($i = 0; $i < $iteration; $i++) {
$profile = new Profile('token' . $i);
$profile->setIp('127.0.0.' . $i);
$profile->setUrl('http://foo.bar/' . $i);
$storage = $this->getStorage();
$storage->write($profile);
$storage->write($profile);
$storage->write($profile);
}
$handle = fopen(self::$tmpDir . '/index.csv', 'r');
for($i = 0; $i < $iteration; $i++) {
$row = fgetcsv($handle);
$this->assertEquals('token' . $i, $row[0]);
$this->assertEquals('127.0.0.' . $i, $row[1]);
$this->assertEquals('http://foo.bar/' . $i, $row[3]);
}
$this->assertFalse(fgetcsv($handle));
}
}