-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #677 from cakephp/import-environment
Import additional adapters to setup Environment
- Loading branch information
Showing
10 changed files
with
1,628 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,172 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
/** | ||
* MIT License | ||
* For full license information, please view the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Migrations\Db\Adapter; | ||
|
||
use RuntimeException; | ||
|
||
/** | ||
* Adapter factory and registry. | ||
* | ||
* Used for registering adapters and creating instances of adapters. | ||
*/ | ||
class AdapterFactory | ||
{ | ||
/** | ||
* @var static|null | ||
*/ | ||
protected static ?AdapterFactory $instance = null; | ||
|
||
/** | ||
* Get the factory singleton instance. | ||
* | ||
* @return static | ||
*/ | ||
public static function instance(): static | ||
{ | ||
if (!static::$instance) { | ||
static::$instance = new static(); | ||
} | ||
|
||
return static::$instance; | ||
} | ||
|
||
/** | ||
* Class map of database adapters, indexed by PDO::ATTR_DRIVER_NAME. | ||
* | ||
* @var array<string, \Migrations\Db\Adapter\AdapterInterface|string> | ||
* @phpstan-var array<string, class-string<\Migrations\Db\Adapter\AdapterInterface>> | ||
* @psalm-var array<string, class-string<\Migrations\Db\Adapter\AdapterInterface>> | ||
*/ | ||
protected array $adapters = [ | ||
'mysql' => MysqlAdapter::class, | ||
'postgres' => PostgresAdapter::class, | ||
'sqlite' => SqliteAdapter::class, | ||
'sqlserver' => SqlserverAdapter::class, | ||
]; | ||
|
||
/** | ||
* Class map of adapters wrappers, indexed by name. | ||
* | ||
* @var array<string, string> | ||
* @psalm-var array<string, class-string<\Migrations\Db\Adapter\WrapperInterface>> | ||
*/ | ||
protected array $wrappers = [ | ||
'record' => RecordingAdapter::class, | ||
'timed' => TimedOutputAdapter::class, | ||
]; | ||
|
||
/** | ||
* Register an adapter class with a given name. | ||
* | ||
* @param string $name Name | ||
* @param string $class Class | ||
* @throws \RuntimeException | ||
* @return $this | ||
*/ | ||
public function registerAdapter(string $name, string $class) | ||
{ | ||
if (!is_subclass_of($class, AdapterInterface::class)) { | ||
throw new RuntimeException(sprintf( | ||
'Adapter class "%s" must implement Migrations\\Db\\Adapter\\AdapterInterface', | ||
$class | ||
)); | ||
} | ||
$this->adapters[$name] = $class; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get an adapter class by name. | ||
* | ||
* @param string $name Name | ||
* @throws \RuntimeException | ||
* @return string | ||
* @phpstan-return class-string<\Migrations\Db\Adapter\AdapterInterface> | ||
*/ | ||
protected function getClass(string $name): object|string | ||
{ | ||
if (empty($this->adapters[$name])) { | ||
throw new RuntimeException(sprintf( | ||
'Adapter "%s" has not been registered', | ||
$name | ||
)); | ||
} | ||
|
||
return $this->adapters[$name]; | ||
} | ||
|
||
/** | ||
* Get an adapter instance by name. | ||
* | ||
* @param string $name Name | ||
* @param array<string, mixed> $options Options | ||
* @return \Migrations\Db\Adapter\AdapterInterface | ||
*/ | ||
public function getAdapter(string $name, array $options): AdapterInterface | ||
{ | ||
$class = $this->getClass($name); | ||
|
||
return new $class($options); | ||
} | ||
|
||
/** | ||
* Add or replace a wrapper with a fully qualified class name. | ||
* | ||
* @param string $name Name | ||
* @param string $class Class | ||
* @throws \RuntimeException | ||
* @return $this | ||
*/ | ||
public function registerWrapper(string $name, string $class) | ||
{ | ||
if (!is_subclass_of($class, WrapperInterface::class)) { | ||
throw new RuntimeException(sprintf( | ||
'Wrapper class "%s" must implement Migrations\\Db\\Adapter\\WrapperInterface', | ||
$class | ||
)); | ||
} | ||
$this->wrappers[$name] = $class; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* Get a wrapper class by name. | ||
* | ||
* @param string $name Name | ||
* @throws \RuntimeException | ||
* @return class-string<\Migrations\Db\Adapter\WrapperInterface> | ||
*/ | ||
protected function getWrapperClass(string $name): string | ||
{ | ||
if (empty($this->wrappers[$name])) { | ||
throw new RuntimeException(sprintf( | ||
'Wrapper "%s" has not been registered', | ||
$name | ||
)); | ||
} | ||
|
||
return $this->wrappers[$name]; | ||
} | ||
|
||
/** | ||
* Get a wrapper instance by name. | ||
* | ||
* @param string $name Name | ||
* @param \Migrations\Db\Adapter\AdapterInterface $adapter Adapter | ||
* @return \Migrations\Db\Adapter\WrapperInterface | ||
*/ | ||
public function getWrapper(string $name, AdapterInterface $adapter): WrapperInterface | ||
{ | ||
$class = $this->getWrapperClass($name); | ||
|
||
return new $class($adapter); | ||
} | ||
} |
Oops, something went wrong.