eZ Publish  [trunk]
ezfunctionhandler.php
Go to the documentation of this file.
00001 <?php
00002 /**
00003  * File containing the eZFunctionHandler class.
00004  *
00005  * @copyright Copyright (C) 1999-2012 eZ Systems AS. All rights reserved.
00006  * @license http://www.gnu.org/licenses/gpl-2.0.txt GNU General Public License v2
00007  * @version //autogentag//
00008  * @package lib
00009  */
00010 
00011 /*!
00012   \class eZFunctionHandler ezfunctionhandler.php
00013   \brief The class eZFunctionHandler does
00014 
00015 */
00016 
00017 class eZFunctionHandler
00018 {
00019     static function moduleFunctionInfo( $moduleName )
00020     {
00021         if ( !isset( $GLOBALS['eZGlobalModuleFunctionList'] ) )
00022         {
00023             $GLOBALS['eZGlobalModuleFunctionList'] = array();
00024         }
00025         if ( isset( $GLOBALS['eZGlobalModuleFunctionList'][$moduleName] ) )
00026         {
00027             return $GLOBALS['eZGlobalModuleFunctionList'][$moduleName];
00028         }
00029         $moduleFunctionInfo = new eZModuleFunctionInfo( $moduleName );
00030         $moduleFunctionInfo->loadDefinition();
00031 
00032         return $GLOBALS['eZGlobalModuleFunctionList'][$moduleName] = $moduleFunctionInfo;
00033     }
00034 
00035     /*!
00036      \static
00037      Execute alias fetch for simplified fetching of objects
00038     */
00039     static function executeAlias( $aliasFunctionName, $functionParameters )
00040     {
00041         $aliasSettings = eZINI::instance( 'fetchalias.ini' );
00042         if ( $aliasSettings->hasSection( $aliasFunctionName ) )
00043         {
00044             $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $aliasSettings->variable( $aliasFunctionName, 'Module' ) );
00045             if ( !$moduleFunctionInfo->isValid() )
00046             {
00047                 eZDebug::writeError( "Cannot execute function '$aliasFunctionName' in module '$moduleName', no valid data", __METHOD__ );
00048                 return null;
00049             }
00050 
00051             $functionName = $aliasSettings->variable( $aliasFunctionName, 'FunctionName' );
00052 
00053             $functionArray = array();
00054             if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Parameter' ) )
00055             {
00056                 $parameterTranslation = $aliasSettings->variable( $aliasFunctionName, 'Parameter' );
00057                 foreach( array_keys( $parameterTranslation ) as $functionKey )
00058                 {
00059                     $translatedParameter = $parameterTranslation[$functionKey];
00060                     if ( array_key_exists( $translatedParameter, $functionParameters ) )
00061                          $functionArray[$functionKey] = $functionParameters[$translatedParameter];
00062                     else
00063                         $functionArray[$functionKey] = null;
00064                 }
00065             }
00066 
00067             if ( $aliasSettings->hasVariable( $aliasFunctionName, 'Constant' ) )
00068             {
00069                 $constantParameterArray = $aliasSettings->variable( $aliasFunctionName, 'Constant' );
00070                 // prevent PHP warning in the loop below
00071                 if ( !is_array( $constantParameterArray ) )
00072                     $constantParameterArray = array();
00073                 foreach ( array_keys( $constantParameterArray ) as $constKey )
00074                 {
00075                     if ( $moduleFunctionInfo->isParameterArray( $functionName, $constKey ) )
00076                     {
00077                         /*
00078                          Check if have Constant overriden by function parameter
00079                          */
00080                         if ( array_key_exists( $constKey, $functionParameters ) )
00081                         {
00082                             $functionArray[$constKey] =& $functionParameters[$constKey] ;
00083                             continue;
00084                         }
00085                         /*
00086                          Split given string using semicolon as delimiter.
00087                          Semicolon may be escaped by prepending it with backslash:
00088                          in this case it is not treated as delimiter.
00089                          I use \x5c instead of \\ here.
00090                          */
00091                         $constantParameter = preg_split( '/((?<=\x5c\x5c)|(?<!\x5c{1}));/',
00092                                                          $constantParameterArray[$constKey] );
00093 
00094                         /*
00095                          Unfortunately, my PHP 4.3.6 doesn't work correctly
00096                          if flag PREG_SPLIT_NO_EMPTY is set.
00097                          That's why we need to manually remove
00098                          empty strings from $constantParameter.
00099                          */
00100                         $constantParameter = array_diff( $constantParameter, array('') );
00101 
00102                         /*
00103                          Hack: force array keys to be consecutive, starting from zero (0, 1, 2, ...).
00104                          Otherwise SQL syntax error occurs.
00105                          */
00106                         $constantParameter = array_values( $constantParameter );
00107 
00108                         if ( $constantParameter ) // if the array is not empty
00109                         {
00110                             // Remove backslashes used for delimiter escaping.
00111                             $constantParameter = preg_replace( '/\x5c{1};/', ';', $constantParameter );
00112                             $constantParameter = str_replace( '\\\\', '\\', $constantParameter );
00113 
00114                             // Return the result.
00115                             $functionArray[$constKey] = $constantParameter;
00116                         }
00117                     }
00118                     else
00119                         $functionArray[$constKey] = $constantParameterArray[$constKey];
00120                 }
00121             }
00122 
00123 /*
00124  */
00125             foreach ( $functionParameters as $paramName => $value )
00126             {
00127                 if ( !array_key_exists( $paramName, $functionArray ) )
00128                 {
00129                     $functionArray[$paramName] = $value;
00130                 }
00131             }
00132             return $moduleFunctionInfo->execute( $functionName, $functionArray );
00133         }
00134         eZDebug::writeWarning( 'Could not execute. Function ' . $aliasFunctionName. ' not found.' , __METHOD__ );
00135     }
00136 
00137     static function execute( $moduleName, $functionName, $functionParameters )
00138     {
00139         $moduleFunctionInfo = eZFunctionHandler::moduleFunctionInfo( $moduleName );
00140         if ( !$moduleFunctionInfo->isValid() )
00141         {
00142             eZDebug::writeError( "Cannot execute function '$functionName' in module '$moduleName', no valid data", __METHOD__ );
00143             return null;
00144         }
00145 
00146         return $moduleFunctionInfo->execute( $functionName, $functionParameters );
00147     }
00148 }
00149 
00150 ?>