From c346772aa453fee36b4a117c024096d920220c5c Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 25 Jul 2020 19:29:41 +0530 Subject: [PATCH 1/2] Fix error when guessing connection name. Closes #93. --- src/Model/EndpointLocator.php | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/Model/EndpointLocator.php b/src/Model/EndpointLocator.php index 75391ed..05b31e7 100644 --- a/src/Model/EndpointLocator.php +++ b/src/Model/EndpointLocator.php @@ -74,10 +74,13 @@ protected function createInstance(string $alias, array $options) if ($options['className'] !== Endpoint::class) { $connectionName = $options['className']::defaultConnectionName(); } else { - /** @psalm-suppress PossiblyNullArgument */ - $pluginParts = explode('/', pluginSplit($alias)[0]); - - $connectionName = Inflector::underscore(end($pluginParts)); + if (strpos($alias, '.') === false) { + $connectionName = 'app'; + } else { + /** @psalm-suppress PossiblyNullArgument */ + $pluginParts = explode('/', pluginSplit($alias)[0]); + $connectionName = Inflector::underscore(end($pluginParts)); + } } $options['connection'] = $this->getConnection($connectionName); From 9fab08a4ec2d0bd3525627fc3a5d950118c8e655 Mon Sep 17 00:00:00 2001 From: ADmad Date: Sat, 25 Jul 2020 20:21:35 +0530 Subject: [PATCH 2/2] Use connection config name "webservice" by default. --- README.md | 21 ++++++++++----------- src/Model/Endpoint.php | 8 ++++++-- src/Model/EndpointLocator.php | 2 +- 3 files changed, 17 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index d99afb5..dae4c98 100644 --- a/README.md +++ b/README.md @@ -23,24 +23,23 @@ bin/cake plugin load Muffin/Webservice ## Usage -In your `app.php`, configure your `app` service like any other configuration, -by adding a new element to the configure array: +### Datasource Configuration + +In your `app.php`, add a new `webservice` config under `Datasources`: ```php - 'Webservices' => [ - 'app' => [ + 'Datasources' => [ + // Other db config here + 'webservice' => [ 'className' => \Muffin\Webservice\Connection::class, 'service' => 'Articles', // Any additional keys will be set as Driver's config. - ] - ] + ], + ], ``` -You will also need to load the webservices in your `bootstrap.php` file: - -```php -ConnectionManager::config(Configure::consume('Webservices')); -``` +If you are making a plugin then conventionally the datasource config key name +should be underscored version of plugin name. ### As an ORM diff --git a/src/Model/Endpoint.php b/src/Model/Endpoint.php index 9260d02..52efae3 100644 --- a/src/Model/Endpoint.php +++ b/src/Model/Endpoint.php @@ -190,9 +190,13 @@ public function __construct(array $config = []) public static function defaultConnectionName(): string { $namespaceParts = explode('\\', static::class); - $plugin = array_slice(array_reverse($namespaceParts), 3, 2); + $plugin = current(array_slice(array_reverse($namespaceParts), 3, 2)); - return Inflector::underscore(current($plugin)); + if ($plugin === 'App') { + return 'webservice'; + } + + return Inflector::underscore($plugin); } /** diff --git a/src/Model/EndpointLocator.php b/src/Model/EndpointLocator.php index 05b31e7..5f93d92 100644 --- a/src/Model/EndpointLocator.php +++ b/src/Model/EndpointLocator.php @@ -75,7 +75,7 @@ protected function createInstance(string $alias, array $options) $connectionName = $options['className']::defaultConnectionName(); } else { if (strpos($alias, '.') === false) { - $connectionName = 'app'; + $connectionName = 'webservice'; } else { /** @psalm-suppress PossiblyNullArgument */ $pluginParts = explode('/', pluginSplit($alias)[0]);