|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace DoctrineDataFixtureModule\Command; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\DataFixtures\Executor\AbstractExecutor; |
|
6
|
|
|
use Doctrine\Common\DataFixtures\Loader; |
|
7
|
|
|
use Doctrine\ORM\EntityManager; |
|
8
|
|
|
use Symfony\Component\Console\Command\Command; |
|
9
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
10
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
12
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
|
13
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
|
14
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* Command for generate migration classes by comparing your current database schema |
|
18
|
|
|
* to your mapping information. |
|
19
|
|
|
* |
|
20
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
|
21
|
|
|
* @link www.doctrine-project.org |
|
22
|
|
|
* @since 2.0 |
|
23
|
|
|
* @author Jonathan Wage <[email protected]> |
|
24
|
|
|
* @author Rob van der Lee <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class ImportCommand extends Command |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array |
|
30
|
|
|
*/ |
|
31
|
|
|
protected $paths; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var EntityManager |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $entityManager; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* The Zend ServiceManager |
|
40
|
|
|
* @var ServiceLocatorInterface |
|
41
|
|
|
*/ |
|
42
|
|
|
protected $serviceLocator; |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @var int |
|
46
|
|
|
*/ |
|
47
|
|
|
const PURGE_MODE_TRUNCATE = 2; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* ImportCommand constructor. |
|
51
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
52
|
|
|
*/ |
|
53
|
|
|
public function __construct(ServiceLocatorInterface $serviceLocator) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->setServiceLocator($serviceLocator); |
|
56
|
|
|
|
|
57
|
|
|
parent::__construct(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Configure the command |
|
62
|
|
|
*/ |
|
63
|
|
|
protected function configure() |
|
64
|
|
|
{ |
|
65
|
|
|
parent::configure(); |
|
66
|
|
|
|
|
67
|
|
|
$this->setName('data-fixture:import') |
|
68
|
|
|
->setDescription('Import Data Fixtures') |
|
69
|
|
|
->setHelp('The import command Imports data-fixtures') |
|
70
|
|
|
->addOption('append', null, InputOption::VALUE_NONE, 'Append data to existing data.') |
|
71
|
|
|
->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Truncate tables before inserting data'); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param InputInterface $input |
|
76
|
|
|
* @param OutputInterface $output |
|
77
|
|
|
* @return int|null|void |
|
78
|
|
|
*/ |
|
79
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
80
|
|
|
{ |
|
81
|
|
|
/** @var array $config */ |
|
82
|
|
|
$config = $this->getServiceLocator()->get('config'); |
|
83
|
|
|
/** @var Loader $loader */ |
|
84
|
|
|
$loader = new Loader(); |
|
85
|
|
|
/** @var ORMPurger $purger */ |
|
86
|
|
|
$purger = new ORMPurger(); |
|
87
|
|
|
|
|
88
|
|
|
// Accept option purge-with-truncate |
|
89
|
|
|
if ($input->getOption('purge-with-truncate')) { |
|
90
|
|
|
$purger->setPurgeMode(self::PURGE_MODE_TRUNCATE); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
if ( ! isset($config['rvdlee']['doctrine-data-fixture']['executor'])) { |
|
94
|
|
|
/** @var ORMExecutor $executor */ |
|
95
|
|
|
$executor = new ORMExecutor($this->getEntityManager(), $purger); |
|
96
|
|
|
} else { |
|
97
|
|
|
/** @var AbstractExecutor $executor */ |
|
98
|
|
|
$executor = $this->getServiceLocator()->get($config['rvdlee']['doctrine-data-fixture']['executor']); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
foreach ($this->getPaths() as $key => $value) { |
|
102
|
|
|
$loader->loadFromDirectory($value); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$executor->execute($loader->getFixtures(), $input->getOption('append')); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @return array |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getPaths(): array |
|
112
|
|
|
{ |
|
113
|
|
|
return $this->paths; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param array $paths |
|
118
|
|
|
*/ |
|
119
|
|
|
public function setPaths(array $paths): void |
|
120
|
|
|
{ |
|
121
|
|
|
$this->paths = $paths; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return EntityManager |
|
126
|
|
|
*/ |
|
127
|
|
|
public function getEntityManager(): EntityManager |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->entityManager; |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @param EntityManager $entityManager |
|
134
|
|
|
*/ |
|
135
|
|
|
public function setEntityManager(EntityManager $entityManager): void |
|
136
|
|
|
{ |
|
137
|
|
|
$this->entityManager = $entityManager; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @return ServiceLocatorInterface |
|
142
|
|
|
*/ |
|
143
|
|
|
public function getServiceLocator(): ServiceLocatorInterface |
|
144
|
|
|
{ |
|
145
|
|
|
return $this->serviceLocator; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
/** |
|
149
|
|
|
* @param ServiceLocatorInterface $serviceLocator |
|
150
|
|
|
*/ |
|
151
|
|
|
public function setServiceLocator(ServiceLocatorInterface $serviceLocator): void |
|
152
|
|
|
{ |
|
153
|
|
|
$this->serviceLocator = $serviceLocator; |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|