CakePHP has an amazing ability to allow you to customise your application. One such way in which this is done, is through the use of a bootstrap file, that allows you to create functions, or include files application-wide.
In this post, I’ll share with you some of the functions that I include in almost every CakePHP application I develop.
Debugging #1
In any language, debugging is an absolute must. I’ve often had it where I was debugging the value of a variable, when I’m called away from my desk. When I get back, I can see the output of the debugging, but I can’t remember where it is coming from.
This brings me to my first two functions I always include in every project. These two functions perform a var_dump()
on all the variables supplied as arguments to the function, and also lets you know which line of which file the debugging is taking place:
/**
* Performs a var_dump() on all the arguments passed to the function.
* Also performs a debug_backtrace(), so that you can see from which file (and on which
* line) the variables are being dumped.
* @param mixed $var1, $var2, ...
* @return void
*/
function v()
{
$debug = debug_backtrace();
$debug = $debug[0];
if(!headers_sent()) {
header('Content-type: text/html');
}
if(defined('CAKEPHP_SHELL')) {
$templateStart = "===============================================================================n";
$templateStart .= "%file% on line %line%:n";
$templateStart .= "===============================================================================n";
$templateEnd = '';
} else {
$templateStart = '
<hr style="height: 0pt;border-right: 0pt none;margin: 10px 0pt" />' . "n";
$templateStart .= '<strong>%file% (Line %line%):</strong>
' . "n";
$templateStart .= '
<div style="text-align: left;font-size: 12px">';
$templateEnd = '</div>
';
}
echo str_replace(array('%line%', '%file%'), array($debug['line'], $debug['file']), $templateStart);
$args = func_get_args();
foreach($args as $a) {
var_dump($a);
}
echo $templateEnd;
}
Debugging #2
This next function relies on the presence of the previously-mentioned v()
function. It calls v()
on all the supplied arguments, and stops the page from loading afterwards.
/**
* Uses the v() variable dumping function on all the supplied arguments,
* and kills the script afterwards.
* @param mixed $var1, $var2, ...
* @return void
*/
function ve()
{
$args = func_get_args();
call_user_func_array('v', $args);
exit();
}
Debugging #3
The last bootstrap function that I always include, is another wrapper - albeit one for CakePHP’s pr()
function.
This function accepts multiple arguments, and runs Cake’s pr()
function on each one, after which it kills the script.
function pre() {
$args = func_get_args();
foreach($args as $arg) {
pr($arg);
}
exit();
}
Text sanitization
The next method is one I use every now and then. It searches through the supplied string, and strips out the common formatting oddities that are inserted when a user copies and pastes text from one of the Microsoft products (and sometimes OpenOffice - depending on the document formatting). If an array is supplied, then the array is searched for these characters recursively:
/**
* Strips common funky characters from text. Is applied recursively on arrays,
* and is only applied to strings. NULL/boolean/etc values are not processed.
* @param string $text The text to be parsed
* @return string The formatted text
*/
function sfc($text) {
if(is_array($text)) {
foreach($text as $key => $val) {
$text[$key] = sfc($val, true);
}
} elseif(is_string($text)) {
return str_replace(
array('‘', '’', '”', '“', '–', '…'),
array("'", "'", '"', '"', '-', '...'),
$text
);
}
return $text;
}
Array manipulation
CakePHP provides a nice convenience function for merging arrays - am()
. However, this only merges the first level of
arrays. What happens if you want to use this same functionality, but recursively? Well, that’s what this next function
does - it functions in the exact same way as CakePHP’s am()
function, except that it merges the arrays recursively:
/**
* Merge a group of arrays recursively.
* @param array $array1, $array2, ...
* @return array All array parameters merged into one
*/
function amr() {
$r = array();
$args = func_get_args();
foreach ($args as $a) {
if (!is_array($a)) {
$a = array($a);
}
$r = array_merge_recursive($r, $a);
}
return $r;
}