[TASK] Discourage usage of $LANG->csConvObj and parserFactory 08/46708/5
authorBenni Mack <benni@typo3.org>
Tue, 16 Feb 2016 11:45:29 +0000 (12:45 +0100)
committerGeorg Ringer <georg.ringer@gmail.com>
Wed, 17 Feb 2016 04:50:51 +0000 (05:50 +0100)
The csConvObj is decoupled from LanguageService and
not used directly anymore.

Any calls directly to the property csConvObj should be avoided
as was marked as deprecated.

Same goes with the property "parserFactory" of the
LanguageService class. The parser factory is not called
from the language object anymore and the property can be
removed.

Resolves: #73482
Releases: master
Change-Id: I94d7fca658c2b9e8cffc62f5bdb0c5ce83c894a2
Reviewed-on: https://review.typo3.org/46708
Reviewed-by: Wouter Wolters <typo3@wouterwolters.nl>
Tested-by: Wouter Wolters <typo3@wouterwolters.nl>
Reviewed-by: Georg Ringer <georg.ringer@gmail.com>
Tested-by: Georg Ringer <georg.ringer@gmail.com>
typo3/sysext/backend/Tests/Unit/Utility/BackendUtilityTest.php
typo3/sysext/core/Documentation/Changelog/master/Deprecation-73482-LANG-csConvObjAndLANG-parserFactory.rst [new file with mode: 0644]
typo3/sysext/impexp/Classes/Import.php
typo3/sysext/lang/Classes/LanguageService.php

index c656413..87aac93 100644 (file)
@@ -23,7 +23,6 @@ use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\ProcessedValueForGroupWithOneA
 use TYPO3\CMS\Backend\Tests\Unit\Utility\Fixtures\ProcessedValueForSelectWithMMRelationFixture;
 use TYPO3\CMS\Backend\Utility\BackendUtility;
 use TYPO3\CMS\Core\Authentication\BackendUserAuthentication;
-use TYPO3\CMS\Core\Charset\CharsetConverter;
 use TYPO3\CMS\Core\Database\DatabaseConnection;
 use TYPO3\CMS\Core\Tests\UnitTestCase;
 use TYPO3\CMS\Core\Utility\GeneralUtility;
@@ -786,11 +785,8 @@ class BackendUtilityTest extends UnitTestCase
         );
         // Stub LanguageService and let sL() return the same value that came in again
         $GLOBALS['LANG'] = $this->getMock(LanguageService::class, array(), array(), '', false);
-        $GLOBALS['LANG']->csConvObj = $this->getMock(CharsetConverter::class);
         $GLOBALS['LANG']->expects($this->any())->method('sL')->will($this->returnArgument(0));
 
-        $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')->will($this->returnArgument(1));
-
         $GLOBALS['TCA'][$table] = $tca;
         $label = BackendUtility::getProcessedValue($table, $col, 'foo,invalidKey,bar');
         $this->assertEquals('aFooLabel, aBarLabel', $label);
@@ -817,11 +813,8 @@ class BackendUtilityTest extends UnitTestCase
         );
         // Stub LanguageService and let sL() return the same value that came in again
         $GLOBALS['LANG'] = $this->getMock(LanguageService::class, array(), array(), '', false);
-        $GLOBALS['LANG']->csConvObj = $this->getMock(CharsetConverter::class);
         $GLOBALS['LANG']->expects($this->any())->method('sL')->will($this->returnArgument(0));
 
-        $GLOBALS['LANG']->csConvObj->expects($this->any())->method('crop')->will($this->returnArgument(1));
-
         $GLOBALS['TCA'][$table] = $tca;
         $label = BackendUtility::getProcessedValue($table, $col, 'invalidKey');
         $this->assertEquals('invalidKey', $label);
diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-73482-LANG-csConvObjAndLANG-parserFactory.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-73482-LANG-csConvObjAndLANG-parserFactory.rst
new file mode 100644 (file)
index 0000000..c8d113e
--- /dev/null
@@ -0,0 +1,31 @@
+===============================================================
+Deprecation: #73482 - $LANG->csConvObj and $LANG->parserFactory
+===============================================================
+
+Description
+===========
+
+The properties of LanguageService (also known as ``$GLOBALS[LANG]``) csConvObj and parserFactory
+have been marked as deprecated. Since these three PHP classes are not dependent on each other, they
+can be decoupled. The getter method ``getParserFactory()`` has thus been marked as deprecated as well.
+
+
+Impact
+======
+
+These properties will be removed in TYPO3 v9. Calling ``LanguageService->getParserFactory()`` will trigger a
+deprecation log entry.
+
+
+Affected Installations
+======================
+
+Installations with custom extension accessing the LanguageService properties and method above.
+
+
+Migration
+=========
+
+Instantiate CharsetConverter (csConvObj) and LocalizationFactory (parserFactory) via ``GeneralUtility::makeInstance``
+directly if needed, as they are Singleton objects and then fetched from the General Utility Object container
+functionalities.
\ No newline at end of file
index c599ed4..0e31fb0 100644 (file)
@@ -15,6 +15,7 @@ namespace TYPO3\CMS\Impexp;
  */
 
 use TYPO3\CMS\Backend\Utility\BackendUtility;
