Passed
Pull Request — 4.3 (#142)
by Jean-Baptiste
04:20
created

TdbmInstallController::displayErrorMsg()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 4
nc 1
nop 1
1
<?php
2
3
namespace Mouf\Database\TDBM\Controllers;
4
5
use Mouf\Composer\ClassNameMapper;
6
use Mouf\Actions\InstallUtils;
7
use Mouf\Console\ConsoleUtils;
8
use Mouf\Database\TDBM\Commands\GenerateCommand;
9
use Mouf\Database\TDBM\Configuration;
10
use Mouf\Database\TDBM\MoufConfiguration;
11
use Mouf\Database\TDBM\Utils\DefaultNamingStrategy;
12
use Mouf\Database\TDBM\Utils\MoufDiListener;
13
use Mouf\MoufManager;
14
use Mouf\Html\HtmlElement\HtmlBlock;
15
use Mouf\Mvc\Splash\Controllers\Controller;
16
17
/**
18
 * The controller used in the TDBM install process.
19
 *
20
 * @Component
21
 */
22
class TdbmInstallController extends Controller
23
{
24
    /**
25
     * @var HtmlBlock
26
     */
27
    public $content;
28
29
    public $selfedit;
30
31
    /**
32
     * The active MoufManager to be edited/viewed.
33
     *
34
     * @var MoufManager
35
     */
36
    public $moufManager;
37
38
    /**
39
     * The template used by the main page for mouf.
40
     *
41
     * @Property
42
     * @Compulsory
43
     *
44
     * @var TemplateInterface
45
     */
46
    public $template;
47
48
    /**
49
     * Displays the first install screen.
50
     *
51
     * @Action
52
     * @Logged
53
     *
54
     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
55
     */
56
    public function defaultAction($selfedit = 'false')
57
    {
58
        $this->selfedit = $selfedit;
59
60
        if ($selfedit == 'true') {
61
            $this->moufManager = MoufManager::getMoufManager();
62
        } else {
63
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
64
        }
65
66
        $this->content->addFile(dirname(__FILE__).'/../../../../views/installStep1.php', $this);
67
        $this->template->toHtml();
68
    }
69
70
    /**
71
     * Skips the install process.
72
     *
73
     * @Action
74
     * @Logged
75
     *
76
     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
77
     */
78
    public function skip($selfedit = 'false')
79
    {
80
        InstallUtils::continueInstall($selfedit == 'true');
81
    }
82
83
    protected $daoNamespace;
84
    protected $beanNamespace;
85
    protected $autoloadDetected;
86
    //protected $storeInUtc;
87
    protected $useCustomComposer = false;
88
    protected $composerFile;
89
90
    /**
91
     * Displays the second install screen.
92
     *
93
     * @Action
94
     * @Logged
95
     *
96
     * @param string $selfedit If true, the name of the component must be a component from the Mouf framework itself (internal use only)
97
     */
98
    public function configure($selfedit = 'false')
99
    {
100
        $this->selfedit = $selfedit;
101
102
        if ($selfedit == 'true') {
103
            $this->moufManager = MoufManager::getMoufManager();
104
        } else {
105
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
106
        }
107
108
        // Let's start by performing basic checks about the instances we assume to exist.
109
        if (!$this->moufManager->instanceExists('dbalConnection')) {
110
            $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.");
111
112
            return;
113
        }
114
115
        if ($this->moufManager->has('tdbmConfiguration')) {
116
            $tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
117
118
            $this->beanNamespace = $tdbmConfiguration->getConstructorArgumentProperty('beanNamespace')->getValue();
119
            $this->daoNamespace = $tdbmConfiguration->getConstructorArgumentProperty('daoNamespace')->getValue();
120
        } else {
121
            // Old TDBM 4.2 fallback
122
            $this->daoNamespace = $this->moufManager->getVariable('tdbmDefaultDaoNamespace_tdbmService');
123
            $this->beanNamespace = $this->moufManager->getVariable('tdbmDefaultBeanNamespace_tdbmService');
124
        }
125
126
        if ($this->daoNamespace == null && $this->beanNamespace == null) {
127
            $classNameMapper = ClassNameMapper::createFromComposerFile(__DIR__.'/../../../../../../../../composer.json');
128
129
            $autoloadNamespaces = $classNameMapper->getManagedNamespaces();
130
            if ($autoloadNamespaces) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $autoloadNamespaces of type array<integer|string> is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
131
                $this->autoloadDetected = true;
132
                $rootNamespace = $autoloadNamespaces[0];
133
                $this->daoNamespace = $rootNamespace.'Dao';
134
                $this->beanNamespace = $rootNamespace.'Model';
135
            } else {
136
                $this->autoloadDetected = false;
137
                $this->daoNamespace = 'YourApplication\\Dao';
138
                $this->beanNamespace = 'YourApplication\\Model';
139
            }
140
        } else {
141
            $this->autoloadDetected = true;
142
        }
143
        $this->defaultPath = true;
144
        $this->storePath = '';
145
146
        $this->castDatesToDateTime = true;
147
148
        $this->content->addFile(__DIR__.'/../../../../views/installStep2.php', $this);
149
        $this->template->toHtml();
150
    }
151
152
    /**
153
     * This action generates the TDBM instance, then the DAOs and Beans.
154
     *
155
     * @Action
156
     *
157
     * @param string $daonamespace
158
     * @param string $beannamespace
159
     * @param string $selfedit
160
     *
161
     * @throws \Mouf\MoufException
162
     */
163
    public function generate($daonamespace, $beannamespace, /*$storeInUtc = 0,*/ $selfedit = 'false', $defaultPath = false, $storePath = '')
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
164
    {
165
        $this->selfedit = $selfedit;
166
167
        if ($selfedit == 'true') {
168
            $this->moufManager = MoufManager::getMoufManager();
169
        } else {
170
            $this->moufManager = MoufManager::getMoufManagerHiddenInstance();
171
        }
172
173
        $doctrineCache = $this->moufManager->getInstanceDescriptor('defaultDoctrineCache');
174
175
        $migratingFrom42 = false;
176
        if ($this->moufManager->has('tdbmService') && !$this->moufManager->has('tdbmConfiguration')) {
177
            $migratingFrom42 = true;
178
        }
179
180
        $namingStrategy = InstallUtils::getOrCreateInstance('namingStrategy', DefaultNamingStrategy::class, $this->moufManager);
181
        if ($migratingFrom42) {
182
            // Let's setup the naming strategy for compatibility
183
            $namingStrategy->getSetterProperty('setBeanPrefix')->setValue('');
184
            $namingStrategy->getSetterProperty('setBeanSuffix')->setValue('Bean');
185
            $namingStrategy->getSetterProperty('setBaseBeanPrefix')->setValue('');
186
            $namingStrategy->getSetterProperty('setBaseBeanSuffix')->setValue('BaseBean');
187
            $namingStrategy->getSetterProperty('setDaoPrefix')->setValue('');
188
            $namingStrategy->getSetterProperty('setDaoSuffix')->setValue('Dao');
189
            $namingStrategy->getSetterProperty('setBaseDaoPrefix')->setValue('');
190
            $namingStrategy->getSetterProperty('setBaseDaoSuffix')->setValue('BaseDao');
191
        }
192
193
        if (!$this->moufManager->instanceExists('tdbmConfiguration')) {
194
            $moufListener = InstallUtils::getOrCreateInstance(MoufDiListener::class, MoufDiListener::class, $this->moufManager);
195
196
            $tdbmConfiguration = $this->moufManager->createInstance(MoufConfiguration::class)->setName('tdbmConfiguration');
197
            $tdbmConfiguration->getConstructorArgumentProperty('connection')->setValue($this->moufManager->getInstanceDescriptor('dbalConnection'));
198
            $tdbmConfiguration->getConstructorArgumentProperty('cache')->setValue($doctrineCache);
199
            $tdbmConfiguration->getConstructorArgumentProperty('namingStrategy')->setValue($namingStrategy);
200
            $tdbmConfiguration->getProperty('daoFactoryInstanceName')->setValue('daoFactory');
201
            $tdbmConfiguration->getConstructorArgumentProperty('generatorListeners')->setValue([$moufListener]);
202
203
            // Let's also delete the tdbmService if migrating versions <= 4.2
204
            if ($migratingFrom42) {
205
                $this->moufManager->removeComponent('tdbmService');
206
            }
207
        } else {
208
            $tdbmConfiguration = $this->moufManager->getInstanceDescriptor('tdbmConfiguration');
209
        }
210
211
        if (!$this->moufManager->instanceExists('tdbmService')) {
212
            $tdbmService = $this->moufManager->createInstance('Mouf\\Database\\TDBM\\TDBMService')->setName('tdbmService');
213
            $tdbmService->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
214
        }
215
216
        // We declare our instance of the Symfony command as a Mouf instance
217
        $generateCommand = InstallUtils::getOrCreateInstance('generateCommand', GenerateCommand::class, $this->moufManager);
218
        $generateCommand->getConstructorArgumentProperty('configuration')->setValue($tdbmConfiguration);
219
220
        // We register that instance descriptor using "ConsoleUtils"
221
        $consoleUtils = new ConsoleUtils($this->moufManager);
222
        $consoleUtils->registerCommand($generateCommand);
223
224
        $this->moufManager->rewriteMouf();
225
226
        TdbmController::generateDaos($this->moufManager, 'tdbmService', $daonamespace, $beannamespace, 'daoFactory', $selfedit, /*$storeInUtc,*/ $defaultPath, $storePath);
227
228
        InstallUtils::continueInstall($selfedit == 'true');
229
    }
230
231
    protected $errorMsg;
232
233
    private function displayErrorMsg($msg)
234
    {
235
        $this->errorMsg = $msg;
236
        $this->content->addFile(__DIR__.'/../../../../views/installError.php', $this);
237
        $this->template->toHtml();
238
    }
239
}
240