Version: current

      1 <?php
      2 /**
      3  * @package sapphire
      4  * @subpackage dev
      5  */
      6
      7 /**
      8  * A basic HTML wrapper for stylish rendering of a developement info view.
      9  * Used to output error messages, and test results.
     10  *
     11  * @package sapphire
     12  * @subpackage dev
     13  */
     14 class DebugView {
     15
     16     protected static $error_types = array(
     17         E_USER_ERROR => array(
     18             'title' => 'User Error',
     19             'class' => 'error'
     20         ),
     21         E_CORE_ERROR => array(
     22             'title' => 'Core Error',
     23             'class' => 'error'
     24         ),
     25         E_NOTICE => array(
     26             'title' => 'Notice',
     27             'class' => 'notice'
     28         ),
     29         E_USER_NOTICE => array(
     30             'title' => 'User Notice',
     31             'class' => 'notice'
     32         ),
     33         E_CORE_ERROR => array(
     34             'title' => 'Core Error',
     35             'class' => 'error'
     36         ),
     37         E_WARNING => array(
     38             'title' => 'Warning',
     39             'class' => 'warning'
     40         ),
     41         E_CORE_WARNING => array(
     42             'title' => 'Core Warning',
     43             'class' => 'warning'
     44         ),
     45         E_USER_WARNING => array(
     46             'title' => 'User Warning',
     47             'class' => 'warning'
     48         )
     49     );
     50
     51     /**
     52      * Generate breadcrumb links to the URL path being displayed
     53      *
     54      * @return string
     55      */
     56     public function Breadcrumbs() {
     57         $basePath = str_replace(Director::protocolAndHost(), '', Director::absoluteBaseURL());
     58         $relPath = parse_url(substr($_SERVER['REQUEST_URI'], strlen($basePath), strlen($_SERVER['REQUEST_URI'])), PHP_URL_PATH);
     59         $parts = explode('/', $relPath);
     60         $base = Director::absoluteBaseURL();
     61         $pathPart = "";
     62         $pathLinks = array();
     63         foreach($parts as $part) {
     64             if ($part != '') {
     65                 $pathPart .= "$part/";
     66                 $pathLinks[] = "<a href=\"$base$pathPart\">$part</a>";
     67             }
     68         }
     69         return implode('&rarr;&nbsp;', $pathLinks);
     70     }
     71
     72     /**
     73      * Render HTML header for development views
     74      */
     75     public function writeHeader() {
     76         echo '<!DOCTYPE html><html><head><title>' . htmlentities($_SERVER['REQUEST_METHOD'] . ' ' . $_SERVER['REQUEST_URI']) . '</title>';
     77         echo '<style type="text/css">';
     78         echo 'body { background-color:#eee; margin:0; padding:0; font-family:Helvetica,Arial,sans-serif; }';
     79         echo '.info { border-bottom:1px dotted #333; background-color:#ccdef3; margin:0; padding:6px 12px; }';
     80         echo '.info h1 { margin:0; padding:0; color:#333; letter-spacing:-2px; }';
     81         echo '.header { margin:0; border-bottom:6px solid #ccdef3; height:23px; background-color:#666673; padding:4px 0 2px 6px; background-image:url('.Director::absoluteBaseURL().'cms/images/mainmenu/top-bg.gif); }';
     82         echo '.trace { padding:6px 12px; }';
     83         echo '.trace li { font-size:14px; margin:6px 0; }';
     84         echo 'pre { margin-left:18px; }';
     85         echo 'pre span { color:#999;}';
     86         echo 'pre .error { color:#f00; }';
     87         echo '.pass { margin-top:18px; padding:2px 20px 2px 40px; color:#006600; background:#E2F9E3 url('.Director::absoluteBaseURL() .'cms/images/alert-good.gif) no-repeat scroll 7px 50%; border:1px solid #8DD38D; }';
     88         echo '.fail { margin-top:18px; padding:2px 20px 2px 40px; color:#C80700; background:#FFE9E9 url('.Director::absoluteBaseURL() .'cms/images/alert-bad.gif) no-repeat scroll 7px 50%; border:1px solid #C80700; }';
     89         echo '.failure span { color:#C80700; font-weight:bold; }';
     90         echo '</style></head>';
     91         echo '<body>';
     92         echo '<div class="header"><img src="'. Director::absoluteBaseURL() .'cms/images/mainmenu/logo.gif" width="26" height="23"></div>';
     93     }
     94
     95     /**
     96      * Render the information header for the view
     97      *
     98      * @param string $title
     99      * @param string $title
    100      */
    101     public function writeInfo($title, $subtitle, $description=false) {
    102         echo '<div class="info">';
    103         echo "<h1>" . Convert::raw2xml($title) . "</h1>";
    104         if($subtitle) echo "<h3>" . Convert::raw2xml($subtitle) . "</h3>";
    105         if ($description) {
    106             echo "<p>$description</p>";
    107         } else {
    108             echo $this->Breadcrumbs();
    109         }
    110         echo '</div>';
    111     }
    112
    113     /**
    114      * Render HTML footer for development views
    115      */
    116     public function writeFooter() {
    117         echo "</body></html>";
    118     }
    119
    120     /**
    121      * Write information about the error to the screen
    122      */
    123     public function writeError($httpRequest, $errno, $errstr, $errfile, $errline, $errcontext) {
    124         $errorType = self::$error_types[$errno];
    125         $httpRequestEnt = htmlentities($httpRequest);
    126         echo '<div class="info ' . $errorType['class'] . '">';
    127         echo "<h1>[" . $errorType['title'] . '] ' . strip_tags($errstr) . "</h1>";
    128         echo "<h3>$httpRequestEnt</h3>";
    129         echo "<p>Line <strong>$errline</strong> in <strong>$errfile</strong></p>";
    130         echo '</div>';
    131     }
    132
    133     /**
    134      * Write a fragment of the a source file
    135      * @param $lines An array of file lines; the keys should be the original line numbers
    136      */
    137     function writeSourceFragment($lines, $errline) {
    138         echo '<div class="trace"><h3>Source</h3>';
    139         echo '<pre>';
    140         foreach($lines as $offset => $line) {
    141             $line = htmlentities($line);
    142             if ($offset == $errline) {
    143                 echo "<span>$offset</span> <span class=\"error\">$line</span>";
    144             } else {
    145                 echo "<span>$offset</span> $line";
    146             }
    147         }
    148         echo '</pre>';
    149     }
    150
    151     /**
    152      * Write a backtrace
    153      */
    154     function writeTrace($trace) {
    155         echo '<h3>Trace</h3>';
    156         echo SS_Backtrace::get_rendered_backtrace($trace);
    157         echo '</div>';
    158     }
    159
    160     /**
    161      * @param string $text
    162      */
    163     function writeParagraph($text) {
    164         echo '<p class="info">' . $text . '</p>';
    165     }
    166 }
    167
    168 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -