-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from php-cache/mongodb
Mongodb factory
- Loading branch information
Showing
2 changed files
with
54 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of php-cache\adapter-bundle package. | ||
* | ||
* (c) 2015-2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]> | ||
* | ||
* This source file is subject to the MIT license that is bundled | ||
* with this source code in the file LICENSE. | ||
*/ | ||
|
||
namespace Cache\AdapterBundle\Factory; | ||
|
||
use Cache\Adapter\MongoDB\MongoDBCachePool; | ||
use MongoDB\Driver\Manager; | ||
use Symfony\Component\OptionsResolver\OptionsResolver; | ||
|
||
/** | ||
* @author Tobias Nyholm <[email protected]> | ||
*/ | ||
class MongoDBFactory extends AbstractAdapterFactory | ||
{ | ||
protected static $dependencies = [ | ||
['requiredClass' => 'Cache\Adapter\MongoDB\MongoDBCachePool', 'packageName' => 'cache/mongodb-adapter'], | ||
]; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function getAdapter(array $config) | ||
{ | ||
$manager = new Manager(sprintf('mongodb://%s:%s', $config['host'], $config['port'])); | ||
$collection = MongoDBCachePool::createCollection($manager, $config['namespace']); | ||
|
||
return new MongoDBCachePool($collection); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
protected static function configureOptionResolver(OptionsResolver $resolver) | ||
{ | ||
$resolver->setDefaults([ | ||
'host' => '127.0.0.1', | ||
'port' => 11211, | ||
'namespace' => 'cache', | ||
]); | ||
|
||
$resolver->setAllowedTypes('host', ['string']); | ||
$resolver->setAllowedTypes('port', ['string', 'int']); | ||
$resolver->setAllowedTypes('namespace', ['string']); | ||
} | ||
} |