Passed
Push — 5.1 ( 37370f...e9e3cd )
by David
01:10 queued 11s
created
src/Mouf/Database/TDBM/Utils/MoufDiListener.php 1 patch
Indentation   +69 added lines, -69 removed lines patch added patch discarded remove patch
@@ -14,73 +14,73 @@
 block discarded – undo
14 14
 class MoufDiListener implements GeneratorListenerInterface
15 15
 {
16 16
 
17
-    /**
18
-     * @param ConfigurationInterface $configuration
19
-     * @param BeanDescriptorInterface[] $beanDescriptors
20
-     */
21
-    public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void
22
-    {
23
-        // Let's generate the needed instance in Mouf.
24
-        $moufManager = MoufManager::getMoufManager();
25
-
26
-        $daoFactoryInstanceName = null;
27
-        if ($configuration instanceof MoufConfiguration) {
28
-            $daoFactoryInstanceName = $configuration->getDaoFactoryInstanceName();
29
-            $daoFactoryClassName = $configuration->getDaoNamespace().'\\Generated\\'.$configuration->getNamingStrategy()->getDaoFactoryClassName();
30
-            $moufManager->declareComponent($daoFactoryInstanceName, $daoFactoryClassName, false, MoufManager::DECLARE_ON_EXIST_KEEP_INCOMING_LINKS);
31
-        }
32
-
33
-        $tdbmServiceInstanceName = $this->getTdbmInstanceName($configuration);
34
-
35
-        foreach ($beanDescriptors as $beanDescriptor) {
36
-            $daoName = $beanDescriptor->getDaoClassName();
37
-
38
-            // Rename all DAOs to full-class name (migration from 5.0 to 5.1+ naming schema)
39
-            $instanceName = TDBMDaoGenerator::toVariableName($daoName);
40
-            if ($moufManager->instanceExists($instanceName) && !$moufManager->instanceExists($configuration->getDaoNamespace().'\\'.$daoName)) {
41
-                $moufManager->renameComponent($instanceName, $configuration->getDaoNamespace().'\\'.$daoName);
42
-
43
-                // Let's create a "Link" between old Mouf::getXXXDao and new Mouf::getNamespaceXXXDao
44
-                $moufManager->createInstanceByCode()->setName($instanceName)->setCode('return $container->get('.var_export($configuration->getDaoNamespace().'\\'.$daoName, true).');');
45
-
46
-                // Let's unset the setter. It is useless now.
47
-                if ($daoFactoryInstanceName !== null) {
48
-                    $moufManager->unsetParameterForSetter($daoFactoryInstanceName, 'set'.$daoName);
49
-                }
50
-            }
51
-
52
-            $instanceName = $configuration->getDaoNamespace().'\\'.$daoName;
53
-            if (!$moufManager->instanceExists($instanceName)) {
54
-                $moufManager->declareComponent($instanceName, $configuration->getDaoNamespace().'\\'.$daoName);
55
-            }
56
-            $moufManager->setParameterViaConstructor($instanceName, 0, $tdbmServiceInstanceName, 'object');
57
-        }
58
-
59
-        if ($daoFactoryInstanceName !== null) {
60
-            $moufManager->setParameterViaConstructor($daoFactoryInstanceName, 0, 'return $container;', 'primitive', 'php');
61
-        }
62
-
63
-        $moufManager->rewriteMouf();
64
-    }
65
-
66
-    private function getTdbmInstanceName(ConfigurationInterface $configuration) : string
67
-    {
68
-        $moufManager = MoufManager::getMoufManager();
69
-
70
-        $configurationInstanceName = $moufManager->findInstanceName($configuration);
71
-        if (!$configurationInstanceName) {
72
-            throw new \TDBMException('Could not find TDBM instance for configuration object.');
73
-        }
74
-
75
-        // Let's find the configuration
76
-        $tdbmServicesNames = $moufManager->findInstances(TDBMService::class);
77
-
78
-        foreach ($tdbmServicesNames as $name) {
79
-            if ($moufManager->getInstanceDescriptor($name)->getConstructorArgumentProperty('configuration')->getValue()->getName() === $configurationInstanceName) {
80
-                return $name;
81
-            }
82
-        }
83
-
84
-        throw new \TDBMException('Could not find TDBMService instance.');
85
-    }
17
+	/**
18
+	 * @param ConfigurationInterface $configuration
19
+	 * @param BeanDescriptorInterface[] $beanDescriptors
20
+	 */
21
+	public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void
22
+	{
23
+		// Let's generate the needed instance in Mouf.
24
+		$moufManager = MoufManager::getMoufManager();
25
+
26
+		$daoFactoryInstanceName = null;
27
+		if ($configuration instanceof MoufConfiguration) {
28
+			$daoFactoryInstanceName = $configuration->getDaoFactoryInstanceName();
29
+			$daoFactoryClassName = $configuration->getDaoNamespace().'\\Generated\\'.$configuration->getNamingStrategy()->getDaoFactoryClassName();
30
+			$moufManager->declareComponent($daoFactoryInstanceName, $daoFactoryClassName, false, MoufManager::DECLARE_ON_EXIST_KEEP_INCOMING_LINKS);
31
+		}
32
+
33
+		$tdbmServiceInstanceName = $this->getTdbmInstanceName($configuration);
34
+
35
+		foreach ($beanDescriptors as $beanDescriptor) {
36
+			$daoName = $beanDescriptor->getDaoClassName();
37
+
38
+			// Rename all DAOs to full-class name (migration from 5.0 to 5.1+ naming schema)
39
+			$instanceName = TDBMDaoGenerator::toVariableName($daoName);
40
+			if ($moufManager->instanceExists($instanceName) && !$moufManager->instanceExists($configuration->getDaoNamespace().'\\'.$daoName)) {
41
+				$moufManager->renameComponent($instanceName, $configuration->getDaoNamespace().'\\'.$daoName);
42
+
43
+				// Let's create a "Link" between old Mouf::getXXXDao and new Mouf::getNamespaceXXXDao
44
+				$moufManager->createInstanceByCode()->setName($instanceName)->setCode('return $container->get('.var_export($configuration->getDaoNamespace().'\\'.$daoName, true).');');
45
+
46
+				// Let's unset the setter. It is useless now.
47
+				if ($daoFactoryInstanceName !== null) {
48
+					$moufManager->unsetParameterForSetter($daoFactoryInstanceName, 'set'.$daoName);
49
+				}
50
+			}
51
+
52
+			$instanceName = $configuration->getDaoNamespace().'\\'.$daoName;
53
+			if (!$moufManager->instanceExists($instanceName)) {
54
+				$moufManager->declareComponent($instanceName, $configuration->getDaoNamespace().'\\'.$daoName);
55
+			}
56
+			$moufManager->setParameterViaConstructor($instanceName, 0, $tdbmServiceInstanceName, 'object');
57
+		}
58
+
59
+		if ($daoFactoryInstanceName !== null) {
60
+			$moufManager->setParameterViaConstructor($daoFactoryInstanceName, 0, 'return $container;', 'primitive', 'php');
61
+		}
62
+
63
+		$moufManager->rewriteMouf();
64
+	}
65
+
66
+	private function getTdbmInstanceName(ConfigurationInterface $configuration) : string
67
+	{
68
+		$moufManager = MoufManager::getMoufManager();
69
+
70
+		$configurationInstanceName = $moufManager->findInstanceName($configuration);
71
+		if (!$configurationInstanceName) {
72
+			throw new \TDBMException('Could not find TDBM instance for configuration object.');
73
+		}
74
+
75
+		// Let's find the configuration
76
+		$tdbmServicesNames = $moufManager->findInstances(TDBMService::class);
77
+
78
+		foreach ($tdbmServicesNames as $name) {
79
+			if ($moufManager->getInstanceDescriptor($name)->getConstructorArgumentProperty('configuration')->getValue()->getName() === $configurationInstanceName) {
80
+				return $name;
81
+			}
82
+		}
83
+
84
+		throw new \TDBMException('Could not find TDBMService instance.');
85
+	}
86 86
 }
