| 1 |
<?php |
| 2 |
|
| 3 |
namespace GeminiLabs\BlackBar; |
| 4 |
|
| 5 |
use GeminiLabs\BlackBar\Controller; |
| 6 |
use GeminiLabs\BlackBar\Console; |
| 7 |
use GeminiLabs\BlackBar\Profiler; |
| 8 |
|
| 9 |
final class Application |
| 10 |
{ |
| 11 |
const CONSOLE_HOOK = 'console'; |
| 12 |
const ID = 'blackbar'; |
| 13 |
const LANG = '/languages/'; |
| 14 |
const PROFILER_HOOK = 'profile'; |
| 15 |
|
| 16 |
public $console; |
| 17 |
public $errors = array(); |
| 18 |
public $file; |
| 19 |
public $profiler; |
| 20 |
|
| 21 |
public function __construct() |
| 22 |
{ |
| 23 |
$this->console = new Console; |
| 24 |
$this->file = realpath( dirname( __DIR__ ).'/'.static::ID.'.php' ); |
| 25 |
$this->profiler = new Profiler; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* @param int $errno |
| 30 |
* @param string $errstr |
| 31 |
* @param string $errfile |
| 32 |
* @param int $errline |
| 33 |
* @return void |
| 34 |
*/ |
| 35 |
public function errorHandler( $errno, $errstr, $errfile, $errline ) |
| 36 |
{ |
| 37 |
$errorCodes = array( |
| 38 |
2 => 'Warning', //E_WARNING |
| 39 |
8 => 'Notice', //E_NOTICE |
| 40 |
2048 => 'Strict', //E_STRICT |
| 41 |
8192 => 'Deprecated', //E_DEPRECATED |
| 42 |
); |
| 43 |
$errname = array_key_exists( $errno, $errorCodes ) |
| 44 |
? $errorCodes[$errno] |
| 45 |
: 'Unknown'; |
| 46 |
$hash = md5( $errno.$errstr.$errfile.$errline ); |
| 47 |
if( array_key_exists( $hash, $this->errors )) { |
| 48 |
$this->errors[$hash]['count']++; |
| 49 |
} |
| 50 |
else { |
| 51 |
$this->errors[$hash] = array( |
| 52 |
"errno" => $errno, |
| 53 |
"message" => $errstr, |
| 54 |
"file" => $errfile, |
| 55 |
"name" => $errname, |
| 56 |
"line" => $errline, |
| 57 |
"count" => 0, |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
/** |
| 63 |
* @return void |
| 64 |
*/ |
| 65 |
public function init() |
| 66 |
{ |
| 67 |
$controller = new Controller( $this ); |
| 68 |
add_filter( 'all', array( $controller, 'initConsole' )); |
| 69 |
add_filter( 'all', array( $controller, 'initProfiler' )); |
| 70 |
add_action( 'plugins_loaded', array( $controller, 'registerLanguages' )); |
| 71 |
add_action( 'init', function() use( $controller ) { |
| 72 |
if( !apply_filters( 'blackbar/enabled', true ))return; |
| 73 |
add_action( 'admin_enqueue_scripts', array( $controller, 'enqueueAssets' )); |
| 74 |
add_action( 'wp_enqueue_scripts', array( $controller, 'enqueueAssets' )); |
| 75 |
add_action( 'admin_footer', array( $controller, 'renderBar' )); |
| 76 |
add_action( 'wp_footer', array( $controller, 'renderBar' )); |
| 77 |
add_filter( 'admin_body_class', array( $controller, 'filterBodyClasses' )); |
| 78 |
}); |
| 79 |
apply_filters( 'debug', 'Profiler Started' ); |
| 80 |
apply_filters( 'debug', 'blackbar/profiler/noise' ); |
| 81 |
set_error_handler( array( $this, 'errorHandler' ), E_ALL|E_STRICT ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* @param string $file |
| 86 |
* @return string |
| 87 |
*/ |
| 88 |
public function path( $file = '' ) |
| 89 |
{ |
| 90 |
return plugin_dir_path( $this->file ).ltrim( trim( $file ), '/' ); |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* @param string $view |
| 95 |
* @return void |
| 96 |
*/ |
| 97 |
public function render( $view, array $data = array() ) |
| 98 |
{ |
| 99 |
$file = $this->path( sprintf( 'views/%s.php', str_replace( '.php', '', $view ))); |
| 100 |
if( !file_exists( $file ))return; |
| 101 |
extract( $data ); |
| 102 |
include $file; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* @param string $path |
| 107 |
* @return string |
| 108 |
*/ |
| 109 |
public function url( $path = '' ) |
| 110 |
{ |
| 111 |
return esc_url( plugin_dir_url( $this->file ).ltrim( trim( $path ), '/' )); |
| 112 |
} |
| 113 |
} |
| 114 |
|