1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace T4web\Migrations\Controller; |
4
|
|
|
|
5
|
|
|
use Zend\Mvc\Controller\AbstractActionController; |
6
|
|
|
use T4web\Migrations\Service\Migration; |
7
|
|
|
use T4web\Migrations\Service\Generator; |
8
|
|
|
use Zend\Console\Request as ConsoleRequest; |
9
|
|
|
use Zend\Mvc\MvcEvent; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Migration commands controller |
13
|
|
|
*/ |
14
|
|
|
class MigrateController extends AbstractActionController |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Migration |
18
|
|
|
*/ |
19
|
|
|
protected $migration; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var Generator |
23
|
|
|
*/ |
24
|
|
|
protected $generator; |
25
|
|
|
|
26
|
|
|
public function onDispatch(MvcEvent $e) |
27
|
|
|
{ |
28
|
|
|
if (!$this->getRequest() instanceof ConsoleRequest) { |
29
|
|
|
throw new \RuntimeException('You can only use this action from a console!'); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
parent::onDispatch($e); |
33
|
|
|
} |
34
|
|
|
/** |
35
|
|
|
* MigrateController constructor. |
36
|
|
|
* |
37
|
|
|
* @param Migration $migration |
38
|
|
|
* @param Generator $generator |
39
|
|
|
*/ |
40
|
|
|
public function __construct(Migration $migration, Generator $generator) |
41
|
|
|
{ |
42
|
|
|
$this->migration = $migration; |
43
|
|
|
$this->generator = $generator; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* List migrations - not applied by default, all with 'all' flag. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function listAction() |
52
|
|
|
{ |
53
|
|
|
$migrations = $this->getMigration()->getMigrationClasses($this->getRequest()->getParam('all')); |
|
|
|
|
54
|
|
|
$list = []; |
55
|
|
|
foreach ($migrations as $m) { |
56
|
|
|
$list[] = sprintf("%s %s - %s", $m['applied'] ? '-' : '+', $m['version'], $m['description']); |
57
|
|
|
} |
58
|
|
|
return (empty($list) ? 'No migrations available.' : implode("\n", $list)) . "\n"; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Apply migration |
63
|
|
|
*/ |
64
|
|
|
public function applyAction() |
65
|
|
|
{ |
66
|
|
|
$version = $this->getRequest()->getParam('version'); |
|
|
|
|
67
|
|
|
$force = $this->getRequest()->getParam('force'); |
|
|
|
|
68
|
|
|
$down = $this->getRequest()->getParam('down'); |
|
|
|
|
69
|
|
|
$fake = $this->getRequest()->getParam('fake'); |
|
|
|
|
70
|
|
|
|
71
|
|
|
if (is_null($version) && $force) { |
72
|
|
|
return "Can't force migration apply without migration version explicitly set.\n"; |
73
|
|
|
} |
74
|
|
|
if (is_null($version) && $fake) { |
75
|
|
|
return "Can't fake migration apply without migration version explicitly set.\n"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$this->getMigration()->migrate($version, $force, $down, $fake); |
79
|
|
|
return "Migrations applied!\n"; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Generate new migration skeleton class |
84
|
|
|
*/ |
85
|
|
|
public function generateAction() |
86
|
|
|
{ |
87
|
|
|
$classPath = $this->getGenerator()->generate(); |
88
|
|
|
|
89
|
|
|
return sprintf("Generated skeleton class @ %s\n", realpath($classPath)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @return Migration |
94
|
|
|
*/ |
95
|
|
|
public function getMigration() |
96
|
|
|
{ |
97
|
|
|
return $this->migration; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @return Generator |
102
|
|
|
*/ |
103
|
|
|
public function getGenerator() |
104
|
|
|
{ |
105
|
|
|
return $this->generator; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
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: