Skip to content

Commit 9d365d0

Browse files
authored
Merge pull request #348 from goetas/document-container-drop
Document that container aware migrations are not special anymore
2 parents 4bf6aa3 + b4685e0 commit 9d365d0

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

Resources/doc/index.rst

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,67 @@ You can skip single migrations by explicitly adding them to the ``migration_vers
229229

230230
Doctrine will then assume that this migration has already been run and will ignore it.
231231

232+
Migration Dependencies
233+
----------------------
234+
235+
Migrations can have dependencies on external services (such as geolocation, mailer, data processing services...) that
236+
can be used to have more powerful migrations. Those dependencies are not automatically injected into your migrations
237+
but need to be injected using custom migrations factories.
238+
239+
Here is an example on how to inject the service container into your migrations:
240+
241+
.. configuration-block::
242+
243+
.. code-block:: yaml
244+
245+
# config/packages/doctrine_migrations.yml
246+
doctrine_migrations:
247+
services:
248+
'Doctrine\Migrations\Version\MigrationFactory': 'App\Migrations\Factory\MigrationFactoryDecorator'
249+
250+
# config/services.yml
251+
services:
252+
Doctrine\Migrations\Version\DbalMigrationFactory: ~
253+
App\Migrations\Factory\MigrationFactoryDecorator:
254+
decorates: Doctrine\Migrations\Version\DbalMigrationFactory
255+
arguments: ['@App\Migrations\Factory\MigrationFactoryDecorator.inner', '@service_container']
256+
257+
258+
.. code-block:: php
259+
260+
declare(strict_types=1);
261+
262+
namespace App\Migrations\Factory;
263+
264+
use Doctrine\Migrations\AbstractMigration;
265+
use Doctrine\Migrations\Version\MigrationFactory;
266+
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
267+
use Symfony\Component\DependencyInjection\ContainerInterface;
268+
269+
class MigrationFactoryDecorator implements MigrationFactory
270+
{
271+
private $migrationFactory;
272+
private $container;
273+
274+
public function __construct(MigrationFactory $migrationFactory, ContainerInterface $container)
275+
{
276+
$this->migrationFactory = $migrationFactory;
277+
$this->container = $container;
278+
}
279+
280+
public function createVersion(string $migrationClassName): AbstractMigration
281+
{
282+
$instance = $this->migrationFactory->createVersion($migrationClassName);
283+
284+
if ($instance instanceof ContainerAwareInterface) {
285+
$instance->setContainer($this->container);
286+
}
287+
288+
return $instance;
289+
}
290+
}
291+
292+
232293
Generating Migrations Automatically
233294
-----------------------------------
234295

UPGRADE.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,6 @@ doctrine_migrations:
7272
Upgrading this bundle to `3.0` will also update the `doctrine/migrations` library to the version `3.0`.
7373
Backward incompatible changes in `doctrine/migrations` 3.0
7474
are documented in the dedicated [UPGRADE](https://github.com/doctrine/migrations/blob/3.0.x/UPGRADE.md) document.
75+
76+
- The container is not automatically injected anymore when a migration implements `ContainerAwareInterface`. Custom
77+
migration factories should be used to inject additional dependencies into migrations.

0 commit comments

Comments
 (0)