+use TYPO3\CMS\Core\Charset\CharsetConverter;
 use TYPO3\CMS\Core\DataHandling\DataHandler;
 use TYPO3\CMS\Core\Exception;
 use TYPO3\CMS\Core\Resource\DuplicationBehavior;
@@ -2169,18 +2170,20 @@ class Import extends ImportExport
         $importCharset = $this->dat['header']['charset'];
         if ($importCharset) {
             if ($importCharset !== 'utf-8') {
+                /** @var CharsetConverter $charsetConverter */
+                $charsetConverter = GeneralUtility::makeInstance(CharsetConverter::class);
                 $this->error('CHARSET: Converting charset of input file (' . $importCharset . ') to the system charset (utf-8)');
                 // Convert meta data:
                 if (is_array($this->dat['header']['meta'])) {
-                    $this->getLanguageService()->csConvObj->convArray($this->dat['header']['meta'], $importCharset, 'utf-8');
+                    $charsetConverter->convArray($this->dat['header']['meta'], $importCharset, 'utf-8');
                 }
                 // Convert record headers:
                 if (is_array($this->dat['header']['records'])) {
-                    $this->getLanguageService()->csConvObj->convArray($this->dat['header']['records'], $importCharset, 'utf-8');
+                    $charsetConverter->convArray($this->dat['header']['records'], $importCharset, 'utf-8');
                 }
                 // Convert records themselves:
                 if (is_array($this->dat['records'])) {
-                    $this->getLanguageService()->csConvObj->convArray($this->dat['records'], $importCharset, 'utf-8');
+                    $charsetConverter->convArray($this->dat['records'], $importCharset, 'utf-8');
                 }
             }
         } else {
index 10131c6..9255ff3 100755 (executable)
@@ -78,6 +78,7 @@ class LanguageService
      * instance of the "\TYPO3\CMS\Core\Charset\CharsetConverter" class. May be used by any application.
      *
      * @var \TYPO3\CMS\Core\Charset\CharsetConverter
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9. Charset is a singleton, load it via GeneralUtility directly
      */
     public $csConvObj;
 
@@ -85,6 +86,8 @@ class LanguageService
      * instance of the parser factory
      *
      * @var LocalizationFactory
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9, as LocalizationFactory is a singleton, load it via
+     * GeneralUtility directly
      */
     public $parserFactory;
 
@@ -96,6 +99,20 @@ class LanguageService
      */
     protected $languageDependencies = array();
 
+    /**
+     * LanguageService constructor.
+     */
+    public function __construct()
+    {
+        // Initialize the conversion object
+        $this->csConvObj = GeneralUtility::makeInstance(CharsetConverter::class);
+        // Initialize the localization factory object
+        $this->parserFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
+        if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
+            $this->debugKey = true;
+        }
+    }
+
     /**
      * Initializes the backend language.
      * This is for example done in \TYPO3\CMS\Backend\Template\DocumentTemplate with lines like these:
@@ -103,40 +120,34 @@ class LanguageService
      * $LANG->init($GLOBALS['BE_USER']->uc['lang']);
      *
      * @throws \RuntimeException
-     * @param string $lang The language key (two character string from backend users profile)
+     * @param string $languageKey The language key (two character string from backend users profile)
      * @return void
      */
-    public function init($lang)
+    public function init($languageKey)
     {
-        // Initialize the conversion object:
-        $this->csConvObj = GeneralUtility::makeInstance(CharsetConverter::class);
-        // Initialize the parser factory object
-        $this->parserFactory = GeneralUtility::makeInstance(LocalizationFactory::class);
-        // Find the requested language in this list based
-        // on the $lang key being inputted to this function.
+        // Find the requested language in this list based on the $languageKey
         /** @var $locales \TYPO3\CMS\Core\Localization\Locales */
         $locales = GeneralUtility::makeInstance(Locales::class);
         // Language is found. Configure it:
-        if (in_array($lang, $locales->getLocales())) {
+        if (in_array($languageKey, $locales->getLocales())) {
             // The current language key
-            $this->lang = $lang;
-            $this->languageDependencies[] = $this->lang;
-            foreach ($locales->getLocaleDependencies($this->lang) as $language) {
+            $this->lang = $languageKey;
+            $this->languageDependencies[] = $languageKey;
+            foreach ($locales->getLocaleDependencies($languageKey) as $language) {
                 $this->languageDependencies[] = $language;
             }
         }
-        if ($GLOBALS['TYPO3_CONF_VARS']['BE']['lang']['debug']) {
-            $this->debugKey = true;
-        }
     }
 
     /**
      * Gets the parser factory.
      *
      * @return LocalizationFactory
+     * @deprecated since TYPO3 v8, will be removed in TYPO3 v9
      */
     public function getParserFactory()
     {
+        GeneralUtility::logDeprecatedFunction();
         return $this->parserFactory;
     }
 
@@ -393,10 +404,6 @@ class LanguageService
      */
     protected function readLLfile($fileRef)
     {
-        // @todo: Usually, an instance of the LocalizationFactory is found in $this->parserFactory.
-        // @todo: This is not the case if $GLOBALS['LANG'] is not used to get hold of this object,
-        // @todo: but the objectManager instead. If then init() is not called, this will fatal ...
-        // @todo: To be sure, we always create an instance here for now.
         /** @var $languageFactory LocalizationFactory */
         $languageFactory = GeneralUtility::makeInstance(LocalizationFactory::class);