Skip to content

Commit

Permalink
Merge pull request #4 from MyOnlineStore/update-desarrolla
Browse files Browse the repository at this point in the history
Fix used config indices and update Desarrolla cache package to ~2.0
  • Loading branch information
sunspikes authored Aug 2, 2016
2 parents ea1d1d2 + ba2f884 commit 553806e
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 23 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=5.5",
"desarrolla2/cache": "~1.8"
"desarrolla2/cache": "~2.0"
},
"require-dev": {
"mockery/mockery": "^0.9.4",
Expand Down
5 changes: 4 additions & 1 deletion config/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@
'host' => '127.0.0.1',
'username' => 'root',
'password' => '',
'dbname' => '',
'port' => 3306
],
'redis' => [
// config for redis
],
'memcache' => [
// config for memcache
'servers' => [
'localhost'
]
],
];
56 changes: 36 additions & 20 deletions src/Cache/Factory/DesarrollaCacheFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@

namespace Sunspikes\Ratelimit\Cache\Factory;

use Desarrolla2\Cache\Adapter\Apc;
use Desarrolla2\Cache\Adapter\Apcu;
use Desarrolla2\Cache\Adapter\File;
use Desarrolla2\Cache\Adapter\MemCache;
use Desarrolla2\Cache\Adapter\Memcache;
use Desarrolla2\Cache\Adapter\Memory;
use Desarrolla2\Cache\Adapter\Mongo;
use Desarrolla2\Cache\Adapter\MySQL;
use Desarrolla2\Cache\Adapter\Mysqli;
use Desarrolla2\Cache\Adapter\NotCache;
use Desarrolla2\Cache\Adapter\Redis;
use Desarrolla2\Cache\Adapter\Predis;
use Desarrolla2\Cache\Cache;
use Sunspikes\Ratelimit\Cache\Exception\DriverNotFoundException;
use Sunspikes\Ratelimit\Cache\Exception\InvalidConfigException;
Expand Down Expand Up @@ -119,30 +119,29 @@ protected function createNotcacheDriver()
*/
protected function createFileDriver()
{
return new File($this->config['cache_dir']);
return new File($this->config['file']['cache_dir']);
}

/**
* Create APC driver
*
* @return Apc
* @return Apcu
*/
protected function createApcDriver()
{
return new Apc();
return new Apcu();
}

/**
* Create Memory driver
*
* @return Memory
* @throws \Desarrolla2\Cache\Adapter\MemoryCacheException
*/
protected function createMemoryDriver()
{
$memory = new Memory();
$memory->setOption('limit',
$this->config['limit']
$this->config['memory']['limit']
?: static::DEFAULT_LIMIT
);

Expand All @@ -156,32 +155,39 @@ protected function createMemoryDriver()
*/
protected function createMongoDriver()
{
return new Mongo($this->config['server']);
return new Mongo($this->config['mongo']['server']);
}

/**
* Create MySQL driver
*
* @return MySQL
* @return Mysqli
*/
protected function createMysqlDriver()
{
return new MySQL(
$this->config['host'],
$this->config['username'],
$this->config['password'],
$this->config['port']
);
$server = null;

if (!empty($this->config['mysql'])) {
$server = new \mysqli(
$this->config['mysql']['host'],
$this->config['mysql']['username'],
$this->config['mysql']['password'],
$this->config['mysql']['dbname'],
$this->config['mysql']['port']
);
}

return new Mysqli($server);
}

/**
* Create Redis driver
*
* @return Redis
* @return Predis
*/
protected function createRedisDriver()
{
return new Redis();
return new Predis();
}

/**
Expand All @@ -191,6 +197,16 @@ protected function createRedisDriver()
*/
protected function createMemcacheDriver()
{
return new MemCache();
$server = null;

if (isset($this->config['servers'])) {
$server = new \Memcache();

foreach ($this->config['servers'] as $host) {
$server->addserver($host);
}
}

return new Memcache($server);
}
}
34 changes: 33 additions & 1 deletion tests/Cache/Factory/DesarrollaCacheFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Sunspikes\Tests\Ratelimit\Cache\Factory;

use Desarrolla2\Cache\Cache;
use Sunspikes\Ratelimit\Cache\Factory\DesarrollaCacheFactory;

class DesarrollaCacheFactoryTest extends \PHPUnit_Framework_TestCase
Expand All @@ -11,6 +12,37 @@ public function testFactory()
$factory = new DesarrollaCacheFactory();
$cache = $factory->make();

$this->assertInstanceOf('\Desarrolla2\Cache\Cache', $cache);
$this->assertInstanceOf(Cache::class, $cache);
}

/**
* @dataProvider configProvider
*
* @param array $config
*/
public function testCreateDrivers(array $config, $driverClass)
{
if (null !== $driverClass && !class_exists($driverClass)) {
$this->markTestSkipped($driverClass.' is not available on this system');
}

$factory = new DesarrollaCacheFactory(null, $config);
$this->assertInstanceOf(Cache::class, $factory->make());
}

/**
* @return array
*/
public function configProvider()
{
return [
[['driver' => 'file'], null],
[['driver' => 'apc'], null],
[['driver' => 'memory'], null],
[['driver' => 'mongo'], \MongoClient::class],
[['driver' => 'redis'], \Predis\Client::class],
[['driver' => 'mysql', 'mysql' => []], \mysqli::class],
[['driver' => 'memcache'], \Memcache::class],
];
}
}

0 comments on commit 553806e

Please sign in to comment.