1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
14
|
|
|
* |
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
16
|
|
|
* and is licensed under the LGPL. For more information, see |
17
|
|
|
* <http://www.doctrine-project.org>. |
18
|
|
|
*/ |
19
|
|
|
|
20
|
|
|
namespace DoctrineFixturesModule\Command; |
21
|
|
|
|
22
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
23
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
24
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
25
|
|
|
use Symfony\Component\Console\Command\Command; |
26
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
27
|
|
|
use Symfony\Component\Console\Input\InputOption; |
28
|
|
|
use Doctrine\ORM\Tools\SchemaTool; |
29
|
|
|
use Doctrine\Common\DataFixtures\Executor\ORMExecutor; |
30
|
|
|
use Doctrine\Common\DataFixtures\Purger\ORMPurger; |
31
|
|
|
use DoctrineFixturesModule\Loader\ServiceLocatorAwareLoader; |
32
|
|
|
use Zend\ServiceManager\ServiceLocatorInterface; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Command for generate migration classes by comparing your current database schema |
36
|
|
|
* to your mapping information. |
37
|
|
|
* |
38
|
|
|
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL |
39
|
|
|
* @link www.doctrine-project.org |
40
|
|
|
* @since 2.0 |
41
|
|
|
* @author Jonathan Wage <[email protected]> |
42
|
|
|
*/ |
43
|
|
|
class LoadCommand extends Command |
44
|
|
|
{ |
45
|
|
|
protected $paths; |
46
|
|
|
|
47
|
|
|
protected $em; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Service Locator instance |
51
|
|
|
* @var \Zend\ServiceManager\ServiceLocatorInterface |
52
|
|
|
*/ |
53
|
|
|
protected $serviceLocator; |
54
|
|
|
|
55
|
|
|
const PURGE_MODE_TRUNCATE = 2; |
56
|
|
|
|
57
|
|
|
public function __construct(ServiceLocatorInterface $serviceLocator) |
58
|
|
|
{ |
59
|
|
|
$this->serviceLocator = $serviceLocator; |
60
|
|
|
parent::__construct(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function configure() |
64
|
|
|
{ |
65
|
|
|
$this->setName('fixtures:load') |
66
|
|
|
->setDescription('Load data fixtures to your database.') |
67
|
|
|
->addOption('fixtures', null, InputOption::VALUE_OPTIONAL | InputOption::VALUE_IS_ARRAY, 'The directory or file to load data fixtures from.') |
68
|
|
|
->addOption('append', null, InputOption::VALUE_NONE, 'Append the data fixtures instead of deleting all data from the database first.') |
69
|
|
|
->addOption('em', null, InputOption::VALUE_REQUIRED, 'The entity manager to use for this command.') |
70
|
|
|
->addOption('purge-with-truncate', null, InputOption::VALUE_NONE, 'Purge data by using a database-level TRUNCATE statement') |
71
|
|
|
->setHelp(<<<EOT |
72
|
|
|
The <info>fixtures:load</info> command loads data fixtures from your bundles: |
73
|
|
|
|
74
|
|
|
<info>fixtures:load</info> |
75
|
|
|
|
76
|
|
|
You can also optionally specify the path to fixtures with the <info>--fixtures</info> option: |
77
|
|
|
|
78
|
|
|
<info>fixtures:load --fixtures=/path/to/fixtures1 --fixtures=/path/to/fixtures2</info> |
79
|
|
|
|
80
|
|
|
If you want to append the fixtures instead of flushing the database first you can use the <info>--append</info> option: |
81
|
|
|
|
82
|
|
|
<info>fixtures:load --append</info> |
83
|
|
|
|
84
|
|
|
By default Doctrine Data Fixtures uses DELETE statements to drop the existing rows from |
85
|
|
|
the database. If you want to use a TRUNCATE statement instead you can use the <info>--purge-with-truncate</info> flag: |
86
|
|
|
|
87
|
|
|
<info>fixtures:load --purge-with-truncate</info> |
88
|
|
|
EOT |
89
|
|
|
); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
93
|
|
|
{ |
94
|
|
|
$em = $this->em; |
95
|
|
|
|
96
|
|
|
if (!$input->getOption('no-interaction')) { |
97
|
|
|
$helper = $this->getHelper('question'); |
98
|
|
|
$question |
99
|
|
|
= new ConfirmationQuestion('Careful, database will be purged. Do you want to continue Y/N?', |
100
|
|
|
false); |
101
|
|
|
|
102
|
|
|
if (!$helper->ask($input, $output, $question)) { |
103
|
|
|
return; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$dirOrFile = $input->getOption('fixtures'); |
108
|
|
|
if ($dirOrFile) { |
109
|
|
|
$paths = is_array($dirOrFile) ? $dirOrFile : array($dirOrFile); |
110
|
|
|
} else { |
111
|
|
|
$paths = $this->paths; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$loader = new ServiceLocatorAwareLoader($this->serviceLocator); |
115
|
|
|
foreach ($paths as $path) { |
116
|
|
|
if (is_dir($path)) { |
117
|
|
|
$loader->loadFromDirectory($path); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
$fixtures = $loader->getFixtures(); |
121
|
|
|
if (!$fixtures) { |
|
|
|
|
122
|
|
|
throw new \InvalidArgumentException( |
123
|
|
|
sprintf('Could not find any fixtures to load in: %s', "\n\n- ".implode("\n- ", $paths)) |
124
|
|
|
); |
125
|
|
|
} |
126
|
|
|
$purger = new ORMPurger($em); |
127
|
|
|
$purger->setPurgeMode($input->getOption('purge-with-truncate') ? ORMPurger::PURGE_MODE_TRUNCATE : ORMPurger::PURGE_MODE_DELETE); |
128
|
|
|
$executor = new ORMExecutor($em, $purger); |
129
|
|
|
$executor->setLogger(function($message) use ($output) { |
130
|
|
|
$output->writeln(sprintf(' <comment>></comment> <info>%s</info>', $message)); |
131
|
|
|
}); |
132
|
|
|
$executor->execute($fixtures, $input->getOption('append')); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
public function setPath($paths) |
136
|
|
|
{ |
137
|
|
|
$this->paths = $paths; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function setEntityManager($em) |
141
|
|
|
{ |
142
|
|
|
$this->em = $em; |
143
|
|
|
} |
144
|
|
|
} |
145
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)
or! empty(...)
instead.