1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Bernard\Driver\Doctrine\Command; |
4
|
|
|
|
5
|
|
|
use Bernard\Driver\Doctrine\MessagesSchema; |
6
|
|
|
use Doctrine\DBAL\Connection; |
7
|
|
|
use Doctrine\DBAL\Schema\Schema; |
8
|
|
|
use Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer as Synchronizer; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
|
14
|
|
|
abstract class AbstractCommand extends Command |
15
|
|
|
{ |
16
|
8 |
|
public function __construct($name) |
17
|
|
|
{ |
18
|
8 |
|
parent::__construct('bernard:doctrine:'.$name); |
19
|
8 |
|
} |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* {@inheritdoc} |
23
|
|
|
*/ |
24
|
8 |
|
public function configure() |
25
|
|
|
{ |
26
|
8 |
|
$this->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Output generated SQL statements instead of applying them'); |
27
|
8 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* {@inheritdoc} |
31
|
|
|
*/ |
32
|
8 |
|
public function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
8 |
|
$schema = new Schema(); |
35
|
8 |
|
MessagesSchema::create($schema); |
36
|
8 |
|
$sync = $this->getSynchronizer($this->getHelper('connection')->getConnection()); |
|
|
|
|
37
|
|
|
|
38
|
8 |
|
if ($input->getOption('dump-sql')) { |
39
|
4 |
|
$output->writeln(implode(';'.PHP_EOL, $this->getSql($sync, $schema)).';'); |
40
|
|
|
|
41
|
4 |
|
return; |
42
|
|
|
} |
43
|
|
|
|
44
|
4 |
|
$output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.'.PHP_EOL); |
45
|
4 |
|
$output->writeln('Applying database schema changes...'); |
46
|
4 |
|
$this->applySql($sync, $schema); |
47
|
4 |
|
$output->writeln('Schema changes applied successfully!'); |
48
|
4 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @return \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer |
52
|
|
|
*/ |
53
|
2 |
|
protected function getSynchronizer(Connection $connection) |
54
|
|
|
{ |
55
|
2 |
|
return new Synchronizer($connection); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync |
60
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
abstract protected function getSql(Synchronizer $sync, Schema $schema); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync |
68
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema |
69
|
|
|
*/ |
70
|
|
|
abstract protected function applySql(Synchronizer $sync, Schema $schema); |
71
|
|
|
} |
72
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: