Issues (153)

Classes/Base.php (2 issues)

Labels
1
<?php
2
3
/**
4
 * @license GPLv3, http://www.gnu.org/copyleft/gpl.html
5
 * @copyright Metaways Infosystems GmbH, 2014
6
 * @copyright Aimeos (aimeos.org), 2014-2017
7
 * @package TYPO3
8
 */
9
10
11
namespace Aimeos\Aimeos;
12
13
use \TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser;
14
use \TYPO3\CMS\Core\Utility\GeneralUtility;
15
16
17
/**
18
 * Aimeos base class with common functionality.
19
 *
20
 * @package TYPO3
21
 */
22
class Base
23
{
24
    private static $extConfig;
25
26
27
    /**
28
     * Returns the Aimeos bootstrap object
29
     *
30
     * @return \Aimeos\Bootstrap Aimeos bootstrap object
31
     */
32
    public static function aimeos() : \Aimeos\Bootstrap
33
    {
34
        $name = 'Aimeos\Aimeos\Base\Aimeos';
35
36
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos'])) {
37
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos']) instanceof \Closure) {
38
                return $name();
39
            }
40
        }
41
42
        return $name::get();
43
    }
44
45
46
    /**
47
     * Creates a new configuration object
48
     *
49
     * @param array $local Multi-dimensional associative list with local configuration
50
     * @return \Aimeos\Base\Config\Iface Configuration object
51
     */
52
    public static function config(array $local = []) : \Aimeos\Base\Config\Iface
53
    {
54
        $name = 'Aimeos\Aimeos\Base\Config';
55
56
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_config'])) {
57
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_config']) instanceof \Closure) {
58
                return $name(self::aimeos()->getConfigPaths(), $local);
59
            }
60
        }
61
62
        return $name::get(self::aimeos()->getConfigPaths(), $local);
63
    }
64
65
66
    /**
67
     * Returns the current context
68
     *
69
     * @param \Aimeos\Base\Config\Iface Configuration object
70
     * @return \Aimeos\MShop\ContextIface Context object
71
     */
72
    public static function context(\Aimeos\Base\Config\Iface $config) : \Aimeos\MShop\ContextIface
73
    {
74
        $name = 'Aimeos\Aimeos\Base\Context';
75
76
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context'])) {
77
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_context']) instanceof \Closure) {
78
                return $name($config);
79
            }
80
        }
81
82
        return $name::get($config);
83
    }
84
85
86
    /**
87
     * Returns the extension configuration
88
     *
89
     * @param string Name of the configuration setting
90
     * @param mixed Value returned if no value in extension configuration was found
91
     * @return mixed Value associated with the configuration setting
92
     */
93
    public static function getExtConfig(string $name, $default = null)
94
    {
95
        if (self::$extConfig === null) {
96
            self::$extConfig = GeneralUtility::makeInstance('TYPO3\CMS\Core\Configuration\ExtensionConfiguration')->get('aimeos');
97
        }
98
99
        if (isset(self::$extConfig[$name])) {
100
            return self::$extConfig[$name];
101
        }
102
103
        return $default;
104
    }
105
106
107
    /**
108
     * Creates new translation objects
109
     *
110
     * @param array $languageIds List of two letter ISO language IDs
111
     * @param array $local List of local translation entries overwriting the standard ones
112
     * @return array List of translation objects implementing MW_Translation_Interface
113
     */
114
    public static function i18n(array $languageIds, array $local = []) : array
115
    {
116
        $name = 'Aimeos\Aimeos\Base\I18n';
117
118
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_i18n'])) {
119
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_i18n']) instanceof \Closure) {
120
                return $name(self::aimeos()->getI18nPaths(), $languageIds, $local);
121
            }
122
        }
123
124
        return $name::get(self::aimeos()->getI18nPaths(), $languageIds, $local);
125
    }
126
127
128
    /**
129
     * Creates a new locale object
130
     *
131
     * @param \Aimeos\MShop\ContextIface $context Context object
132
     * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface|null $request Request object
133
     * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
134
     */
135
    public static function locale(\Aimeos\MShop\ContextIface $context,
136
        \TYPO3\CMS\Extbase\Mvc\RequestInterface $request = null) : \Aimeos\MShop\Locale\Item\Iface
137
    {
138
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale_frontend'])) {
139
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale_frontend']) instanceof \Closure) {
140
                return $name($context, $request);
141
            }
142
        }
143
144
        $name = 'Aimeos\Aimeos\Base\Locale';
145
146
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale'])) {
147
            $name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale'];
148
        }
149
150
        return $name::get($context, $request);
151
    }
152
153
154
    /**
155
     * Creates a new locale object
156
     *
157
     * @param \Aimeos\MShop\ContextIface $context Context object
158
     * @param string $sitecode Unique site code
159
     * @return \Aimeos\MShop\Locale\Item\Iface Locale item object
160
     */
161
    public static function getLocaleBackend(\Aimeos\MShop\ContextIface $context,
162
        string $sitecode) : \Aimeos\MShop\Locale\Item\Iface
163
    {
164
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale_backend'])) {
165
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale_backend']) instanceof \Closure) {
166
                return $name($context, $sitecode);
167
            }
