Passed
Branch feature/6.x (3df42d)
by Schlaefer
08:49
created

AppController::beforeRender()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 9
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Saito - The Threaded Web Forum
7
 *
8
 * @copyright Copyright (c) the Saito Project Developers
9
 * @link https://github.com/Schlaefer/Saito
10
 * @license http://opensource.org/licenses/MIT
11
 */
12
13
namespace Installer\Controller;
14
15
use App\Model\Table\SettingsTable;
16
use Cake\Cache\Cache;
17
use Cake\Controller\Controller;
18
use Cake\Core\Configure;
19
use Cake\Datasource\ConnectionManager;
20
use Cake\Event\EventInterface;
21
use Cake\I18n\I18n;
22
use Cake\Log\LogTrait;
23
use Cake\ORM\Table;
24
use Migrations\Migrations;
25
26
/**
27
 * Installer App Controller
28
 *
29
 * @property SettingsTable $Settings
30
 */
31
class AppController extends Controller
32
{
33
    use LogTrait;
34
35
    /** @var Migrations */
36
    protected $migrations;
37
38
    /**
39
     * {@inheritDoc}
40
     */
41
    public function initialize(): void
42
    {
43
        Cache::clear();
44
        Cache::disable();
45
46
        parent::initialize();
47
48
        $this->loadModel('Settings');
49
        $this->migrations = $this->initializeMigrations($this->Settings);
50
51
        $locale = Configure::read('Saito.language');
52
        I18n::setLocale($locale);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function beforeRender(EventInterface $event)
59
    {
60
        parent::beforeRender($event);
61
        $this->viewBuilder()->setHelpers([
62
            'Breadcrumbs' => ['className' => 'BootstrapUI.Breadcrumbs'],
63
            'Flash' => ['className' => 'BootstrapUI.Flash'],
64
            'Form' => ['className' => 'BootstrapUI.Form'],
65
            'Html' => ['className' => 'BootstrapUI.Html'],
66
            'Paginator' => ['className' => 'BootstrapUI.Paginator'],
67
        ]);
68
    }
69
70
    /**
71
     * Initialize migration property
72
     *
73
     * @param Table $table a table to read the config from
74
     * @return Migrations
75
     */
76
    private function initializeMigrations(Table $table)
77
    {
78
        $installerConfigName = 'installer';
79
        // if: static configuration only allowed once, but done multiple times in test-cases
80
        if (ConnectionManager::getConfig($installerConfigName) === null) {
81
            $defaultConfigName = $table->getConnection()->configName();
82
            $connectionConfig = ConnectionManager::getConfig($defaultConfigName);
83
            $connectionConfig['quoteIdentifiers'] = true;
84
            ConnectionManager::setConfig($installerConfigName, $connectionConfig);
85
        }
86
87
        return new Migrations(['connection' => $installerConfigName]);
88
    }
89
}
90