Skip to content

Commit 8d6f0f1

Browse files
committed
Added mongodb factory
1 parent 04b087f commit 8d6f0f1

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

src/Factory/MongoDBFactory.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* This file is part of php-cache\adapter-bundle package.
5+
*
6+
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
7+
*
8+
* This source file is subject to the MIT license that is bundled
9+
* with this source code in the file LICENSE.
10+
*/
11+
12+
namespace Cache\AdapterBundle\Factory;
13+
14+
use Cache\Adapter\MongoDB\MongoDBCachePool;
15+
use MongoDB\Driver\Manager;
16+
use Symfony\Component\OptionsResolver\OptionsResolver;
17+
18+
/**
19+
* @author Tobias Nyholm <[email protected]>
20+
*/
21+
class MongoDBFactory extends AbstractAdapterFactory
22+
{
23+
protected static $dependencies = [
24+
['requiredClass' => 'Cache\Adapter\MongoDB\MongoDBCachePool', 'packageName' => 'cache/mongodb-adapter'],
25+
];
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function getAdapter(array $config)
31+
{
32+
$manager = new Manager(sprintf('mongodb://%s:%s', $config['host'], $config['port']));
33+
$collection = MongoDBCachePool::createCollection($manager, $config['namespace']);
34+
35+
return new MongoDBCachePool($collection);
36+
}
37+
38+
/**
39+
* {@inheritdoc}
40+
*/
41+
protected static function configureOptionResolver(OptionsResolver $resolver)
42+
{
43+
$resolver->setDefaults([
44+
'host' => '127.0.0.1',
45+
'port' => 11211,
46+
'namespace' => 'cache',
47+
]);
48+
49+
$resolver->setAllowedTypes('host', ['string']);
50+
$resolver->setAllowedTypes('port', ['string', 'int']);
51+
$resolver->setAllowedTypes('namespace', ['string']);
52+
}
53+
}

0 commit comments

Comments
 (0)