Skip to content

Commit 248a425

Browse files
jamisonbryantJamison Bryant
and
Jamison Bryant
authored
Add support for customizable connection alias for table lookups (#462)
* Add $connectionName property to config file and Configuration class * Model scanner consume and use configured connection name Co-authored-by: Jamison Bryant <[email protected]>
1 parent bb72b53 commit 248a425

File tree

5 files changed

+52
-8
lines changed

5 files changed

+52
-8
lines changed

assets/swagger_bake.php

+3
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@
2626
* ################################
2727
* # OPTIONAL SETTINGS:
2828
* ################################
29+
*
30+
* @var string $connectionName The connection name to use when loading tables for building schemas from models.
31+
* Default: default
2932
*
3033
* @var array $editActionMethods The default HTTP methods to use for CakePHPs edit() action.
3134
* Default: ['PATCH']

src/Lib/Configuration.php

+37
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
namespace SwaggerBake\Lib;
55

66
use Cake\Core\Configure;
7+
use Cake\Datasource\ConnectionManager;
78
use InvalidArgumentException;
89
use LogicException;
910
use Symfony\Component\Yaml\Yaml;
@@ -77,6 +78,11 @@ class Configuration
7778
*/
7879
private array $editActionMethods = ['PATCH'];
7980

81+
/**
82+
* @var string The connection name to use when loading tables for building schemas from models.
83+
*/
84+
private string $connectionName = 'default';
85+
8086
/**
8187
* @var array Array of namespaces. Useful if your controllers or entities exist in non-standard namespace such
8288
* as a plugin. This was mostly added to aid in unit testing, but there are cases where controllers may
@@ -455,6 +461,37 @@ public function setJsonOptions(int $jsonOptions)
455461
return $this;
456462
}
457463

464+
/**
465+
* @return string
466+
*/
467+
public function getConnectionName(): string
468+
{
469+
return $this->connectionName;
470+
}
471+
472+
/**
473+
* @param string $connectionName Connection name to use when loading tables for building schemas from models.
474+
* @return $this
475+
*/
476+
public function setConnectionName(string $connectionName)
477+
{
478+
$configuredConnections = ConnectionManager::configured();
479+
480+
if (!in_array($connectionName, $configuredConnections)) {
481+
throw new InvalidArgumentException(
482+
sprintf(
483+
'Invalid connectionName supplied: %s. Must be one of %s',
484+
$connectionName,
485+
implode(', ', $configuredConnections)
486+
)
487+
);
488+
}
489+
490+
$this->connectionName = $connectionName;
491+
492+
return $this;
493+
}
494+
458495
/**
459496
* @return string[]
460497
*/

src/Lib/Model/ModelScanner.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ public function getModelDecorators(): array
4646
{
4747
$return = [];
4848

49-
$connection = ConnectionManager::get('default');
49+
$connection = ConnectionManager::get($this->config->getConnectionName());
5050
$namespaces = $this->config->getNamespaces();
5151

5252
foreach ($namespaces['tables'] as $tableNs) {

tests/TestCase/Lib/ConfigurationTest.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace SwaggerBake\Test\TestCase\Lib;
44

5+
use Cake\Core\Configure;
56
use Cake\TestSuite\TestCase;
67
use InvalidArgumentException;
78
use SwaggerBake\Lib\Configuration;
@@ -22,7 +23,8 @@ class ConfigurationTest extends TestCase
2223

2324
public function setUp(): void
2425
{
25-
parent::setUp(); // TODO: Change the autogenerated stub
26+
parent::setUp();
27+
2628
$this->configuration = $this->createConfiguration();
2729
}
2830

@@ -112,6 +114,7 @@ public function dataProviderInvalidConfig(): array
112114
['webPath', 'nope', \InvalidArgumentException::class, 'Invalid webPath'],
113115
['docType', 'nope', \InvalidArgumentException::class, 'Invalid docType'],
114116
['editActionMethods', ['nope'], \InvalidArgumentException::class, 'Invalid editActionMethod'],
117+
['connectionName', 'nope', \InvalidArgumentException::class, 'Invalid connectionName'],
115118
];
116119
}
117120
}

tests/TestCase/Lib/Model/ModelScannerTest.php

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@
33
namespace SwaggerBake\Test\TestCase\Lib\Model;
44

55
use Cake\Collection\Collection;
6-
use Cake\Datasource\ConnectionManager;
76
use Cake\Routing\RouteBuilder;
87
use Cake\Routing\Router;
98
use Cake\TestSuite\TestCase;
109
use SwaggerBake\Lib\Configuration;
11-
use SwaggerBake\Lib\Exception\SwaggerBakeRunTimeException;
1210
use SwaggerBake\Lib\Model\ModelDecorator;
1311
use SwaggerBake\Lib\Model\ModelScanner;
1412
use SwaggerBake\Lib\Route\RouteScanner;
@@ -29,12 +27,14 @@ class ModelScannerTest extends TestCase
2927

3028
public function setUp(): void
3129
{
32-
parent::setUp(); // TODO: Change the autogenerated stub
30+
parent::setUp();
31+
3332
$router = new Router();
3433
$router::scope('/', function (RouteBuilder $builder) {
3534
$builder->setExtensions(['json']);
3635
$builder->resources('Employees');
3736
});
37+
3838
$this->router = $router;
3939

4040
$this->config = [
@@ -50,7 +50,8 @@ public function setUp(): void
5050
'controllers' => ['\SwaggerBakeTest\App\\'],
5151
'entities' => ['\SwaggerBakeTest\App\\'],
5252
'tables' => ['\SwaggerBakeTest\App\\'],
53-
]
53+
],
54+
'connectionName' => 'test',
5455
];
5556
}
5657

@@ -66,7 +67,7 @@ public function test_model_should_not_be_decorated_when_no_route_exists(): void
6667
$this->assertCount(0, $collection);
6768
}
6869

69-
/* public function test_should_use_naming_conventions_when_multiple_models_found(): void
70+
/* public function test_should_use_naming_conventions_when_multiple_models_found(): void
7071
{
7172
$config = new Configuration($this->config, SWAGGER_BAKE_TEST_APP);
7273
$routeScanner = new RouteScanner($this->router, $config);
@@ -75,4 +76,4 @@ public function test_model_should_not_be_decorated_when_no_route_exists(): void
7576
7677
$modelDecorators = (new ModelScanner($routeScanner, $config))->getModelDecorators();
7778
}*/
78-
}
79+
}

0 commit comments

Comments
 (0)