2
0

ResourceAbstract.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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-2009 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-2009 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. if ('bootstrap' == $key) {
  89. unset($options[$key]);
  90. }
  91. }
  92. $this->_options = $this->mergeOptions($this->_options, $options);
  93. return $this;
  94. }
  95. /**
  96. * Retrieve resource options
  97. *
  98. * @return array
  99. */
  100. public function getOptions()
  101. {
  102. return $this->_options;
  103. }
  104. /**
  105. * Merge options recursively
  106. *
  107. * @param array $array1
  108. * @param mixed $array2
  109. * @return array
  110. */
  111. public function mergeOptions(array $array1, $array2 = null)
  112. {
  113. if (is_array($array2)) {
  114. foreach ($array2 as $key => $val) {
  115. if (is_array($array2[$key])) {
  116. $array1[$key] = (array_key_exists($key, $array1) && is_array($array1[$key]))
  117. ? $this->mergeOptions($array1[$key], $array2[$key])
  118. : $array2[$key];
  119. } else {
  120. $array1[$key] = $val;
  121. }
  122. }
  123. }
  124. return $array1;
  125. }
  126. /**
  127. * Set the bootstrap to which the resource is attached
  128. *
  129. * @param Zend_Application_Bootstrap_Bootstrapper $bootstrap
  130. * @return Zend_Application_Resource_Resource
  131. */
  132. public function setBootstrap(Zend_Application_Bootstrap_Bootstrapper $bootstrap)
  133. {
  134. $this->_bootstrap = $bootstrap;
  135. return $this;
  136. }
  137. /**
  138. * Retrieve the bootstrap to which the resource is attached
  139. *
  140. * @return null|Zend_Application_Bootstrap_Bootstrapper
  141. */
  142. public function getBootstrap()
  143. {
  144. return $this->_bootstrap;
  145. }
  146. }