SHARE
TWEET

Untitled

a guest Jan 18th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. class Cache
  2. {
  3.     public static function create($adapter, array $params)
  4.     {
  5.         $class_name = 'Cache_Adapter_' . $adapter;
  6.  
  7.         return new $class_name($params);  
  8.     }
  9. }
  10.  
  11. abstract class Cache_Adapter
  12. {
  13.     public function __construct(array $params)
  14.     {
  15.  
  16.     }
  17. }
  18.  
  19. class Cache_Adapter_File extends Cache_Adapter
  20. {
  21.     // ...
  22. }
  23.    
  24. // config.php
  25. return array(
  26.     'cache' => array(
  27.         'adapter' => 'file',
  28.         'params' => array(
  29.             'path' => '/path',
  30.         ),
  31.     ),
  32. );
  33.  
  34. // bootstrap.php
  35. $dic->cache = Cache::create($config['cache']['adapter'], $config['cache']['params']);
  36.    
  37. // config.php
  38. return array(
  39.     'db1' => array(
  40.         'adapter' => 'mysql',
  41.         'params' => array(
  42.             'user' => 'root',
  43.             'connect_timeout' => 5,
  44.         ),
  45.     ),
  46.     'db2' => array(
  47.         'adapter' => 'sqlsrv',
  48.         'params' => array(
  49.             'db' => 'foo',
  50.         ),
  51.     ),
  52. );
  53.  
  54. // bootstrap.php
  55. $dic->db1 = Site:Db::create($config['db1']['adapter'], $config['db1']['params']);
  56. $dic->db2 = Site:Db::create($config['db2']['adapter'], $config['db2']['params']);
  57.    
  58. class Cache_Adapter_Db extends Cache_Adapter
  59. {
  60.     // Instead of abstract Cache_Adapter::__construct(array $params)
  61.     // something like this is needed:
  62.     // public function __construct(array $params, Db_Adapter $db)
  63.     public function __construct(array $params)
  64.     {
  65.  
  66.     }
  67. }
  68.    
  69. class Cache_Adapter_Db extends Cache_Adapter
  70. {
  71.     public function __construct(array $params, Db_Adapter $db)
  72.     {
  73.        parent::__construct($params);
  74.     }
  75. }
  76.    
  77. class Cache
  78. {
  79.     public static function create($adapter, array $params, $optparams = null )
  80.     {
  81.         $class_name = 'Cache_Adapter_' . $adapter;
  82.  
  83.         return new $class_name($params, $optparams);  
  84.     }
  85. }
  86.    
  87. // config.php
  88. return array(
  89.     'db1' => array(
  90.         'adapter' => 'mysql',
  91.         'params' => array(
  92.             'user' => 'root',
  93.             'connect_timeout' => 5,
  94.         ),
  95.     ),
  96.     'db2' => array(
  97.         'adapter' => 'sqlsrv',
  98.         'params' => array(
  99.             'db' => 'foo',
  100.         ),
  101.         'options' => 'extraoption'
  102.     ),
  103. );
  104.    
  105. $dic->db2 = Site:Db::create(
  106.    $config['db2']['adapter'],
  107.    $config['db2']['params'],
  108.    $config['db2']['options']
  109. );
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top