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

DebugService::boot()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 16
rs 9.4285
cc 3
eloc 9
nc 2
nop 2
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