Completed
Push — master ( 3ac426...d3ebfb )
by Craig
07:17
created

ControllerHelper::requirementsMet()   D

Complexity

Conditions 9
Paths 24

Size

Total Lines 43
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 9
eloc 28
nc 24
nop 1
dl 0
loc 43
rs 4.909
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * This file is part of the Zikula package.
5
 *
6
 * Copyright Zikula Foundation - http://zikula.org/
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Zikula\Bundle\CoreInstallerBundle\Helper;
13
14
use Symfony\Component\DependencyInjection\ContainerInterface;
15
use Zikula\Core\Exception\FatalErrorException;
16
use Zikula\Component\Wizard\StageInterface;
17
use Zikula\Bundle\CoreBundle\YamlDumper;
18
use Symfony\Component\Filesystem\Exception\IOException;
19
use Zikula\Component\Wizard\AbortStageException;
20
21
class ControllerHelper
22
{
23
    /**
24
     * return an array of variables to assign to all installer templates
25
     *
26
     * @return array
27
     */
28
    public function getTemplateGlobals(StageInterface $currentStage)
29
    {
30
        $globals = [
31
            'version' => \ZikulaKernel::VERSION,
32
            'currentstage' => $currentStage->getName()
33
        ];
34
35
        return array_merge($globals, $currentStage->getTemplateParams());
36
    }
37
38
    /**
39
     * Set up php for zikula install
40
     *
41
     * @throws FatalErrorException if settings are not capable of performing install or sustaining Zikula
42
     */
43
    public function initPhp()
44
    {
45
        $warnings = [];
46
        if (version_compare(\PHP_VERSION, '5.6.0', '<') && false === ini_set('mbstring.internal_encoding', 'UTF-8')) {
47
            // mbstring.internal_encoding is deprecated in php 5.6.0
48
            $currentSetting = ini_get('mbstring.internal_encoding');
49
            $warnings[] = __f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', ['ini_set', 'mbstring.internal_encoding', 'UTF-8', $currentSetting]);
50
        }
51 View Code Duplication
        if (false === ini_set('default_charset', 'UTF-8')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
52
            $currentSetting = ini_get('default_charset');
53
            $warnings[] = __f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', ['ini_set', 'default_charset', 'UTF-8', $currentSetting]);
54
        }
55
        if (false === mb_regex_encoding('UTF-8')) {
56
            $currentSetting = mb_regex_encoding();
57
            $warnings[] = __f('Could not set %1$s to the value of %2$s. The install or upgrade process may fail at your current setting of %3$s.', ['mb_regex_encoding', 'UTF-8', $currentSetting]);
58
        }
59 View Code Duplication
        if (false === ini_set('memory_limit', '128M')) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
60
            $currentSetting = ini_get('memory_limit');
61
            $warnings[] = __f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', ['ini_set', 'memory_limit', '128M', $currentSetting]);
62
        }
63 View Code Duplication
        if (false === ini_set('max_execution_time', 86400)) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
64
            // 86400 = 24 hours
65
            $currentSetting = ini_get('max_execution_time');
66
            if ($currentSetting > 0) {
67
                // 0 = unlimited time
68
                $warnings[] = __f('Could not use %1$s to set the %2$s to the value of %3$s. The install or upgrade process may fail at your current setting of %4$s.', ['ini_set', 'max_execution_time', '86400', $currentSetting]);
69
            }
70
        }
71
72
        return $warnings;
73
    }
74
75
    public function requirementsMet(ContainerInterface $container)
76
    {
77
        // several other requirements are checked before Symfony is loaded.
78
        // @see app/SymfonyRequirements.php
79
        // @see \Zikula\Bundle\CoreInstallerBundle\Util\ZikulaRequirements::runSymfonyChecks
80
        $results = [];
81
82
        $x = explode('.', str_replace('-', '.', phpversion()));
83
        $phpVersion = "$x[0].$x[1].$x[2]";
84
        $results['phpsatisfied'] = version_compare($phpVersion, \ZikulaKernel::PHP_MINIMUM_VERSION, ">=");
85
86
        $results['pdo'] = extension_loaded('pdo');
87
        $isEnabled = @preg_match('/^\p{L}+$/u', 'TheseAreLetters');
88
        $results['pcreUnicodePropertiesEnabled'] = (isset($isEnabled) && (bool)$isEnabled);
89
        $rootDir = $container->get('kernel')->getRootDir();
90
        if ($container->hasParameter('upgrading') && $container->getParameter('upgrading') === true) {
91
            $files = [
92
                'custom_parameters' => '/config/custom_parameters.yml'
93
            ];
94
            foreach ($files as $key => $file) {
95
                $path = realpath($rootDir . $file);
96
                if ($path === false) {
97
                    $results[$key] = false;
98
                } else {
99
                    $results[$key] = is_writable($path);
100
                }
101
            }
102
        }
103
        $requirementsMet = true;
104
        foreach ($results as $check) {
105
            if (!$check) {
106
                $requirementsMet = false;
107
                break;
108
            }
109
        }
110
        if ($requirementsMet) {
111
            return true;
112
        }
113
        $results['phpversion'] = phpversion();
114
        $results['phpcoreminversion'] = \ZikulaKernel::PHP_MINIMUM_VERSION;
115
116
        return $results;
117
    }
118
119
    /**
120
     * Write admin credentials to param file as encoded values
121
     *
122
     * @param YamlDumper $yamlManager
123
     * @param array $data
124
     * @throws AbortStageException
125
     */
126
    public function writeEncodedAdminCredentials(YamlDumper $yamlManager, array $data)
127
    {
128
        foreach ($data as $k => $v) {
129
            $data[$k] = base64_encode($v); // encode so values are 'safe' for json
130
        }
131
        $params = array_merge($yamlManager->getParameters(), $data);
132
        try {
133
            $yamlManager->setParameters($params);
134
        } catch (IOException $e) {
135
            throw new AbortStageException(__f('Cannot write parameters to %s file.', 'custom_parameters.yml'));
136
        }
137
    }
138
}
139