Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[HttpKernel] Fix #4719. Added condition which verify existence of pro…
…filer dump file. If file does not exists script inserts record to csv file.
  • Loading branch information
yanoosh committed Jul 8, 2012
1 parent b8f99ee commit 74aa502
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 17 deletions.
39 changes: 22 additions & 17 deletions src/Symfony/Component/HttpKernel/Profiler/FileProfilerStorage.php
Expand Up @@ -129,10 +129,13 @@ public function write(Profile $profile)
{
$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
Expand All @@ -151,20 +154,22 @@ public function write(Profile $profile)
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;
}
Expand Down
Expand Up @@ -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
{
Expand Down Expand Up @@ -57,4 +58,28 @@ protected function getStorage()
{
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));
}
}

0 comments on commit 74aa502

Please sign in to comment.