server_entity.inc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. <?php
  2. /**
  3. * Class representing a search server.
  4. *
  5. * This can handle the same calls as defined in the SearchApiServiceInterface
  6. * and pass it on to the service implementation appropriate for this server.
  7. */
  8. class SearchApiServer extends Entity {
  9. /* Database values that will be set when object is loaded: */
  10. /**
  11. * The primary identifier for a server.
  12. *
  13. * @var integer
  14. */
  15. public $id = 0;
  16. /**
  17. * The displayed name for a server.
  18. *
  19. * @var string
  20. */
  21. public $name = '';
  22. /**
  23. * The machine name for a server.
  24. *
  25. * @var string
  26. */
  27. public $machine_name = '';
  28. /**
  29. * The displayed description for a server.
  30. *
  31. * @var string
  32. */
  33. public $description = '';
  34. /**
  35. * The id of the service class to use for this server.
  36. *
  37. * @var string
  38. */
  39. public $class = '';
  40. /**
  41. * The options used to configure the service object.
  42. *
  43. * @var array
  44. */
  45. public $options = array();
  46. /**
  47. * A flag indicating whether the server is enabled.
  48. *
  49. * @var integer
  50. */
  51. public $enabled = 1;
  52. /**
  53. * Proxy object for invoking service methods.
  54. *
  55. * @var SearchApiServiceInterface
  56. */
  57. protected $proxy;
  58. /**
  59. * Constructor as a helper to the parent constructor.
  60. */
  61. public function __construct(array $values = array()) {
  62. parent::__construct($values, 'search_api_server');
  63. }
  64. /**
  65. * Helper method for updating entity properties.
  66. *
  67. * NOTE: You shouldn't change any properties of this object before calling
  68. * this method, as this might lead to the fields not being saved correctly.
  69. *
  70. * @param array $fields
  71. * The new field values.
  72. *
  73. * @return
  74. * SAVE_UPDATED on success, FALSE on failure, 0 if the fields already had
  75. * the specified values.
  76. */
  77. public function update(array $fields) {
  78. $changeable = array('name' => 1, 'enabled' => 1, 'description' => 1, 'options' => 1);
  79. $changed = FALSE;
  80. foreach ($fields as $field => $value) {
  81. if (isset($changeable[$field]) && $value !== $this->$field) {
  82. $this->$field = $value;
  83. $changed = TRUE;
  84. }
  85. }
  86. // If there are no new values, just return 0.
  87. if (!$changed) {
  88. return 0;
  89. }
  90. return $this->save();
  91. }
  92. /**
  93. * Magic method for determining which fields should be serialized.
  94. *
  95. * Serialize all properties except the proxy object.
  96. *
  97. * @return array
  98. * An array of properties to be serialized.
  99. */
  100. public function __sleep() {
  101. $ret = get_object_vars($this);
  102. unset($ret['proxy'], $ret['status'], $ret['module'], $ret['is_new']);
  103. return array_keys($ret);
  104. }
  105. /**
  106. * Helper method for ensuring the proxy object is set up.
  107. */
  108. protected function ensureProxy() {
  109. if (!isset($this->proxy)) {
  110. $class = search_api_get_service_info($this->class);
  111. if ($class && class_exists($class['class'])) {
  112. if (empty($this->options)) {
  113. // We always have to provide the options.
  114. $this->options = array();
  115. }
  116. $this->proxy = new $class['class']($this);
  117. }
  118. if (!($this->proxy instanceof SearchApiServiceInterface)) {
  119. throw new SearchApiException(t('Search server with machine name @name specifies illegal service class @class.', array('@name' => $this->machine_name, '@class' => $this->class)));
  120. }
  121. }
  122. }
  123. /**
  124. * If the service class defines additional methods, not specified in the
  125. * SearchApiServiceInterface interface, then they are called via this magic
  126. * method.
  127. */
  128. public function __call($name, $arguments = array()) {
  129. $this->ensureProxy();
  130. return call_user_func_array(array($this->proxy, $name), $arguments);
  131. }
  132. // Proxy methods
  133. // For increased clarity, and since some parameters are passed by reference,
  134. // we don't use the __call() magic method for those.
  135. public function configurationForm(array $form, array &$form_state) {
  136. $this->ensureProxy();
  137. return $this->proxy->configurationForm($form, $form_state);
  138. }
  139. public function configurationFormValidate(array $form, array &$values, array &$form_state) {
  140. $this->ensureProxy();
  141. return $this->proxy->configurationFormValidate($form, $values, $form_state);
  142. }
  143. public function configurationFormSubmit(array $form, array &$values, array &$form_state) {
  144. $this->ensureProxy();
  145. return $this->proxy->configurationFormSubmit($form, $values, $form_state);
  146. }
  147. public function supportsFeature($feature) {
  148. $this->ensureProxy();
  149. return $this->proxy->supportsFeature($feature);
  150. }
  151. public function viewSettings() {
  152. $this->ensureProxy();
  153. return $this->proxy->viewSettings();
  154. }
  155. public function postCreate() {
  156. $this->ensureProxy();
  157. return $this->proxy->postCreate();
  158. }
  159. public function postUpdate() {
  160. $this->ensureProxy();
  161. return $this->proxy->postUpdate();
  162. }
  163. public function preDelete() {
  164. $this->ensureProxy();
  165. return $this->proxy->preDelete();
  166. }
  167. public function addIndex(SearchApiIndex $index) {
  168. $this->ensureProxy();
  169. return $this->proxy->addIndex($index);
  170. }
  171. public function fieldsUpdated(SearchApiIndex $index) {
  172. $this->ensureProxy();
  173. return $this->proxy->fieldsUpdated($index);
  174. }
  175. public function removeIndex($index) {
  176. $this->ensureProxy();
  177. return $this->proxy->removeIndex($index);
  178. }
  179. public function indexItems(SearchApiIndex $index, array $items) {
  180. $this->ensureProxy();
  181. return $this->proxy->indexItems($index, $items);
  182. }
  183. public function deleteItems($ids = 'all', SearchApiIndex $index = NULL) {
  184. $this->ensureProxy();
  185. return $this->proxy->deleteItems($ids, $index);
  186. }
  187. public function query(SearchApiIndex $index, $options = array()) {
  188. $this->ensureProxy();
  189. return $this->proxy->query($index, $options);
  190. }
  191. public function search(SearchApiQueryInterface $query) {
  192. $this->ensureProxy();
  193. return $this->proxy->search($query);
  194. }
  195. }