[TYPO3-dev] how to iterate over LoadedExtensionArrayElement

Franz Holzinger franz at ttproducts.de
Wed Nov 6 18:23:26 CET 2013


Hello Alex,

thank you for your help. Your alternative code does the job.

The function getValidKey is needed for another function guessKey. This 
function guesses the extension key from a given extension key, classname 
or filename. The extension key part inside of the classname has the 
underscore stripped off. This makes it necessary to go sequentially 
through a loop of all possible extension keys in order to detect the 
corresponding extension key which can have an underscores inside of its 
name.

----------------
	/**
	 * Guess the key from the given information
	 *
	 * Guessing has the following order:
	 *
	 * 1. A KEY itself is tried.
	 *    <pre>
	 *     Example: my_extension
	 *    </pre>
	 * 2. A classname of the pattern tx_KEY_something_else is tried.
	 *    <pre>
	 *     Example: tx_myextension_view
	 *    </pre>
	 * 3. A full classname of the pattern ' * tx_KEY_something_else.php' is 
tried.
	 *    <pre>
	 *     Example: class.tx_myextension_view.php
	 *     Example: brokenPath/class.tx_myextension_view.php
	 *    </pre>
	 * 4. A path that starts with the KEY is tried.
	 *    <pre>
	 *     Example: my_extension/class.view.php
	 *    </pre>
	 *
	 * @param	string		the minimal necessary information (see 1-4)
	 * @return	string		the guessed key, FALSE if no result
	 */
	public function guessKey ($minimalInformation) {
		$info=trim($minimalInformation);
		$key = FALSE;
		if($info){
			// Can it be the key itself?
			if(!$key && preg_match('/^([A-Za-z_]*)$/', $info, $matches ) ) {
				$key = $matches[1];
				$key = self::getValidKey($key);
			}
-----------


Here is the resulting PHP function.


use TYPO3\CMS\Core\Utility\GeneralUtility;


/**
* Check if the given extension key is within the loaded extensions
*
* The key can be given in the regular format or with underscores stripped.
*
* @param	string		extension key to check
* @return	boolean		is the key valid?
*/
public function getValidKey ($rawKey) {

	$result = FALSE;
	$rawKey = str_replace('_', '', $rawKey);
	$packageManager = 
GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Package\\PackageManager');
	foreach ($packageManager->getActivePackages() as $package) {
		$packageKey = $package->getPackageKey();
		if (
			str_replace('_', '', $packageKey) == $rawKey
		) {
			$result =  $packageKey;
			break;
		}
	}

	return $result;
}


This will be part of the next div2007 1.1.2.


- Franz

Le 06. 11. 13 09:59, Alexander Stehlik a écrit :
> Sorry, just got the point with the replacement of the underscore in the
> Extension key.
>
> You can have a look a a patch in gerrit, that is also iterating over the
> active packages:
>
> https://review.typo3.org/#/c/25002/2/typo3/sysext/impexp/Classes/Controller/ImportExportController.php
>
>
> Code looks basically like:
>
> $packageManager =
> GeneralUtility::makeInstance('TYPO3\\CMS\\Core\\Package\\PackageManager');
> foreach ($packageManager->getActivePackages() as $package) {
>      $packageKey = $package->getPackageKey();
>      ... // do your own stuff here
> }
>
> Hope this helps.
>
> Cheers,
> Alex
>




More information about the TYPO3-dev mailing list