Skip to content

Commit

Permalink
Merge pull request #14 from Nyholm/doctrine
Browse files Browse the repository at this point in the history
Added doctrine factories
  • Loading branch information
Nyholm committed Jan 11, 2016
2 parents 16d9895 + 82b1942 commit 34afeb7
Show file tree
Hide file tree
Showing 14 changed files with 507 additions and 13 deletions.
22 changes: 22 additions & 0 deletions src/Factory/AbstractDoctrineAdapterFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?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;

/**
* @author Tobias Nyholm <[email protected]>
*/
abstract class AbstractDoctrineAdapterFactory extends AbstractAdapterFactory
{
protected static $dependencies = [
['requiredClass' => 'Cache\Adapter\Doctrine\DoctrineCachePool', 'packageName' => 'cache/doctrine-adapter'],
];
}
54 changes: 54 additions & 0 deletions src/Factory/DoctrineCouchbaseFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Doctrine\DoctrineCachePool;
use Couchbase;
use Doctrine\Common\Cache\CouchbaseCache;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineCouchbaseFactory extends AbstractDoctrineAdapterFactory
{
/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$couchbase = new Couchbase($config['host'], $config['user'], $config['password'], $config['bucket']);

$client = new CouchbaseCache();
$client->setCouchbase($couchbase);

return new DoctrineCachePool($client);
}

/**
* {@inheritdoc}
*/
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'host' => '127.0.0.1',
'user' => 'Administrator',
'password' => 'password',
'bucket' => 'default',
]);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('user', ['string']);
$resolver->setAllowedTypes('password', ['string']);
$resolver->setAllowedTypes('bucket', ['string']);
}
}
48 changes: 48 additions & 0 deletions src/Factory/DoctrineMemcacheFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Doctrine\DoctrineCachePool;
use Doctrine\Common\Cache\MemcacheCache;
use Memcache;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineMemcacheFactory extends AbstractDoctrineAdapterFactory
{
/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$memcache = new Memcache();
$memcache->connect($config['host'], $config['port']);

return new DoctrineCachePool(new MemcacheCache($memcache));
}

/**
* {@inheritdoc}
*/
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'host' => '127.0.0.1',
'port' => '11211',
]);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('port', ['string', 'int']);
}
}
48 changes: 48 additions & 0 deletions src/Factory/DoctrineMemcachedFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?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\Doctrine\DoctrineCachePool;
use Doctrine\Common\Cache\MemcachedCache;
use Memcached;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineMemcachedFactory extends AbstractDoctrineAdapterFactory
{
/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$memcached = new Memcached();
$memcached->addServer($config['host'], $config['port']);

return new DoctrineCachePool(new MemcachedCache($memcached));
}

/**
* {@inheritdoc}
*/
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'host' => '127.0.0.1',
'port' => '11211',
]);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('port', ['string', 'int']);
}
}
54 changes: 54 additions & 0 deletions src/Factory/DoctrineMongodDBFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?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\Doctrine\DoctrineCachePool;
use Doctrine\Common\Cache\MongoDBCache;
use MongoClient;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineMongodDBFactory extends AbstractDoctrineAdapterFactory
{
/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$mongo = new MongoClient();
$collection = $mongo->selectCollection($config['host'], $config['collection']);

return new DoctrineCachePool(new MongoDBCache($collection));
}

/**
* {@inheritdoc}
*/
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setRequired(['host', 'collection']);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('collection', ['string']);
}

protected static function verifyDependencies()
{
if (!version_compare(phpversion('mongo'), '1.3.0', '>=')) {
throw new \LogicException('Mongo >= 1.3.0 is required.');
}

parent::verifyDependencies();
}
}
58 changes: 58 additions & 0 deletions src/Factory/DoctrinePredisFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?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\Doctrine\DoctrineCachePool;
use Doctrine\Common\Cache\PredisCache;
use Predis\Client;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrinePredisFactory extends AbstractDoctrineAdapterFactory
{
protected static $dependencies = [
['requiredClass' => 'Cache\Adapter\Doctrine\DoctrineCachePool', 'packageName' => 'cache/doctrine-adapter'],
['requiredClass' => 'Predis\Client', 'packageName' => 'predis/predis'],
];

/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$client = new Client([
'scheme' => $config['scheme'],
'host' => $config['host'],
'port' => $config['port'],
]);

return new DoctrineCachePool(new PredisCache($client));
}

/**
* {@inheritdoc}
*/
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'host' => '127.0.0.1',
'port' => '6379',
'scheme' => 'tcp',
]);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('port', ['string', 'int']);
$resolver->setAllowedTypes('scheme', ['string']);
}
}
6 changes: 1 addition & 5 deletions src/Factory/DoctrineRedisFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@
/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineRedisFactory extends AbstractAdapterFactory
class DoctrineRedisFactory extends AbstractDoctrineAdapterFactory
{
protected static $dependencies = [
['requiredClass' => 'Cache\Adapter\Doctrine\DoctrineCachePool', 'packageName' => 'cache/doctrine-adapter'],
];

/**
* {@inheritdoc}
*/
Expand Down
51 changes: 51 additions & 0 deletions src/Factory/DoctrineRiakFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?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\Doctrine\DoctrineCachePool;
use Doctrine\Common\Cache\RiakCache;
use Riak\Bucket;
use Riak\Connection;
use Symfony\Component\OptionsResolver\OptionsResolver;

/**
* @author Tobias Nyholm <[email protected]>
*/
class DoctrineRiakFactory extends AbstractDoctrineAdapterFactory
{
/**
* {@inheritdoc}
*/
public function getAdapter(array $config)
{
$connection = new Connection($config['host'], $config['port']);
$bucket = new Bucket($connection, $config['type']);

return new DoctrineCachePool(new RiakCache($bucket));
}

/**
* {@inheritdoc}
*/
protected static function configureOptionResolver(OptionsResolver $resolver)
{
$resolver->setDefaults([
'host' => '127.0.0.1',
'port' => '8087',
'type' => null,
]);

$resolver->setAllowedTypes('host', ['string']);
$resolver->setAllowedTypes('port', ['string', 'int']);
$resolver->setAllowedTypes('type', ['string', 'null']);
}
}
Loading

0 comments on commit 34afeb7

Please sign in to comment.