├── .gitignore ├── LICENSE ├── README.md ├── composer.json ├── examples ├── basic_example.png ├── examples.php ├── label_example.png └── pre_comparison.png └── src └── Paste └── Pre.php /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2013 Aaron Oxborrow 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## Pre 2 | A handsome replacement for print\_r & var\_dump. Output debugging info in a minimally styled `
` block. 
 3 | 
 4 | ```php
 5 | // basic usage
 6 | echo Pre::r($data);
 7 | ```
 8 | 
 9 | ![Basic Example](https://github.com/paste/Pre.php/raw/master/examples/basic_example.png)  
10 | 
11 | ```php
12 | // add data to storage queue with label
13 | Pre::add($data, 'My Debug Data');
14 | 
15 | // configure dimensions
16 | Pre::$config['width'] = 400;
17 | Pre::$config['height'] = 80;
18 | 
19 | // render and clear queue
20 | echo Pre::render();
21 | ```
22 | 
23 | ![Label Example](https://github.com/paste/Pre.php/raw/master/examples/label_example.png)  
24 | 
25 | 
26 | 
27 | Installation
28 | ------------
29 | 
30 | [Use Composer](http://getcomposer.org/). Add `paste/pre` to your project's `composer.json`:
31 | 
32 | ```json
33 | {
34 |     "require": {
35 |         "paste/pre": "dev-master"
36 |     }
37 | }
38 | ```
39 | 
40 | Or just include Pre.php directly into your project. You might also want to setup the Pre() function shortcut for convenience:
41 | 
42 | ```php
43 | ` block
63 | 
64 | #### Direct Output
65 | ```php
66 | =5.3.0"
18 | 	},
19 | 	"autoload": {
20 | 		"psr-0": {"Paste": "src/"}
21 | 	}
22 | }


--------------------------------------------------------------------------------
/examples/basic_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aoxborrow/Pre.php/bf47c87c8fd8f1f75d6b2a3235ee072bb45a5842/examples/basic_example.png


--------------------------------------------------------------------------------
/examples/examples.php:
--------------------------------------------------------------------------------
 1 |  NULL, 
21 | 	'boolean' => FALSE, 
22 | 	'integer' => 1981,
23 | 	'float' => 25.651,
24 | 	'string' => 'string cheese',
25 | );
26 | 
27 | // test data 
28 | class TestClass {
29 | 	public $null = NULL;
30 | 	public $boolean = FALSE;
31 | 	public $float= 67.993;
32 | 	public $integer = 1981;
33 | 	public $string = 'string cheese';
34 | 	protected $protected = 'protected';
35 | 	private $private = 'private';
36 | 	public static $static = 'static property'; // static methods are not shown
37 | 	public $assoc_array = array('key' => 'value', 'space key' => 'space value');
38 | 	public $indexed_array = array('one', 'two', 'three');
39 | }
40 | 
41 | // test instance
42 | $object = new TestClass;
43 | 
44 | // Example #1: using function shortcut (without a label)
45 | echo Pre($object);
46 | 
47 | // Example #2: using queue and configuring dimensions
48 | // configure height/width
49 | Pre::$config['width'] = 600;
50 | Pre::$config['height'] = 200;
51 | 
52 | // add to data queue with a label
53 | Pre::add($array, 'My Debug Data');
54 | 
55 | // later in the script...
56 | 
57 | // add to data queue without a label and output
58 | echo Pre::render($object);
59 | 
60 | 
61 | 
62 | 


--------------------------------------------------------------------------------
/examples/label_example.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aoxborrow/Pre.php/bf47c87c8fd8f1f75d6b2a3235ee072bb45a5842/examples/label_example.png


--------------------------------------------------------------------------------
/examples/pre_comparison.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/aoxborrow/Pre.php/bf47c87c8fd8f1f75d6b2a3235ee072bb45a5842/examples/pre_comparison.png


