Context.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Elgg;
  3. /**
  4. * PRIVATE CLASS. API IN FLUX. DO NOT USE DIRECTLY.
  5. *
  6. * PLUGIN DEVELOPERS SHOULD USE elgg_*_context FUNCTIONS INSTEAD.
  7. *
  8. * Views can modify their output based on the local context. You may want to
  9. * display a list of blogs on a blog page or in a small widget. The rendered
  10. * output could be different for those two contexts ('blog' vs 'widget').
  11. *
  12. * Pages that pass through the page handling system set the context to the
  13. * first string after the root url. Example: http://example.org/elgg/bookmarks/
  14. * results in the initial context being set to 'bookmarks'.
  15. *
  16. * The context is a stack so that for a widget on a profile, the context stack
  17. * may contain first 'profile' and then 'widget'.
  18. *
  19. * @warning The context is not available until the page_handler runs (after
  20. * the 'init, system' event processing has completed).
  21. *
  22. * @package Elgg.Core
  23. * @access private
  24. * @since 1.10.0
  25. */
  26. final class Context {
  27. private $stack = array();
  28. /**
  29. * Get the most recently pushed context value.
  30. *
  31. * @return string|null
  32. */
  33. public function peek() {
  34. $last = end($this->stack);
  35. return ($last === false) ? null : $last;
  36. }
  37. /**
  38. * Push a context onto the top of the stack
  39. *
  40. * @param string $context The context string to add to the context stack
  41. * @return void
  42. */
  43. public function push($context) {
  44. $this->stack[] = "$context";
  45. }
  46. /**
  47. * Removes and returns the top context string from the stack
  48. *
  49. * @return string|null
  50. */
  51. public function pop() {
  52. return array_pop($this->stack);
  53. }
  54. /**
  55. * Sets the page context
  56. *
  57. * @param string $context The context of the page
  58. * @return bool
  59. */
  60. public function set($context) {
  61. $context = trim($context);
  62. if (empty($context)) {
  63. return false;
  64. }
  65. $context = strtolower($context);
  66. $this->pop();
  67. $this->push($context);
  68. return true;
  69. }
  70. /**
  71. * Check if this context exists anywhere in the stack
  72. *
  73. * This is useful for situations with more than one element in the stack. For
  74. * example, a widget has a context of 'widget'. If a widget view needs to render
  75. * itself differently based on being on the dashboard or profile pages, it
  76. * can check the stack.
  77. *
  78. * @param string $context The context string to check for
  79. * @return bool
  80. */
  81. public function contains($context) {
  82. return in_array($context, $this->stack);
  83. }
  84. /**
  85. * Get the entire context stack as an array (e.g. for backing it up)
  86. *
  87. * @return string[]
  88. */
  89. public function toArray() {
  90. return $this->stack;
  91. }
  92. /**
  93. * Overwrite the entire context stack from an array of strings
  94. *
  95. * @param string[] $stack All contexts to be placed on the stack
  96. * @return void
  97. */
  98. public function fromArray(array $stack) {
  99. $this->stack = array_map('strval', $stack);
  100. }
  101. }