168
        }
169
170
        $name = 'Aimeos\Aimeos\Base\Locale';
171
172
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale'])) {
173
            $name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_locale'];
174
        }
175
176
        return $name::getBackend($context, $sitecode);
177
    }
178
179
180
    /**
181
     * Returns the version of the Aimeos TYPO3 extension
182
     *
183
     * @return string Version string
184
     */
185
    public static function getVersion() : string
186
    {
187
        $match = [];
188
        $content = @file_get_contents(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'ext_emconf.php');
189
190
        if (preg_match("/'version' => '([^']+)'/", $content, $match) === 1) {
0 ignored issues
show
It seems like $content can also be of type false; however, parameter $subject of preg_match() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

190
        if (preg_match("/'version' => '([^']+)'/", /** @scrutinizer ignore-type */ $content, $match) === 1) {
Loading history...
191
            return $match[1];
192
        }
193
194
        return '';
195
    }
196
197
198
    /**
199
     * Creates the view object for the HTML client.
200
     *
201
     * @param \Aimeos\MShop\ContextIface $context Context object
202
     * @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder|\TYPO3\CMS\Core\Routing\RouterInterface $uriBuilder URL builder
203
     * @param array $templatePaths List of base path names with relative template paths as key/value pairs
204
     * @param \TYPO3\CMS\Extbase\Mvc\RequestInterface|null $request Request object
205
     * @param string|null $langid ISO code of the current language ("de"/"de_CH") or null for no translation
206
     * @return \Aimeos\Base\View\Iface View object
207
     */
208
    public static function view(\Aimeos\MShop\ContextIface $context, $uriBuilder, array $templatePaths,
209
        \TYPO3\CMS\Extbase\Mvc\RequestInterface $request = null, string $langid = null) : \Aimeos\Base\View\Iface
210
    {
211
        $name = 'Aimeos\Aimeos\Base\View';
212
213
        if (isset($GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view'])) {
214
            if (($name = $GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['aimeos']['aimeos_view']) instanceof \Closure) {
215
                return $name($context, $uriBuilder, $templatePaths, $request, $langid);
216
            }
217
        }
218
219
        return $name::get($context, $uriBuilder, $templatePaths, $request, $langid);
220
    }
221
222
223
    /**
224
     * Parses TypoScript configuration string.
225
     *
226
     * @param string $tsString TypoScript string
227
     * @return array Mulit-dimensional, associative list of key/value pairs
228
     * @throws Exception If parsing the configuration string fails
229
     */
230
    public static function parseTS(string $tsString) : array
231
    {
232
        $parser = GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\Parser\TypoScriptParser');
233
        $parser->parse(TypoScriptParser::checkIncludeLines($tsString));
234
235
        if (!empty($parser->errors)) {
236
            throw new \InvalidArgumentException('Invalid TypoScript: \"' . $tsString . "\"\n" . print_r($parser->errors, true));
0 ignored issues
show
Are you sure print_r($parser->errors, true) of type string|true can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

236
            throw new \InvalidArgumentException('Invalid TypoScript: \"' . $tsString . "\"\n" . /** @scrutinizer ignore-type */ print_r($parser->errors, true));
Loading history...
237
        }
238
239
        $service = GeneralUtility::makeInstance('TYPO3\CMS\Core\TypoScript\TypoScriptService');
240
        $tsConfig = $service->convertTypoScriptArrayToPlainArray($parser->setup);
241
242
        // Allows "plugin.tx_aimeos.settings." prefix everywhere
243
        if (isset($tsConfig['plugin']['tx_aimeos']['settings'])
244
            && is_array($tsConfig['plugin']['tx_aimeos']['settings'])
245
        ) {
246
            $tsConfig = array_replace_recursive($tsConfig['plugin']['tx_aimeos']['settings'], $tsConfig);
247
            unset($tsConfig['plugin']['tx_aimeos']);
248
        }
249
250
        return $tsConfig;
251
    }
252
253
254
    /**
255
     * Clears basket information on logout
256
     *
257
     * @return void
258
     */
259
    public static function logout()
260
    {
261
        $context = self::context(self::config());
262
        $context->setLocale(self::locale($context));
263
264
        \Aimeos\Controller\Frontend::create( $context, 'basket' )->clear();
265
266
        $session = $context->session();
267
        $session->remove(array_keys($session->get('aimeos/basket/list', [])));
268
        $session->remove(array_keys($session->get('aimeos/basket/cache', [])));
269
    }
270
271
272
    /**
273
     * Doing a complete ACPu wipe with the red system cache.
274
     *
275
     * @hook clearCachePostProc
276
     *
277
     * @param array $cacheType The caching type.
278
     *
279
     * @return void
280
     */
281
    public static function clearCache(array $cacheType)
282
    {
283
        if (isset($cacheType['cacheCmd']) && $cacheType['cacheCmd'] === 'all'
284
            && (bool) static::getExtConfig('useAPC', false) === true
285
            && function_exists('apcu_clear_cache')
286
        ) {
287
            apcu_clear_cache();
288
        }
289
    }
290
}
291