Completed
Push — master ( f4b826...4d0daa )
by Chris
04:52 queued 02:17
created

RoutingService::register()   B

Complexity

Conditions 3
Paths 1

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 8.8571
cc 3
eloc 17
nc 1
nop 1
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
    public function register(Container $container)
16
    {
17
        $container->register(array(
18
            'Darya\Routing\Router' => function ($container) {
19
            	$config = $container->config;
20
            	
21
                $routes = $config['routes'] ?: array(
22
                    '/:controller/:action/:params' => null,
23
                    '/:controller/:params' => null,
24
                    '/:action/:params' => null,
25
                    '/' => null
26
                );
27
                
28
                $projectNamespace = $config['project.namespace'] ?: 'Application';
29
                
30
                $defaultNamespace = "{$projectNamespace}\Controllers";
31
                
32
                $router = new Router($routes, array(
33
                    'namespace' => $defaultNamespace
34
                ));
35
                
36
                $router->base($config['base_url']);
37
                
38
                $router->setServiceContainer($container);
39
                
40
                $router->setEventDispatcher($container->resolve('Darya\Events\Dispatchable'));
41
                
42
                return $router;
43
            }
44
        ));
45
    }
46
}
47