Page.inc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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->config = new Config($context);
  54. $this->initializeHead();
  55. $this->initializeBody();
  56. // Will fail if not reimplemented as a concrete method in a child class
  57. if (method_exists($this, 'build')) {
  58. $this->build();
  59. }
  60. }
  61. public function __toString() {
  62. $this->finalizeHeader();
  63. $html = new Element('html', NULL, $this->getHead() . $this->getBody());
  64. return (string) $html;
  65. }
  66. //public abstract function build();
  67. public function emitHeaders() {
  68. $this->finalizeHeader();
  69. foreach ($this->headers as $name => $value) {
  70. header("$name: $value");
  71. }
  72. }
  73. public function finalizeBody() {
  74. if (isset($this->finalized['body'])) {
  75. throw new Exception('Attempt to finalize already finalized body');
  76. }
  77. if ($message = $this->context->getMessage(TRUE)) {
  78. $items = array();
  79. foreach ($message as $row) {
  80. $items[] = new Element('li', array(
  81. 'class' => array($this->context->getLogLevelClass($row[1])),
  82. ), $row[0]);
  83. }
  84. $this->setBody(new Element('div', array('class' => array('messages')), $items), 'messages');
  85. }
  86. foreach ($this->getRegions() as $region) {
  87. $this->body[$region] = implode('', $this->body[$region]);
  88. }
  89. $this->finalized['body'] = TRUE;
  90. }
  91. public function finalizeHead() {
  92. if (isset($this->finalized['head'])) {
  93. throw new Exception('Attempt to finalize already finalized head');
  94. }
  95. $cssLink = new Element('link', array(
  96. 'rel' => 'stylesheet',
  97. 'type' => 'text/css',
  98. 'href' => $this->context->getBase() .'/memcache_ui.css',
  99. ));
  100. $this->setHead($cssLink);
  101. $this->finalized['head'] = TRUE;
  102. }
  103. public function finalizeHeader() {
  104. $contentType = $this->getHeader('content-type');
  105. if (empty($contentType)) {
  106. $this->setHeader('content-type', $this->contentType);
  107. }
  108. }
  109. public function getBody() {
  110. $this->finalizeBody();
  111. $body = new Element('body', NULL, $this->body);
  112. $ret = (string) $body;
  113. return $ret;
  114. }
  115. public function getHead() {
  116. $this->finalizeHead();
  117. $head = new Element('head', NULL, $this->head);
  118. $ret = (string) $head;
  119. return $ret;
  120. }
  121. public function getHeader($name) {
  122. }
  123. public function getRegions() {
  124. $ret = array(
  125. 'header',
  126. 'first sidebar',
  127. 'content top',
  128. 'messages',
  129. 'help',
  130. 'content',
  131. 'content bottom',
  132. 'second sidebar',
  133. 'footer',
  134. );
  135. return $ret;
  136. }
  137. public function initializeBody() {
  138. foreach ($this->getRegions() as $region) {
  139. $this->body[$region] = array();
  140. }
  141. }
  142. public function initializeHead() {
  143. $this->setHead(new Element('title', NULL, 'Memcache info'));
  144. }
  145. public function setBody($fragment, $region = 'content') {
  146. if (!in_array($region, $this->getRegions())) {
  147. $this->context->setMessage(strtr('Attempted to insert data in nonexistent region @region', array(
  148. '@region' => self::check_plain($region),
  149. )), LOG_WARNING);
  150. }
  151. $this->body[$region][] = $fragment;
  152. }
  153. public function setHead($item) {
  154. $this->head[] = $item;
  155. }
  156. public function setHeader($name, $value) {
  157. $this->headers[$name] = $value;
  158. }
  159. }
  160. }