/mnemo/lib/Factory/Driver.php

https://github.com/finger2000/horde · PHP · 77 lines · 37 code · 6 blank · 34 comment · 3 complexity · 1f986c251faedd609efd04caaabb3336 MD5 · raw file

  1. <?php
  2. /**
  3. * Horde_Injector factory to create Mnemo_Driver instances.
  4. *
  5. * Copyright 2011 Horde LLC (http://www.horde.org/)
  6. *
  7. * See the enclosed file LICENSE for license information (ASL). If you
  8. * did not receive this file, see http://www.horde.org/licenses/asl.php.
  9. *
  10. * @author Michael J. Rubinsky <mrubinsk@horde.org>
  11. * @package Mnemo
  12. */
  13. class Mnemo_Factory_Driver
  14. {
  15. /**
  16. * Instances.
  17. *
  18. * @var array
  19. */
  20. private $_instances = array();
  21. /**
  22. * The injector.
  23. *
  24. * @var Horde_Injector
  25. */
  26. private $_injector;
  27. /**
  28. * Constructor.
  29. *
  30. * @param Horde_Injector $injector The injector to use.
  31. */
  32. public function __construct(Horde_Injector $injector)
  33. {
  34. $this->_injector = $injector;
  35. }
  36. /**
  37. * Return the Mnemo_Driver:: instance.
  38. *
  39. * @param mixed $name The notepad to open
  40. *
  41. * @return Mnemo_Driver
  42. * @throws Mnemo_Exception
  43. */
  44. public function create($name = '')
  45. {
  46. if (!isset($this->_instances[$name])) {
  47. $driver = $GLOBALS['conf']['storage']['driver'];
  48. $params = Horde::getDriverConfig('storage', $driver);
  49. $class = 'Mnemo_Driver_' . ucfirst(basename($driver));
  50. if (!class_exists($class)) {
  51. throw new Mnemo_Exception(sprintf('Unable to load the definition of %s.', $class));
  52. }
  53. switch ($class) {
  54. case 'Mnemo_Driver_Sql':
  55. $params = array(
  56. 'db' => $this->_injector->getInstance('Horde_Db_Adapter'),
  57. 'table' => 'mnemo_memos',
  58. 'charset' => $params['charset'],
  59. );
  60. break;
  61. case 'Mnemo_Driver_Kolab':
  62. $params = array(
  63. 'storage' => $this->_injector->getInstance('Horde_Kolab_Storage')
  64. );
  65. }
  66. $driver = new $class($name, $params);
  67. $this->_instances[$name] = $driver;
  68. }
  69. return $this->_instances[$name];
  70. }
  71. }