HashSiteStore.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * In-memory implementation of SiteStore.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * In-memory SiteStore implementation, storing sites in an associative array.
  24. *
  25. * @author Daniel Kinzler
  26. * @author Katie Filbert < aude.wiki@gmail.com >
  27. *
  28. * @since 1.25
  29. * @ingroup Site
  30. */
  31. class HashSiteStore implements SiteStore {
  32. /**
  33. * @var Site[]
  34. */
  35. private $sites = [];
  36. /**
  37. * @param Site[] $sites
  38. */
  39. public function __construct( $sites = [] ) {
  40. $this->saveSites( $sites );
  41. }
  42. /**
  43. * Saves the provided site.
  44. *
  45. * @since 1.25
  46. *
  47. * @param Site $site
  48. *
  49. * @return bool Success indicator
  50. */
  51. public function saveSite( Site $site ) {
  52. $this->sites[$site->getGlobalId()] = $site;
  53. return true;
  54. }
  55. /**
  56. * Saves the provided sites.
  57. *
  58. * @since 1.25
  59. *
  60. * @param Site[] $sites
  61. *
  62. * @return bool Success indicator
  63. */
  64. public function saveSites( array $sites ) {
  65. foreach ( $sites as $site ) {
  66. $this->saveSite( $site );
  67. }
  68. return true;
  69. }
  70. /**
  71. * Returns the site with provided global id, or null if there is no such site.
  72. *
  73. * @since 1.25
  74. *
  75. * @param string $globalId
  76. * @param string $source either 'cache' or 'recache'.
  77. * If 'cache', the values can (but not obliged) come from a cache.
  78. *
  79. * @return Site|null
  80. */
  81. public function getSite( $globalId, $source = 'cache' ) {
  82. return $this->sites[$globalId] ?? null;
  83. }
  84. /**
  85. * Returns a list of all sites. By default this site is
  86. * fetched from the cache, which can be changed to loading
  87. * the list from the database using the $useCache parameter.
  88. *
  89. * @since 1.25
  90. *
  91. * @param string $source either 'cache' or 'recache'.
  92. * If 'cache', the values can (but not obliged) come from a cache.
  93. *
  94. * @return SiteList
  95. */
  96. public function getSites( $source = 'cache' ) {
  97. return new SiteList( $this->sites );
  98. }
  99. /**
  100. * Deletes all sites from the database. After calling clear(), getSites() will return an empty
  101. * list and getSite() will return null until saveSite() or saveSites() is called.
  102. * @return bool
  103. */
  104. public function clear() {
  105. $this->sites = [];
  106. return true;
  107. }
  108. }