[TASK] Decouple extMgm::extPath from TYPO3_LOADED_EXT
authorChristian Kuhn <lolli@schwarzbu.ch>
Sat, 9 Jul 2011 13:22:53 +0000 (15:22 +0200)
committerSusanne Moog <typo3@susannemoog.de>
Sat, 9 Jul 2011 20:39:25 +0000 (22:39 +0200)
To simplify the bootstrap it is required to make
t3lib_extMgm::extList() work more early. This is achieved
by removing the dependency to $GLOBALS['TYPO3_LOADED_EXT']

Change-Id: I73aa8c3a72979c85bf6f34cf027565199e380920
Resolves: #28034
Reviewed-on: http://review.typo3.org/3191
Reviewed-by: Susanne Moog
Tested-by: Susanne Moog
t3lib/class.t3lib_extmgm.php
tests/t3lib/class.t3lib_extmgmTest.php

index 3f7fe4a..40bb15b 100644 (file)
@@ -77,18 +77,47 @@ final class t3lib_extMgm {
         * Useful for internal fileoperations
         * Usage: 136
         *
-        * @param       string          Extension key
-        * @param       string          $script is appended to the output if set.
-        * @return      string
+        * @param $key string Extension key
+        * @param $script string $script is appended to the output if set.
+        * @throws BadFunctionCallException
+        * @return string
         */
        public static function extPath($key, $script = '') {
-               if (!isset($GLOBALS['TYPO3_LOADED_EXT'][$key])) {
-                       throw new BadFunctionCallException(
-                               'TYPO3 Fatal Error: Extension key "' . $key . '" was NOT loaded!',
-                               1270853878
-                       );
+               if (isset($GLOBALS['TYPO3_LOADED_EXT'])) {
+                       if (!isset($GLOBALS['TYPO3_LOADED_EXT'][$key])) {
+                               throw new BadFunctionCallException(
+                                       'TYPO3 Fatal Error: Extension key "' . $key . '" was NOT loaded!',
+                                       1270853878
+                               );
+                       }
+
+                       $extensionPath = PATH_site . $GLOBALS['TYPO3_LOADED_EXT'][$key]['siteRelPath'];
+               } else {
+                       $extensionList = $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] . ',' . $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'];
+                       $loadedExtensions = array_flip(array_unique(t3lib_div::trimExplode(',', $extensionList, 1)));
+
+                       if (!isset($loadedExtensions[$key])) {
+                               throw new BadFunctionCallException(
+                                       'TYPO3 Fatal Error: Extension key "' . $key . '" was NOT loaded!',
+                                       1294430950
+                               );
+                       }
+
+                       if (@is_dir(PATH_typo3conf . 'ext/' . $key . '/')) {
+                               $extensionPath = PATH_typo3conf . 'ext/' . $key . '/';
+                       } elseif (@is_dir(PATH_typo3 . 'ext/' . $key . '/')) {
+                               $extensionPath = PATH_typo3 . 'ext/' . $key . '/';
+                       } elseif (@is_dir(PATH_typo3 . 'sysext/' . $key . '/')) {
+                               $extensionPath = PATH_typo3 . 'sysext/' . $key . '/';
+                       } else {
+                               throw new BadFunctionCallException(
+                                       'TYPO3 Fatal Error: Extension "' . $key . '" was NOT found!',
+                                       1294430951
+                               );
+                       }
                }
-               return PATH_site . $GLOBALS['TYPO3_LOADED_EXT'][$key]['siteRelPath'] . $script;
+
+               return $extensionPath . $script;
        }
 
        /**
index 73da8aa..4e1ec62 100644 (file)
@@ -48,8 +48,27 @@ class t3lib_extmgmTest extends tx_phpunit_testcase {
         */
        protected $backupGlobalsBlacklist = array('TYPO3_DB');
 
+       /**
+        * phpunit still needs some globals that are
+        * reconstructed before $backupGlobals is handled. Those
+        * important globals are handled in tearDown() directly.
+        *
+        * @var array
+        */
+       protected $globals = array();
+
+       public function setUp() {
+               $this->globals = array(
+                       'TYPO3_LOADED_EXT' => serialize($GLOBALS['TYPO3_LOADED_EXT']),
+               );
+       }
+
        public function tearDown() {
                t3lib_extMgm::clearExtensionKeyMap();
+
+               foreach ($this->globals as $key => $value) {
+                       $GLOBALS[$key] = unserialize($value);
+               }
        }
 
        ///////////////////////////////
@@ -73,6 +92,41 @@ class t3lib_extmgmTest extends tx_phpunit_testcase {
                $this->assertSame(PATH_site . 'foo/bar.txt', t3lib_extMgm::extPath('foo', 'bar.txt'));
        }
 
+       /**
+        * @test
+        * @expectedException BadFunctionCallException
+        */
+       public function extPathThrowsExceptionIfExtensionIsNotLoadedAndTypo3LoadedExtensionsIsEmpty() {
+               unset($GLOBALS['TYPO3_LOADED_EXT']);
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = '';
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = '';
+               t3lib_extMgm::extPath('bar');
+       }
+
+       /**
+        * @test
+        */
+       public function extPathSearchesForPathOfExtensionInRequiredExtensionList() {
+               $this->setExpectedException('Exception', '', 1294430951);
+
+               unset($GLOBALS['TYPO3_LOADED_EXT']);
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = 'foo';
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = '';
+               t3lib_extMgm::extPath('foo');
+       }
+
+       /**
+        * @test
+        */
+       public function extPathSearchesForPathOfExtensionInExtList() {
+               $this->setExpectedException('Exception', '', 1294430951);
+
+               unset($GLOBALS['TYPO3_LOADED_EXT']);
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['requiredExt'] = '';
+               $GLOBALS['TYPO3_CONF_VARS']['EXT']['extList'] = 'foo';
+               t3lib_extMgm::extPath('foo');
+       }
+
        //////////////////////
        // Utility functions
        //////////////////////