|
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
|
|
|
/** |
|
15
|
|
|
* @package Bernard |
|
16
|
|
|
*/ |
|
17
|
|
|
abstract class AbstractCommand extends Command |
|
18
|
|
|
{ |
|
19
|
|
|
public function __construct($name) |
|
20
|
|
|
{ |
|
21
|
|
|
parent::__construct('bernard:doctrine:' . $name); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
|
|
public function configure() |
|
28
|
|
|
{ |
|
29
|
|
|
$this->addOption('dump-sql', null, InputOption::VALUE_NONE, 'Output generated SQL statements instead of applying them'); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* {@inheritDoc} |
|
34
|
|
|
*/ |
|
35
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
|
36
|
|
|
{ |
|
37
|
|
|
$schema = new Schema; |
|
38
|
|
|
MessagesSchema::create($schema); |
|
39
|
|
|
$sync = $this->getSynchronizer($this->getHelper('connection')->getConnection()); |
|
|
|
|
|
|
40
|
|
|
|
|
41
|
|
|
if ($input->getOption('dump-sql')) { |
|
42
|
|
|
$output->writeln(implode(';' . PHP_EOL, $this->getSql($sync, $schema)) . ';'); |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
$output->writeln('<comment>ATTENTION</comment>: This operation should not be executed in a production environment.' . PHP_EOL); |
|
47
|
|
|
$output->writeln('Applying database schema changes...'); |
|
48
|
|
|
$this->applySql($sync, $schema); |
|
49
|
|
|
$output->writeln('Schema changes applied successfully!'); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer |
|
54
|
|
|
*/ |
|
55
|
|
|
protected function getSynchronizer(Connection $connection) |
|
56
|
|
|
{ |
|
57
|
|
|
return new Synchronizer($connection); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync |
|
62
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema |
|
63
|
|
|
* @return array |
|
64
|
|
|
*/ |
|
65
|
|
|
abstract protected function getSql(Synchronizer $sync, Schema $schema); |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param \Doctrine\DBAL\Schema\Synchronizer\SingleDatabaseSynchronizer $sync |
|
69
|
|
|
* @param \Doctrine\DBAL\Schema\Schema $schema |
|
70
|
|
|
*/ |
|
71
|
|
|
abstract protected function applySql(Synchronizer $sync, Schema $schema); |
|
72
|
|
|
} |
|
73
|
|
|
|
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: