b13 / distributed-locks

Adds a Redis Locking Strategy for TYPO3 frontend page generation, useful on distributed systems with NFS.
GNU General Public License v2.0
12 stars 8 forks source link

Problem with use statement in ext_localconf.php #4

Closed sypets closed 4 years ago

sypets commented 4 years ago

I tested this a while ago, but now I get errors with 9.5.13:

Fatal error: Cannot use TYPO3\CMS\Core\Utility\GeneralUtility as GeneralUtility
because the name is already in use in 
/var/www/mysite/public/typo3temp/var/cache/code/cache_core/ext_localconf_feb178af00fe22e00dc62d7dcd6d4d16f5d4fc3a.php
on line 4508

The problematic line:

4496  /**
  4497   * Extension: distributed-locks
  4498   * File: /var/www/mysite/public/typo3conf/ext/distributed-locks/ext_localconf.php
  4499   */
  4500  
  4501  $_EXTKEY = 'distributed-locks';
  4502  $_EXTCONF = $GLOBALS['TYPO3_CONF_VARS']['EXT']['extConf'][$_EXTKEY] ?? null;
  4503  
  4504  
  4505  
  4506  defined('TYPO3_MODE') or die();
  4507  
  4508  use TYPO3\CMS\Core\Utility\GeneralUtility;
  4509  use TYPO3\CMS\Core\Locking\LockFactory;
  4510  use B13\DistributedLocks\RedisLockingStrategy;

ext_localconf.php contains use statements. Since multiple ext_localconf.php are concatenated into one this may cause problems if more than one extension uses the same use statement.

I believe, the recommended solution is to enclose this in a function:

<?php
defined('TYPO3_MODE') || die();

(function () {
    // Add your code here
    // use ....
})();

You must never use a use statement in the files global scope - that would make the cached script concept break and could conflict with other extensions.

see https://docs.typo3.org/m/typo3/reference-coreapi/master/en-us/ExtensionArchitecture/ConfigurationFiles/Index.html

sypets commented 4 years ago

Actually, this does not work. Sorry, I thought I tested it but had the wrong version installed. This results in syntax error.