refactoring and adding more dependency injection through ioc container.

This commit is contained in:
Taylor Otwell
2011-08-24 22:51:32 -05:00
parent 99adf09ac7
commit 6a8aafc259
46 changed files with 1039 additions and 1276 deletions

68
laravel/cache/apc.php vendored
View File

@@ -2,8 +2,70 @@
use Laravel\Config;
/**
* Wrap the APC functions in a class that can be injected into driver.
* Since the APC functions are global, the driver is untestable without
* injecting a wrapper around them.
*/
class APC_Engine {
/**
* Get an item from the APC cache.
*
* @param string $key
* @return mixed
*/
public function get($key)
{
return apc_fetch($key);
}
/**
* Store an item in the APC cache.
*
* @param string $key
* @param mixed $value
* @param int $minutes
* @return void
*/
public function put($key, $value, $seconds)
{
apc_store($key, $value, $seconds);
}
/**
* Delete an item from the APC cache.
*
* @param string $key
* @return void
*/
public function forget($key)
{
apc_delete($key);
}
}
class APC extends Driver {
/**
* The APC Engine instance.
*
* @var APC_Engine
*/
private $apc;
/**
* Create a new APC cache driver instance.
*
* @param APC_Engine $apc
* @return void
*/
public function __construct(APC_Engine $apc)
{
$this->apc = $apc;
}
/**
* Determine if an item exists in the cache.
*
@@ -28,7 +90,7 @@ class APC extends Driver {
*/
protected function retrieve($key)
{
return ( ! is_null($cache = apc_fetch(Config::get('cache.key').$key))) ? $cache : null;
return ( ! is_null($cache = $this->apc->get(Config::get('cache.key').$key))) ? $cache : null;
}
/**
@@ -46,7 +108,7 @@ class APC extends Driver {
*/
public function put($key, $value, $minutes)
{
apc_store(Config::get('cache.key').$key, $value, $minutes * 60);
$this->apc->put(Config::get('cache.key').$key, $value, $minutes * 60);
}
/**
@@ -57,7 +119,7 @@ class APC extends Driver {
*/
public function forget($key)
{
apc_delete(Config::get('cache.key').$key);
$this->apc->forget(Config::get('cache.key').$key);
}
}

View File

@@ -1,7 +1,79 @@
<?php namespace Laravel\Cache;
/**
* Wrap the file functions in a class that can be injected into driver.
* Since the file functions are global, the driver is untestable without
* injecting a wrapper around them.
*/
class File_Engine {
/**
* Determine if a file exists.
*
* @param string $file
* @return bool
*/
public function exists($file)
{
return file_exists($file);
}
/**
* Get the contents of a file.
*
* @param string $file
* @return string
*/
public function get($file)
{
return file_get_contents($file);
}
/**
* Write to a file.
*
* @param string $file
* @param string $value
* @return void
*/
public function put($file, $value)
{
file_put_contents($file, $value, LOCK_EX);
}
/**
* Delete a file.
*
* @param string $file
* @return void
*/
public function forget($file)
{
@unlink($file);
}
}
class File extends Driver {
/**
* The File cache engine.
*
* @var File_Engine
*/
private $file;
/**
* Create a new File cache driver instance.
*
* @param File_Engine $file
* @return void
*/
public function __construct(File_Engine $file)
{
$this->file = $file;
}
/**
* Determine if an item exists in the cache.
*
@@ -26,9 +98,9 @@ class File extends Driver {
*/
protected function retrieve($key)
{
if ( ! file_exists(CACHE_PATH.$key)) return null;
if ( ! $this->file->exists(CACHE_PATH.$key)) return null;
if (time() >= substr($cache = file_get_contents(CACHE_PATH.$key), 0, 10))
if (time() >= substr($cache = $this->file->get(CACHE_PATH.$key), 0, 10))
{
return $this->forget($key);
}
@@ -51,7 +123,7 @@ class File extends Driver {
*/
public function put($key, $value, $minutes)
{
file_put_contents(CACHE_PATH.$key, (time() + ($minutes * 60)).serialize($value), LOCK_EX);
$this->file->put(CACHE_PATH.$key, (time() + ($minutes * 60)).serialize($value));
}
/**
@@ -62,7 +134,7 @@ class File extends Driver {
*/
public function forget($key)
{
@unlink(CACHE_PATH.$key);
$this->file->forget(CACHE_PATH.$key);
}
}

View File

@@ -1,10 +1,27 @@
<?php namespace Laravel\Cache;
use Laravel\Config;
use Laravel\Memcached as Mem;
class Memcached extends Driver {
/**
* The Memcache instance.
*
* @var Memcache
*/
private $memcache;
/**
* Create a new Memcached cache driver instance.
*
* @param Memcache $memcache
* @return void
*/
public function __construct(\Memcache $memcache)
{
$this->memcache = $memcache;
}
/**
* Determine if an item exists in the cache.
*
@@ -29,7 +46,7 @@ class Memcached extends Driver {
*/
protected function retrieve($key)
{
return (($cache = Mem::instance()->get(Config::get('cache.key').$key)) !== false) ? $cache : null;
return (($cache = $this->memcache->get(Config::get('cache.key').$key)) !== false) ? $cache : null;
}
/**
@@ -47,7 +64,7 @@ class Memcached extends Driver {
*/
public function put($key, $value, $minutes)
{
Mem::instance()->set(Config::get('cache.key').$key, $value, 0, $minutes * 60);
$this->memcache->set(Config::get('cache.key').$key, $value, 0, $minutes * 60);
}
/**
@@ -58,7 +75,7 @@ class Memcached extends Driver {
*/
public function forget($key)
{
Mem::instance()->delete(Config::get('cache.key').$key);
$this->memcache->delete(Config::get('cache.key').$key);
}
}