Skip to content

Commit a7d761b

Browse files
authored
Merge pull request #94 from UseMuffin/endpoint-locator
Fix error when guessing connection name.
2 parents f9c27fa + 9fab08a commit a7d761b

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

README.md

+10-11
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,23 @@ bin/cake plugin load Muffin/Webservice
2323

2424
## Usage
2525

26-
In your `app.php`, configure your `app` service like any other configuration,
27-
by adding a new element to the configure array:
26+
### Datasource Configuration
27+
28+
In your `app.php`, add a new `webservice` config under `Datasources`:
2829

2930
```php
30-
'Webservices' => [
31-
'app' => [
31+
'Datasources' => [
32+
// Other db config here
33+
'webservice' => [
3234
'className' => \Muffin\Webservice\Connection::class,
3335
'service' => 'Articles',
3436
// Any additional keys will be set as Driver's config.
35-
]
36-
]
37+
],
38+
],
3739
```
3840

39-
You will also need to load the webservices in your `bootstrap.php` file:
40-
41-
```php
42-
ConnectionManager::config(Configure::consume('Webservices'));
43-
```
41+
If you are making a plugin then conventionally the datasource config key name
42+
should be underscored version of plugin name.
4443

4544
### As an ORM
4645

src/Model/Endpoint.php

+6-2
Original file line numberDiff line numberDiff line change
@@ -190,9 +190,13 @@ public function __construct(array $config = [])
190190
public static function defaultConnectionName(): string
191191
{
192192
$namespaceParts = explode('\\', static::class);
193-
$plugin = array_slice(array_reverse($namespaceParts), 3, 2);
193+
$plugin = current(array_slice(array_reverse($namespaceParts), 3, 2));
194194

195-
return Inflector::underscore(current($plugin));
195+
if ($plugin === 'App') {
196+
return 'webservice';
197+
}
198+
199+
return Inflector::underscore($plugin);
196200
}
197201

198202
/**

src/Model/EndpointLocator.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,13 @@ protected function createInstance(string $alias, array $options)
7474
if ($options['className'] !== Endpoint::class) {
7575
$connectionName = $options['className']::defaultConnectionName();
7676
} else {
77-
/** @psalm-suppress PossiblyNullArgument */
78-
$pluginParts = explode('/', pluginSplit($alias)[0]);
79-
80-
$connectionName = Inflector::underscore(end($pluginParts));
77+
if (strpos($alias, '.') === false) {
78+
$connectionName = 'webservice';
79+
} else {
80+
/** @psalm-suppress PossiblyNullArgument */
81+
$pluginParts = explode('/', pluginSplit($alias)[0]);
82+
$connectionName = Inflector::underscore(end($pluginParts));
83+
}
8184
}
8285

8386
$options['connection'] = $this->getConnection($connectionName);

0 commit comments

Comments
 (0)