Skip to content

Commit

Permalink
Merge pull request #94 from UseMuffin/endpoint-locator
Browse files Browse the repository at this point in the history
Fix error when guessing connection name.
  • Loading branch information
ADmad authored Jul 26, 2020
2 parents f9c27fa + 9fab08a commit a7d761b
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 17 deletions.
21 changes: 10 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 6 additions & 2 deletions src/Model/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
11 changes: 7 additions & 4 deletions src/Model/EndpointLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = 'webservice';
} else {
/** @psalm-suppress PossiblyNullArgument */
$pluginParts = explode('/', pluginSplit($alias)[0]);
$connectionName = Inflector::underscore(end($pluginParts));
}
}

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

0 comments on commit a7d761b

Please sign in to comment.