Page.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. namespace Memcache_UI\Core {
  3. abstract class Page extends Element {
  4. /**
  5. * The HTML body element of the page.
  6. *
  7. * @var array
  8. */
  9. protected $body;
  10. /**
  11. * The MIME content type with charset
  12. *
  13. * @var string
  14. */
  15. protected $contentType = 'text/html; charset=utf8';
  16. /**
  17. * @var Context
  18. */
  19. protected $context;
  20. /**
  21. * The HTML head element of the page.
  22. *
  23. * @var array
  24. */
  25. protected $head;
  26. /**
  27. * The array of HTTP headers
  28. *
  29. * @var array
  30. */
  31. protected $headers = array();
  32. /**
  33. * @var array
  34. */
  35. protected $finalized = array();
  36. /**
  37. * @var array
  38. */
  39. protected $styles;
  40. /**
  41. * Page constructor.
  42. *
  43. * @param Context $context
  44. * @param array $item
  45. * A router info array.
  46. *
  47. * @see Router::getInfo()
  48. */
  49. public function __construct(Context $context, array $item) {
  50. parent::__construct('html');
  51. $context->setMessage($item, LOG_DEBUG);
  52. $this->context = $context;
  53. $this->initializeHead();
  54. $this->initializeBody();
  55. // Will fail if not reimplemented as a concrete method in a child class
  56. if (method_exists($this, 'build')) {
  57. $this->build();
  58. }
  59. }
  60. public function __toString() {
  61. $this->finalizeHeader();
  62. $html = new Element('html', NULL, $this->getHead() . $this->getBody());
  63. return (string) $html;
  64. }
  65. //public abstract function build();
  66. public function emitHeaders() {
  67. $this->finalizeHeader();
  68. foreach ($this->headers as $name => $value) {
  69. header("$name: $value");
  70. }
  71. }
  72. public function finalizeBody() {
  73. if (isset($this->finalized['body'])) {
  74. throw new Exception('Attempt to finalize already finalized body');
  75. }
  76. if ($message = $this->context->getMessage(TRUE)) {
  77. $items = array();
  78. foreach ($message as $row) {
  79. $items[] = new Element('li', array(
  80. 'class' => array($this->context->getLogLevelClass($row[1])),
  81. ), $row[0]);
  82. }
  83. $this->setBody(new Element('div', array('class' => array('messages')), $items), 'messages');
  84. }
  85. foreach ($this->getRegions() as $region) {
  86. $this->body[$region] = implode('', $this->body[$region]);
  87. }
  88. $this->finalized['body'] = TRUE;
  89. }
  90. public function finalizeHead() {
  91. if (isset($this->finalized['head'])) {
  92. throw new Exception('Attempt to finalize already finalized head');
  93. }
  94. $cssLink = new Element('link', array(
  95. 'rel' => 'stylesheet',
  96. 'type' => 'text/css',
  97. 'href' => $this->context->getBase() .'/memcache_ui.css',
  98. ));
  99. $this->setHead($cssLink);
  100. $this->finalized['head'] = TRUE;
  101. }
  102. public function finalizeHeader() {
  103. $contentType = $this->getHeader('content-type');
  104. if (empty($contentType)) {
  105. $this->setHeader('content-type', $this->contentType);
  106. }
  107. }
  108. public function getBody() {
  109. $this->finalizeBody();
  110. $body = new Element('body', NULL, $this->body);
  111. $ret = (string) $body;
  112. return $ret;
  113. }
  114. public function getHead() {
  115. $this->finalizeHead();
  116. $head = new Element('head', NULL, $this->head);
  117. $ret = (string) $head;
  118. return $ret;
  119. }
  120. public function getHeader($name) {
  121. }
  122. public function getRegions() {
  123. $ret = array(
  124. 'header',
  125. 'first sidebar',
  126. 'content top',
  127. 'messages',
  128. 'help',
  129. 'content',
  130. 'content bottom',
  131. 'second sidebar',
  132. 'footer',
  133. );
  134. return $ret;
  135. }
  136. public function initializeBody() {
  137. foreach ($this->getRegions() as $region) {
  138. $this->body[$region] = array();
  139. }
  140. }
  141. public function initializeHead() {
  142. $this->setHead(new Element('title', NULL, 'Memcache info'));
  143. }
  144. public function setBody($fragment, $region = 'content') {
  145. if (!in_array($region, $this->getRegions())) {
  146. $this->context->setMessage(strtr('Attempted to insert data in nonexistent region @region', array(
  147. '@region' => self::check_plain($region),
  148. )), LOG_WARNING);
  149. }
  150. $this->body[$region][] = $fragment;
  151. }
  152. public function setHead($item) {
  153. $this->head[] = $item;
  154. }
  155. public function setHeader($name, $value) {
  156. $this->headers[$name] = $value;
  157. }
  158. }
  159. }