--------------------------------------------------------------------------------
/src/Paste/Pre.php:
--------------------------------------------------------------------------------
  1 |  tag and fixes whitespace and formatting.
  5 |  * Uses magic toString so that data can be both added and rendered with the same method call.
  6 |  *
  7 |  * @author     Aaron Oxborrow 
  8 |  * @link       http://github.com/paste/Pre
  9 |  * @copyright  (c) 2013 Aaron Oxborrow
 10 |  * @license    http://www.opensource.org/licenses/mit-license.php MIT License
 11 |  */
 12 | namespace Paste;
 13 | 
 14 | class Pre {
 15 | 	
 16 | 	// data to be shown next output
 17 | 	public $data = array();
 18 | 	
 19 | 	// pre default styling
 20 | 	public $style = 'font-family: Menlo, monospace; color: #000; font-size: 11px !important; line-height: 17px !important; text-align: left;';
 21 | 	
 22 | 	// default config for output
 23 | 	public static $config = array(
 24 | 		'width' => 'auto',
 25 | 		'height' => 'auto',
 26 | 	);
 27 | 
 28 | 	// singleton instance
 29 | 	public static $instance;
 30 | 	
 31 | 	// add data to be shown next time Pre is rendered
 32 | 	public static function data($data = NULL, $label = NULL) {
 33 | 		
 34 | 		// get Pre instance
 35 | 		$pre = self::instance();
 36 | 	
 37 | 		// $data will be cleared after it is output
 38 | 		if  (func_num_args())
 39 | 			$pre->data[] = array('data' => $data, 'label' => $label);
 40 | 
 41 | 		// return instance for method chaining or __toString
 42 | 		return $pre;
 43 | 	
 44 | 	}
 45 | 
 46 | 	// shortcut -- Pre::add() -- same as Pre::data()
 47 | 	public static function add($data = NULL, $label = NULL) {
 48 | 		return (func_num_args()) ? self::data($data, $label) : self::instance();
 49 | 	}
 50 | 	
 51 | 	// shortcut to Pre::render()
 52 | 	public static function r($data = NULL, $label = NULL) {
 53 | 		return (func_num_args()) ? self::data($data, $label) : self::instance();
 54 | 	}
 55 | 
 56 | 	// render all Pre data to a string and return
 57 | 	public static function render($data = NULL, $label = NULL) {
 58 | 
 59 | 		// add data if passed
 60 | 		$pre = (func_num_args()) ? self::data($data, $label) : self::instance();
 61 | 		
 62 | 		// return rendered string
 63 | 		return (string) $pre;
 64 | 	}
 65 | 
 66 | 	// simple wrapper for var_dump that outputs within a styled 
 tag and fixes whitespace and formatting
 67 | 	public function __toString() {
 68 | 		
 69 | 		// extract config to this context for convenience
 70 | 		extract(self::$config);
 71 | 		
 72 | 		// you can specify dimensions for a scrollble div
 73 | 		if ($height !== 'auto' OR $width !== 'auto') {
 74 | 
 75 | 			// add "px" to height and width
 76 | 			if (is_numeric($height) AND ! strstr($height, '%')) $height .= 'px';
 77 | 			if (is_numeric($width) AND ! strstr($width, '%')) $width .= 'px';
 78 | 			
 79 | 			// all scrollables get a border
 80 | 			$this->style .= " border: 1px solid #ddd; padding: 10px; overflow-y: scroll; height: $height; width: $width;";
 81 | 			
 82 | 		}
 83 | 
 84 | 		// styled pre tag
 85 | 		$pre = '
';
 86 | 
 87 | 		// iterate over data objects and var_dump 'em
 88 | 		foreach ($this->data as $data) {
 89 | 			
 90 | 			// pull out data and label
 91 | 			extract($data);
 92 | 			
 93 | 			// capture var_dump
 94 | 			ob_start();
 95 | 			var_dump($data);
 96 | 			$data = ob_get_clean();
 97 | 
 98 | 			// special case for NULL values
 99 | 			if (trim($data) == 'NULL')
100 | 				$data = 'NULL'."\n";
101 | 			
102 | 			// add label
103 | 			if (! empty($label))
104 | 				$data = ''.$label.": $data";
105 | 			
106 | 			// compile list of class names by searching: object(PreObject)#4 (2) {
107 | 			preg_match_all('/object\(([A-Za-z0-9_]+)\)\#[0-9]+\ \([0-9]+\)\ {/', $data, $objects);
108 | 			
109 | 			// we have some objects w/ class names
110 | 			if (! empty($objects)) {
111 | 			
112 | 				// just get the list of object names, without duplicates
113 | 				$objects = array_unique($objects[1]);
114 | 
115 | 				// fix all class names to look like this: stdClass#3 object(4) {
116 | 				$data = preg_replace('/object\(([A-Za-z0-9_]+)\)\#([0-9]+)\ \(([0-9]+)\)\ {/', '\\1 #\\2 object(\\3) {', $data);
117 | 
118 | 				// remove class name from private members
119 | 				foreach ($objects as $object)
120 | 					$data = str_replace(':"'.$object.'"', '', $data);
121 | 				
122 | 			}
123 | 
124 | 			// consistent styling of =>'s
125 | 			$arrow = ' => ';
126 | 			
127 | 			// style special case  NULL
128 | 			$data = preg_replace('/=>\s*NULL/i', '=> NULL', $data);
129 | 			
130 | 			// array keys are bolder
131 | 			$data = preg_replace('/\[\"([a-z0-9_\ \@+\-\(\):]+)\"(:?[a-z]*)\]\s*=>\s*/i', '[\\1]\\2'.$arrow, $data);
132 | 
133 | 			// numeric keys are bolder
134 | 			$data = preg_replace('/\[([0-9]+)\]\s*=>\s*/', '[\\1]'.$arrow, $data);
135 | 			
136 | 			// style :private and :protected
137 | 			$data = preg_replace('/(:(private|protected))/', '\\1', $data);
138 | 
139 | 			// de-emphasize string labels
140 | 			$data = preg_replace('/string\(([0-9]+)\)/', 'string(\\1)', $data);
141 | 
142 | 			// de-emphasize int labels
143 | 			$data = preg_replace('/int\(([0-9-]+)\)/', 'int(\\1)', $data);
144 | 			
145 | 			// de-emphasize float labels
146 | 			$data = preg_replace('/float\(([0-9\.-]+)\)/', 'float(\\1)', $data);
147 | 
148 | 			// de-emphasize bool label
149 | 			$data = preg_replace('/bool\(([A-Za-z]+)\)/', 'bool(\\1)', $data);
150 | 			// de-emphasize array labels
151 | 			$data = preg_replace('/array\(([0-9]+)\)/', 'array(\\1)', $data);
152 | 
153 | 			// boost spacing
154 | 			$data = preg_replace_callback('/^(\s*)(\S.*)$/m', function($matches) {
155 | 				// number of spaces that var_dump gave it
156 | 				$indent = strlen($matches[1]);
157 | 				// each 2 spaces is one column
158 | 				$indent = ($indent > 0) ? str_repeat("    ", $indent/2) : "";
159 |             	return $indent.$matches[2];
160 |         	}, $data);
161 | 
162 | 			// style opening brackets
163 | 			$data = preg_replace('/^(.*)(<\/span>\ \{)$/m', '\\1\\2', $data);
164 | 
165 | 			// style closing brackets
166 | 			$data = preg_replace('/^(\s*)(\})$/m', '\\1\\2', $data);
167 | 			
168 | 			// add to pre output
169 | 			$pre .= "$data";
170 | 
171 | 		}
172 | 		
173 | 		// close pre tag
174 | 		$pre .= "
\n\n"; 175 | 176 | // reset data 177 | $this->data = array(); 178 | 179 | // return output 180 | return $pre; 181 | 182 | } 183 | 184 | // uses singleton pattern to take advantage of __toString magic 185 | public static function instance() { 186 | 187 | // create a new instance 188 | if (! isset(self::$instance)) 189 | self::$instance = new Pre; 190 | 191 | // return instance 192 | return self::$instance; 193 | 194 | } 195 | 196 | } 197 | 198 | --------------------------------------------------------------------------------