/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
- <?php
- namespace phpdotnet\phd;
- /* $Id: Functions.php 287923 2009-08-31 16:07:39Z moacir $ */
- class Package_PHP_Functions extends Package_Generic_Manpage {
- const OPEN_CHUNK = 0x01;
- const CLOSE_CHUNK = 0x02;
- const OPENED_CHUNK = 0x03;
- protected $elementmap = array(
- 'set' => 'format_set',
- 'refentry' => 'format_refentry',
- 'refname' => 'format_refname',
- );
- protected $textmap = array(
- 'pubdate' => 'format_pubdate',
- 'refname' => 'format_refname_text',
- 'title' => array(
- /* DEFAULT */ false,
- 'set' => array(
- /* DEFAULT */ 'format_bookname',
- 'set' => false,
- ),
- ),
- );
- protected $CURRENT_ID = "";
- /* Current Chunk settings */
- protected $cchunk = array();
- /* Default Chunk settings */
- protected $dchunk = array(
- "funcname" => array(),
- "firstrefname" => true,
- );
- /* Common properties for all functions pages */
- protected $bookName = "";
- protected $date = "";
- protected $isFunctionRefSet = false;
- protected $isChunk = false;
- public function __construct() {
- parent::__construct();
- $this->registerFormatName("PHP-Functions");
- $this->setTitle("PHP Manual");
- $this->setExt("3.gz");
- $this->setChunked(true);
- $this->dchunk = array_merge(parent::getDefaultChunkInfo(), static::getDefaultChunkInfo());
- }
- public function __destruct() {
- $this->close();
- }
- public function update($event, $val = null) {
- switch($event) {
- case Render::CHUNK:
- $this->isChunk = $val;
- break;
- case Render::STANDALONE:
- if ($val) {
- $this->registerElementMap(array_merge(parent::getDefaultElementMap(), $this->elementmap));
- $this->registerTextMap(array_merge(parent::getDefaultTextMap(), $this->textmap));
- } else {
- $this->registerElementMap(static::getDefaultElementMap());
- $this->registerTextMap(static::getDefaultTextMap());
- }
- break;
- case Render::INIT:
- $this->setOutputDir(Config::output_dir() . strtolower($this->getFormatName()) . '/');
- if (file_exists($this->getOutputDir())) {
- if (!is_dir($this->getOutputDir())) {
- v("Output directory is a file?", E_USER_ERROR);
- }
- } else {
- if (!mkdir($this->getOutputDir())) {
- v("Can't create output directory", E_USER_ERROR);
- }
- }
- break;
- case Render::VERBOSE:
- v("Starting %s rendering", $this->getFormatName(), VERBOSE_FORMAT_RENDERING);
- break;
- }
- }
- public function header($index) {
- return ".TH " . strtoupper($this->cchunk["funcname"][$index]) . " 3 \"" . $this->date . "\" \"PHP Documentation Group\" \"" . $this->bookName . "\"";
- }
- public function writeChunk($stream) {
- if (!isset($this->cchunk["funcname"][0]))
- return;
- $index = 0;
- rewind($stream);
- $filename = $this->cchunk["funcname"][$index] . '.' .$this->getExt();
- $gzfile = gzopen($this->getOutputDir() . $filename, "w9");
- gzwrite($gzfile, $this->header($index));
- gzwrite($gzfile, stream_get_contents($stream));
- gzclose($gzfile);
- v("Wrote %s", $this->getOutputDir() . $filename, VERBOSE_CHUNK_WRITING);
- while(isset($this->cchunk["funcname"][++$index])) {
- $filename = $this->cchunk["funcname"][$index] . '.' . $this->getExt();
- rewind($stream);
- // Replace the default function name by the alternative one
- $content = preg_replace('/'.$this->cchunk["funcname"][0].'/',
- $this->cchunk["funcname"][$index], stream_get_contents($stream), 1);
- $gzfile = gzopen($this->getOutputDir() . $filename, "w9");
- gzwrite($gzfile, $this->header($index));
- gzwrite($gzfile, $content);
- gzclose($gzfile);
- v("Wrote %s", $this->getOutputDir() . $filename, VERBOSE_CHUNK_WRITING);
- }
- }
- public function close() {
- foreach ($this->getFileStream() as $fp) {
- fclose($fp);
- }
- }
- public function appendData($data) {
- if (!$this->isFunctionRefSet)
- return 0;
- switch($this->isChunk) {
- case self::CLOSE_CHUNK:
- $stream = $this->popFileStream();
- $retval = (trim($data) === "") ? false : fwrite($stream, $data);
- $this->writeChunk($stream);
- fclose($stream);
- $this->isChunk = null;
- $this->cchunk = array();
- return $retval;
- break;
- case self::OPEN_CHUNK:
- $this->pushFileStream(fopen("php://temp/maxmemory", "r+"));
- $this->isChunk = self::OPENED_CHUNK;
- case self::OPENED_CHUNK:
- $stream = $this->getFileStream();
- // Remove whitespace nodes
- $retval = ($data != "\n" && trim($data) === "") ? false : fwrite(end($stream), $data);
- return $retval;
- default:
- return 0;
- }
- }
- public function format_bookname($value, $tag) {
- $this->bookName = $value;
- return false;
- }
- public function format_pubdate($value, $tag) {
- $this->date = $value;
- return false;
- }
- public function format_set($open, $name, $attrs, $props) {
- if (isset($attrs[Reader::XMLNS_XML]["id"]) && $attrs[Reader::XMLNS_XML]["id"] == "funcref") {
- $this->isFunctionRefSet = $open;
- }
- return false;
- }
- public function format_refentry($open, $name, $attrs, $props) {
- if (!$this->isFunctionRefSet)
- return false;
- if ($open) {
- $this->notify(Render::CHUNK, self::OPEN_CHUNK);
- $this->newChunk();
- $this->cchunk = $this->dchunk;
- } else {
- $this->notify(Render::CHUNK, self::CLOSE_CHUNK);
- $this->closeChunk();
- }
- return false;
- }
- public function format_refname($open, $name, $attrs, $props) {
- if ($open) {
- return (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) ? false : "";
- } else {
- if (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) {
- $this->cchunk["firstrefname"] = false;
- return false;
- }
- return "";
- }
- }
- public function format_refname_text($value, $tag) {
- $this->cchunk["funcname"][] = $this->toValidName(trim($value));
- if (isset($this->cchunk["firstrefname"]) && $this->cchunk["firstrefname"]) {
- return false;
- }
- return "";
- }
- }
- /*
- * vim600: sw=4 ts=4 syntax=php et
- * vim<600: sw=4 ts=4
- */