Browse Source

Accept only ConnectionInterface instances.

ConnectionManager and ConnectionRegistry no longer accept arbitrary objects.
ADmad 4 years ago
parent
commit
50737aa6d4

+ 4 - 3
src/Datasource/ConnectionManager.php

@@ -23,6 +23,7 @@ use Cake\Database\Driver\Postgres;
 use Cake\Database\Driver\Sqlite;
 use Cake\Database\Driver\Sqlserver;
 use Cake\Datasource\Exception\MissingDatasourceConfigException;
+use Closure;
 
 /**
  * Manages and loads instances of Connection
@@ -73,12 +74,12 @@ class ConnectionManager
      * The connection will not be constructed until it is first used.
      *
      * @param array|string $key The name of the connection config, or an array of multiple configs.
-     * @param object|array|null $config An array of name => config data for adapter.
+     * @param \Cake\Datasource\ConnectionInterface|\Closure|array|null $config An array of name => config data for adapter.
      * @return void
      * @throws \Cake\Core\Exception\CakeException When trying to modify an existing config.
      * @see \Cake\Core\StaticConfigTrait::config()
      */
-    public static function setConfig(array|string $key, object|array|null $config = null): void
+    public static function setConfig(array|string $key, ConnectionInterface|Closure|array|null $config = null): void
     {
         if (is_array($config)) {
             $config['name'] = $key;
@@ -194,7 +195,7 @@ class ConnectionManager
      * @throws \Cake\Datasource\Exception\MissingDatasourceConfigException When config
      * data is missing.
      */
-    public static function get(string $name, bool $useAliases = true)
+    public static function get(string $name, bool $useAliases = true): ConnectionInterface
     {
         if ($useAliases && isset(static::$_aliasMap[$name])) {
             $name = static::$_aliasMap[$name];

+ 13 - 10
src/Datasource/ConnectionRegistry.php

@@ -19,6 +19,7 @@ namespace Cake\Datasource;
 use Cake\Core\App;
 use Cake\Core\ObjectRegistry;
 use Cake\Datasource\Exception\MissingDatasourceException;
+use Closure;
 
 /**
  * A registry object for connection instances.
@@ -68,24 +69,26 @@ class ConnectionRegistry extends ObjectRegistry
      * If a callable is passed as first argument, The returned value of this
      * function will be the result of the callable.
      *
-     * @param callable|object|string $class The classname or object to make.
+     * @param object|string $class The classname or object to make.
      * @param string $alias The alias of the object.
      * @param array<string, mixed> $config An array of settings to use for the datasource.
-     * @return object A connection with the correct settings.
+     * @return \Cake\Datasource\ConnectionInterface A connection with the correct settings.
      */
-    protected function _create(callable|object|string $class, string $alias, array $config): object
+    protected function _create(object|string $class, string $alias, array $config): ConnectionInterface
     {
-        if (is_callable($class)) {
-            return $class($alias);
-        }
+        if (is_string($class)) {
+            unset($config['className']);
 
-        if (is_object($class)) {
-            return $class;
+            /** @var \Cake\Datasource\ConnectionInterface */
+            return new $class($config);
         }
 
-        unset($config['className']);
+        if ($class instanceof Closure) {
+            return $class($alias);
+        }
 
-        return new $class($config);
+        /** @var \Cake\Datasource\ConnectionInterface */
+        return $class;
     }
 
     /**

+ 3 - 25
tests/test_app/Plugin/TestPlugin/src/Datasource/TestSource.php

@@ -3,30 +3,8 @@ declare(strict_types=1);
 
 namespace TestPlugin\Datasource;
 
-class TestSource
-{
-    /**
-     * Config
-     *
-     * @var array
-     */
-    protected $_config;
-
-    /**
-     * Constructor
-     */
-    public function __construct(array $config)
-    {
-        $this->_config = $config;
-    }
+use TestApp\Datasource\FakeConnection;
 
-    /**
-     * config
-     *
-     * @return array
-     */
-    public function config(): array
-    {
-        return $this->_config;
-    }
+class TestSource extends FakeConnection
+{
 }

+ 45 - 1
tests/test_app/TestApp/Datasource/FakeConnection.php

@@ -3,7 +3,12 @@ declare(strict_types=1);
 
 namespace TestApp\Datasource;
 
-class FakeConnection
+use Cake\Datasource\ConnectionInterface;
+use Psr\Log\LoggerInterface;
+use Psr\SimpleCache\CacheInterface;
+use RuntimeException;
+
+class FakeConnection implements ConnectionInterface
 {
     protected $_config = [];
 
@@ -38,4 +43,43 @@ class FakeConnection
 
         return $this->_config['name'];
     }
+
+    public function getDriver(): object
+    {
+        throw new RuntimeException('Not implemented');
+    }
+
+    public function getLogger(): LoggerInterface
+    {
+        throw new RuntimeException('Not implemented');
+    }
+
+    public function setLogger(LoggerInterface $logger): void
+    {
+    }
+
+    public function setCacher(CacheInterface $cacher)
+    {
+        throw new RuntimeException('Not implemented');
+    }
+
+    public function getCacher(): CacheInterface
+    {
+        throw new RuntimeException('Not implemented');
+    }
+
+    public function enableQueryLogging(bool $enable = true)
+    {
+        return $this;
+    }
+
+    public function disableQueryLogging()
+    {
+        return $this;
+    }
+
+    public function isQueryLoggingEnabled(): bool
+    {
+        return false;
+    }
 }