|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace IgniteKit\Backports\Database\Capsule; |
| 4 | + |
| 5 | +use PDO; |
| 6 | +use IgniteKit\Backports\Container\Container; |
| 7 | +use IgniteKit\Backports\Database\DatabaseManager; |
| 8 | +use IgniteKit\Backports\Contracts\Events\Dispatcher; |
| 9 | +use IgniteKit\Backports\Support\Traits\CapsuleManagerTrait; |
| 10 | +use IgniteKit\Backports\Database\Eloquent\Model as Eloquent; |
| 11 | +use IgniteKit\Backports\Database\Connectors\ConnectionFactory; |
| 12 | + |
| 13 | +class Manager |
| 14 | +{ |
| 15 | + use CapsuleManagerTrait; |
| 16 | + |
| 17 | + /** |
| 18 | + * The database manager instance. |
| 19 | + * |
| 20 | + * @var \IgniteKit\Backports\Database\DatabaseManager |
| 21 | + */ |
| 22 | + protected $manager; |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a new database capsule manager. |
| 26 | + * |
| 27 | + * @param \IgniteKit\Backports\Container\Container|null $container |
| 28 | + * @return void |
| 29 | + */ |
| 30 | + public function __construct(Container $container = null) |
| 31 | + { |
| 32 | + $this->setupContainer($container ?: new Container); |
| 33 | + |
| 34 | + // Once we have the container setup, we will setup the default configuration |
| 35 | + // options in the container "config" binding. This will make the database |
| 36 | + // manager work correctly out of the box without extreme configuration. |
| 37 | + $this->setupDefaultConfiguration(); |
| 38 | + |
| 39 | + $this->setupManager(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Setup the default database configuration options. |
| 44 | + * |
| 45 | + * @return void |
| 46 | + */ |
| 47 | + protected function setupDefaultConfiguration() |
| 48 | + { |
| 49 | + $this->container['config']['database.fetch'] = PDO::FETCH_OBJ; |
| 50 | + |
| 51 | + $this->container['config']['database.default'] = 'default'; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Build the database manager instance. |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + protected function setupManager() |
| 60 | + { |
| 61 | + $factory = new ConnectionFactory($this->container); |
| 62 | + |
| 63 | + $this->manager = new DatabaseManager($this->container, $factory); |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Get a connection instance from the global manager. |
| 68 | + * |
| 69 | + * @param string|null $connection |
| 70 | + * @return \IgniteKit\Backports\Database\Connection |
| 71 | + */ |
| 72 | + public static function connection($connection = null) |
| 73 | + { |
| 74 | + return static::$instance->getConnection($connection); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Get a fluent query builder instance. |
| 79 | + * |
| 80 | + * @param string $table |
| 81 | + * @param string|null $connection |
| 82 | + * @return \IgniteKit\Backports\Database\Query\Builder |
| 83 | + */ |
| 84 | + public static function table($table, $connection = null) |
| 85 | + { |
| 86 | + return static::$instance->connection($connection)->table($table); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Get a schema builder instance. |
| 91 | + * |
| 92 | + * @param string|null $connection |
| 93 | + * @return \IgniteKit\Backports\Database\Schema\Builder |
| 94 | + */ |
| 95 | + public static function schema($connection = null) |
| 96 | + { |
| 97 | + return static::$instance->connection($connection)->getSchemaBuilder(); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * Get a registered connection instance. |
| 102 | + * |
| 103 | + * @param string|null $name |
| 104 | + * @return \IgniteKit\Backports\Database\Connection |
| 105 | + */ |
| 106 | + public function getConnection($name = null) |
| 107 | + { |
| 108 | + return $this->manager->connection($name); |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Register a connection with the manager. |
| 113 | + * |
| 114 | + * @param array $config |
| 115 | + * @param string $name |
| 116 | + * @return void |
| 117 | + */ |
| 118 | + public function addConnection(array $config, $name = 'default') |
| 119 | + { |
| 120 | + $connections = $this->container['config']['database.connections']; |
| 121 | + |
| 122 | + $connections[$name] = $config; |
| 123 | + |
| 124 | + $this->container['config']['database.connections'] = $connections; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Bootstrap Eloquent so it is ready for usage. |
| 129 | + * |
| 130 | + * @return void |
| 131 | + */ |
| 132 | + public function bootEloquent() |
| 133 | + { |
| 134 | + Eloquent::setConnectionResolver($this->manager); |
| 135 | + |
| 136 | + // If we have an event dispatcher instance, we will go ahead and register it |
| 137 | + // with the Eloquent ORM, allowing for model callbacks while creating and |
| 138 | + // updating "model" instances; however, it is not necessary to operate. |
| 139 | + if ($dispatcher = $this->getEventDispatcher()) { |
| 140 | + Eloquent::setEventDispatcher($dispatcher); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * Set the fetch mode for the database connections. |
| 146 | + * |
| 147 | + * @param int $fetchMode |
| 148 | + * @return $this |
| 149 | + */ |
| 150 | + public function setFetchMode($fetchMode) |
| 151 | + { |
| 152 | + $this->container['config']['database.fetch'] = $fetchMode; |
| 153 | + |
| 154 | + return $this; |
| 155 | + } |
| 156 | + |
| 157 | + /** |
| 158 | + * Get the database manager instance. |
| 159 | + * |
| 160 | + * @return \IgniteKit\Backports\Database\DatabaseManager |
| 161 | + */ |
| 162 | + public function getDatabaseManager() |
| 163 | + { |
| 164 | + return $this->manager; |
| 165 | + } |
| 166 | + |
| 167 | + /** |
| 168 | + * Get the current event dispatcher instance. |
| 169 | + * |
| 170 | + * @return \IgniteKit\Backports\Contracts\Events\Dispatcher|null |
| 171 | + */ |
| 172 | + public function getEventDispatcher() |
| 173 | + { |
| 174 | + if ($this->container->bound('events')) { |
| 175 | + return $this->container['events']; |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + /** |
| 180 | + * Set the event dispatcher instance to be used by connections. |
| 181 | + * |
| 182 | + * @param \IgniteKit\Backports\Contracts\Events\Dispatcher $dispatcher |
| 183 | + * @return void |
| 184 | + */ |
| 185 | + public function setEventDispatcher(Dispatcher $dispatcher) |
| 186 | + { |
| 187 | + $this->container->instance('events', $dispatcher); |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Dynamically pass methods to the default connection. |
| 192 | + * |
| 193 | + * @param string $method |
| 194 | + * @param array $parameters |
| 195 | + * @return mixed |
| 196 | + */ |
| 197 | + public static function __callStatic($method, $parameters) |
| 198 | + { |
| 199 | + return static::connection()->$method(...$parameters); |
| 200 | + } |
| 201 | +} |
0 commit comments