|
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
|
|
|
* Apply migration |
|
48
|
|
|
*/ |
|
49
|
|
|
public function applyAction() |
|
50
|
|
|
{ |
|
51
|
|
|
$version = $this->getRequest()->getParam('version'); |
|
|
|
|
|
|
52
|
|
|
$force = $this->getRequest()->getParam('force'); |
|
|
|
|
|
|
53
|
|
|
$down = $this->getRequest()->getParam('down'); |
|
|
|
|
|
|
54
|
|
|
$fake = $this->getRequest()->getParam('fake'); |
|
|
|
|
|
|
55
|
|
|
|
|
56
|
|
|
if (is_null($version) && $force) { |
|
57
|
|
|
return "Can't force migration apply without migration version explicitly set.\n"; |
|
58
|
|
|
} |
|
59
|
|
|
if (is_null($version) && $fake) { |
|
60
|
|
|
return "Can't fake migration apply without migration version explicitly set.\n"; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$this->getMigration()->migrate($version, $force, $down, $fake); |
|
64
|
|
|
return "Migrations applied!\n"; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Generate new migration skeleton class |
|
69
|
|
|
*/ |
|
70
|
|
|
public function generateAction() |
|
71
|
|
|
{ |
|
72
|
|
|
$classPath = $this->getGenerator()->generate(); |
|
73
|
|
|
|
|
74
|
|
|
return sprintf("Generated skeleton class @ %s\n", realpath($classPath)); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return Migration |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getMigration() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->migration; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return Generator |
|
87
|
|
|
*/ |
|
88
|
|
|
public function getGenerator() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->generator; |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
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: