Completed
Push — master ( 036b27...9e474e )
by Chris
02:43
created

DebugService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 2
Bugs 0 Features 1
Metric Value
wmc 7
c 2
b 0
f 1
lcom 0
cbo 3
dl 0
loc 47
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register() 0 14 4
A boot() 0 16 3
1
<?php
2
namespace Darya\Foundation\Providers;
3
4
use Chrome;
5
use Darya\Events\Dispatcher;
6
use Darya\Foundation\Configuration;
7
use Darya\Service\Contracts\Container;
8
use Darya\Service\Contracts\Provider;
9
10
/**
11
 * A service provider that provides application debugging services, if debugging
12
 * is configured.
13
 * 
14
 * @author Chris Andrew <[email protected]>
15
 */
16
class DebugService implements Provider
17
{
18
	/**
19
	 * Alias ChromePhp to Chrome and, if debugging is configured, enable
20
	 * display_errors.
21
	 * 
22
	 * @param Container $container
23
	 */
24
	public function register(Container $container)
25
	{
26
		if (class_exists('ChromePhp') && !class_exists('Chrome')) {
27
			class_alias('ChromePhp', 'Chrome');
28
		}
29
		
30
		$configuration = $container->resolve('Darya\Foundation\Configuration');
31
		
32
		if (!$configuration->get('debug')) {
33
			return;
34
		}
35
		
36
		ini_set('display_errors', 1);
37
	}
38
	
39
	/**
40
	 * When the application boots, if debugging is enabled, attach an event
41
	 * listener to database queries that outputs to ChromePhp.
42
	 * 
43
	 * @param Configuration $configuration
44
	 * @param Dispatcher    $events
45
	 */
46
	public function boot(Configuration $configuration, Dispatcher $events) {
47
		if (!$configuration->get('debug')) {
48
			return;
49
		}
50
		
51
		$listener = function ($result) {
52
			Chrome::log(array($result->query->string, json_encode($result->query->parameters)));
53
			
54
			if ($result->error) {
55
				Chrome::error(array($result->error->number, $result->error->message));
56
			}
57
		};
58
		
59
		$events->listen('mysql.query', $listener);
60
		$events->listen('mssql.query', $listener);
61
	}
62
}
63