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 $daoFactoryInstanceName; |
29
|
|
|
protected $autoloadDetected; |
30
|
|
|
///protected $storeInUtc; |
31
|
|
|
protected $useCustomComposer; |
32
|
|
|
protected $composerFile; |
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
|
|
|
$this->daoNamespace = self::getFromConfiguration($this->moufManager, $name, 'daoNamespace'); |
45
|
|
|
$this->beanNamespace = self::getFromConfiguration($this->moufManager, $name, 'beanNamespace'); |
46
|
|
|
$this->daoFactoryInstanceName = self::getFromConfiguration($this->moufManager, $name, 'daoFactoryInstanceName'); |
47
|
|
|
//$this->storeInUtc = self::getFromConfiguration($this->moufManager, $name, 'storeInUtc'); |
|
|
|
|
48
|
|
|
$this->composerFile = self::getFromConfiguration($this->moufManager, $name, 'customComposerFile'); |
49
|
|
|
$this->useCustomComposer = $this->composerFile ? true : false; |
50
|
|
|
|
51
|
|
|
if ($this->daoNamespace == null && $this->beanNamespace == null) { |
52
|
|
|
$classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json'); |
53
|
|
|
|
54
|
|
|
$autoloadNamespaces = $classNameMapper->getManagedNamespaces(); |
55
|
|
|
if ($autoloadNamespaces) { |
|
|
|
|
56
|
|
|
$this->autoloadDetected = true; |
57
|
|
|
$rootNamespace = $autoloadNamespaces[0]; |
58
|
|
|
$this->daoNamespace = $rootNamespace.'Dao'; |
59
|
|
|
$this->beanNamespace = $rootNamespace.'Dao\\Bean'; |
60
|
|
|
} else { |
61
|
|
|
$this->autoloadDetected = false; |
62
|
|
|
$this->daoNamespace = 'YourApplication\\Dao'; |
63
|
|
|
$this->beanNamespace = 'YourApplication\\Dao\\Bean'; |
64
|
|
|
} |
65
|
|
|
} else { |
66
|
|
|
$this->autoloadDetected = true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$this->content->addFile(__DIR__.'/../../../../views/tdbmGenerate.php', $this); |
70
|
|
|
$this->template->toHtml(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* This action generates the DAOs and Beans for the TDBM service passed in parameter. |
75
|
|
|
* |
76
|
|
|
* @Action |
77
|
|
|
* |
78
|
|
|
* @param string $name |
79
|
|
|
* @param bool $selfedit |
80
|
|
|
*/ |
81
|
|
|
public function generate($name, $daonamespace, $beannamespace, $daofactoryinstancename, /*$storeInUtc = 0,*/ $selfedit = 'false', $useCustomComposer = false, $composerFile = '') |
|
|
|
|
82
|
|
|
{ |
83
|
|
|
$this->initController($name, $selfedit); |
84
|
|
|
|
85
|
|
|
self::generateDaos($this->moufManager, $name, $daonamespace, $beannamespace, $daofactoryinstancename, $selfedit, /*$storeInUtc,*/ $useCustomComposer, $composerFile); |
86
|
|
|
|
87
|
|
|
// TODO: better: we should redirect to a screen that list the number of DAOs generated, etc... |
88
|
|
|
header('Location: '.ROOT_URL.'ajaxinstance/?name='.urlencode($name).'&selfedit='.$selfedit); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* This function generates the DAOs and Beans for the TDBM service passed in parameter. |
93
|
|
|
* |
94
|
|
|
* @param MoufManager $moufManager |
95
|
|
|
* @param string $name |
96
|
|
|
* @param string $daonamespace |
97
|
|
|
* @param string $beannamespace |
98
|
|
|
* @param string $daofactoryclassname |
|
|
|
|
99
|
|
|
* @param string $daofactoryinstancename |
100
|
|
|
* @param string $selfedit |
101
|
|
|
* |
102
|
|
|
* @throws \Mouf\MoufException |
103
|
|
|
*/ |
104
|
|
|
public static function generateDaos(MoufManager $moufManager, $name, $daonamespace, $beannamespace, $daofactoryinstancename, $selfedit = 'false', /*$storeInUtc = null,*/ $useCustomComposer = null, $composerFile = null) |
|
|
|
|
105
|
|
|
{ |
106
|
|
|
self::setInConfiguration($moufManager, $name, 'daoNamespace', $daonamespace); |
107
|
|
|
self::setInConfiguration($moufManager, $name, 'beanNamespace', $beannamespace); |
108
|
|
|
self::setInConfiguration($moufManager, $name, 'daoFactoryInstanceName', $daofactoryinstancename); |
109
|
|
|
//self::setInConfiguration($moufManager, $name, 'storeInUtc', $storeInUtc); |
|
|
|
|
110
|
|
|
if ($useCustomComposer) { |
111
|
|
|
self::setInConfiguration($moufManager, $name, 'customComposerFile', $composerFile); |
112
|
|
|
} else { |
113
|
|
|
self::setInConfiguration($moufManager, $name, 'customComposerFile', null); |
114
|
|
|
} |
115
|
|
|
// Let's rewrite before calling the DAO generator |
116
|
|
|
$moufManager->rewriteMouf(); |
117
|
|
|
|
118
|
|
|
|
119
|
|
|
$tdbmService = new InstanceProxy($name); |
120
|
|
|
/* @var $tdbmService TDBMService */ |
121
|
|
|
$tdbmService->generateAllDaosAndBeans(($useCustomComposer ? $composerFile : null)); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
private static function getConfigurationDescriptor(MoufManager $moufManager, string $tdbmInstanceName) |
125
|
|
|
{ |
126
|
|
|
return $moufManager->getInstanceDescriptor($tdbmInstanceName)->getConstructorArgumentProperty('configuration')->getValue(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
private static function getFromConfiguration(MoufManager $moufManager, string $tdbmInstanceName, string $property) |
130
|
|
|
{ |
131
|
|
|
$configuration = self::getConfigurationDescriptor($moufManager, $tdbmInstanceName); |
132
|
|
|
if ($configuration === null) { |
133
|
|
|
throw new \RuntimeException('Unable to find the configuration object linked to TDBMService.'); |
134
|
|
|
} |
135
|
|
|
return $configuration->getProperty($property)->getValue(); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
private static function setInConfiguration(MoufManager $moufManager, string $tdbmInstanceName, string $property, ?string $value) |
139
|
|
|
{ |
140
|
|
|
$configuration = self::getConfigurationDescriptor($moufManager, $tdbmInstanceName); |
141
|
|
|
if ($configuration === null) { |
142
|
|
|
throw new \RuntimeException('Unable to find the configuration object linked to TDBMService.'); |
143
|
|
|
} |
144
|
|
|
$configuration->getProperty($property)->setValue($value); |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.