ResourceAbstract.php 4.2 KB

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