PHP_CompatInfo
[ class tree: PHP_CompatInfo ] [ index: PHP_CompatInfo ] [ all elements ]

Source for file Html.php

Documentation is available at Html.php

  1. <?php
  2. /**
  3.  * Copyright (c) 2008, Laurent Laville <pear@laurent-laville.org>
  4.  *
  5.  * All rights reserved.
  6.  *
  7.  * Redistribution and use in source and binary forms, with or without
  8.  * modification, are permitted provided that the following conditions
  9.  * are met:
  10.  *
  11.  *     * Redistributions of source code must retain the above copyright
  12.  *       notice, this list of conditions and the following disclaimer.
  13.  *     * Redistributions in binary form must reproduce the above copyright
  14.  *       notice, this list of conditions and the following disclaimer in the
  15.  *       documentation and/or other materials provided with the distribution.
  16.  *     * Neither the name of the authors nor the names of its contributors
  17.  *       may be used to endorse or promote products derived from this software
  18.  *       without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  22.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  23.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
  24.  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  25.  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  26.  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  27.  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28.  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  30.  * POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  * PHP versions 4 and 5
  33.  *
  34.  * @category PHP
  35.  * @package  PHP_CompatInfo
  36.  * @author   Laurent Laville <pear@laurent-laville.org>
  37.  * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
  38.  * @version  CVS: $Id: Html.php,v 1.12 2008/07/22 20:26:19 farell Exp $
  39.  * @link     http://pear.php.net/package/PHP_CompatInfo
  40.  * @since    File available since Release 1.8.0b4
  41.  */
  42.  
  43. require_once 'HTML/Table.php';
  44. require_once 'HTML/CSS.php';
  45.  
  46. /**
  47.  * Html renderer for PHP_CompatInfo component.
  48.  *
  49.  * The PHP_CompatInfo_Renderer_Html class is a concrete implementation
  50.  * of PHP_CompatInfo_Renderer abstract class. It simply display results
  51.  * as web/html content with help of PEAR::Html_Table
  52.  *
  53.  * @category PHP
  54.  * @package  PHP_CompatInfo
  55.  * @author   Laurent Laville <pear@laurent-laville.org>
  56.  * @license  http://www.opensource.org/licenses/bsd-license.php  BSD
  57.  * @version  Release: @package_version@
  58.  * @link     http://pear.php.net/package/PHP_CompatInfo
  59.  * @since    Class available since Release 1.8.0b4
  60.  */
  61. {
  62.     /**
  63.      * Style sheet for the custom layout
  64.      *
  65.      * @var    string 
  66.      * @access public
  67.      * @since  1.8.0b4
  68.      */
  69.     var $css;
  70.  
  71.     /**
  72.      * Html Renderer Class constructor (ZE1) for PHP4
  73.      *
  74.      * @param object &$parser Instance of the parser (model of MVC pattern)
  75.      * @param array  $conf    A hash containing any additional configuration
  76.      *
  77.      * @access public
  78.      * @since  version 1.8.0b4 (2008-06-18)
  79.      */
  80.     function PHP_CompatInfo_Renderer_Html(&$parser$conf)
  81.     {
  82.         $this->__construct($parser$conf);
  83.     }
  84.  
  85.     /**
  86.      * Html Renderer Class constructor (ZE2) for PHP5+
  87.      *
  88.      * @param object &$parser Instance of the parser (model of MVC pattern)
  89.      * @param array  $conf    A hash containing any additional configuration
  90.      *
  91.      * @access public
  92.      * @since  version 1.8.0b4 (2008-06-18)
  93.      */
  94.     function __construct(&$parser$conf)
  95.     {
  96.         $defaults = array('tdwidth' => array(1842713));
  97.         $conf     array_merge($defaults$conf);
  98.  
  99.         parent::PHP_CompatInfo_Renderer($parser$conf);
  100.  
  101.     }
  102.  
  103.     /**
  104.      * Display final results
  105.      *
  106.      * Display final results, when data source parsing is over.
  107.      *
  108.      * @access public
  109.      * @return void 
  110.      * @since  version 1.8.0b4 (2008-06-18)
  111.      */
  112.     function display()
  113.     {
  114.         $o    $this->args['output-level'];
  115.         $info $this->parseData;
  116.  
  117.         if ($info == false{
  118.             // protect against invalid data source
  119.             print 'Invalid data source';
  120.             return;
  121.         }
  122.  
  123.         $src $this->_parser->dataSource;
  124.         if ($src['dataType'== 'directory'{
  125.             $dir      $src['dataSource'];
  126.             $hdr_col1 'Directory';
  127.         elseif ($src['dataType'== 'file'{
  128.             $file     $src['dataSource'];
  129.             $hdr_col1 'File';
  130.         else {
  131.             $string   $src['dataSource'];
  132.             $hdr_col1 'Source code';
  133.         }
  134.  
  135.         $dataTable = new HTML_Table();
  136.         $thead     =$dataTable->getHeader();
  137.         $tbody     =$dataTable->getBody();
  138.         $tfoot     =$dataTable->getFooter();
  139.  
  140.         $hdr = array($hdr_col1);
  141.         $atr = array('scope="col"');
  142.         if ($o 16{
  143.             $hdr['Version';
  144.             $atr['scope="col"';
  145.         }
  146.         if ($o 1{
  147.             $hdr['C';
  148.             $atr['scope="col"';
  149.         }
  150.         if ($o 2{
  151.             $hdr['Extensions';
  152.             $atr['scope="col"';
  153.         }
  154.         if ($o 4{
  155.             if ($o 8{
  156.                 $hdr['Constants/Tokens';
  157.                 $atr['scope="col"';
  158.             else {
  159.                 $hdr['Constants';
  160.                 $atr['scope="col"';
  161.             }
  162.         else {
  163.             if ($o 8{
  164.                 $hdr['Tokens';
  165.                 $atr['scope="col"';
  166.             }
  167.         }
  168.  
  169.         $thead->addRow($hdr$atr);
  170.  
  171.         $ext   implode("<br/>"$info['extensions']);
  172.         $const implode("<br/>"array_merge($info['constants']$info['tokens']));
  173.         if (isset($dir)) {
  174.             $ds    = DIRECTORY_SEPARATOR;
  175.             $dir   str_replace(array('\\''/')$ds$dir);
  176.             $title $src['dataCount'' file';
  177.             if ($src['dataCount'> 1{
  178.                 $title .= 's'// plural
  179.             }
  180.         elseif (isset($file)) {
  181.             $title '1 file';
  182.         else {
  183.             $title '1 chunk of code';
  184.         }
  185.         $data = array('Summary: '$title ' parsed');
  186.  
  187.         if ($o 16{
  188.             if (empty($info['max_version'])) {
  189.                 $data[$info['version'];
  190.             else {
  191.                 $data[implode("<br/>"array($info['version'],
  192.                                                 $info['max_version']));
  193.             }
  194.         }
  195.         if ($o 1{
  196.             $data[$info['cond_code'][0];
  197.         }
  198.         if ($o 2{
  199.             $data[$ext;
  200.         }
  201.         if ($o 4{
  202.             if ($o 8{
  203.                 $data[$const;
  204.             else {
  205.                 $data[implode("<br/>"$info['constants']);
  206.             }
  207.         else {
  208.             if ($o 8{
  209.                 $data[implode("<br/>"$info['tokens']);
  210.             }
  211.         }
  212.  
  213.         // summary informations
  214.         $tfoot->addRow($data);
  215.  
  216.         // summarize : print only summary for directory without files details
  217.         if ($this->args['summarize'=== false && isset($dir)) {
  218.             // display result of parsing multiple files
  219.  
  220.             unset($info['max_version']);
  221.             unset($info['version']);
  222.             unset($info['functions']);
  223.             unset($info['extensions']);
  224.             unset($info['constants']);
  225.             unset($info['tokens']);
  226.             unset($info['cond_code']);
  227.  
  228.             $ignored $info['ignored_files'];
  229.  
  230.             unset($info['ignored_files']);
  231.             unset($info['ignored_functions']);
  232.             unset($info['ignored_extensions']);
  233.             unset($info['ignored_constants']);
  234.  
  235.             foreach ($info as $file => $info{
  236.                 if ($info === false{
  237.                     continue;  // skip this (invalid) file
  238.                 }
  239.                 $ext   implode("<br/>"$info['extensions']);
  240.                 $const implode("<br/>"array_merge($info['constants'],
  241.                                                       $info['tokens']));
  242.  
  243.                 $file str_replace(array('\\''/')$ds$file);
  244.  
  245.                 $path dirname($file);
  246.                 $tbody->addRow(array($path)array('class' => 'dirname',
  247.                                                    'colspan' => count($hdr)));
  248.  
  249.                 $data = array(basename($file));
  250.                 if ($o 16{
  251.                     if (empty($info['max_version'])) {
  252.                         $data[$info['version'];
  253.                     else {
  254.                         $data[implode("<br/>"array($info['version'],
  255.                                                          $info['max_version']));
  256.                     }
  257.                 }
  258.                 if ($o 1{
  259.                     $data[$info['cond_code'][0];
  260.                 }
  261.                 if ($o 2{
  262.                     $data[$ext;
  263.                 }
  264.                 if ($o 4{
  265.                     if ($o 8{
  266.                         $data[$const;
  267.                     else {
  268.                         $data[implode("<br/>"$info['constants']);
  269.                     }
  270.                 else {
  271.                     if ($o 8{
  272.                         $data[implode("<br/>"$info['tokens']);
  273.                     }
  274.                 }
  275.  
  276.                 $tbody->addRow($data);
  277.             }
  278.         elseif ($this->args['summarize'=== false && !isset($dir)) {
  279.             // display result of parsing a single file, or a chunk of code
  280.             if (isset($file)) {
  281.                 $path dirname($file);
  282.             else {
  283.                 $path '.';
  284.             }
  285.             $tbody->addRow(array($path)array('class' => 'dirname',
  286.                                                'colspan' => count($hdr)));
  287.             if (isset($file)) {
  288.                 $data[0basename($file);
  289.             else {
  290.                 $data[0htmlspecialchars('<?php ... ?>');
  291.             }
  292.             $tbody->addRow($data);
  293.         else {
  294.             // display only result summary of parsing a data source
  295.             if (isset($dir)) {
  296.                 $path dirname($dir[0]);
  297.             elseif (isset($file)) {
  298.                 $path dirname($file);
  299.             else {
  300.                 $path '.';
  301.             }
  302.             $tbody->addRow(array($path)array('class' => 'dirname',
  303.                                                'colspan' => count($hdr)));
  304.         }
  305.  
  306.         $evenRow = array('class' => 'even');
  307.         $oddRow  = null;
  308.         $tbody->altRowAttributes(1$evenRow$oddRowtrue);
  309.  
  310.         echo $this->toHtml($dataTable);
  311.     }
  312.  
  313.     /**
  314.      * Returns the custom style sheet
  315.      *
  316.      * Returns the custom style sheet to use for layout
  317.      *
  318.      * @param int   $destination (optional) Destination of css content
  319.      * @param mixed $extra       (optional) Additional data depending of destination
  320.      *
  321.      * @return mixed 
  322.      * @access public
  323.      * @since  version 1.8.0b4 (2008-06-18)
  324.      */
  325.     function getStyleSheet($destination = 1$extra = null)
  326.     {
  327.         $css = new HTML_CSS();
  328.         $css->parseFile($this->css);
  329.  
  330.         $tdw $this->conf['tdwidth'];
  331.         $em  array_sum($tdw);
  332.         $td  'td';
  333.         $o   $this->args['output-level'];
  334.  
  335.         $css->setStyle('.outer td.dirname''width'$em.'em');
  336.         if ($o 16{
  337.             $td .= '+td';
  338.             $css->setStyle('.outer '.$td'width'$tdw[1].'em');
  339.             $em $em $tdw[1];
  340.         }
  341.         if ($o 1{
  342.             $td .= '+td';
  343.             $css->setStyle('.outer '.$td'width'$tdw[2].'em');
  344.             $em $em $tdw[2];
  345.         }
  346.         if ($o 2{
  347.             $td .= '+td';
  348.             $css->setStyle('.outer '.$td'width'$tdw[3].'em');
  349.             $em $em $tdw[3];
  350.         }
  351.         if ($o 12{
  352.             $td .= '+td';
  353.             $css->setStyle('.outer '.$td'width'$tdw[4].'em');
  354.             $em $em $tdw[4];
  355.         }
  356.         $css->setStyle('.outer td''width'$em .'em');
  357.  
  358.         $styles '';
  359.  
  360.         switch ($destination{
  361.         case 1:  // embedded styles
  362.             $styles $css->toString();
  363.             break;
  364.         case 2:  // save only to file
  365.             $css->toFile($extra);
  366.             $styles $extra;
  367.             break;
  368.         case 3:  // apply a user function
  369.             if (is_callable($extra)) {
  370.                 $styles call_user_func_array($extraarray($css));
  371.             }
  372.             break;
  373.         default:
  374.             break;
  375.         }
  376.         return $styles;
  377.     }
  378.  
  379.     /**
  380.      * Set a custom style sheet
  381.      *
  382.      * Set a custom style sheet to use your own styles
  383.      *
  384.      * @param string $css (optional) File to read user-defined styles from
  385.      *
  386.      * @return bool    True if custom styles, false if default styles applied
  387.      * @access public
  388.      * @since  version 1.8.0b4 (2008-06-18)
  389.      */
  390.     function setStyleSheet($css = null)
  391.     {
  392.         // default stylesheet is into package data directory
  393.         if (!isset($css)) {
  394.             $css '@data_dir@' . DIRECTORY_SEPARATOR
  395.                  . '@package_name@' . DIRECTORY_SEPARATOR
  396.                  . 'pci.css';
  397.         }
  398.  
  399.         $res = isset($css&& file_exists($css);
  400.         if ($res{
  401.             $this->css = $css;
  402.         }
  403.         return $res;
  404.     }
  405.  
  406.     /**
  407.      * Returns HTML code
  408.      *
  409.      * Returns HTML code of parsing result
  410.      *
  411.      * @param object $obj instance of HTML_Table
  412.      *
  413.      * @access public
  414.      * @return string 
  415.      * @since  version 1.8.0b4 (2008-06-18)
  416.      */
  417.     function toHtml($obj)
  418.     {
  419.         if (!isset($this->css)) {
  420.             // when no user-styles defined, used the default values
  421.             $this->setStyleSheet();
  422.         }
  423.         $styles $this->getStyleSheet();
  424.  
  425.         $body $obj->toHtml();
  426.  
  427.         $html = <<<HTML
  428. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  429.     "http://www.w3c.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  430. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  431. <head>
  432. <title>PHP_CompatInfo</title>
  433. <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
  434. <style type="text/css">
  435. <!--
  436. $styles
  437.  -->
  438. </style>
  439. </head>
  440. <body>
  441. <div class="outer">
  442. <div class="inner">
  443. $body
  444. </div>
  445. </div>
  446. </body>
  447. </html>
  448. HTML;
  449.         return $html;
  450.     }
  451. }
  452. ?>

Documentation generated on Sun, 30 Nov 2008 16:30:27 -0500 by phpDocumentor 1.4.0. PEAR Logo Copyright © PHP Group 2004.