o2system /
database
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the O2System Framework package. |
||
| 4 | * |
||
| 5 | * For the full copyright and license information, please view the LICENSE |
||
| 6 | * file that was distributed with this source code. |
||
| 7 | * |
||
| 8 | * @author Steeve Andrian Salim |
||
| 9 | * @copyright Copyright (c) Steeve Andrian Salim |
||
| 10 | */ |
||
| 11 | |||
| 12 | // ------------------------------------------------------------------------ |
||
| 13 | |||
| 14 | namespace O2System\Database; |
||
| 15 | |||
| 16 | // ------------------------------------------------------------------------ |
||
| 17 | |||
| 18 | use O2System\Spl\Patterns\Structural\Provider\AbstractProvider; |
||
| 19 | use O2System\Spl\Patterns\Structural\Provider\ValidationInterface; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class Connections |
||
| 23 | * |
||
| 24 | * @package O2System\Database |
||
| 25 | */ |
||
| 26 | class Connections extends AbstractProvider implements ValidationInterface |
||
| 27 | {
|
||
| 28 | /** |
||
| 29 | * Connections::$config |
||
| 30 | * |
||
| 31 | * @var DataStructures\Config |
||
| 32 | */ |
||
| 33 | private $config; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Connections::__construct |
||
| 37 | * |
||
| 38 | * @param DataStructures\Config $config |
||
| 39 | * |
||
| 40 | * @return Connections |
||
| 41 | */ |
||
| 42 | public function __construct(DataStructures\Config $config) |
||
| 43 | {
|
||
| 44 | $this->config = $config; |
||
| 45 | } |
||
| 46 | |||
| 47 | // ------------------------------------------------------------------------ |
||
| 48 | |||
| 49 | public function &loadConnection($connectionOffset) |
||
| 50 | {
|
||
| 51 | $loadConnection[ $connectionOffset ] = false; |
||
| 52 | |||
| 53 | if ( ! $this->exists($connectionOffset) and $this->config->offsetExists($connectionOffset)) {
|
||
| 54 | |||
| 55 | $connectionConfig = $this->config->offsetGet($connectionOffset); |
||
| 56 | |||
| 57 | if (is_array($connectionConfig)) {
|
||
| 58 | new DataStructures\Config($this->config[ $connectionOffset ]); |
||
| 59 | } |
||
| 60 | |||
| 61 | $this->createConnection($connectionOffset, $connectionConfig); |
||
| 62 | |||
| 63 | return $this->getObject($connectionOffset); |
||
| 64 | |||
| 65 | } elseif ($this->exists($connectionOffset)) {
|
||
| 66 | return $this->getObject($connectionOffset); |
||
| 67 | } |
||
| 68 | |||
| 69 | return $loadConnection; |
||
| 70 | } |
||
| 71 | |||
| 72 | // ------------------------------------------------------------------------ |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Connections::createConnection |
||
| 76 | * |
||
| 77 | * Create Item Pool |
||
| 78 | * |
||
| 79 | * @param string $connectionOffset |
||
| 80 | * @param DataStructures\Config $connectionConfig |
||
| 81 | * |
||
| 82 | * @return bool|\O2System\Database\Sql\Abstracts\AbstractConnection|\O2System\Database\NoSql\Abstracts\AbstractConnection |
||
| 83 | */ |
||
| 84 | public function &createConnection($connectionOffset, DataStructures\Config $connectionConfig) |
||
| 85 | {
|
||
| 86 | $driverMaps = [ |
||
| 87 | 'mongodb' => '\O2System\Database\NoSql\Drivers\MongoDb\Connection', |
||
| 88 | 'mysql' => '\O2System\Database\Sql\Drivers\MySql\Connection', |
||
| 89 | 'sqlite' => '\O2System\Database\Sql\Drivers\Sqlite\Connection', |
||
| 90 | ]; |
||
| 91 | |||
| 92 | if (array_key_exists($connectionConfig->driver, $driverMaps)) {
|
||
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 93 | if (class_exists($driverClassName = $driverMaps[ $connectionConfig->driver ])) {
|
||
| 94 | $driverInstance = new $driverClassName($connectionConfig); |
||
| 95 | $this->register($driverInstance, $connectionOffset); |
||
| 96 | } |
||
| 97 | |||
| 98 | return $this->getObject($connectionOffset); |
||
| 99 | } |
||
| 100 | |||
| 101 | return false; |
||
| 102 | } |
||
| 103 | |||
| 104 | // ------------------------------------------------------------------------ |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Connections::validate |
||
| 108 | * |
||
| 109 | * Determine if value is meet requirement. |
||
| 110 | * |
||
| 111 | * @param mixed $value |
||
| 112 | * |
||
| 113 | * @return bool |
||
| 114 | */ |
||
| 115 | public function validate($value) |
||
| 116 | {
|
||
| 117 | if ($value instanceof \O2System\Database\Sql\Abstracts\AbstractConnection || $value instanceof \O2System\Database\NoSql\Abstracts\AbstractConnection) {
|
||
| 118 | return true; |
||
| 119 | } |
||
| 120 | |||
| 121 | return false; |
||
| 122 | } |
||
| 123 | } |