PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/phpdotnet/phd/Package/PHP/Web.php

https://github.com/philip/phd
PHP | 196 lines | 164 code | 27 blank | 5 comment | 18 complexity | 8418d83151a8123664ae852b5e4d7730 MD5 | raw file
  1. <?php
  2. namespace phpdotnet\phd;
  3. /* $Id$ */
  4. class Package_PHP_Web extends Package_PHP_XHTML {
  5. public function __construct() {
  6. parent::__construct();
  7. $this->registerFormatName("PHP-Web");
  8. $this->setTitle("PHP Manual");
  9. $this->setChunked(true);
  10. $this->setExt(Config::ext() === null ? ".php" : Config::ext());
  11. }
  12. public function close() {
  13. foreach ($this->getFileStream() as $fp) {
  14. fclose($fp);
  15. }
  16. }
  17. public function __destruct() {
  18. $this->close();
  19. }
  20. public function appendData($data) {
  21. if ($this->appendToBuffer) {
  22. $this->buffer .= $data;
  23. return;
  24. } elseif ($this->flags & Render::CLOSE) {
  25. $fp = $this->popFileStream();
  26. fwrite($fp, $data);
  27. $this->writeChunk($this->CURRENT_CHUNK, $fp);
  28. fclose($fp);
  29. $this->flags ^= Render::CLOSE;
  30. } elseif ($this->flags & Render::OPEN) {
  31. $fp = fopen("php://temp/maxmemory", "r+");
  32. fwrite($fp, $data);
  33. $this->pushFileStream($fp);
  34. $this->flags ^= Render::OPEN;
  35. } else {
  36. $fp = $this->getFileStream();
  37. fwrite(end($fp), $data);
  38. }
  39. }
  40. public function writeChunk($id, $fp) {
  41. $filename = $this->getOutputDir() . $id . $this->getExt();
  42. rewind($fp);
  43. file_put_contents($filename, $this->header($id));
  44. file_put_contents($filename, $fp, FILE_APPEND);
  45. file_put_contents($filename, $this->footer($id), FILE_APPEND);
  46. }
  47. public function update($event, $val = null) {
  48. switch($event) {
  49. case Render::CHUNK:
  50. $this->flags = $val;
  51. break;
  52. case Render::STANDALONE:
  53. if ($val) {
  54. $this->registerElementMap(static::getDefaultElementMap());
  55. $this->registerTextMap(static::getDefaultTextMap());
  56. }
  57. break;
  58. case Render::INIT:
  59. $this->loadVersionAcronymInfo();
  60. $this->setOutputDir(Config::output_dir() . strtolower($this->getFormatName()) . '/');
  61. $this->postConstruct();
  62. if (file_exists($this->getOutputDir())) {
  63. if (!is_dir($this->getOutputDir())) {
  64. v("Output directory is a file?", E_USER_ERROR);
  65. }
  66. } else {
  67. if (!mkdir($this->getOutputDir())) {
  68. v("Can't create output directory", E_USER_ERROR);
  69. }
  70. }
  71. if ($this->getFormatName() == "PHP-Web") {
  72. if (!file_exists($this->getOutputDir() . "toc") || is_file($this->getOutputDir() . "toc")) {
  73. mkdir($this->getOutputDir() . "toc") or die("Can't create the toc directory");
  74. }
  75. }
  76. if (Config::css()) {
  77. $this->fetchStylesheet();
  78. }
  79. break;
  80. case Render::VERBOSE:
  81. v("Starting %s rendering", $this->getFormatName(), VERBOSE_FORMAT_RENDERING);
  82. break;
  83. }
  84. }
  85. public function header($id) {
  86. $ext = $this->getExt();
  87. $parent = Format::getParent($id);
  88. $filename = "toc" . DIRECTORY_SEPARATOR . $parent . ".inc";
  89. $up = array(0 => null, 1 => null);
  90. $incl = '';
  91. $next = $prev = array(null, null);
  92. if ($parent && $parent != "ROOT") {
  93. $siblings = Format::getChildren($parent);
  94. if (!file_exists($this->getOutputDir() . $filename)) {
  95. $toc = array();
  96. foreach($siblings as $sid) {
  97. $toc[] = array(
  98. Format::getFilename($sid).$ext,
  99. Format::getShortDescription($sid),
  100. );
  101. }
  102. $parents = array();
  103. $p = $parent;
  104. while (($p = Format::getParent($p)) && $p != "ROOT") {
  105. $parents[] = array(
  106. Format::getFilename($p).$ext,
  107. Format::getShortDescription($p),
  108. );
  109. }
  110. $content = '<?php
  111. $TOC = ' . var_export($toc, true) . ';
  112. $PARENTS = ' . var_export($parents, true) . ';';
  113. file_put_contents($this->getOutputDir() . $filename, $content);
  114. v("Wrote TOC (%s)", $this->getOutputDir() . $filename, VERBOSE_TOC_WRITING);
  115. }
  116. $incl = 'include_once dirname(__FILE__) ."/toc/' .$parent. '.inc";';
  117. $up = array(Format::getFilename($parent).$ext, Format::getShortDescription($parent));
  118. if ($prevId = Format::getPrevious($id)) {
  119. $prev = array(
  120. Format::getFilename($prevId).$ext,
  121. Format::getShortDescription($prevId),
  122. );
  123. }
  124. if ($nextId = Format::getNext($id)) {
  125. $next = array(
  126. Format::getFilename($nextId).$ext,
  127. Format::getShortDescription($nextId),
  128. );
  129. }
  130. }
  131. $setup = array(
  132. "home" => array('index'.$ext, $this->getTitle()),
  133. "head" => array("UTF-8", $this->lang),
  134. "this" => array($id.$ext, Format::getShortDescription($id)),
  135. "up" => $up,
  136. "prev" => $prev,
  137. "next" => $next,
  138. );
  139. if ($this->getChildren($id)) {
  140. $lang = Config::language();
  141. $setup["extra_header_links"] = array(
  142. "rel" => "alternate",
  143. "href" => "/manual/{$lang}/feeds/{$id}.atom",
  144. "type" => "application/atom+xml",
  145. );
  146. }
  147. $var = var_export($setup, true);
  148. return '<?php
  149. include_once $_SERVER[\'DOCUMENT_ROOT\'] . \'/include/shared-manual.inc\';
  150. $TOC = array();
  151. $PARENTS = array();
  152. '.$incl.'
  153. $setup = '.$var.';
  154. $setup["toc"] = $TOC;
  155. $setup["parents"] = $PARENTS;
  156. manual_setup($setup);
  157. manual_header();
  158. ?>
  159. ';
  160. }
  161. public function footer($id) {
  162. return "<?php manual_footer(); ?>";
  163. }
  164. }
  165. /*
  166. * vim600: sw=4 ts=4 syntax=php et
  167. * vim<600: sw=4 ts=4
  168. */