PageRenderTime 61ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Database/DatabaseManager.php

https://gitlab.com/dzakiafif/cokelatklasik
PHP | 297 lines | 120 code | 42 blank | 135 comment | 10 complexity | 2486930d9837b864a4c32794e44f22bd MD5 | raw file
  1. <?php
  2. namespace Illuminate\Database;
  3. use Illuminate\Support\Str;
  4. use InvalidArgumentException;
  5. use Illuminate\Database\Connectors\ConnectionFactory;
  6. class DatabaseManager implements ConnectionResolverInterface
  7. {
  8. /**
  9. * The application instance.
  10. *
  11. * @var \Illuminate\Foundation\Application
  12. */
  13. protected $app;
  14. /**
  15. * The database connection factory instance.
  16. *
  17. * @var \Illuminate\Database\Connectors\ConnectionFactory
  18. */
  19. protected $factory;
  20. /**
  21. * The active connection instances.
  22. *
  23. * @var array
  24. */
  25. protected $connections = [];
  26. /**
  27. * The custom connection resolvers.
  28. *
  29. * @var array
  30. */
  31. protected $extensions = [];
  32. /**
  33. * Create a new database manager instance.
  34. *
  35. * @param \Illuminate\Foundation\Application $app
  36. * @param \Illuminate\Database\Connectors\ConnectionFactory $factory
  37. * @return void
  38. */
  39. public function __construct($app, ConnectionFactory $factory)
  40. {
  41. $this->app = $app;
  42. $this->factory = $factory;
  43. }
  44. /**
  45. * Get a database connection instance.
  46. *
  47. * @param string $name
  48. * @return \Illuminate\Database\Connection
  49. */
  50. public function connection($name = null)
  51. {
  52. list($name, $type) = $this->parseConnectionName($name);
  53. // If we haven't created this connection, we'll create it based on the config
  54. // provided in the application. Once we've created the connections we will
  55. // set the "fetch mode" for PDO which determines the query return types.
  56. if (!isset($this->connections[$name])) {
  57. $connection = $this->makeConnection($name);
  58. $this->setPdoForType($connection, $type);
  59. $this->connections[$name] = $this->prepare($connection);
  60. }
  61. return $this->connections[$name];
  62. }
  63. /**
  64. * Parse the connection into an array of the name and read / write type.
  65. *
  66. * @param string $name
  67. * @return array
  68. */
  69. protected function parseConnectionName($name)
  70. {
  71. $name = $name ?: $this->getDefaultConnection();
  72. return Str::endsWith($name, ['::read', '::write'])
  73. ? explode('::', $name, 2) : [$name, null];
  74. }
  75. /**
  76. * Disconnect from the given database and remove from local cache.
  77. *
  78. * @param string $name
  79. * @return void
  80. */
  81. public function purge($name = null)
  82. {
  83. $this->disconnect($name);
  84. unset($this->connections[$name]);
  85. }
  86. /**
  87. * Disconnect from the given database.
  88. *
  89. * @param string $name
  90. * @return void
  91. */
  92. public function disconnect($name = null)
  93. {
  94. if (isset($this->connections[$name = $name ?: $this->getDefaultConnection()])) {
  95. $this->connections[$name]->disconnect();
  96. }
  97. }
  98. /**
  99. * Reconnect to the given database.
  100. *
  101. * @param string $name
  102. * @return \Illuminate\Database\Connection
  103. */
  104. public function reconnect($name = null)
  105. {
  106. $this->disconnect($name = $name ?: $this->getDefaultConnection());
  107. if (!isset($this->connections[$name])) {
  108. return $this->connection($name);
  109. }
  110. return $this->refreshPdoConnections($name);
  111. }
  112. /**
  113. * Refresh the PDO connections on a given connection.
  114. *
  115. * @param string $name
  116. * @return \Illuminate\Database\Connection
  117. */
  118. protected function refreshPdoConnections($name)
  119. {
  120. $fresh = $this->makeConnection($name);
  121. return $this->connections[$name]
  122. ->setPdo($fresh->getPdo())
  123. ->setReadPdo($fresh->getReadPdo());
  124. }
  125. /**
  126. * Make the database connection instance.
  127. *
  128. * @param string $name
  129. * @return \Illuminate\Database\Connection
  130. */
  131. protected function makeConnection($name)
  132. {
  133. $config = $this->getConfig($name);
  134. // First we will check by the connection name to see if an extension has been
  135. // registered specifically for that connection. If it has we will call the
  136. // Closure and pass it the config allowing it to resolve the connection.
  137. if (isset($this->extensions[$name])) {
  138. return call_user_func($this->extensions[$name], $config, $name);
  139. }
  140. $driver = $config['driver'];
  141. // Next we will check to see if an extension has been registered for a driver
  142. // and will call the Closure if so, which allows us to have a more generic
  143. // resolver for the drivers themselves which applies to all connections.
  144. if (isset($this->extensions[$driver])) {
  145. return call_user_func($this->extensions[$driver], $config, $name);
  146. }
  147. return $this->factory->make($config, $name);
  148. }
  149. /**
  150. * Prepare the database connection instance.
  151. *
  152. * @param \Illuminate\Database\Connection $connection
  153. * @return \Illuminate\Database\Connection
  154. */
  155. protected function prepare(Connection $connection)
  156. {
  157. $connection->setFetchMode($this->app['config']['database.fetch']);
  158. if ($this->app->bound('events')) {
  159. $connection->setEventDispatcher($this->app['events']);
  160. }
  161. // Here we'll set a reconnector callback. This reconnector can be any callable
  162. // so we will set a Closure to reconnect from this manager with the name of
  163. // the connection, which will allow us to reconnect from the connections.
  164. $connection->setReconnector(function ($connection) {
  165. $this->reconnect($connection->getName());
  166. });
  167. return $connection;
  168. }
  169. /**
  170. * Prepare the read write mode for database connection instance.
  171. *
  172. * @param \Illuminate\Database\Connection $connection
  173. * @param string $type
  174. * @return \Illuminate\Database\Connection
  175. */
  176. protected function setPdoForType(Connection $connection, $type = null)
  177. {
  178. if ($type == 'read') {
  179. $connection->setPdo($connection->getReadPdo());
  180. } elseif ($type == 'write') {
  181. $connection->setReadPdo($connection->getPdo());
  182. }
  183. return $connection;
  184. }
  185. /**
  186. * Get the configuration for a connection.
  187. *
  188. * @param string $name
  189. * @return array
  190. *
  191. * @throws \InvalidArgumentException
  192. */
  193. protected function getConfig($name)
  194. {
  195. $name = $name ?: $this->getDefaultConnection();
  196. // To get the database connection configuration, we will just pull each of the
  197. // connection configurations and get the configurations for the given name.
  198. // If the configuration doesn't exist, we'll throw an exception and bail.
  199. $connections = $this->app['config']['database.connections'];
  200. if (is_null($config = array_get($connections, $name))) {
  201. throw new InvalidArgumentException("Database [$name] not configured.");
  202. }
  203. return $config;
  204. }
  205. /**
  206. * Get the default connection name.
  207. *
  208. * @return string
  209. */
  210. public function getDefaultConnection()
  211. {
  212. return $this->app['config']['database.default'];
  213. }
  214. /**
  215. * Set the default connection name.
  216. *
  217. * @param string $name
  218. * @return void
  219. */
  220. public function setDefaultConnection($name)
  221. {
  222. $this->app['config']['database.default'] = $name;
  223. }
  224. /**
  225. * Register an extension connection resolver.
  226. *
  227. * @param string $name
  228. * @param callable $resolver
  229. * @return void
  230. */
  231. public function extend($name, callable $resolver)
  232. {
  233. $this->extensions[$name] = $resolver;
  234. }
  235. /**
  236. * Return all of the created connections.
  237. *
  238. * @return array
  239. */
  240. public function getConnections()
  241. {
  242. return $this->connections;
  243. }
  244. /**
  245. * Dynamically pass methods to the default connection.
  246. *
  247. * @param string $method
  248. * @param array $parameters
  249. * @return mixed
  250. */
  251. public function __call($method, $parameters)
  252. {
  253. return call_user_func_array([$this->connection(), $method], $parameters);
  254. }
  255. }