-
Notifications
You must be signed in to change notification settings - Fork 179
[feature] Master slave connection #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
fureszpeter
wants to merge
2
commits into
laravel-doctrine:1.1
from
fureszpeter:MasterSlaveConnection
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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,76 @@ | ||
| <?php | ||
|
|
||
| namespace LaravelDoctrine\ORM\Utilities; | ||
|
|
||
| use UnexpectedValueException; | ||
|
|
||
| class MasterSlaveConfigParser | ||
| { | ||
| /** | ||
| * @param array $config | ||
| * | ||
| * @throws UnexpectedValueException If config not contains valid read / write configuration. | ||
| * | ||
| * @return array If config is not valid for parse. | ||
| * | ||
| */ | ||
| public static function parseConfig(array $config) | ||
| { | ||
| if ( ! self::hasValidConfig($config)) { | ||
| throw new UnexpectedValueException('Config not contains configuration for master/slave setup.'); | ||
| } | ||
|
|
||
| $masterSlave = [ | ||
| 'write' => [], | ||
| 'read' => [], | ||
| ]; | ||
|
|
||
| $masterSlave['write'] = [ | ||
| 'user' => array_get($config, 'write.username', $config['username']), | ||
| 'password' => array_get($config, 'write.password', $config['password']), | ||
| 'host' => array_get($config, 'write.host', $config['host']), | ||
| 'dbname' => array_get($config, 'write.database', $config['database']), | ||
| 'port' => array_get($config, 'write.port', $config['port']), | ||
| ]; | ||
|
|
||
| foreach (array_get($config, 'read', []) as $slaveConfig) { | ||
| $config = [ | ||
| 'user' => array_get($slaveConfig, 'username', $config['username']), | ||
| 'password' => array_get($slaveConfig, 'password', $config['password']), | ||
| 'host' => array_get($slaveConfig, 'host', $config['host']), | ||
| 'dbname' => array_get($slaveConfig, 'database', $config['database']), | ||
| 'port' => array_get($config, 'write.port', $config['port']), | ||
| ]; | ||
|
|
||
| $masterSlave['read'][] = $config; | ||
| } | ||
|
|
||
| return $masterSlave; | ||
| } | ||
|
|
||
| /** | ||
| * @param array $config | ||
| * | ||
| * @return bool | ||
| */ | ||
| public static function hasValidConfig(array $config) | ||
| { | ||
| if ( | ||
| array_key_exists('read', $config) | ||
| && ! empty($config['read']) | ||
| && is_array($config['read']) | ||
| //All value in $config['read'] should be an array | ||
| && ! in_array(false, array_map(function ($readConfigValue) { | ||
| return is_array($readConfigValue); | ||
| }, $config['read'])) | ||
| //All value in $config['read'] should have at least one config difference | ||
| && ! in_array(false, array_map(function (array $readConfig) { | ||
| return count($readConfig) > 0; | ||
| }, $config['read'])) | ||
| ) { | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
| } | ||
This file contains hidden or 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,174 @@ | ||
| <?php | ||
|
|
||
| use LaravelDoctrine\ORM\Utilities\MasterSlaveConfigParser; | ||
|
|
||
| class MasterSlaveConfigParserTest extends PHPUnit_Framework_TestCase | ||
| { | ||
|
|
||
| /** | ||
| * @dataProvider validConfigProvider | ||
| * | ||
| * @param array $validConfig | ||
| */ | ||
| public function test_HasValidConfig_returns_true_on_valid_config(array $validConfig) | ||
| { | ||
| $this->assertTrue(MasterSlaveConfigParser::hasValidConfig($validConfig)); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider invalidConfigProvider | ||
| * | ||
| * @param array $invalidConfig | ||
| */ | ||
| public function test_HasValidConfig_returns_false_on_missing_config(array $invalidConfig) | ||
| { | ||
| $this->assertFalse(MasterSlaveConfigParser::hasValidConfig($invalidConfig)); | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function validConfigProvider() | ||
| { | ||
| $valid1['config'] = array_merge( | ||
| $this->getDatabaseConfigTemplate(), | ||
| [ | ||
| 'write' => [ | ||
| 'host' => '192.168.0.1', | ||
| ], | ||
| 'read' => [ | ||
| [ | ||
| 'host' => '192.168.0.2', | ||
| 'username' => 'read_user', | ||
| ], | ||
| ], | ||
| ] | ||
| ); | ||
| $valid1['expectedWrite'] = [ | ||
| 'user' => 'write_db_username', | ||
| 'password' => 'write_db_password', | ||
| 'host' => '192.168.0.1', //Override the parent's localhost | ||
| 'dbname' => 'write_db_name', | ||
| 'port' => 3306, | ||
| ]; | ||
| $valid1['expectedRead'] = [ | ||
| [ | ||
| 'user' => 'read_user', | ||
| 'password' => 'write_db_password', | ||
| 'host' => '192.168.0.2', //Override the parent's localhost | ||
| 'dbname' => 'write_db_name', | ||
| 'port' => 3306, | ||
| ], | ||
| ]; | ||
|
|
||
| $valid2['config'] = array_merge( | ||
| $this->getDatabaseConfigTemplate(), | ||
| [ | ||
| 'read' => [ | ||
| [ | ||
| 'host' => '192.168.0.1', | ||
| ], | ||
| ] | ||
| ] | ||
| ); | ||
| $valid2['expectedWrite'] = [ | ||
| 'user' => 'write_db_username', | ||
| 'password' => 'write_db_password', | ||
| 'host' => 'localhost', | ||
| 'dbname' => 'write_db_name', | ||
| 'port' => 3306, | ||
| ]; | ||
| $valid2['expectedRead'] = [ | ||
| [ | ||
| 'user' => 'write_db_username', | ||
| 'password' => 'write_db_password', | ||
| 'host' => '192.168.0.1', //Override the parent's localhost | ||
| 'dbname' => 'write_db_name', | ||
| 'port' => 3306, | ||
| ], | ||
| ]; | ||
|
|
||
| return [ | ||
| [$valid1['config'], $valid1['expectedWrite'], $valid1['expectedRead']], | ||
| [$valid2['config'], $valid2['expectedWrite'], $valid2['expectedRead']], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function getDatabaseConfigTemplate() | ||
| { | ||
| return [ | ||
| 'host' => 'localhost', | ||
| 'database' => 'write_db_name', | ||
| 'username' => 'write_db_username', | ||
| 'password' => 'write_db_password', | ||
| 'driver' => 'mysql', | ||
| 'port' => 3306, | ||
| 'charset' => 'utf8', | ||
| 'collation' => 'utf8_unicode_ci', | ||
| 'prefix' => '', | ||
| 'strict' => false, | ||
| 'engine' => null, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @return array | ||
| */ | ||
| public function invalidConfigProvider() | ||
| { | ||
| $invalidConfig1 = []; | ||
| $invalidConfig2 = array_merge( | ||
| $this->getDatabaseConfigTemplate(), | ||
| [ | ||
| 'write' => [], | ||
| // 'read' => [], //Read is missing, it's invalid | ||
| ] | ||
| ); | ||
| $invalidConfig3 = array_merge( | ||
| $this->getDatabaseConfigTemplate(), | ||
| [ | ||
| 'write' => [], | ||
| 'read' => ['host' => '123'], //Read should be an array | ||
| ] | ||
| ); | ||
|
|
||
| return [ | ||
| [$invalidConfig1], | ||
| [$invalidConfig2], | ||
| [$invalidConfig3], | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider invalidConfigProvider | ||
| * | ||
| * @expectedException UnexpectedValueException | ||
| * | ||
| * @param array $invalidConfig | ||
| */ | ||
| public function test_ParseConfig_throws_exception_on_invalid_config(array $invalidConfig) | ||
| { | ||
| MasterSlaveConfigParser::parseConfig($invalidConfig); | ||
| } | ||
|
|
||
| /** | ||
| * @dataProvider validConfigProvider | ||
| * | ||
| * @param array $config | ||
| * @param array $expectedWrite | ||
| * @param array $expectedRead | ||
| */ | ||
| public function test_ParseConfig_can_inherit_settings( | ||
| array $config, | ||
| array $expectedWrite, | ||
| array $expectedRead | ||
| ) { | ||
| $readWriteConfig = MasterSlaveConfigParser::parseConfig($config); | ||
|
|
||
| $this->assertEquals($expectedRead, $readWriteConfig['read']); | ||
| $this->assertEquals($expectedWrite, $readWriteConfig['write']); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fureszpeter I don't think this should be extracted to a whole class.
This could live inside the
EntityManagerFactory, as no other code will need to call it. Plus, a big, big, BIG 👎 to utility (static) classes.