Completed
Pull Request — master (#4293)
by Craig
05:00
created

CoreInstallerExtensionHelper::categorize()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 30
Code Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 24
c 0
b 0
f 0
nc 2
nop 0
dl 0
loc 30
rs 9.536
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Zikula package.
7
 *
8
 * Copyright Zikula - https://ziku.la/
9
 *
10
 * For the full copyright and license information, please view the LICENSE
11
 * file that was distributed with this source code.
12
 */
13
14
namespace Zikula\Bundle\CoreInstallerBundle\Helper;
15
16
use Doctrine\Persistence\ManagerRegistry;
17
use Symfony\Component\Filesystem\Filesystem;
18
use Symfony\Component\HttpFoundation\Session\SessionInterface;
19
use Symfony\Contracts\Translation\TranslatorInterface;
20
use Zikula\BlocksModule\Entity\BlockEntity;
21
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaHttpKernelInterface;
22
use Zikula\Bundle\CoreBundle\HttpKernel\ZikulaKernel;
23
use Zikula\Bundle\CoreBundle\YamlDumper;
24
use Zikula\ExtensionsModule\AbstractExtension;
25
use Zikula\ExtensionsModule\Api\ApiInterface\VariableApiInterface;
26
use Zikula\ExtensionsModule\Api\VariableApi;
27
use Zikula\ExtensionsModule\Collector\InstallerCollector;
28
use Zikula\ExtensionsModule\Constant;
29
use Zikula\ExtensionsModule\Entity\ExtensionEntity;
30
use Zikula\ExtensionsModule\Helper\BundleSyncHelper;
31
use Zikula\ExtensionsModule\Helper\ExtensionHelper;
32
33
class CoreInstallerExtensionHelper
34
{
35
    /**
36
     * @var ZikulaHttpKernelInterface
37
     */
38
    private $kernel;
39
40
    /**
41
     * @var ManagerRegistry
42
     */
43
    private $managerRegistry;
44
45
    /**
46
     * @var TranslatorInterface
47
     */
48
    private $translator;
49
50
    /**
51
     * @var YamlDumper
52
     */
53
    private $yamlHelper;
54
55
    /**
56
     * @var BundleSyncHelper
57
     */
58
    private $bundleSyncHelper;
59
60
    /**
61
     * @var ExtensionHelper
62
     */
63
    private $extensionHelper;
64
65
    /**
66
     * @var InstallerCollector
67
     */
68
    private $installerCollector;
69
70
    /**
71
     * @var VariableApiInterface
72
     */
73
    private $variableApi;
74
75
    /**
76
     * @var SessionInterface
77
     */
78
    private $session;
79
80
    private $adminCategoryHelper;
81
82
    public function __construct(
83
        ZikulaHttpKernelInterface $kernel,
84
        ManagerRegistry $managerRegistry,
85
        TranslatorInterface $translator,
86
        ParameterHelper $parameterHelper,
87
        BundleSyncHelper $bundleSyncHelper,
88
        ExtensionHelper $extensionHelper,
89
        InstallerCollector $installerCollector,
90
        VariableApiInterface $variableApi,
91
        SessionInterface $session,
92
        AdminCategoryHelper $adminCategoryHelper
93
    ) {
94
        $this->kernel = $kernel;
95
        $this->managerRegistry = $managerRegistry;
96
        $this->translator = $translator;
97
        $this->yamlHelper = $parameterHelper->getYamlHelper();
98
        $this->bundleSyncHelper = $bundleSyncHelper;
99
        $this->extensionHelper = $extensionHelper;
100
        $this->installerCollector = $installerCollector;
101
        $this->variableApi = $variableApi;
102
        $this->session = $session;
103
        $this->adminCategoryHelper = $adminCategoryHelper;
104
    }
105
106
    public function install(string $extensionName): bool
107
    {
108
        $extensionBundle = $this->kernel->getModule($extensionName);
109
        /** @var AbstractExtension $extensionBundle */
110
        $className = $extensionBundle->getInstallerClass();
111
        $installer = $this->installerCollector->get($className);
112
113
        if ($installer->install()) {
114
            return true;
115
        }
116
117
        return false;
118
    }
119
120
    /**
121
     * Scan the filesystem and sync the extensions table. Set all system extensions to active state.
122
     */
123
    public function reSyncAndActivate(): bool
124
    {
125
        $extensionsInFileSystem = $this->bundleSyncHelper->scanForBundles(true);
126
        $this->bundleSyncHelper->syncExtensions($extensionsInFileSystem);
127
128
        /** @var ExtensionEntity[] $extensions */
129
        $extensions = $this->managerRegistry->getRepository('ZikulaExtensionsModule:ExtensionEntity')
130
            ->findBy(['name' => array_keys(ZikulaKernel::$coreExtension)]);
131
        foreach ($extensions as $extension) {
132
            $extension->setState(Constant::STATE_ACTIVE);
133
        }
134
        $this->managerRegistry->getManager()->flush();
135
136
        return true;
137
    }
138
139
    /**
140
     * Attempt to upgrade ALL the core extensions. Some will need it, some will not.
141
     * Extensions that do not need upgrading return TRUE as a result of the upgrade anyway.
142
     */
143
    public function upgrade(): bool
144
    {
145
        $coreModulesInPriorityUpgradeOrder = [
146
            'ZikulaExtensionsModule',
147
            'ZikulaUsersModule',
148
            'ZikulaZAuthModule',
149
            'ZikulaGroupsModule',
150
            'ZikulaPermissionsModule',
151
            'ZikulaAdminModule',
152
            'ZikulaBlocksModule',
153
            'ZikulaThemeModule',
154
            'ZikulaSettingsModule',
155
            'ZikulaCategoriesModule',
156
            'ZikulaSecurityCenterModule',
157
            'ZikulaRoutesModule',
158
            'ZikulaMailerModule',
159
            'ZikulaSearchModule',
160
            'ZikulaMenuModule',
161
            'ZikulaBootstrapTheme',
162
            'ZikulaAtomTheme',
163
            'ZikulaRssTheme',
164
            'ZikulaPrinterTheme',
165
        ];
166
        $result = true;
167
        foreach ($coreModulesInPriorityUpgradeOrder as $moduleName) {
168
            $extensionEntity = $this->managerRegistry->getRepository(ExtensionEntity::class)->get($moduleName);
169
            if (isset($extensionEntity)) {
170
                $result = $result && $this->extensionHelper->upgrade($extensionEntity);
171
            }
172
        }
173
174
        return $result;
175
    }
176
177
    public function executeCoreMetaUpgrade($currentCoreVersion): bool
178
    {
179
        /**
180
         * NOTE: There are *intentionally* no `break` statements within each case here so that the process continues
181
         * through each case until the end.
182
         */
183
        switch ($currentCoreVersion) {
184
            case '1.4.3':
185
                $this->install('ZikulaMenuModule');
186
                $this->reSyncAndActivate();
187
                $this->adminCategoryHelper->setCategory('ZikulaMenuModule', $this->translator->trans('Content'));
188
            case '1.4.4':
189
                // nothing
190
            case '1.4.5':
191
                // Menu module was introduced in 1.4.4 but not installed on upgrade
192
                $schemaManager = $this->managerRegistry->getConnection()->getSchemaManager();
193
                if (!$schemaManager->tablesExist(['menu_items'])) {
194
                    $this->install('ZikulaMenuModule');
195
                    $this->reSyncAndActivate();
196
                    $this->adminCategoryHelper->setCategory('ZikulaMenuModule', $this->translator->trans('Content'));
197
                }
198
            case '1.4.6':
199
                // nothing needed
200
            case '1.4.7':
201
                // nothing needed
202
            case '1.5.0':
203
                // nothing needed
204
            case '1.9.99':
205
                // upgrades required for 2.0.0
206
                foreach (['objectdata_attributes', 'objectdata_log', 'objectdata_meta', 'workflows'] as $table) {
207
                    $sql = "DROP TABLE ${table};";
208
                    /** @var \Doctrine\DBAL\Driver\PDOConnection $connection */
209
                    $connection = $this->managerRegistry->getConnection();
210
                    $stmt = $connection->prepare($sql);
211
                    $stmt->execute();
212
                    $stmt->closeCursor();
213
                }
214
                $this->variableApi->del(VariableApi::CONFIG, 'metakeywords');
215
                $this->variableApi->del(VariableApi::CONFIG, 'startpage');
216
                $this->variableApi->del(VariableApi::CONFIG, 'startfunc');
217
                $this->variableApi->del(VariableApi::CONFIG, 'starttype');
218
                if ('userdata' === $this->yamlHelper->getParameter('datadir')) {
219
                    $this->yamlHelper->setParameter('datadir', 'public/uploads');
220
                    $fs = new Filesystem();
221
                    $src = $this->kernel->getProjectDir();
222
                    try {
223
                        if ($fs->exists($src . '/userdata')) {
224
                            $fs->mirror($src . '/userdata', $src . '/public/uploads');
225
                        }
226
                    } catch (\Exception $exception) {
227
                        $this->session->getFlashBag()->add(
228
                            'info',
229
                            'Attempt to copy files from `userdata` to `public/uploads` failed. You must manually copy the contents.'
230
                        );
231
                    }
232
                }
233
                // remove legacy blocks
234
                $blocksToRemove = $this->managerRegistry->getRepository(BlockEntity::class)->findBy(['blocktype' => ['Extmenu', 'Menutree', 'Menu']]);
235
                foreach ($blocksToRemove as $block) {
236
                    $this->managerRegistry->getManager()->remove($block);
237
                }
238
                $this->managerRegistry->getManager()->flush();
239
            case '2.0.15':
240
                // nothing
241
            case '3.0.0':
242
                // current version - cannot perform anything yet
243
        }
244
245
        // always do this
246
        $this->reSyncAndActivate();
247
        // set default themes to ZikulaBootstrapTheme
248
        $this->variableApi->set(VariableApi::CONFIG, 'Default_Theme', 'ZikulaBootstrapTheme');
249
        $this->variableApi->set('ZikulaAdminModule', 'admintheme', '');
250
        // unset start page information to avoid missing module errors
251
        $this->variableApi->set(VariableApi::CONFIG, 'startController_en', '');
252
253
        return true;
254
    }
255
}
256