I designed my database and cache layer after Zend Framework 1, like this:
class Cache
{
public static function create($adapter, array $params)
{
$class_name = 'Cache_Adapter_' . $adapter;
return new $class_name($params);
}
}
abstract class Cache_Adapter
{
public function __construct(array $params)
{
}
}
class Cache_Adapter_File extends Cache_Adapter
{
// ...
}
Usage example:
// config.php
return array(
'cache' => array(
'adapter' => 'file',
'params' => array(
'path' => '/path',
),
),
);
// bootstrap.php
$dic->cache = Cache::create($config['cache']['adapter'], $config['cache']['params']);
This approach is great, because each cache adapter can have different parameters, for example, file cache need path to directory where to store cache files.
Then I wanted to create cache adapter for storing data in database and realized that instead of scalar parameter array, database abstraction class dependency is needed.
Currently database connections are registered in dependency injection container:
// config.php
return array(
'db1' => array(
'adapter' => 'mysql',
'params' => array(
'user' => 'root',
'connect_timeout' => 5,
),
),
'db2' => array(
'adapter' => 'sqlsrv',
'params' => array(
'db' => 'foo',
),
),
);
// bootstrap.php
$dic->db1 = Site:Db::create($config['db1']['adapter'], $config['db1']['params']);
$dic->db2 = Site:Db::create($config['db2']['adapter'], $config['db2']['params']);
So I wanted to ask how in addition to scalar configuration parameter array, zero or more specific dependencies can be passed to cache adapters and this can be done in config.php.
class Cache_Adapter_Db extends Cache_Adapter
{
// Instead of abstract Cache_Adapter::__construct(array $params)
// something like this is needed:
// public function __construct(array $params, Db_Adapter $db)
public function __construct(array $params)
{
}
}