ContainerAwareLoader.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\Doctrine\DataFixtures;
  11. use Doctrine\Common\DataFixtures\FixtureInterface;
  12. use Doctrine\Common\DataFixtures\Loader;
  13. use Symfony\Component\DependencyInjection\ContainerAwareInterface;
  14. use Symfony\Component\DependencyInjection\ContainerInterface;
  15. /**
  16. * Doctrine data fixtures loader that injects the service container into
  17. * fixture objects that implement ContainerAwareInterface.
  18. *
  19. * Note: Use of this class requires the Doctrine data fixtures extension, which
  20. * is a suggested dependency for Symfony.
  21. */
  22. class ContainerAwareLoader extends Loader
  23. {
  24. /**
  25. * @var ContainerInterface
  26. */
  27. private $container;
  28. /**
  29. * Constructor.
  30. *
  31. * @param ContainerInterface $container A ContainerInterface instance
  32. */
  33. public function __construct(ContainerInterface $container)
  34. {
  35. $this->container = $container;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function addFixture(FixtureInterface $fixture)
  41. {
  42. if ($fixture instanceof ContainerAwareInterface) {
  43. $fixture->setContainer($this->container);
  44. }
  45. parent::addFixture($fixture);
  46. }
  47. }