1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RDV\Bundle\MigrationBundle\Twig; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Persistence\ManagerRegistry; |
6
|
|
|
use Doctrine\DBAL\Platforms\AbstractPlatform; |
7
|
|
|
use Doctrine\DBAL\Schema\Column; |
8
|
|
|
use Doctrine\DBAL\Types\Type; |
9
|
|
|
|
10
|
|
|
class SchemaDumperExtension extends \Twig_Extension |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ManagerRegistry |
14
|
|
|
*/ |
15
|
|
|
protected $managerRegistry; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var AbstractPlatform |
19
|
|
|
*/ |
20
|
|
|
protected $platform; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var Column |
24
|
|
|
*/ |
25
|
|
|
protected $defaultColumn; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $defaultColumnOptions = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var array |
34
|
|
|
*/ |
35
|
|
|
protected $optionNames = [ |
36
|
|
|
'default', |
37
|
|
|
'notnull', |
38
|
|
|
'length', |
39
|
|
|
'precision', |
40
|
|
|
'scale', |
41
|
|
|
'fixed', |
42
|
|
|
'unsigned', |
43
|
|
|
'autoincrement' |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
|
|
public function getName() |
50
|
|
|
{ |
51
|
|
|
return 'schema_dumper_extension'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param ManagerRegistry $managerRegistry |
56
|
|
|
*/ |
57
|
|
|
public function __construct(ManagerRegistry $managerRegistry) |
58
|
|
|
{ |
59
|
|
|
$this->managerRegistry = $managerRegistry; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritdoc} |
64
|
|
|
*/ |
65
|
|
|
public function getFunctions() |
66
|
|
|
{ |
67
|
|
|
return array( |
|
|
|
|
68
|
|
|
'rdv_migration_get_schema_column_options' => new \Twig_Function_Method($this, 'getColumnOptions'), |
69
|
|
|
); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param Column $column |
74
|
|
|
* @return array |
75
|
|
|
*/ |
76
|
|
|
public function getColumnOptions(Column $column) |
77
|
|
|
{ |
78
|
|
|
$defaultOptions = $this->getDefaultOptions(); |
79
|
|
|
$platform = $this->getPlatform(); |
80
|
|
|
$options = []; |
81
|
|
|
|
82
|
|
|
foreach ($this->optionNames as $optionName) { |
83
|
|
|
$value = $this->getColumnOption($column, $optionName); |
84
|
|
|
if ($value !== $defaultOptions[$optionName]) { |
85
|
|
|
$options[$optionName] = $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
$comment = $column->getComment(); |
90
|
|
|
if ($platform && $platform->isCommentedDoctrineType($column->getType())) { |
91
|
|
|
$comment .= $platform->getDoctrineTypeComment($column->getType()); |
92
|
|
|
} |
93
|
|
|
if (!empty($comment)) { |
94
|
|
|
$options['comment'] = $comment; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $options; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Column $column |
102
|
|
|
* @param string $optionName |
103
|
|
|
* @return mixed |
104
|
|
|
*/ |
105
|
|
|
protected function getColumnOption(Column $column, $optionName) |
106
|
|
|
{ |
107
|
|
|
$method = "get" . $optionName; |
108
|
|
|
|
109
|
|
|
return $column->$method(); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return AbstractPlatform |
114
|
|
|
*/ |
115
|
|
|
protected function getPlatform() |
116
|
|
|
{ |
117
|
|
|
if (!$this->platform) { |
118
|
|
|
$this->platform = $this->managerRegistry->getConnection()->getDatabasePlatform(); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
return $this->platform; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @return array |
126
|
|
|
*/ |
127
|
|
|
protected function getDefaultOptions() |
128
|
|
|
{ |
129
|
|
|
if (!$this->defaultColumn) { |
130
|
|
|
$this->defaultColumn = new Column('_template_', Type::getType(Type::STRING)); |
131
|
|
|
} |
132
|
|
|
if (!$this->defaultColumnOptions) { |
|
|
|
|
133
|
|
|
foreach ($this->optionNames as $optionName) { |
134
|
|
|
$this->defaultColumnOptions[$optionName] = $this->getColumnOption($this->defaultColumn, $optionName); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
return $this->defaultColumnOptions; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_function
expects aPost
object, and outputs the author of the post. The base classPost
returns a simple string and outputting a simple string will work just fine. However, the child classBlogPost
which is a sub-type ofPost
instead decided to return anobject
, and is therefore violating the SOLID principles. If aBlogPost
were passed tomy_function
, PHP would not complain, but ultimately fail when executing thestrtoupper
call in its body.