1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Database\TDBM\Controllers; |
4
|
|
|
|
5
|
|
|
use Mouf\Composer\ClassNameMapper; |
6
|
|
|
use Mouf\Controllers\AbstractMoufInstanceController; |
7
|
|
|
use Mouf\Database\TDBM\TDBMService; |
8
|
|
|
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator; |
9
|
|
|
use Mouf\Html\HtmlElement\HtmlBlock; |
10
|
|
|
use Mouf\MoufManager; |
11
|
|
|
use Mouf\InstanceProxy; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The controller to generate automatically the Beans, Daos, etc... |
15
|
|
|
* Sweet! |
16
|
|
|
* |
17
|
|
|
* @Component |
18
|
|
|
*/ |
19
|
|
|
class TdbmController extends AbstractMoufInstanceController |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @var HtmlBlock |
23
|
|
|
*/ |
24
|
|
|
public $content; |
25
|
|
|
|
26
|
|
|
protected $daoNamespace; |
27
|
|
|
protected $beanNamespace; |
28
|
|
|
protected $daoFactoryName; |
29
|
|
|
protected $daoFactoryInstanceName; |
30
|
|
|
protected $autoloadDetected; |
31
|
|
|
protected $storeInUtc; |
32
|
|
|
protected $useCustomComposer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Admin page used to display the DAO generation form. |
36
|
|
|
* |
37
|
|
|
* @Action |
38
|
|
|
*/ |
39
|
|
|
public function defaultAction($name, $selfedit = 'false') |
40
|
|
|
{ |
41
|
|
|
$this->initController($name, $selfedit); |
42
|
|
|
|
43
|
|
|
// Fill variables |
44
|
|
|
if ($this->moufManager->getVariable('tdbmDefaultSourceDirectory_'.$name) != null) { |
45
|
|
|
$this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_'.$name); |
46
|
|
|
$this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_'.$name); |
47
|
|
|
$this->daoFactoryName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryName_'.$name); |
48
|
|
|
$this->daoFactoryInstanceName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryInstanceName_'.$name); |
49
|
|
|
$this->storeInUtc = $this->moufManager->getVariable('tdbmDefaultStoreInUtc_'.$name); |
50
|
|
|
$this->useCustomComposer = $this->moufManager->getVariable('tdbmDefaultUseCustomComposer_'.$name); |
51
|
|
|
$this->composerFile = $this->moufManager->getVariable('tdbmDefaultComposerFile_'.$name); |
52
|
|
|
} else { |
53
|
|
|
$this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace'); |
54
|
|
|
$this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace'); |
55
|
|
|
$this->daoFactoryName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryName'); |
56
|
|
|
$this->daoFactoryInstanceName = $this->moufManager->getVariable('tdbmDefaultDaoFactoryInstanceName'); |
57
|
|
|
$this->storeInUtc = $this->moufManager->getVariable('tdbmDefaultStoreInUtc'); |
58
|
|
|
$this->useCustomComposer = $this->moufManager->getVariable('tdbmDefaultUseCustomComposer'); |
59
|
|
|
$this->composerFile = $this->moufManager->getVariable('tdbmDefaultComposerFile'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
View Code Duplication |
if ($this->daoNamespace == null && $this->beanNamespace == null) { |
|
|
|
|
63
|
|
|
$classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json'); |
64
|
|
|
|
65
|
|
|
$autoloadNamespaces = $classNameMapper->getManagedNamespaces(); |
66
|
|
|
if ($autoloadNamespaces) { |
67
|
|
|
$this->autoloadDetected = true; |
68
|
|
|
$rootNamespace = $autoloadNamespaces[0]; |
69
|
|
|
$this->daoNamespace = $rootNamespace.'Dao'; |
70
|
|
|
$this->beanNamespace = $rootNamespace.'Dao\\Bean'; |
71
|
|
|
} else { |
72
|
|
|
$this->autoloadDetected = false; |
73
|
|
|
$this->daoNamespace = 'YourApplication\\Dao'; |
74
|
|
|
$this->beanNamespace = 'YourApplication\\Dao\\Bean'; |
75
|
|
|
} |
76
|
|
|
} else { |
77
|
|
|
$this->autoloadDetected = true; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
$this->content->addFile(__DIR__.'/../../../../views/tdbmGenerate.php', $this); |
81
|
|
|
$this->template->toHtml(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* This action generates the DAOs and Beans for the TDBM service passed in parameter. |
86
|
|
|
* |
87
|
|
|
* @Action |
88
|
|
|
* |
89
|
|
|
* @param string $name |
90
|
|
|
* @param bool $selfedit |
91
|
|
|
*/ |
92
|
|
|
public function generate($name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $storeInUtc = 0, $selfedit = 'false', $useCustomComposer = false, $composerFile = '') |
93
|
|
|
{ |
94
|
|
|
$this->initController($name, $selfedit); |
95
|
|
|
|
96
|
|
|
self::generateDaos($this->moufManager, $name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $selfedit, $storeInUtc, $useCustomComposer, $composerFile); |
97
|
|
|
|
98
|
|
|
// TODO: better: we should redirect to a screen that list the number of DAOs generated, etc... |
99
|
|
|
header('Location: '.ROOT_URL.'ajaxinstance/?name='.urlencode($name).'&selfedit='.$selfedit); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* This function generates the DAOs and Beans for the TDBM service passed in parameter. |
104
|
|
|
* |
105
|
|
|
* @param MoufManager $moufManager |
106
|
|
|
* @param string $name |
107
|
|
|
* @param string $daonamespace |
108
|
|
|
* @param string $beannamespace |
109
|
|
|
* @param string $daofactoryclassname |
110
|
|
|
* @param string $daofactoryinstancename |
111
|
|
|
* @param string $selfedit |
112
|
|
|
* @param bool $storeInUtc |
113
|
|
|
* |
114
|
|
|
* @throws \Mouf\MoufException |
115
|
|
|
*/ |
116
|
|
|
public static function generateDaos(MoufManager $moufManager, $name, $daonamespace, $beannamespace, $daofactoryclassname, $daofactoryinstancename, $selfedit = 'false', $storeInUtc = null, $useCustomComposer = null, $composerFile = null) |
117
|
|
|
{ |
118
|
|
|
$moufManager->setVariable('tdbmDefaultDaoNamespace_'.$name, $daonamespace); |
119
|
|
|
$moufManager->setVariable('tdbmDefaultBeanNamespace_'.$name, $beannamespace); |
120
|
|
|
$moufManager->setVariable('tdbmDefaultDaoFactoryName_'.$name, $daofactoryclassname); |
121
|
|
|
$moufManager->setVariable('tdbmDefaultDaoFactoryInstanceName_'.$name, $daofactoryinstancename); |
122
|
|
|
$moufManager->setVariable('tdbmDefaultStoreInUtc_'.$name, $storeInUtc); |
123
|
|
|
$moufManager->setVariable('tdbmDefaultUseCustomComposer_'.$name, $useCustomComposer); |
124
|
|
|
$moufManager->setVariable('tdbmDefaultComposerFile_'.$name, $composerFile); |
125
|
|
|
|
126
|
|
|
// In case of instance renaming, let's use the last used settings |
127
|
|
|
$moufManager->setVariable('tdbmDefaultDaoNamespace', $daonamespace); |
128
|
|
|
$moufManager->setVariable('tdbmDefaultBeanNamespace', $beannamespace); |
129
|
|
|
$moufManager->setVariable('tdbmDefaultDaoFactoryName', $daofactoryclassname); |
130
|
|
|
$moufManager->setVariable('tdbmDefaultDaoFactoryInstanceName', $daofactoryinstancename); |
131
|
|
|
$moufManager->setVariable('tdbmDefaultStoreInUtc', $storeInUtc); |
132
|
|
|
$moufManager->setVariable('tdbmDefaultUseCustomComposer', $useCustomComposer); |
133
|
|
|
$moufManager->setVariable('tdbmDefaultComposerFile', $composerFile); |
134
|
|
|
|
135
|
|
|
// Remove first and last slash in namespace. |
136
|
|
|
$daonamespace = trim($daonamespace, '\\'); |
137
|
|
|
$beannamespace = trim($beannamespace, '\\'); |
138
|
|
|
|
139
|
|
|
$tdbmService = new InstanceProxy($name); |
140
|
|
|
/* @var $tdbmService TDBMService */ |
141
|
|
|
$tables = $tdbmService->generateAllDaosAndBeans($daofactoryclassname, $daonamespace, $beannamespace, $storeInUtc, ($useCustomComposer ? $composerFile : null)); |
|
|
|
|
142
|
|
|
|
143
|
|
|
$moufManager->declareComponent($daofactoryinstancename, $daonamespace.'\\Generated\\'.$daofactoryclassname, false, MoufManager::DECLARE_ON_EXIST_KEEP_INCOMING_LINKS); |
144
|
|
|
|
145
|
|
|
$tableToBeanMap = []; |
146
|
|
|
|
147
|
|
|
//$tdbmServiceDescriptor = $moufManager->getInstanceDescriptor('tdbmService'); |
|
|
|
|
148
|
|
|
|
149
|
|
|
foreach ($tables as $table) { |
150
|
|
|
$daoName = TDBMDaoGenerator::getDaoNameFromTableName($table); |
151
|
|
|
|
152
|
|
|
$instanceName = TDBMDaoGenerator::toVariableName($daoName); |
153
|
|
|
if (!$moufManager->instanceExists($instanceName)) { |
154
|
|
|
$moufManager->declareComponent($instanceName, $daonamespace.'\\'.$daoName); |
155
|
|
|
} |
156
|
|
|
$moufManager->setParameterViaConstructor($instanceName, 0, $name, 'object'); |
157
|
|
|
$moufManager->bindComponentViaSetter($daofactoryinstancename, 'set'.$daoName, $instanceName); |
158
|
|
|
|
159
|
|
|
$tableToBeanMap[$table] = $beannamespace.'\\'.TDBMDaoGenerator::getBeanNameFromTableName($table); |
160
|
|
|
} |
161
|
|
|
$tdbmServiceDescriptor = $moufManager->getInstanceDescriptor($name); |
162
|
|
|
$tdbmServiceDescriptor->getSetterProperty('setTableToBeanMap')->setValue($tableToBeanMap); |
163
|
|
|
$moufManager->rewriteMouf(); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.