Completed
Branch develop (c2aa4c)
by Anton
05:17
created

DatabaseManager::getDrivers()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 9
rs 9.6667
cc 2
eloc 5
nc 2
nop 0
1
<?php
2
/**
3
 * Spiral Framework.
4
 *
5
 * @license   MIT
6
 * @author    Anton Titov (Wolfy-J)
7
 */
8
namespace Spiral\Database;
9
10
use Spiral\Core\Component;
11
use Spiral\Core\FactoryInterface;
12
use Spiral\Core\Container\InjectorInterface;
13
use Spiral\Database\Configs\DatabasesConfig;
14
use Spiral\Database\Entities\Database;
15
use Spiral\Database\Entities\Driver;
16
use Spiral\Database\Exceptions\DatabaseException;
17
18
/**
19
 * DatabaseManager responsible for database creation, configuration storage and drivers factory.
20
 */
21
class DatabaseManager extends Component implements InjectorInterface, DatabasesInterface
22
{
23
    /**
24
     * Declares to Spiral IoC that component instance should be treated as singleton.
25
     */
26
    const SINGLETON = self::class;
27
28
    /**
29
     * By default spiral will force time conversion into single timezone before storing in
30
     * database, it will help us to ensure that we have no problems with switching timezones and
31
     * save a lot of time while development (potentially).
32
     */
33
    const DEFAULT_TIMEZONE = 'UTC';
34
35
    /**
36
     * @var Database[]
37
     */
38
    private $databases = [];
39
40
    /**
41
     * @var Driver[]
42
     */
43
    private $drivers = [];
44
45
    /**
46
     * @var DatabasesConfig
47
     */
48
    protected $config = null;
49
50
    /**
51
     * @invisible
52
     * @var FactoryInterface
53
     */
54
    protected $factory = null;
55
56
    /**
57
     * @param DatabasesConfig  $config
58
     * @param FactoryInterface $factory
59
     */
60
    public function __construct(DatabasesConfig $config, FactoryInterface $factory)
61
    {
62
        $this->config = $config;
63
        $this->factory = $factory;
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     *
69
     * @return Database
70
     */
71 View Code Duplication
    public function database($database = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
72
    {
73
        if (empty($database)) {
74
            $database = $this->config->defaultDatabase();
75
        }
76
77
        //Spiral support ability to link multiple virtual databases together using aliases
78
        $database = $this->config->resolveAlias($database);
79
80
        if (isset($this->databases[$database])) {
81
            return $this->databases[$database];
82
        }
83
84
        if (!$this->config->hasDatabase($database)) {
85
            throw new DatabaseException(
86
                "Unable to create database, no presets for '{$database}' found."
87
            );
88
        }
89
90
        //No need to benchmark here, due connection will happen later
91
        $this->databases[$database] = $this->factory->make(Database::class, [
92
            'name'   => $database,
93
            'driver' => $this->driver($this->config->databaseConnection($database)),
94
            'prefix' => $this->config->databasePrefix($database)
95
        ]);
96
97
        return $this->databases[$database];
98
    }
99
100
    /**
101
     * Get driver by it's name. Every driver associated with configured connection, there is minor
102
     * de-sync in naming due legacy code.
103
     *
104
     * @param string $connection
105
     * @return Driver
106
     * @throws DatabaseException
107
     */
108
    public function driver($connection)
109
    {
110
        if (isset($this->drivers[$connection])) {
111
            return $this->drivers[$connection];
112
        }
113
114
        if (!$this->config->hasConnection($connection)) {
115
            throw new DatabaseException(
116
                "Unable to create Driver, no presets for '{$connection}' found."
117
            );
118
        }
119
120
        $this->drivers[$connection] = $this->factory->make(
121
            $this->config->connectionDriver($connection),
122
            [
123
                'name'   => $connection,
124
                'config' => $this->config->connectionConfig($connection)
125
            ]
126
        );
127
128
        return $this->drivers[$connection];
129
    }
130
131
    /**
132
     * Get instance of every available database.
133
     *
134
     * @return Database[]
135
     * @throws DatabaseException
136
     */
137
    public function getDatabases()
138
    {
139
        $result = [];
140
        foreach ($this->config->databaseNames() as $name) {
141
            $result[] = $this->database($name);
142
        }
143
144
        return $result;
145
    }
146
147
    /**
148
     * Get instance of every available driver/connection.
149
     *
150
     * @return Driver[]
151
     * @throws DatabaseException
152
     */
153
    public function getDrivers()
154
    {
155
        $result = [];
156
        foreach ($this->config->connectionNames() as $name) {
157
            $result[] = $this->driver($name);
158
        }
159
160
        return $result;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function createInjection(\ReflectionClass $class, $context = null)
167
    {
168
        //If context is empty default database will be returned
169
        return $this->database($context);
170
    }
171
}