forked from uvdesk/community-skeleton
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PopulateDatabase.php
73 lines (58 loc) · 2.06 KB
/
PopulateDatabase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<?php
namespace Webkul\UVDesk\Setup\Command;
use Doctrine\DBAL\DBALException;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Console\Input\ArrayInput as ConsoleOptions;
class PopulateDatabase extends Command
{
private $container;
private $entityManager;
private $questionHelper;
public function __construct(ContainerInterface $container, EntityManagerInterface $entityManager)
{
$this->container = $container;
$this->entityManager = $entityManager;
parent::__construct();
}
protected function configure()
{
$this->setName('uvdesk-wizard:populate-database');
$this->setDescription('Migrate your database to the latest schema version.');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Check 1: Verify database connection
$database = $this->entityManager->getConnection()->getDatabase();
if ($this->isDatabaseConfigurationValid()) {
$this->runDataFixtures(new NullOutput());
}
return 0;
}
private function runDataFixtures(OutputInterface $consoleOutput)
{
$command = $this->getApplication()->find('doctrine:fixtures:load');
$commandOptions = new ConsoleOptions([
'command' => 'fixtures:load',
'--append' => true,
]);
$command->run($commandOptions, $consoleOutput);
return $this;
}
private function isDatabaseConfigurationValid()
{
$databaseConnection = $this->entityManager->getConnection();
if (false === $databaseConnection->isConnected()) {
try {
$databaseConnection->connect();
} catch (DBALException $e) {
return false;
}
}
return true;
}
}