Completed
Push — master ( 8e8d93...e4f639 )
by Anton
07:07 queued 02:42
created

SpiralBindings::activeSession()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 14
Ratio 100 %
Metric Value
dl 14
loc 14
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Core\Bootloaders;
9
10
use Cocur\Slugify\Slugify;
11
use Cocur\Slugify\SlugifyInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Spiral\Core\Exceptions\Container\AutowireException;
14
use Spiral\Core\Exceptions\SugarException;
15
use Spiral\Http\Routing\RouteInterface;
16
17
/**
18
 * Shared components and short bindings.
19
 */
20
class SpiralBindings extends Bootloader
21
{
22
    /**
23
     * No need to boot, all cached.
24
     */
25
    const BOOT = false;
26
27
    /**
28
     * @var array
29
     */
30
    protected $bindings = [
31
        //Core components (see SharedTrait)
32
        'memory'                             => 'Spiral\Core\HippocampusInterface',
33
        'modules'                            => 'Spiral\Modules\ModuleManager',
34
        'debugger'                           => 'Spiral\Debug\Debugger',
35
36
        //Container
37
        'container'                          => 'Spiral\Core\ContainerInterface',
38
39
        //Dispatchers
40
        'http'                               => 'Spiral\Http\HttpDispatcher',
41
        'console'                            => 'Spiral\Console\ConsoleDispatcher',
42
43
        //Shared components
44
        'files'                              => 'Spiral\Files\FilesInterface',
45
        'tokenizer'                          => 'Spiral\Tokenizer\TokenizerInterface',
46
        'locator'                            => 'Spiral\Tokenizer\ClassLocatorInterface',
47
        'invocationLocator'                  => 'Spiral\Tokenizer\InvocationLocatorInterface',
48
        'storage'                            => 'Spiral\Storage\StorageInterface',
49
50
        //Concrete for now
51
        'views'                              => 'Spiral\Views\ViewManager',
52
        'translator'                         => 'Spiral\Translator\Translator',
53
54
        //Databases and models
55
        'dbal'                               => 'Spiral\Database\DatabaseManager',
56
        'orm'                                => 'Spiral\ORM\ORM',
57
        'odm'                                => 'Spiral\ODM\ODM',
58
59
        //Entities
60
        'encrypter'                          => 'Spiral\Encrypter\EncrypterInterface',
61
        'cache'                              => 'Spiral\Cache\StoreInterface',
62
63
        //Concrete for now, replace with better interface in future
64
        'db'                                 => 'Spiral\Database\Entities\Database',
65
        'mongo'                              => 'Spiral\ODM\Entities\MongoDatabase',
66
67
        //Http scope dependent
68
        'cookies'                            => 'Spiral\Http\Cookies\CookieQueue',
69
        'router'                             => 'Spiral\Http\Routing\RouterInterface',
70
        'request'                            => 'Psr\Http\Message\ServerRequestInterface',
71
72
        //Http scope depended data routes and wrappers
73
        'input'                              => 'Spiral\Http\Input\InputManager',
74
        'response'                           => 'Spiral\Http\Responses\Responder',
75
        'responses'                          => 'Spiral\Http\Responses\Responder',
76
        'responder'                          => 'Spiral\Http\Responses\Responder',
77
78
        //Thought request attributes
79
        'Spiral\Http\Routing\RouteInterface' => [self::class, 'activeRoute'],
80
81
        //Short aliases
82
        'route'                              => 'Spiral\Http\Router\RouteInterface',
83
        'session'                            => 'Spiral\Session\SessionInterface'
84
    ];
85
86
    /**
87
     * @var array
88
     */
89
    protected $singletons = [
90
        SlugifyInterface::class => [self::class, 'slugify']
91
    ];
92
93
    /**
94
     * @param ServerRequestInterface $request
95
     * @return RouteInterface
96
     */
97
    public function activeRoute(ServerRequestInterface $request = null)
98
    {
99
        if (empty($request)) {
100
            throw new AutowireException("No active request found");
101
        }
102
103
        $route = $request->getAttribute('route');
104
105
        if (!$route instanceof RouteInterface) {
106
            throw new SugarException("Unable to resolve active route using active request");
107
        }
108
109
        return $route;
110
    }
111
112
    /**
113
     * @return Slugify
114
     */
115
    public function slugify()
116
    {
117
        return new Slugify();
118
    }
119
}
120