symfony-doge /
veslo
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Veslo project <https://github.com/symfony-doge/veslo>. |
||
| 5 | * |
||
| 6 | * (C) 2019 Pavel Petrov <[email protected]>. |
||
| 7 | * |
||
| 8 | * For the full copyright and license information, please view the LICENSE |
||
| 9 | * file that was distributed with this source code. |
||
| 10 | * |
||
| 11 | * @license https://opensource.org/licenses/GPL-3.0 GPL-3.0 |
||
| 12 | */ |
||
| 13 | |||
| 14 | declare(strict_types=1); |
||
| 15 | |||
| 16 | namespace Veslo\AppBundle\Fixture; |
||
| 17 | |||
| 18 | use Doctrine\Bundle\FixturesBundle\Fixture; |
||
| 19 | use Doctrine\Common\Persistence\ObjectManager; |
||
| 20 | use Nelmio\Alice\FileLoaderInterface; |
||
| 21 | use Symfony\Component\Config\FileLocatorInterface; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Fixture that loads entities from file |
||
| 25 | */ |
||
| 26 | class FileFixture extends Fixture |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * Locates file by physical or logical '@' path |
||
| 30 | * |
||
| 31 | * @var FileLocatorInterface |
||
| 32 | */ |
||
| 33 | private $fileLocator; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Loads a fixture files |
||
| 37 | * |
||
| 38 | * @var FileLoaderInterface |
||
| 39 | */ |
||
| 40 | private $fixtureLoader; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * Fixture path to load |
||
| 44 | * |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $fixturePath; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * FileFixture constructor. |
||
| 51 | * |
||
| 52 | * @param FileLocatorInterface $fileLocator Locates file by physical or logic '@' path |
||
| 53 | * @param FileLoaderInterface $fixtureLoader Loads a fixture files |
||
| 54 | * @param string $fixturePath Fixture path to load |
||
| 55 | */ |
||
| 56 | public function __construct( |
||
| 57 | FileLocatorInterface $fileLocator, |
||
| 58 | FileLoaderInterface $fixtureLoader, |
||
| 59 | string $fixturePath |
||
| 60 | ) { |
||
| 61 | $this->fileLocator = $fileLocator; |
||
| 62 | $this->fixtureLoader = $fixtureLoader; |
||
| 63 | $this->fixturePath = $fixturePath; |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * {@inheritdoc} |
||
| 68 | */ |
||
| 69 | public function load(ObjectManager $objectManager) |
||
| 70 | { |
||
| 71 | $fixtureRealPath = $this->fileLocator->locate($this->fixturePath); |
||
| 72 | $objectSet = $this->fixtureLoader->loadFile($fixtureRealPath); |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 73 | $entities = $objectSet->getObjects(); |
||
| 74 | |||
| 75 | foreach ($entities as $entity) { |
||
| 76 | $objectManager->persist($entity); |
||
| 77 | } |
||
| 78 | |||
| 79 | $objectManager->flush(); |
||
| 80 | } |
||
| 81 | } |
||
| 82 |