|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rentgen\DependencyInjection; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
|
6
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
|
7
|
|
|
use Symfony\Component\DependencyInjection\Extension\ExtensionInterface; |
|
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
|
9
|
|
|
use Symfony\Component\Config\FileLocator; |
|
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
11
|
|
|
use Rentgen\Schema\Factory; |
|
12
|
|
|
|
|
13
|
|
|
class RentgenExtension implements ExtensionInterface |
|
14
|
|
|
{ |
|
15
|
|
|
public function load(array $configs, ContainerBuilder $container) |
|
16
|
|
|
{ |
|
17
|
|
|
foreach ($configs as $config) { |
|
18
|
|
|
if ($this->isConnectionConfig($config)) { |
|
19
|
|
|
$connectionConfig = $config; |
|
20
|
|
|
} |
|
21
|
|
|
} |
|
22
|
|
|
if ($container->hasParameter('connection_config')) { |
|
23
|
|
|
$connectionConfig = $container->getParameter('connection_config'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
$this->defineParameters($container); |
|
27
|
|
|
|
|
28
|
|
|
$definition = new Definition('Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher'); |
|
29
|
|
|
$definition->setArguments(array(new Reference('service_container'))); |
|
30
|
|
|
$container->setDefinition('event_dispatcher', $definition); |
|
31
|
|
|
|
|
32
|
|
|
$definition = new Definition('Rentgen\Schema\Manipulation'); |
|
33
|
|
|
$definition->setArguments(array(new Reference('service_container'))); |
|
34
|
|
|
$container->setDefinition('rentgen.schema.manipulation', $definition); |
|
35
|
|
|
|
|
36
|
|
|
$definition = new Definition('Rentgen\Schema\Info'); |
|
37
|
|
|
$definition->setArguments(array(new Reference('service_container'))); |
|
38
|
|
|
$container->setDefinition('rentgen.schema.info', $definition); |
|
39
|
|
|
|
|
40
|
|
|
if (!isset($connectionConfig)) { |
|
41
|
|
|
$fileLocator = new FileLocator(getcwd()); |
|
42
|
|
|
try { |
|
43
|
|
|
$configFile = $fileLocator->locate('rentgen.yml'); |
|
|
|
|
|
|
44
|
|
|
$content = file_get_contents('rentgen.yml'); |
|
45
|
|
|
$config = Yaml::parse($content); |
|
46
|
|
|
$connectionConfig = $config; |
|
47
|
|
|
} catch (\InvalidArgumentException $e) { |
|
48
|
|
|
$connectionConfig['environments']['dev'] = array( |
|
|
|
|
|
|
49
|
|
|
'adapter' => 'pgsql', |
|
50
|
|
|
'host' => 'localhost', |
|
51
|
|
|
'database' => null, |
|
52
|
|
|
'username' => 'postgres', |
|
53
|
|
|
'password' => '', |
|
54
|
|
|
'port' => 5432, |
|
55
|
|
|
); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
$definition = new Definition('Rentgen\Database\Connection\ConnectionConfig'); |
|
60
|
|
|
$definition->setArguments(array($connectionConfig['environments'])); |
|
61
|
|
|
$container->setDefinition('connection_config', $definition); |
|
62
|
|
|
|
|
63
|
|
|
$definition = new Definition('Rentgen\Database\Connection\Connection'); |
|
64
|
|
|
$definition->setArguments(array(new Reference('connection_config'))); |
|
65
|
|
|
$container->setDefinition('connection', $definition); |
|
66
|
|
|
|
|
67
|
|
|
$this->connection = $container->getDefinition('connection'); |
|
|
|
|
|
|
68
|
|
|
$this->eventDispatcher = $container->getDefinition('event_dispatcher'); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
$this->setDefinition('rentgen.create_table', 'rentgen.command.manipulation.create_table.class', $container); |
|
71
|
|
|
$this->setDefinition('rentgen.drop_table', 'rentgen.command.manipulation.drop_table.class', $container); |
|
72
|
|
|
$this->setDefinition('rentgen.add_column', 'rentgen.command.manipulation.add_column.class', $container); |
|
73
|
|
|
$this->setDefinition('rentgen.drop_column', 'rentgen.command.manipulation.drop_column.class', $container); |
|
74
|
|
|
$this->setDefinition('rentgen.add_constraint', 'rentgen.command.manipulation.add_constraint.class', $container); |
|
75
|
|
|
$this->setDefinition('rentgen.drop_constraint', 'rentgen.command.manipulation.drop_constraint.class', $container); |
|
76
|
|
|
$this->setDefinition('rentgen.create_index', 'rentgen.command.manipulation.create_index.class', $container); |
|
77
|
|
|
$this->setDefinition('rentgen.create_schema', 'rentgen.command.manipulation.create_schema.class', $container); |
|
78
|
|
|
$this->setDefinition('rentgen.drop_schema', 'rentgen.command.manipulation.drop_schema.class', $container); |
|
79
|
|
|
$this->setDefinition('rentgen.drop_index', 'rentgen.command.manipulation.drop_index.class', $container); |
|
80
|
|
|
|
|
81
|
|
|
$this->setDefinition('rentgen.table_exists', 'rentgen.command.info.table_exists.class', $container); |
|
82
|
|
|
$this->setDefinition('rentgen.get_table', 'rentgen.command.info.get_table.class', $container); |
|
83
|
|
|
$this->setDefinition('rentgen.get_tables', 'rentgen.command.info.get_tables.class', $container); |
|
84
|
|
|
$this->setDefinition('rentgen.get_schemas', 'rentgen.command.info.get_schemas.class', $container); |
|
85
|
|
|
$this->setDefinition('rentgen.schema_exists', 'rentgen.command.info.schema_exists.class', $container); |
|
86
|
|
|
|
|
87
|
|
|
$definition = new Definition($container->getParameter('rentgen.command.manipulation.clear_database.class'), |
|
88
|
|
|
array(new Reference('rentgen.get_schemas'))); |
|
89
|
|
|
$definition->addMethodCall('setConnection', array(new Reference('connection'))); |
|
90
|
|
|
$definition->addMethodCall('setEventDispatcher', array(new Reference('event_dispatcher'))); |
|
91
|
|
|
$container->setDefinition('rentgen.clear_database', $definition); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function getAlias() |
|
95
|
|
|
{ |
|
96
|
|
|
return 'rentgen'; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
public function getXsdValidationBasePath() |
|
100
|
|
|
{ |
|
101
|
|
|
return false; |
|
|
|
|
|
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getNamespace() |
|
105
|
|
|
{ |
|
106
|
|
|
return 'http://www.example.com/symfony/schema/'; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
private function setDefinition($name, $classParam, $container) |
|
110
|
|
|
{ |
|
111
|
|
|
$className = $container->getParameter($classParam); |
|
112
|
|
|
|
|
113
|
|
|
$definition = new Definition($className); |
|
114
|
|
|
$definition->addMethodCall('setConnection', array(new Reference('connection'))); |
|
115
|
|
|
$definition->addMethodCall('setEventDispatcher', array(new Reference('event_dispatcher'))); |
|
116
|
|
|
$container->setDefinition($name, $definition); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
private function defineParameters(ContainerBuilder $container) |
|
120
|
|
|
{ |
|
121
|
|
|
$container->setParameter('rentgen.command.factory.class', 'Rentgen\Schema\Factory'); |
|
122
|
|
|
$container->setParameter('rentgen.command.manipulation.create_table.class', 'Rentgen\Schema\Manipulation\CreateTableCommand'); |
|
123
|
|
|
$container->setParameter('rentgen.command.manipulation.drop_table.class', 'Rentgen\Schema\Manipulation\DropTableCommand'); |
|
124
|
|
|
$container->setParameter('rentgen.command.manipulation.add_column.class', 'Rentgen\Schema\Manipulation\AddColumnCommand'); |
|
125
|
|
|
$container->setParameter('rentgen.command.manipulation.drop_column.class', 'Rentgen\Schema\Manipulation\DropColumnCommand'); |
|
126
|
|
|
$container->setParameter('rentgen.command.manipulation.add_constraint.class', 'Rentgen\Schema\Manipulation\AddConstraintCommand'); |
|
127
|
|
|
$container->setParameter('rentgen.command.manipulation.drop_constraint.class', 'Rentgen\Schema\Manipulation\DropConstraintCommand'); |
|
128
|
|
|
$container->setParameter('rentgen.command.manipulation.create_index.class', 'Rentgen\Schema\Manipulation\CreateIndexCommand'); |
|
129
|
|
|
$container->setParameter('rentgen.command.manipulation.drop_index.class', 'Rentgen\Schema\Manipulation\DropIndexCommand'); |
|
130
|
|
|
$container->setParameter('rentgen.command.manipulation.create_schema.class', 'Rentgen\Schema\Manipulation\CreateSchemaCommand'); |
|
131
|
|
|
$container->setParameter('rentgen.command.manipulation.drop_schema.class', 'Rentgen\Schema\Manipulation\DropSchemaCommand'); |
|
132
|
|
|
$container->setParameter('rentgen.command.manipulation.clear_database.class', 'Rentgen\Schema\Manipulation\ClearDatabaseCommand'); |
|
133
|
|
|
$container->setParameter('rentgen.command.info.table_exists.class', 'Rentgen\Schema\Info\TableExistsCommand'); |
|
134
|
|
|
$container->setParameter('rentgen.command.info.get_table.class', 'Rentgen\Schema\Info\GetTableCommand'); |
|
135
|
|
|
$container->setParameter('rentgen.command.info.get_tables.class', 'Rentgen\Schema\Info\GetTablesCommand'); |
|
136
|
|
|
$container->setParameter('rentgen.command.info.get_schemas.class', 'Rentgen\Schema\Info\GetSchemasCommand'); |
|
137
|
|
|
$container->setParameter('rentgen.command.info.schema_exists.class', 'Rentgen\Schema\Info\SchemaExistsCommand'); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
private function isConnectionConfig($config) |
|
141
|
|
|
{ |
|
142
|
|
|
return isset($config['username']) |
|
143
|
|
|
&& isset($config['password']) |
|
144
|
|
|
&& isset($config['dsn']); |
|
145
|
|
|
} |
|
146
|
|
|
} |
|
147
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.