Skip to content

Commit

Permalink
Merge pull request #352 from perftools/mongo-dot-encode-in-keys
Browse files Browse the repository at this point in the history
Encode dot in MongoDB saver as underscore
  • Loading branch information
glensc committed Oct 22, 2020
2 parents bf48644 + 356e70d commit 05d1146
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion src/Xhgui/Saver/MongoSaver.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,34 @@ public function save(array $data, string $id = null): string
$a = [
'_id' => new MongoId($id),
'meta' => $meta,
'profile' => $data['profile'],
'profile' => $this->encodeProfile($data['profile']),
];

$this->_collection->insert($a, ['w' => 0]);

return (string)$a['_id'];
}

/**
* MongoDB can't save keys with values containing a dot:
*
* InvalidArgumentException: invalid document for insert: keys cannot contain ".":
* "Zend_Controller_Dispatcher_Standard::loadClass==>load::controllers/ArticleController.php"
*
* Replace the dots with underscrore in keys.
*
* @link https://github.com/perftools/xhgui/issues/209
*/
private function encodeProfile(array $profile): array
{
$results = [];
foreach ($profile as $k => $v) {
if (strpos($k, '.') !== false) {
$k = str_replace('.', '_', $k);
}
$results[$k] = $v;
}

return $results;
}
}

0 comments on commit 05d1146

Please sign in to comment.