/build/zf1/doc-utils/pear/php/phpdotnet/phd/Package/PHP/Functions.php

https://github.com/zendframework/ZF2Package · PHP · 218 lines · 203 code · 11 blank · 4 comment · 6 complexity · 72e016db1c41962520733073d6c45622 MD5 · raw file

  1. <?php
  2. namespace phpdotnet\phd;
  3. /* $Id: Functions.php 287923 2009-08-31 16:07:39Z moacir $ */
  4. class Package_PHP_Functions extends Package_Generic_Manpage {
  5. const OPEN_CHUNK = 0x01;
  6. const CLOSE_CHUNK = 0x02;
  7. const OPENED_CHUNK = 0x03;
  8. protected $elementmap = array(
  9. 'set' => 'format_set',
  10. 'refentry' => 'format_refentry',
  11. 'refname' => 'format_refname',
  12. );
  13. protected $textmap = array(
  14. 'pubdate' => 'format_pubdate',
  15. 'refname' => 'format_refname_text',
  16. 'title' => array(
  17. /* DEFAULT */ false,
  18. 'set' => array(
  19. /* DEFAULT */ 'format_bookname',
  20. 'set' => false,
  21. ),
  22. ),
  23. );
  24. protected $CURRENT_ID = "";
  25. /* Current Chunk settings */
  26. protected $cchunk = array();
  27. /* Default Chunk settings */
  28. protected $dchunk = array(
  29. "funcname" => array(),
  30. "firstrefname" => true,
  31. );
  32. /* Common properties for all functions pages */
  33. protected $bookName = "";
  34. protected $date = "";
  35. protected $isFunctionRefSet = false;
  36. protected $isChunk = false;
  37. public function __construct() {
  38. parent::__construct();
  39. $this->registerFormatName("PHP-Functions");
  40. $this->setTitle("PHP Manual");
  41. $this->setExt("3.gz");
  42. $this->setChunked(true);
  43. $this->dchunk = array_merge(parent::getDefaultChunkInfo(), static::getDefaultChunkInfo());
  44. }
  45. public function __destruct() {
  46. $this->close();
  47. }
  48. public function update($event, $val = null) {
  49. switch($event) {
  50. case Render::CHUNK:
  51. $this->isChunk = $val;
  52. break;
  53. case Render::STANDALONE:
  54. if ($val) {
  55. $this->registerElementMap(array_merge(parent::getDefaultElementMap(), $this->elementmap));
  56. $this->registerTextMap(array_merge(parent::getDefaultTextMap(), $this->textmap));
  57. } else {
  58. $this->registerElementMap(static::getDefaultElementMap());
  59. $this->registerTextMap(static::getDefaultTextMap());
  60. }
  61. break;
  62. case Render::INIT:
  63. $this->setOutputDir(Config::output_dir() . strtolower($this->getFormatName()) . '/');
  64. if (file_exists($this->getOutputDir())) {
  65. if (!is_dir($this->getOutputDir())) {
  66. v("Output directory is a file?", E_USER_ERROR);
  67. }
  68. } else {
  69. if (!mkdir($this->getOutputDir())) {
  70. v("Can't create output directory", E_USER_ERROR);
  71. }
  72. }
  73. break;
  74. case Render::VERBOSE:
  75. v("Starting %s rendering", $this->getFormatName(), VERBOSE_FORMAT_RENDERING);
  76. break;
  77. }
  78. }
  79. public function header($index) {
  80. return ".TH " . strtoupper($this->cchunk["funcname"][$index]) . " 3 \"" . $this->date . "\" \"PHP Documentation Group\" \"" . $this->bookName . "\"";
  81. }
  82. public function writeChunk($stream) {
  83. if (!isset($this->cchunk["funcname"][0]))
  84. return;
  85. $index = 0;
  86. rewind($stream);
  87. $filename = $this->cchunk["funcname"][$index] . '.' .$this->getExt();
  88. $gzfile = gzopen($this->getOutputDir() . $filename, "w9");
  89. gzwrite($gzfile, $this->header($index));
  90. gzwrite($gzfile, stream_get_contents($stream));
  91. gzclose($gzfile);
  92. v("Wrote %s", $this->getOutputDir() . $filename, VERBOSE_CHUNK_WRITING);
  93. while(isset($this->cchunk["funcname"][++$index])) {
  94. $filename = $this->cchunk["funcname"][$index] . '.' . $this->getExt();
  95. rewind($stream);
  96. // Replace the default function name by the alternative one
  97. $content = preg_replace('/'.$this->cchunk["funcname"][0].'/',
  98. $this->cchunk["funcname"][$index], stream_get_contents($stream), 1);
  99. $gzfile = gzopen($this->getOutputDir() . $filename, "w9");
  100. gzwrite($gzfile, $this->header($index));
  101. gzwrite($gzfile, $content);
  102. gzclose($gzfile);
  103. v("Wrote %s", $this->getOutputDir() . $filename, VERBOSE_CHUNK_WRITING);
  104. }
  105. }
  106. public function close() {
  107. foreach ($this->getFileStream() as $fp) {
  108. fclose($fp);
  109. }
  110. }
  111. public function appendData($data) {
  112. if (!$this->isFunctionRefSet)
  113. return 0;
  114. switch($this->isChunk) {
  115. case self::CLOSE_CHUNK:
  116. $stream = $this->popFileStream();
  117. $retval = (trim($data) === "") ? false : fwrite($stream, $data);
  118. $this->writeChunk($stream);
  119. fclose($stream);
  120. $this->isChunk = null;
  121. $this->cchunk = array();
  122. return $retval;
  123. break;
  124. case self::OPEN_CHUNK:
  125. $this->pushFileStream(fopen("php://temp/maxmemory", "r+"));
  126. $this->isChunk = self::OPENED_CHUNK;
  127. case self::OPENED_CHUNK:
  128. $stream = $this->getFileStream();
  129. // Remove whitespace nodes
  130. $retval = ($data != "\n" && trim($data) === "") ? false : fwrite(end($stream), $data);
  131. return $retval;
  132. default:
  133. return 0;
  134. }
  135. }
  136. public function format_bookname($value, $tag) {
  137. $this->bookName = $value;
  138. return false;
  139. }
  140. public function format_pubdate($value, $tag) {
  141. $this->date = $value;
  142. return false;
  143. }
  144. public function format_set($open, $name, $attrs, $props) {
  145. if (isset($attrs[Reader::XMLNS_XML]["id"]) && $attrs[Reader::XMLNS_XML]["id"] == "funcref") {
  146. $this->isFunctionRefSet = $open;
  147. }
  148. return false;
  149. }
  150. public function format_refentry($open, $name, $attrs, $props) {
  151. if (!$this->isFunctionRefSet)
  152. return false;
  153. if ($open) {
  154. $this->notify(Render::CHUNK, self::OPEN_CHUNK);
  155. $this->newChunk();
  156. $this->cchunk = $this->dchunk;
  157. } else {
  158. $this->notify(Render::CHUNK, self::CLOSE_CHUNK);
  159. $this->closeChunk();
  160. }
  161. return false;
  162. }
  163. public function format_refname($open, $name, $attrs, $props) {
  164. if ($open) {
  165. return (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) ? false : "";
  166. } else {
  167. if (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) {
  168. $this->cchunk["firstrefname"] = false;
  169. return false;
  170. }
  171. return "";
  172. }
  173. }
  174. public function format_refname_text($value, $tag) {
  175. $this->cchunk["funcname"][] = $this->toValidName(trim($value));
  176. if (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) {
  177. return false;
  178. }
  179. return "";
  180. }
  181. }
  182. /*
  183. * vim600: sw=4 ts=4 syntax=php et
  184. * vim<600: sw=4 ts=4
  185. */