File indexing completed on 2024-04-28 06:00:01

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Debug
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /**
0023  * Concrete class for generating debug dumps related to the output source.
0024  *
0025  * @category   Zend
0026  * @package    Zend_Debug
0027  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0028  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0029  */
0030 
0031 class Zend_Debug
0032 {
0033 
0034     /**
0035      * @var string
0036      */
0037     protected static $_sapi = null;
0038 
0039     /**
0040      * Get the current value of the debug output environment.
0041      * This defaults to the value of PHP_SAPI.
0042      *
0043      * @return string;
0044      */
0045     public static function getSapi()
0046     {
0047         if (self::$_sapi === null) {
0048             self::$_sapi = PHP_SAPI;
0049         }
0050         return self::$_sapi;
0051     }
0052 
0053     /**
0054      * Set the debug ouput environment.
0055      * Setting a value of null causes Zend_Debug to use PHP_SAPI.
0056      *
0057      * @param string $sapi
0058      * @return void;
0059      */
0060     public static function setSapi($sapi)
0061     {
0062         self::$_sapi = $sapi;
0063     }
0064 
0065     /**
0066      * Debug helper function.  This is a wrapper for var_dump() that adds
0067      * the <pre /> tags, cleans up newlines and indents, and runs
0068      * htmlentities() before output.
0069      *
0070      * @param  mixed  $var   The variable to dump.
0071      * @param  string $label OPTIONAL Label to prepend to output.
0072      * @param  bool   $echo  OPTIONAL Echo output if true.
0073      * @return string
0074      */
0075     public static function dump($var, $label=null, $echo=true)
0076     {
0077         // format the label
0078         $label = ($label===null) ? '' : rtrim($label) . ' ';
0079 
0080         // var_dump the variable into a buffer and keep the output
0081         ob_start();
0082         var_dump($var);
0083         $output = ob_get_clean();
0084 
0085         // neaten the newlines and indents
0086         $output = preg_replace("/\]\=\>\n(\s+)/m", "] => ", $output);
0087         if (self::getSapi() == 'cli') {
0088             $output = PHP_EOL . $label
0089                     . PHP_EOL . $output
0090                     . PHP_EOL;
0091         } else {
0092             if(!extension_loaded('xdebug')) {
0093                 $flags = ENT_QUOTES;
0094                 // PHP 5.4.0+
0095                 if (defined('ENT_SUBSTITUTE')) {
0096                     $flags = ENT_QUOTES | ENT_SUBSTITUTE;
0097                 }
0098                 $output = htmlspecialchars($output, $flags);
0099             }
0100 
0101             $output = '<pre>'
0102                     . $label
0103                     . $output
0104                     . '</pre>';
0105         }
0106 
0107         if ($echo) {
0108             echo($output);
0109         }
0110         return $output;
0111     }
0112 
0113 }