Please login to merge, or discard this patch.
src/Mouf/Database/TDBM/Controllers/TdbmInstallController.php 1 patch
Indentation   +267 added lines, -267 removed lines patch added patch discarded remove patch
@@ -38,271 +38,271 @@
 block discarded – undo
38 38
  */
39 39
 class TdbmInstallController extends Controller
40 40
 {
41
-    /**
42
-     * @var HtmlBlock
43
-     */
44
-    public $content;
45
-
46
-    public $selfedit;
47
-
48
-    /**
49
-     * The active MoufManager to be edited/viewed.
50
-     *
51
-     * @var MoufManager
52
-     */
53
-    public $moufManager;
54
-
55
-    /**
56
-     * The template used by the main page for mouf.
57
-     *
58
-     * @Property
59
-     * @Compulsory
60
-     *
61
-     * @var TemplateInterface
62
-     */
63
-    public $template;
64
-
65
-    /**
66
-     * Displays the first install screen.
67
-     *
68
-     * @Action
69
-     * @Logged
70
-     *
71
-     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
72
-     */
73
-    public function defaultAction($selfedit = 'false')
74
-    {
75
-        $this->selfedit = $selfedit;
76
-
77
-        if ($selfedit == 'true') {
78
-            $this->moufManager = MoufManager::getMoufManager();
79
-        } else {
80
-            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
81
-        }
82
-
83
-        $this->content->addFile(__DIR__.'/../../../../views/installStep1.php', $this);
84
-        $this->template->toHtml();
85
-    }
86
-
87
-    /**
88
-     * Skips the install process.
89
-     *
90
-     * @Action
91
-     * @Logged
92
-     *
93
-     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
94
-     */
95
-    public function skip($selfedit = 'false')
96
-    {
97
-        InstallUtils::continueInstall($selfedit == 'true');
98
-    }
99
-
100
-    protected $daoNamespace;
101
-    protected $beanNamespace;
102
-    protected $autoloadDetected;
103
-    //protected $storeInUtc;
104
-    protected $useCustomComposer = false;
105
-    protected $composerFile;
106
-
107
-    /**
108
-     * Displays the second install screen.
109
-     *
110
-     * @Action
111
-     * @Logged
112
-     *
113
-     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
114
-     */
115
-    public function configure($selfedit = 'false')
116
-    {
117
-        $this->selfedit = $selfedit;
118
-
119
-        if ($selfedit == 'true') {
120
-            $this->moufManager = MoufManager::getMoufManager();
121
-        } else {
122
-            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
123
-        }
124
-
125
-        // Let's start by performing basic checks about the instances we assume to exist.
126
-        if (!$this->moufManager->instanceExists('dbalConnection')) {
127
-            $this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance.");
128
-
129
-            return;
130
-        }
131
-
132
-        if ($this->moufManager->has('tdbmConfiguration')) {
133
-            $tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
134
-
135
-            $this->beanNamespace = $tdbmConfiguration->getConstructorArgumentProperty('beanNamespace')->getValue();
136
-            $this->daoNamespace = $tdbmConfiguration->getConstructorArgumentProperty('daoNamespace')->getValue();
137
-        } else {
138
-            // Old TDBM 4.2 fallback
139
-            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService');
140
-            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService');
141
-        }
142
-
143
-        if ($this->daoNamespace == null && $this->beanNamespace == null) {
144
-            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
145
-
146
-            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
147
-            if ($autoloadNamespaces) {
148
-                $this->autoloadDetected = true;
149
-                $rootNamespace = $autoloadNamespaces[0];
150
-                $this->daoNamespace = $rootNamespace.'Dao';
151
-                $this->beanNamespace = $rootNamespace.'Model';
152
-            } else {
153
-                $this->autoloadDetected = false;
154
-                $this->daoNamespace = 'YourApplication\\Dao';
155
-                $this->beanNamespace = 'YourApplication\\Model';
156
-            }
157
-        } else {
158
-            $this->autoloadDetected = true;
159
-        }
160
-        $this->defaultPath = true;
161
-        $this->storePath = '';
162
-
163
-        $this->castDatesToDateTime = true;
164
-
165
-        $this->content->addFile(__DIR__.'/../../../../views/installStep2.php', $this);
166
-        $this->template->toHtml();
167
-    }
168
-
169
-    /**
170
-     * This action generates the TDBM instance, then the DAOs and Beans.
171
-     *
172
-     * @Action
173
-     *
174
-     * @param string $daonamespace
175
-     * @param string $beannamespace
176
-     * @param string $selfedit
177
-     *
178
-     * @throws \Mouf\MoufException
179
-     */
180
-    public function generate($daonamespace, $beannamespace, /*$storeInUtc = 0,*/ $selfedit = 'false', $defaultPath = false, $storePath = '')
181
-    {
182
-        $this->selfedit = $selfedit;
183
-
184
-        if ($selfedit == 'true') {
185
-            $this->moufManager = MoufManager::getMoufManager();
186
-        } else {
187
-            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
188
-        }
189
-
190
-        $doctrineCache = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');
191
-
192
-        $migratingFrom42 = false;
193
-        if ($this->moufManager->has('tdbmService') && !$this->moufManager->has('tdbmConfiguration')) {
194
-            $migratingFrom42 = true;
195
-        }
196
-
197
-        if ($this->moufManager->has('tdbmService') && $this->moufManager->getInstanceDescriptor('tdbmService')->getClassName() === 'Mouf\\Database\\TDBM\\TDBMService') {
198
-            $this->migrateNamespaceTo50($this->moufManager);
199
-        }
200
-
201
-        $annotationParser = InstallUtils::getOrCreateInstance(AnnotationParser::class, AnnotationParser::class, $this->moufManager);
202
-        $annotationParser->getConstructorArgumentProperty('annotations')->setValue([
203
-            'UUID' => UUID::class,
204
-            'Autoincrement' => Autoincrement::class,
205
-            'Bean' => Bean::class,
206
-            'ProtectedGetter' => ProtectedGetter::class,
207
-            'ProtectedSetter' => ProtectedSetter::class,
208
-            'ProtectedOneToMany' => ProtectedOneToMany::class,
209
-            'JsonKey' => JsonKey::class,
210
-            'JsonIgnore' => JsonIgnore::class,
211
-            'JsonInclude' => JsonInclude::class,
212
-            'JsonRecursive' => JsonRecursive::class,
213
-            'JsonCollection' => JsonCollection::class,
214
-            'JsonFormat' => JsonFormat::class,
215
-            'AddInterface' => AddInterface::class,
216
-            'AddInterfaceOnDao' => AddInterfaceOnDao::class,
217
-            'AddTrait' => AddTrait::class,
218
-            'AddTraitOnDao' => AddTraitOnDao::class,
219
-        ]);
220
-
221
-        $namingStrategy = InstallUtils::getOrCreateInstance('namingStrategy', DefaultNamingStrategy::class, $this->moufManager);
222
-        if ($migratingFrom42) {
223
-            // Let's setup the naming strategy for compatibility
224
-            $namingStrategy->getSetterProperty('setBeanPrefix')->setValue('');
225
-            $namingStrategy->getSetterProperty('setBeanSuffix')->setValue('Bean');
226
-            $namingStrategy->getSetterProperty('setBaseBeanPrefix')->setValue('');
227
-            $namingStrategy->getSetterProperty('setBaseBeanSuffix')->setValue('BaseBean');
228
-            $namingStrategy->getSetterProperty('setDaoPrefix')->setValue('');
229
-            $namingStrategy->getSetterProperty('setDaoSuffix')->setValue('Dao');
230
-            $namingStrategy->getSetterProperty('setBaseDaoPrefix')->setValue('');
231
-            $namingStrategy->getSetterProperty('setBaseDaoSuffix')->setValue('BaseDao');
232
-        }
233
-        if ($namingStrategy->getClassName() === DefaultNamingStrategy::class) {
234
-            $namingStrategy->getConstructorArgumentProperty('annotationParser')->setValue($this->moufManager->getInstanceDescriptor(AnnotationParser::class));
235
-            $namingStrategy->getConstructorArgumentProperty('schemaManager')->setOrigin('php')->setValue('return $container->get(\'dbalConnection\')->getSchemaManager();');
236
-        }
237
-
238
-
239
-        if (!$this->moufManager->instanceExists('tdbmConfiguration')) {
240
-            $moufListener = InstallUtils::getOrCreateInstance(MoufDiListener::class, MoufDiListener::class, $this->moufManager);
241
-
242
-            $tdbmConfiguration = $this->moufManager->createInstance(MoufConfiguration::class)->setName('tdbmConfiguration');
243
-            $tdbmConfiguration->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection'));
244
-            $tdbmConfiguration->getConstructorArgumentProperty('cache')->setValue($doctrineCache);
245
-            $tdbmConfiguration->getConstructorArgumentProperty('namingStrategy')->setValue($namingStrategy);
246
-            $tdbmConfiguration->getProperty('daoFactoryInstanceName')->setValue('daoFactory');
247
-            $tdbmConfiguration->getConstructorArgumentProperty('generatorListeners')->setValue([$moufListener]);
248
-
249
-            // Let's also delete the tdbmService if migrating versions <= 4.2
250
-            if ($migratingFrom42) {
251
-                $this->moufManager->removeComponent('tdbmService');
252
-            }
253
-        } else {
254
-            $tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
255
-        }
256
-
257
-        if (!$this->moufManager->instanceExists('tdbmService')) {
258
-            $tdbmService = $this->moufManager->createInstance('TheCodingMachine\\TDBM\\TDBMService')->setName('tdbmService');
259
-            $tdbmService->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
260
-        }
261
-
262
-        // We declare our instance of the Symfony command as a Mouf instance
263
-        $generateCommand = InstallUtils::getOrCreateInstance('generateCommand', GenerateCommand::class, $this->moufManager);
264
-        $generateCommand->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
265
-
266
-        // We register that instance descriptor using "ConsoleUtils"
267
-        $consoleUtils = new ConsoleUtils($this->moufManager);
268
-        $consoleUtils->registerCommand($generateCommand);
269
-
270
-        $this->moufManager->rewriteMouf();
271
-
272
-        TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'daoFactory', $selfedit, /*$storeInUtc,*/ $defaultPath, $storePath);
273
-
274
-        InstallUtils::continueInstall($selfedit == 'true');
275
-    }
276
-
277
-    protected $errorMsg;
278
-
279
-    private function displayErrorMsg($msg)
280
-    {
281
-        $this->errorMsg = $msg;
282
-        $this->content->addFile(__DIR__.'/../../../../views/installError.php', $this);
283
-        $this->template->toHtml();
284
-    }
285
-
286
-    /**
287
-     * Migrate classes from old 4.x namespace (Mouf\Database\TDBM) to new 5.x namespace (TheCodingMachine\TDBM)
288
-     *
289
-     * @param MoufManager $moufManager
290
-     */
291
-    private function migrateNamespaceTo50(MoufManager $moufManager)
292
-    {
293
-        $instanceList = $moufManager->getInstancesList();
294
-
295
-        $migratedClasses = [
296
-            'Mouf\\Database\\TDBM\\Configuration' => 'TheCodingMachine\\TDBM\\Configuration',
297
-            'Mouf\\Database\\TDBM\\TDBMService' => 'TheCodingMachine\\TDBM\\TDBMService',
298
-            'Mouf\\Database\\TDBM\\Commands\\GenerateCommand' => 'TheCodingMachine\\TDBM\\Commands\\GenerateCommand',
299
-            'Mouf\\Database\\TDBM\\Utils\\DefaultNamingStrategy' => 'TheCodingMachine\\TDBM\\Utils\\DefaultNamingStrategy',
300
-        ];
301
-
302
-        foreach ($instanceList as $instanceName => $className) {
303
-            if (isset($migratedClasses[$className])) {
304
-                $moufManager->alterClass($instanceName, $migratedClasses[$className]);
305
-            }
306
-        }
307
-    }
41
+	/**
42
+	 * @var HtmlBlock
43
+	 */
44
+	public $content;
45
+
46
+	public $selfedit;
47
+
48
+	/**
49
+	 * The active MoufManager to be edited/viewed.
50
+	 *
51
+	 * @var MoufManager
52
+	 */
53
+	public $moufManager;
54
+
55
+	/**
56
+	 * The template used by the main page for mouf.
57
+	 *
58
+	 * @Property
59
+	 * @Compulsory
60
+	 *
61
+	 * @var TemplateInterface
62
+	 */
63
+	public $template;
64
+
65
+	/**
66
+	 * Displays the first install screen.
67
+	 *
68
+	 * @Action
69
+	 * @Logged
70
+	 *
71
+	 * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
72
+	 */
73
+	public function defaultAction($selfedit = 'false')
74
+	{
75
+		$this->selfedit = $selfedit;
76
+
77
+		if ($selfedit == 'true') {
78
+			$this->moufManager = MoufManager::getMoufManager();
79
+		} else {
80
+			$this->moufManager = MoufManager::getMoufManagerHiddenInstance();
81
+		}
82
+
83
+		$this->content->addFile(__DIR__.'/../../../../views/installStep1.php', $this);
84
+		$this->template->toHtml();
85
+	}
86
+
87
+	/**
88
+	 * Skips the install process.
89
+	 *
90
+	 * @Action
91
+	 * @Logged
92
+	 *
93
+	 * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
94
+	 */
95
+	public function skip($selfedit = 'false')
96
+	{
97
+		InstallUtils::continueInstall($selfedit == 'true');
98
+	}
99
+
100
+	protected $daoNamespace;
101
+	protected $beanNamespace;
102
+	protected $autoloadDetected;
103
+	//protected $storeInUtc;
104
+	protected $useCustomComposer = false;
105
+	protected $composerFile;
106
+
107
+	/**
108
+	 * Displays the second install screen.
109
+	 *
110
+	 * @Action
111
+	 * @Logged
112
+	 *
113
+	 * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
114
+	 */
115
+	public function configure($selfedit = 'false')
116
+	{
117
+		$this->selfedit = $selfedit;
118
+
119
+		if ($selfedit == 'true') {
120
+			$this->moufManager = MoufManager::getMoufManager();
121
+		} else {
122
+			$this->moufManager = MoufManager::getMoufManagerHiddenInstance();
123
+		}
124
+
125
+		// Let's start by performing basic checks about the instances we assume to exist.
126
+		if (!$this->moufManager->instanceExists('dbalConnection')) {
127
+			$this->displayErrorMsg("The TDBM install process assumes your database connection instance is already created, and that the name of this instance is 'dbalConnection'. Could not find the 'dbalConnection' instance.");
128
+
129
+			return;
130
+		}
131
+
132
+		if ($this->moufManager->has('tdbmConfiguration')) {
133
+			$tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
134
+
135
+			$this->beanNamespace = $tdbmConfiguration->getConstructorArgumentProperty('beanNamespace')->getValue();
136
+			$this->daoNamespace = $tdbmConfiguration->getConstructorArgumentProperty('daoNamespace')->getValue();
137
+		} else {
138
+			// Old TDBM 4.2 fallback
139
+			$this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService');
140
+			$this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService');
141
+		}
142
+
143
+		if ($this->daoNamespace == null && $this->beanNamespace == null) {
144
+			$classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
145
+
146
+			$autoloadNamespaces = $classNameMapper->getManagedNamespaces();
147
+			if ($autoloadNamespaces) {
148
+				$this->autoloadDetected = true;
149
+				$rootNamespace = $autoloadNamespaces[0];
150
+				$this->daoNamespace = $rootNamespace.'Dao';
151
+				$this->beanNamespace = $rootNamespace.'Model';
152
+			} else {
153
+				$this->autoloadDetected = false;
154
+				$this->daoNamespace = 'YourApplication\\Dao';
155
+				$this->beanNamespace = 'YourApplication\\Model';
156
+			}
157
+		} else {
158
+			$this->autoloadDetected = true;
159
+		}
160
+		$this->defaultPath = true;
161
+		$this->storePath = '';
162
+
163
+		$this->castDatesToDateTime = true;
164
+
165
+		$this->content->addFile(__DIR__.'/../../../../views/installStep2.php', $this);
166
+		$this->template->toHtml();
167
+	}
168
+
169
+	/**
170
+	 * This action generates the TDBM instance, then the DAOs and Beans.
171
+	 *
172
+	 * @Action
173
+	 *
174
+	 * @param string $daonamespace
175
+	 * @param string $beannamespace
176
+	 * @param string $selfedit
177
+	 *
178
+	 * @throws \Mouf\MoufException
179
+	 */
180
+	public function generate($daonamespace, $beannamespace, /*$storeInUtc = 0,*/ $selfedit = 'false', $defaultPath = false, $storePath = '')
181
+	{
182
+		$this->selfedit = $selfedit;
183
+
184
+		if ($selfedit == 'true') {
185
+			$this->moufManager = MoufManager::getMoufManager();
186
+		} else {
187
+			$this->moufManager = MoufManager::getMoufManagerHiddenInstance();
188
+		}
189
+
190
+		$doctrineCache = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');
191
+
192
+		$migratingFrom42 = false;
193
+		if ($this->moufManager->has('tdbmService') && !$this->moufManager->has('tdbmConfiguration')) {
194
+			$migratingFrom42 = true;
195
+		}
196
+
197
+		if ($this->moufManager->has('tdbmService') && $this->moufManager->getInstanceDescriptor('tdbmService')->getClassName() === 'Mouf\\Database\\TDBM\\TDBMService') {
198
+			$this->migrateNamespaceTo50($this->moufManager);
199
+		}
200
+
201
+		$annotationParser = InstallUtils::getOrCreateInstance(AnnotationParser::class, AnnotationParser::class, $this->moufManager);
202
+		$annotationParser->getConstructorArgumentProperty('annotations')->setValue([
203
+			'UUID' => UUID::class,
204
+			'Autoincrement' => Autoincrement::class,
205
+			'Bean' => Bean::class,
206
+			'ProtectedGetter' => ProtectedGetter::class,
207
+			'ProtectedSetter' => ProtectedSetter::class,
208
+			'ProtectedOneToMany' => ProtectedOneToMany::class,
209
+			'JsonKey' => JsonKey::class,
210
+			'JsonIgnore' => JsonIgnore::class,
211
+			'JsonInclude' => JsonInclude::class,
212
+			'JsonRecursive' => JsonRecursive::class,
213
+			'JsonCollection' => JsonCollection::class,
214
+			'JsonFormat' => JsonFormat::class,
215
+			'AddInterface' => AddInterface::class,
216
+			'AddInterfaceOnDao' => AddInterfaceOnDao::class,
217
+			'AddTrait' => AddTrait::class,
218
+			'AddTraitOnDao' => AddTraitOnDao::class,
219
+		]);
220
+
221
+		$namingStrategy = InstallUtils::getOrCreateInstance('namingStrategy', DefaultNamingStrategy::class, $this->moufManager);
222
+		if ($migratingFrom42) {
223
+			// Let's setup the naming strategy for compatibility
224
+			$namingStrategy->getSetterProperty('setBeanPrefix')->setValue('');
225
+			$namingStrategy->getSetterProperty('setBeanSuffix')->setValue('Bean');
226
+			$namingStrategy->getSetterProperty('setBaseBeanPrefix')->setValue('');
227
+			$namingStrategy->getSetterProperty('setBaseBeanSuffix')->setValue('BaseBean');
228
+			$namingStrategy->getSetterProperty('setDaoPrefix')->setValue('');
229
+			$namingStrategy->getSetterProperty('setDaoSuffix')->setValue('Dao');
230
+			$namingStrategy->getSetterProperty('setBaseDaoPrefix')->setValue('');
231
+			$namingStrategy->getSetterProperty('setBaseDaoSuffix')->setValue('BaseDao');
232
+		}
233
+		if ($namingStrategy->getClassName() === DefaultNamingStrategy::class) {
234
+			$namingStrategy->getConstructorArgumentProperty('annotationParser')->setValue($this->moufManager->getInstanceDescriptor(AnnotationParser::class));
235
+			$namingStrategy->getConstructorArgumentProperty('schemaManager')->setOrigin('php')->setValue('return $container->get(\'dbalConnection\')->getSchemaManager();');
236
+		}
237
+
238
+
239
+		if (!$this->moufManager->instanceExists('tdbmConfiguration')) {
240
+			$moufListener = InstallUtils::getOrCreateInstance(MoufDiListener::class, MoufDiListener::class, $this->moufManager);
241
+
242
+			$tdbmConfiguration = $this->moufManager->createInstance(MoufConfiguration::class)->setName('tdbmConfiguration');
243
+			$tdbmConfiguration->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection'));
244
+			$tdbmConfiguration->getConstructorArgumentProperty('cache')->setValue($doctrineCache);
245
+			$tdbmConfiguration->getConstructorArgumentProperty('namingStrategy')->setValue($namingStrategy);
246
+			$tdbmConfiguration->getProperty('daoFactoryInstanceName')->setValue('daoFactory');
247
+			$tdbmConfiguration->getConstructorArgumentProperty('generatorListeners')->setValue([$moufListener]);
248
+
249
+			// Let's also delete the tdbmService if migrating versions <= 4.2
250
+			if ($migratingFrom42) {
251
+				$this->moufManager->removeComponent('tdbmService');
252
+			}
253
+		} else {
254
+			$tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
255
+		}
256
+
257
+		if (!$this->moufManager->instanceExists('tdbmService')) {
258
+			$tdbmService = $this->moufManager->createInstance('TheCodingMachine\\TDBM\\TDBMService')->setName('tdbmService');
259
+			$tdbmService->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
260
+		}
261
+
262
+		// We declare our instance of the Symfony command as a Mouf instance
263
+		$generateCommand = InstallUtils::getOrCreateInstance('generateCommand', GenerateCommand::class, $this->moufManager);
264
+		$generateCommand->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
265
+
266
+		// We register that instance descriptor using "ConsoleUtils"
267
+		$consoleUtils = new ConsoleUtils($this->moufManager);
268
+		$consoleUtils->registerCommand($generateCommand);
269
+
270
+		$this->moufManager->rewriteMouf();
271
+
272
+		TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'daoFactory', $selfedit, /*$storeInUtc,*/ $defaultPath, $storePath);
273
+
274
+		InstallUtils::continueInstall($selfedit == 'true');
275
+	}
276
+
277
+	protected $errorMsg;
278
+
279
+	private function displayErrorMsg($msg)
280
+	{
281
+		$this->errorMsg = $msg;
282
+		$this->content->addFile(__DIR__.'/../../../../views/installError.php', $this);
283
+		$this->template->toHtml();
284
+	}
285
+
286
+	/**
287
+	 * Migrate classes from old 4.x namespace (Mouf\Database\TDBM) to new 5.x namespace (TheCodingMachine\TDBM)
288
+	 *
289
+	 * @param MoufManager $moufManager
290
+	 */
291
+	private function migrateNamespaceTo50(MoufManager $moufManager)
292
+	{
293
+		$instanceList = $moufManager->getInstancesList();
294
+
295
+		$migratedClasses = [
296
+			'Mouf\\Database\\TDBM\\Configuration' => 'TheCodingMachine\\TDBM\\Configuration',
297
+			'Mouf\\Database\\TDBM\\TDBMService' => 'TheCodingMachine\\TDBM\\TDBMService',
298
+			'Mouf\\Database\\TDBM\\Commands\\GenerateCommand' => 'TheCodingMachine\\TDBM\\Commands\\GenerateCommand',
299
+			'Mouf\\Database\\TDBM\\Utils\\DefaultNamingStrategy' => 'TheCodingMachine\\TDBM\\Utils\\DefaultNamingStrategy',
300
+		];
301
+
302
+		foreach ($instanceList as $instanceName => $className) {
303
+			if (isset($migratedClasses[$className])) {
304
+				$moufManager->alterClass($instanceName, $migratedClasses[$className]);
305
+			}
306
+		}
307
+	}
308 308
 }
Please login to merge, or discard this patch.