diff --git a/src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php b/src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php index 7f85cdefb7..dfab9a600e 100644 --- a/src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php +++ b/src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php @@ -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; } diff --git a/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php b/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php index 16c615a05d..b7e25137a5 100644 --- a/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php +++ b/src/Symfony/Component/HttpKernel/Tests/Profiler/FileProfilerStorageTest.php @@ -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)); + } }