-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added core and database configuration examples.
- Loading branch information
1 parent
9f6a34e
commit 342ada2
Showing
3 changed files
with
173 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
/* | ||
* Includes Core Datasource Database classes path. | ||
* This path is needed so as to extend the default MySQL Database class (DboDatasource adapter). | ||
* This class is needed for MySQLReplica class, said class can switch between Source database | ||
* and Replica database. | ||
*/ | ||
App::build(['Model/Datasource/Database' => App::core('Datasource/Database')]); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
<?php | ||
/** | ||
* DATABASE CONFIG. | ||
*/ | ||
class DATABASE_CONFIG | ||
{ | ||
/** | ||
* CakePHP's Database Config Default Schema. | ||
* | ||
* @var array | ||
*/ | ||
protected $base_config = [ | ||
'datasource' => 'Database/Mysql', | ||
'persistent' => false, | ||
'host' => null, | ||
'login' => null, | ||
'password' => null, | ||
'port' => null, | ||
'database' => null, | ||
'encoding' => null, | ||
'prefix' => null, | ||
'ssl_ca' => null, | ||
]; | ||
|
||
/** | ||
* Default DataSource. | ||
* | ||
* @var string | ||
*/ | ||
protected $default_datasource = 'Database/Mysql'; | ||
|
||
/** | ||
* Replication DataSource. | ||
* | ||
* @var string | ||
*/ | ||
protected $replica_datasource = 'Database/MysqlReplica'; | ||
|
||
/** | ||
* Datasource in use. | ||
* | ||
* @var string | ||
*/ | ||
protected $datasource = 'Database/Mysql'; | ||
|
||
/** | ||
* Use of Replication flag. | ||
* | ||
* @var string | ||
*/ | ||
protected $replication = false; | ||
|
||
/** | ||
* Source database. | ||
* | ||
* @var array | ||
*/ | ||
public $source = null; | ||
|
||
/** | ||
* Replica databases. | ||
* | ||
* @var array | ||
*/ | ||
public $replicas = null; | ||
|
||
/** | ||
* Sets the database configuration for MySQL Replication. | ||
* | ||
* @return void | ||
*/ | ||
public function __construct() | ||
{ | ||
$this->replication = false; | ||
$this->datasource = $this->default_datasource; | ||
$this->base_config = array_merge($this->base_config, ['datasource' => $this->datasource]); | ||
|
||
/* | ||
* Assuming your database configuration is an assoc array | ||
* having 'source' as an array and 'replicas' as an array of arrays | ||
* with $this->base_config structure. | ||
*/ | ||
$database = Configure::read('database'); | ||
|
||
// Source will Read and Write when not in replication mode. | ||
$this->setSource($database['source']); | ||
$this->default = $this->source; | ||
|
||
// If there are replicas we can then enable replication mode. | ||
if ((array) $database['replicas']) | ||
{ | ||
$this->replication = true; | ||
|
||
/* | ||
* For Source-Replica configuration, set datasource to MysqlReplica | ||
* so as to make it possible to switch between Source and Replica servers. | ||
*/ | ||
$this->datasource = $this->replica_datasource; | ||
$this->base_config = array_merge($this->base_config, ['datasource' => $this->datasource]); | ||
|
||
$this->setSource($database['source']); | ||
$this->setReplicas($database['replicas']); | ||
|
||
// Assign a random replica for the Read transactions, overrides previous setting. | ||
if ($this->replicas) | ||
{ | ||
$this->default = $this->replicas[rand(0, count($this->replicas) - 1)]; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Set source database configuration. | ||
* | ||
* @param array $source Source database configuration. | ||
* @return void | ||
*/ | ||
private function setSource($source) | ||
{ | ||
// Source can Read and Write. | ||
$this->source = array_merge($this->base_config, (array) $source); | ||
} | ||
|
||
/** | ||
* Set replicas database configuration. | ||
* | ||
* @param array $replicas An array of replica database configurations. | ||
* @return void | ||
*/ | ||
private function setReplicas($replicas) | ||
{ | ||
// Replicas can only Read. | ||
$this->replicas = []; | ||
foreach ((array) $replicas as $replica) | ||
{ | ||
$this->replicas[] = array_merge($this->base_config, $replica); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,25 @@ | ||
# CakeReplication | ||
CakePHP for MySQL Replication | ||
# CakePHP Replication for MySQL | ||
CakeReplication helps you setup CakePHP 2.10 (latest and last) Replication for MySQL 5.7 in a more integrated way | ||
adding a layer between Model and the MySQL default driver. | ||
|
||
## Requirements | ||
* PHP `7.1.4` | ||
* CakePHP `2.10.4` | ||
* MySQL `5.7` | ||
* Updating your `app/Config/database.php` | ||
|
||
*There are other versions of this implementation but are quite outdated, made for older CakePHP versions that* | ||
*have missmatching class methods and arguments.* | ||
|
||
## Installation | ||
Install the Plugin with [Composer](https://getcomposer.org) from your CakePHP's ROOT directory. | ||
|
||
``` | ||
$ composer require --dev adrian0350/cakephp-replication | ||
``` | ||
\ | ||
Add CakePHP's datasource Database `core` `path` to your `app/Config/core.php`. | ||
|
||
```` | ||
App::build(['Model/Datasource/Database' => App::core('Datasource/Database')]); | ||
```` |