55
66use Cake \Core \App ;
77use Cake \Datasource \ConnectionManager ;
8+ use Cake \Datasource \Exception \MissingDatasourceConfigException ;
89use Cake \Utility \Inflector ;
10+ use Muffin \Webservice \Datasource \Connection ;
911use RuntimeException ;
1012
1113/**
@@ -99,9 +101,9 @@ public function get(string $alias, array $options = []): Endpoint
99101 $ connectionName = Inflector::underscore (end ($ pluginParts ));
100102 }
101103
102- $ options ['connection ' ] = ConnectionManager:: get ($ connectionName );
104+ $ options ['connection ' ] = $ this -> getConnection ($ connectionName );
103105 } elseif (is_string ($ options ['connection ' ])) {
104- $ options ['connection ' ] = ConnectionManager:: get ($ options ['connection ' ]);
106+ $ options ['connection ' ] = $ this -> getConnection ($ options ['connection ' ]);
105107 }
106108
107109 $ options ['registryAlias ' ] = $ alias ;
@@ -111,6 +113,26 @@ public function get(string $alias, array $options = []): Endpoint
111113 return $ this ->_instances [$ alias ];
112114 }
113115
116+ /**
117+ * Get connection instance.
118+ *
119+ * @param string $connectionName Connection name.
120+ * @return \Muffin\Webservice\Datasource\Connection
121+ */
122+ protected function getConnection (string $ connectionName ): Connection
123+ {
124+ try {
125+ /** @var \Muffin\Webservice\Datasource\Connection */
126+ return ConnectionManager::get ($ connectionName );
127+ } catch (MissingDatasourceConfigException $ e ) {
128+ $ message = $ e ->getMessage ()
129+ . ' You can override Endpoint::defaultConnectionName() to return the connection name you want. ' ;
130+
131+ /** @psalm-suppress PossiblyInvalidArgument */
132+ throw new MissingDatasourceConfigException ($ message , $ e ->getCode (), $ e ->getPrevious ());
133+ }
134+ }
135+
114136 /**
115137 * Check to see if an instance exists in the locator.
116138 *
0 commit comments