From 342ada2451418a3f4eb4def44a2909c84a075e98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Z=C3=BA=C3=B1iga?= Date: Fri, 19 Jul 2024 23:29:10 -0600 Subject: [PATCH] Added core and database configuration examples. --- Config/core.php | 9 +++ Config/database.php | 139 ++++++++++++++++++++++++++++++++++++++++++++ README.md | 27 ++++++++- 3 files changed, 173 insertions(+), 2 deletions(-) create mode 100644 Config/core.php create mode 100644 Config/database.php diff --git a/Config/core.php b/Config/core.php new file mode 100644 index 0000000..bc7d39d --- /dev/null +++ b/Config/core.php @@ -0,0 +1,9 @@ + App::core('Datasource/Database')]); diff --git a/Config/database.php b/Config/database.php new file mode 100644 index 0000000..1ae1afa --- /dev/null +++ b/Config/database.php @@ -0,0 +1,139 @@ + '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); + } + } +} diff --git a/README.md b/README.md index 08b4cad..e3d9448 100644 --- a/README.md +++ b/README.md @@ -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')]); +````