refactoring for dependency injection and testability.

This commit is contained in:
Taylor Otwell
2011-08-25 22:53:05 -05:00
parent 0b86c94551
commit 1e7850d9ba
75 changed files with 1441 additions and 1461 deletions

View File

@@ -1,75 +1,21 @@
<?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.
* The file manager instance.
*
* @var File_Engine
* @var Laravel\File
*/
private $file;
/**
* Create a new File cache driver instance.
*
* @param File_Engine $file
* @param Laravel\File $file
* @return void
*/
public function __construct(File_Engine $file)
public function __construct(\Laravel\File $file)
{
$this->file = $file;
}
@@ -134,7 +80,7 @@ class File extends Driver {
*/
public function forget($key)
{
$this->file->forget(CACHE_PATH.$key);
$this->file->delete(CACHE_PATH.$key);
}
}

65
laravel/cache/manager.php vendored Normal file
View File

@@ -0,0 +1,65 @@
<?php namespace Laravel\Cache;
use Laravel\IoC;
use Laravel\Config;
class Manager {
/**
* All of the active cache drivers.
*
* @var Cache\Driver
*/
public static $drivers = array();
/**
* Get a cache driver instance.
*
* If no driver name is specified, the default cache driver will be returned
* as defined in the cache configuration file.
*
* <code>
* // Get the default cache driver
* $driver = Cache::driver();
*
* // Get the APC cache driver
* $apc = Cache::driver('apc');
* </code>
*
* @param string $driver
* @return Cache\Driver
*/
public static function driver($driver = null)
{
if (is_null($driver)) $driver = Config::get('cache.driver');
if ( ! array_key_exists($driver, static::$drivers))
{
if ( ! in_array($driver, array('apc', 'file', 'memcached')))
{
throw new \Exception("Cache driver [$driver] is not supported.");
}
return static::$drivers[$driver] = IoC::container()->resolve('laravel.cache.'.$driver);
}
return static::$drivers[$driver];
}
/**
* Pass all other methods to the default cache driver.
*
* Passing method calls to the driver instance provides a convenient API for the developer
* when always using the default cache driver.
*
* <code>
* // Get an item from the default cache driver
* $name = Cache::get('name');
* </code>
*/
public static function __callStatic($method, $parameters)
{
return call_user_func_array(array(static::driver(), $method), $parameters);
}
}