robmorgan/phinx — pull request #255 by cyrusboadway (2014-05-20 16:12:34+00:00)
Title: Adapter Factories, Custom Adapters

This alters how adapters are instantiated, separating the implementation of the Environment's adapter management from the individual adapter instantiation protocols. The default adapters are registered with the Environment on construction via `Environment::registerAdapter(string $adapterName, callable $adapterFactoryClosure)`, but `::registerAdapter(...)` is public, allowing users to register their own custom adapters, without altering the source of Phinx (similar to what was proposed in [#204](https://github.com/robmorgan/phinx/pull/204) ).

The closures accept the Environment as a parameter so that they can configure themselves as they are requested, and must return an object which implements `Evisors\Db\Adapter\AdapterInterface`:

```php
$adapterFactory = function(Phinx\Migration\Manager\Environment $env) {
// Configure my adapter with the env and return.
$adapter = new \My\Adapter(...);
return $adapter;
}
```

For PHP >= 5.4, the factory closure can [import other configuration](http://www.php.net/manual/en/functions.anonymous.php) parameters into it's scope with the `use` statement for further adapter configuration:

```php
$adapterFactory = function() use ($notEnv) { // 5.4 only
return new \My\Adapter($notEnv);
}
```

A more OOP implementation of this would involve an AdapterFactory classes, but that seems like overkill.