Completed
Push — master ( 77abb4...5b1cd3 )
by Chris
02:37
created

RoutingService   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

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

1 Method

Rating   Name   Duplication   Size   Complexity  
B register() 0 31 3
1
<?php
2
namespace Darya\Foundation\Providers;
3
4
use Darya\Routing\Router;
5
use Darya\Service\Contracts\Container;
6
use Darya\Service\Contracts\Provider;
7
8
/**
9
 * A service provider that provides a configured router.
10
 * 
11
 * @author Chris Andrew <[email protected]>
12
 */
13
class RoutingService implements Provider
14
{
15
	/**
16
	 * Register a router with the service container.
17
	 * 
18
	 * @param Container $container
19
	 */
20
	public function register(Container $container)
21
	{
22
		$container->register(array(
23
			'Darya\Routing\Router' => function ($container) {
24
				$config = $container->config;
25
				
26
				$routes = $config['routes'] ?: array(
27
					'/:controller/:action/:params' => null,
28
					'/:controller/:params' => null,
29
					'/:action/:params' => null,
30
					'/' => null
31
				);
32
				
33
				$projectNamespace = $config['project.namespace'] ?: 'Application';
34
				
35
				$defaultNamespace = "{$projectNamespace}\Controllers";
36
				
37
				$router = new Router($routes, array(
38
					'namespace' => $defaultNamespace
39
				));
40
				
41
				$router->base($config['base_url']);
42
				
43
				$router->setServiceContainer($container);
44
				
45
				$router->setEventDispatcher($container->resolve('Darya\Events\Dispatchable'));
46
				
47
				return $router;
48
			}
49
		));
50
	}
51
}
52