valdeir2000

StackOverflow 266968 - Basics.php

Jan 4th, 2018
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.74 KB | None | 0 0
  1. <?php
  2. /**
  3.  * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4.  * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5.  *
  6.  * Licensed under The MIT License
  7.  * For full copyright and license information, please see the LICENSE.txt
  8.  * Redistributions of files must retain the above copyright notice.
  9.  *
  10.  * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11.  * @link          https://cakephp.org CakePHP(tm) Project
  12.  * @since         0.2.9
  13.  * @license       https://opensource.org/licenses/mit-license.php MIT License
  14.  */
  15. use Cake\Error\Debugger;
  16.  
  17.     /**
  18.      * Basic defines for timing functions.
  19.      */
  20.     define('SECOND', 1);
  21.     define('MINUTE', 60);
  22.     define('HOUR', 3600);
  23.     define('DAY', 86400);
  24.     define('WEEK', 604800);
  25.     define('MONTH', 2592000);
  26.     define('YEAR', 31536000);
  27.  
  28. if (!function_exists('debug')) {
  29.     /**
  30.      * Prints out debug information about given variable and returns the
  31.      * variable that was passed.
  32.      *
  33.      * Only runs if debug mode is enabled.
  34.      *
  35.      * @param mixed $var Variable to show debug information for.
  36.      * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  37.      * @param bool $showFrom If set to true, the method prints from where the function was called.
  38.      * @return mixed The same $var that was passed
  39.      * @link https://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
  40.      * @link https://book.cakephp.org/3.0/en/core-libraries/global-constants-and-functions.html#debug
  41.      */
  42.     function debug($var, $showHtml = true, $showFrom = true)
  43.     {
  44.         $location = [];
  45.         if ($showFrom) {
  46.             $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
  47.             $location = [
  48.                 'line' => $trace[0]['line'],
  49.                 'file' => $trace[0]['file']
  50.             ];
  51.         }
  52.  
  53.         Debugger::printVar($var, $location, $showHtml);
  54.  
  55.         return $var;
  56.     }
  57.  
  58. }
  59.  
  60. if (!function_exists('stackTrace')) {
  61.     /**
  62.      * Outputs a stack trace based on the supplied options.
  63.      *
  64.      * ### Options
  65.      *
  66.      * - `depth` - The number of stack frames to return. Defaults to 999
  67.      * - `args` - Should arguments for functions be shown? If true, the arguments for each method call
  68.      *   will be displayed.
  69.      * - `start` - The stack frame to start generating a trace from. Defaults to 1
  70.      *
  71.      * @param array $options Format for outputting stack trace
  72.      * @return mixed Formatted stack trace
  73.      */
  74.     function stackTrace(array $options = [])
  75.     {
  76.         $options += ['start' => 0];
  77.         $options['start']++;
  78.         echo Debugger::trace($options);
  79.     }
  80.  
  81. }
  82.  
  83. if (!function_exists('breakpoint')) {
  84.     /**
  85.      * Command to return the eval-able code to startup PsySH in interactive debugger
  86.      * Works the same way as eval(\Psy\sh());
  87.      * psy/psysh must be loaded in your project
  88.      * @link http://psysh.org/
  89.      * ```
  90.      * eval(breakpoint());
  91.      * ```
  92.      * @return string
  93.      */
  94.     function breakpoint()
  95.     {
  96.         if ((PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') && class_exists('\Psy\Shell')) {
  97.             return 'extract(\Psy\Shell::debug(get_defined_vars(), isset($this) ? $this : null));';
  98.         }
  99.         trigger_error(
  100.             'psy/psysh must be installed and you must be in a CLI environment to use the breakpoint function',
  101.             E_USER_WARNING
  102.         );
  103.     }
  104. }
  105.  
  106. if (!function_exists('dd')) {
  107.     /**
  108.      * Prints out debug information about given variable and dies.
  109.      *
  110.      * Only runs if debug mode is enabled.
  111.      * It will otherwise just continue code execution and ignore this function.
  112.      *
  113.      * @param mixed $var Variable to show debug information for.
  114.      * @param bool|null $showHtml If set to true, the method prints the debug data in a browser-friendly way.
  115.      * @return void
  116.      * @link https://book.cakephp.org/3.0/en/development/debugging.html#basic-debugging
  117.      */
  118.     function dd($var, $showHtml = null)
  119.     {
  120.         $trace = Debugger::trace(['start' => 1, 'depth' => 2, 'format' => 'array']);
  121.         $location = [
  122.             'line' => $trace[0]['line'],
  123.             'file' => $trace[0]['file']
  124.         ];
  125.  
  126.         Debugger::printVar($var, $location, $showHtml);
  127.         die(1);
  128.     }
  129. }
  130.  
  131. if (!function_exists('loadPHPUnitAliases')) {
  132.     /**
  133.      * Loads PHPUnit aliases
  134.      *
  135.      * This is an internal function used for backwards compatibility during
  136.      * fixture related tests.
  137.      *
  138.      * @return void
  139.      */
  140.     function loadPHPUnitAliases()
  141.     {
  142.         require_once dirname(__DIR__) . DS . 'tests' . DS . 'phpunit_aliases.php';
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment