| 1 |
<?php namespace WpFluent; |
| 2 |
|
| 3 |
use Viocon\Container; |
| 4 |
|
| 5 |
class Connection |
| 6 |
{ |
| 7 |
|
| 8 |
/** |
| 9 |
* @var Container |
| 10 |
*/ |
| 11 |
protected $container; |
| 12 |
|
| 13 |
/** |
| 14 |
* @var string |
| 15 |
*/ |
| 16 |
protected $adapter; |
| 17 |
|
| 18 |
/** |
| 19 |
* @var array |
| 20 |
*/ |
| 21 |
protected $adapterConfig; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var \wpdb $wpdb |
| 25 |
*/ |
| 26 |
protected $dbInstance; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var \wpdb $wpdb |
| 30 |
*/ |
| 31 |
protected $wpdb; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var Connection |
| 35 |
*/ |
| 36 |
protected static $storedConnection; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var EventHandler |
| 40 |
*/ |
| 41 |
protected $eventHandler; |
| 42 |
|
| 43 |
/** |
| 44 |
* @param $wpdb |
| 45 |
* @param array $adapterConfig |
| 46 |
* @param null|string $alias |
| 47 |
* @param null|Container $container |
| 48 |
*/ |
| 49 |
public function __construct($wpdb, array $config = array(), $alias = null, Container $container = null) |
| 50 |
{ |
| 51 |
$container = $container ? : new Container(); |
| 52 |
|
| 53 |
$this->container = $container; |
| 54 |
|
| 55 |
$this->wpdb = $wpdb; |
| 56 |
|
| 57 |
$this->setAdapter()->setAdapterConfig($config)->connect(); |
| 58 |
|
| 59 |
// Create event dependency |
| 60 |
$this->eventHandler = $this->container->build('\\WpFluent\\EventHandler'); |
| 61 |
|
| 62 |
if ($alias) { |
| 63 |
$this->createAlias($alias); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Create an easily accessible query builder alias |
| 69 |
* |
| 70 |
* @param $alias |
| 71 |
*/ |
| 72 |
public function createAlias($alias) |
| 73 |
{ |
| 74 |
class_alias('WpFluent\\AliasFacade', $alias); |
| 75 |
|
| 76 |
$builder = $this->container->build('\\WpFluent\\QueryBuilder\\QueryBuilderHandler', array($this)); |
| 77 |
|
| 78 |
AliasFacade::setQueryBuilderInstance($builder); |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Returns an instance of Query Builder |
| 83 |
*/ |
| 84 |
public function getQueryBuilder() |
| 85 |
{ |
| 86 |
return $this->container->build('\\WpFluent\\QueryBuilder\\QueryBuilderHandler', array($this)); |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
/** |
| 91 |
* Create the connection adapter |
| 92 |
*/ |
| 93 |
protected function connect() |
| 94 |
{ |
| 95 |
$this->setDbInstance($this->wpdb); |
| 96 |
|
| 97 |
// Preserve the first database connection with a static property |
| 98 |
if (! static::$storedConnection) { |
| 99 |
static::$storedConnection = $this; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* @param $db |
| 105 |
* |
| 106 |
* @return $this |
| 107 |
*/ |
| 108 |
public function setDbInstance($db) |
| 109 |
{ |
| 110 |
$this->dbInstance = $db; |
| 111 |
|
| 112 |
return $this; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* @return \wpdb |
| 117 |
*/ |
| 118 |
public function getDbInstance() |
| 119 |
{ |
| 120 |
return $this->dbInstance; |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* @param $adapter |
| 125 |
* |
| 126 |
* @return $this |
| 127 |
*/ |
| 128 |
public function setAdapter($adapter = 'mysql') |
| 129 |
{ |
| 130 |
$this->adapter = $adapter; |
| 131 |
|
| 132 |
return $this; |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* @return string |
| 137 |
*/ |
| 138 |
public function getAdapter() |
| 139 |
{ |
| 140 |
return $this->adapter; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* @param array $adapterConfig |
| 145 |
* |
| 146 |
* @return $this |
| 147 |
*/ |
| 148 |
public function setAdapterConfig(array $adapterConfig) |
| 149 |
{ |
| 150 |
$this->adapterConfig = $adapterConfig; |
| 151 |
|
| 152 |
return $this; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* @return array |
| 157 |
*/ |
| 158 |
public function getAdapterConfig() |
| 159 |
{ |
| 160 |
return $this->adapterConfig; |
| 161 |
} |
| 162 |
|
| 163 |
/** |
| 164 |
* @return Container |
| 165 |
*/ |
| 166 |
public function getContainer() |
| 167 |
{ |
| 168 |
return $this->container; |
| 169 |
} |
| 170 |
|
| 171 |
/** |
| 172 |
* @return EventHandler |
| 173 |
*/ |
| 174 |
public function getEventHandler() |
| 175 |
{ |
| 176 |
return $this->eventHandler; |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* @return Connection |
| 181 |
*/ |
| 182 |
public static function getStoredConnection() |
| 183 |
{ |
| 184 |
return static::$storedConnection; |
| 185 |
} |
| 186 |
} |
| 187 |
|