ResourceAbstract.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_Application
  17. * @subpackage Resource
  18. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id$
  21. */
  22. /**
  23. * @see Zend_Application_Resource_Resource
  24. */
  25. require_once 'Zend/Application/Resource/Resource.php';
  26. /**
  27. * Abstract class for bootstrap resources
  28. *
  29. * @uses Zend_Application_Resource_Resource
  30. * @category Zend
  31. * @package Zend_Application
  32. * @subpackage Resource
  33. * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. */
  36. abstract class Zend_Application_Resource_ResourceAbstract implements Zend_Application_Resource_Resource
  37. {
  38. /**
  39. * Parent bootstrap
  40. *
  41. * @var Zend_Application_Bootstrap_Bootstrapper
  42. */
  43. protected $_bootstrap;
  44. /**
  45. * Options for the resource
  46. *
  47. * @var array
  48. */
  49. protected $_options = array();
  50. /**
  51. * Option keys to skip when calling setOptions()
  52. *
  53. * @var array
  54. */
  55. protected $_skipOptions = array(
  56. 'options',
  57. 'config',
  58. );
  59. /**
  60. * Create a instance with options
  61. *
  62. * @param mixed $options
  63. */
  64. public function __construct($options = null)
  65. {
  66. if (is_array($options)) {
  67. $this->setOptions($options);
  68. } else if ($options instanceof Zend_Config) {
  69. $this->setOptions($options->toArray());
  70. }
  71. }
  72. /**
  73. * Set options from array
  74. *
  75. * @param array $options Configuration for resource
  76. * @return Zend_Application_Resource_ResourceAbstract
  77. */
  78. public function setOptions(array $options)
  79. {
  80. foreach ($options as $key => $value) {
  81. if (in_array(strtolower($key), $this->_skipOptions)) {
  82. continue;
  83. }
  84. $method = 'set' . strtolower($key);
  85. if (method_exists($this, $method)) {
  86. $this->$method($value);
  87. }
  88. }
  89. $this->_options = array_merge_recursive($this->_options, $options);
  90. return $this;
  91. }
  92. /**
  93. * Retrieve resource options
  94. *
  95. * @return array
  96. */
  97. public function getOptions()
  98. {
  99. return $this->_options;
  100. }
  101. /**
  102. * Set the bootstrap to which the resource is attached
  103. *
  104. * @param Zend_Application_Bootstrap_Bootstrapper $bootstrap
  105. * @return Zend_Application_Resource_Resource
  106. */
  107. public function setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap)
  108. {
  109. $this->_bootstrap = $bootstrap;
  110. return $this;
  111. }
  112. /**
  113. * Retrieve the bootstrap to which the resource is attached
  114. *
  115. * @return null|Zend_Application_Bootstrap_Bootstrapper
  116. */
  117. public function getBootstrap()
  118. {
  119. return $this->_bootstrap;
  120. }
  121. }