eZ Publish  [4.0]
ezmodulefunctioninfo.php
Go to the documentation of this file.
00001 <?php
00002 //
00003 // Definition of eZModuleFunctionInfo class
00004 //
00005 // Created on: <06-Oct-2002 16:27:36 amos>
00006 //
00007 // ## BEGIN COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00008 // SOFTWARE NAME: eZ Publish
00009 // SOFTWARE RELEASE: 4.0.x
00010 // COPYRIGHT NOTICE: Copyright (C) 1999-2008 eZ Systems AS
00011 // SOFTWARE LICENSE: GNU General Public License v2.0
00012 // NOTICE: >
00013 //   This program is free software; you can redistribute it and/or
00014 //   modify it under the terms of version 2.0  of the GNU General
00015 //   Public License as published by the Free Software Foundation.
00016 //
00017 //   This program is distributed in the hope that it will be useful,
00018 //   but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //   GNU General Public License for more details.
00021 //
00022 //   You should have received a copy of version 2.0 of the GNU General
00023 //   Public License along with this program; if not, write to the Free
00024 //   Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
00025 //   MA 02110-1301, USA.
00026 //
00027 //
00028 // ## END COPYRIGHT, LICENSE AND WARRANTY NOTICE ##
00029 //
00030 
00031 /*! \file ezmodulefunctioninfo.php
00032 */
00033 
00034 /*!
00035   \class eZModuleFunctionInfo ezmodulefunctioninfo.php
00036   \brief The class eZModuleFunctionInfo does
00037 
00038 */
00039 
00040 //include_once( 'lib/ezutils/classes/ezmodule.php' );
00041 require_once( 'lib/ezutils/classes/ezdebug.php' );
00042 
00043 class eZModuleFunctionInfo
00044 {
00045     const ERROR_NO_CLASS = 5;
00046     const ERROR_NO_CLASS_METHOD = 6;
00047     const ERROR_CLASS_INSTANTIATE_FAILED = 7;
00048     const ERROR_MISSING_PARAMETER = 8;
00049 
00050     /*!
00051      Constructor
00052     */
00053     function eZModuleFunctionInfo( $moduleName )
00054     {
00055         $this->ModuleName = $moduleName;
00056         $this->IsValid = false;
00057         $this->FunctionList = array();
00058     }
00059 
00060     function isValid()
00061     {
00062         return $this->IsValid;
00063     }
00064 
00065     function loadDefinition()
00066     {
00067         $definitionFile = null;
00068         $pathList = eZModule::globalPathList();
00069         if ( $pathList )
00070         {
00071             foreach ( $pathList as $path )
00072             {
00073                 $definitionFile = $path . '/' . $this->ModuleName . '/function_definition.php';
00074                 if ( file_exists( $definitionFile ) )
00075                     break;
00076                 $definitionFile = null;
00077             }
00078         }
00079         if ( $definitionFile === null )
00080         {
00081             eZDebug::writeError( 'Missing function definition file for module: ' . $this->ModuleName,
00082                                  'eZModuleFunctionInfo::loadDefinition' );
00083             return false;
00084         }
00085         unset( $FunctionList );
00086         include( $definitionFile );
00087         if ( !isset( $FunctionList ) )
00088         {
00089             eZDebug::writeError( 'Missing function definition list for module: ' . $this->ModuleName,
00090                                  'eZModuleFunctionInfo::loadDefinition' );
00091             return false;
00092         }
00093         $this->FunctionList = $FunctionList;
00094         $this->IsValid = true;
00095         return true;
00096     }
00097 
00098     /*!
00099       Check if a parameter for a function is an array
00100 
00101       \param function name
00102       \param parameter name
00103 
00104       \return true if parameter is supposed to be array
00105      */
00106     function isParameterArray( $functionName, $parameterName )
00107     {
00108         if ( isset( $this->FunctionList[$functionName]['parameters'] ) )
00109         {
00110             $parameterList = $this->FunctionList[$functionName]['parameters'];
00111             foreach ( $parameterList as $parameter )
00112             {
00113                 if ( $parameter['name'] == $parameterName )
00114                 {
00115                     if ( $parameter['type'] == 'array' )
00116                     {
00117                         return true;
00118                     }
00119                     return false;
00120                 }
00121             }
00122         }
00123         return false;
00124     }
00125 
00126     /*!
00127      Pre execute, used by template compilation to check as much as possible before runtime.
00128 
00129      \param function name
00130 
00131      \return function definition, false if fails.
00132     */
00133     function preExecute( $functionName )
00134     {
00135         /* Code copied from this->execute() */
00136         $moduleName = $this->ModuleName;
00137         if ( !isset( $this->FunctionList[$functionName] ) )
00138         {
00139             eZDebug::writeError( "No such function '$functionName' in module '$moduleName'",
00140                                  'eZModuleFunctionInfo::execute' );
00141             return false;
00142         }
00143         $functionDefinition = $this->FunctionList[$functionName];
00144         if ( !isset( $functionName['call_method'] ) )
00145         {
00146             eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'",
00147                                  'eZModuleFunctionInfo::execute' );
00148             return false;
00149         }
00150         if ( !isset( $functionName['parameters'] ) )
00151         {
00152             eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'",
00153                                  'eZModuleFunctionInfo::execute' );
00154             return false;
00155         }
00156         $callMethod = $functionDefinition['call_method'];
00157         if ( isset( $callMethod['class'] ) &&
00158              isset( $callMethod['method'] ) )
00159         {
00160             if ( !class_exists( $callMethod['class'] ) )
00161             {
00162                 return false;
00163             }
00164             $classObject = $this->objectForClass( $callMethod['class'] );
00165             if ( $classObject === null )
00166             {
00167                 return false;
00168             }
00169             if ( !method_exists( $classObject, $callMethod['method'] ) )
00170             {
00171                 return false;
00172             }
00173 
00174             return $functionDefinition;
00175         }
00176         return false;
00177     }
00178 
00179     function execute( $functionName, $functionParameters )
00180     {
00181         $moduleName = $this->ModuleName;
00182         if ( !isset( $this->FunctionList[$functionName] ) )
00183         {
00184             eZDebug::writeError( "No such function '$functionName' in module '$moduleName'",
00185                                  'eZModuleFunctionInfo::execute' );
00186             return null;
00187         }
00188         $functionDefinition = $this->FunctionList[$functionName];
00189         if ( !isset( $functionName['call_method'] ) )
00190         {
00191             eZDebug::writeError( "No call method defined for function '$functionName' in module '$moduleName'",
00192                                  'eZModuleFunctionInfo::execute' );
00193             return null;
00194         }
00195         if ( !isset( $functionName['parameters'] ) )
00196         {
00197             eZDebug::writeError( "No parameters defined for function '$functionName' in module '$moduleName'",
00198                                  'eZModuleFunctionInfo::execute' );
00199             return null;
00200         }
00201         $callMethod = $functionDefinition['call_method'];
00202         if ( isset( $callMethod['class'] ) &&
00203              isset( $callMethod['method'] ) )
00204         {
00205             $resultArray = $this->executeClassMethod( $callMethod['class'], $callMethod['method'],
00206                                                       $functionDefinition['parameters'], $functionParameters );
00207         }
00208         else
00209         {
00210             eZDebug::writeError( "No valid call methods found for function '$functionName' in module '$moduleName'",
00211                                  'eZModuleFunctionInfo::execute' );
00212             return null;
00213         }
00214         if ( !is_array( $resultArray ) )
00215         {
00216             eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result array",
00217                                  'eZFunctionHandler::execute' );
00218             return null;
00219         }
00220         if ( isset( $resultArray['internal_error'] ) )
00221         {
00222             switch ( $resultArray['internal_error'] )
00223             {
00224                 case eZModuleFunctionInfo::ERROR_NO_CLASS:
00225                 {
00226                     $className = $resultArray['internal_error_class_name'];
00227                     eZDebug::writeError( "No class '$className' available for function '$functionName' in module '$moduleName'",
00228                                          'eZModuleFunctionInfo::execute' );
00229                     return null;
00230                 } break;
00231                 case eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD:
00232                 {
00233                     $className = $resultArray['internal_error_class_name'];
00234                     $classMethodName = $resultArray['internal_error_class_method_name'];
00235                     eZDebug::writeError( "No method '$classMethodName' in class '$className' available for function '$functionName' in module '$moduleName'",
00236                                          'eZModuleFunctionInfo::execute' );
00237                     return null;
00238                 } break;
00239                 case eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED:
00240                 {
00241                     $className = $resultArray['internal_error_class_name'];
00242                     eZDebug::writeError( "Failed instantiating class '$className' which is needed for function '$functionName' in module '$moduleName'",
00243                                          'eZModuleFunctionInfo::execute' );
00244                     return null;
00245                 } break;
00246                 case eZModuleFunctionInfo::ERROR_MISSING_PARAMETER:
00247                 {
00248                     $parameterName = $resultArray['internal_error_parameter_name'];
00249                     eZDebug::writeError( "Missing parameter '$parameterName' for function '$functionName' in module '$moduleName'",
00250                                          "eZModuleFunctionInfo::execute $moduleName::$functionName" );
00251                     return null;
00252                 } break;
00253                 default:
00254                 {
00255                     $internalError = $resultArray['internal_error'];
00256                     eZDebug::writeError( "Unknown internal error '$internalError' for function '$functionName' in module '$moduleName'",
00257                                          'eZModuleFunctionInfo::execute' );
00258                     return null;
00259                 } break;
00260             }
00261             return null;
00262         }
00263         else if ( isset( $resultArray['error'] ) )
00264         {
00265         }
00266         else if ( isset( $resultArray['result'] ) )
00267         {
00268             return $resultArray['result'];
00269         }
00270         else
00271         {
00272             eZDebug::writeError( "Function '$functionName' in module '$moduleName' did not return a result value",
00273                                  'eZFunctionHandler::execute' );
00274         }
00275         return null;
00276     }
00277 
00278     function objectForClass( $className )
00279     {
00280         if ( !isset( $GLOBALS['eZModuleFunctionClassObjectList'] ) )
00281         {
00282             $GLOBALS['eZModuleFunctionClassObjectList'] = array();
00283         }
00284         if ( isset( $GLOBALS['eZModuleFunctionClassObjectList'][$className] ) )
00285         {
00286             return $GLOBALS['eZModuleFunctionClassObjectList'][$className];
00287         }
00288         return $GLOBALS['eZModuleFunctionClassObjectList'][$className] = new $className();
00289     }
00290 
00291     function executeClassMethod( $className, $methodName,
00292                                  $functionParameterDefinitions, $functionParameters )
00293     {
00294         if ( !class_exists( $className ) )
00295         {
00296             return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS,
00297                           'internal_error_class_name' => $className );
00298         }
00299         $classObject = $this->objectForClass( $className );
00300         if ( $classObject === null )
00301         {
00302             return array( 'internal_error' => eZModuleFunctionInfo::ERROR_CLASS_INSTANTIATE_FAILED,
00303                           'internal_error_class_name' => $className );
00304         }
00305         if ( !method_exists( $classObject, $methodName ) )
00306         {
00307             return array( 'internal_error' => eZModuleFunctionInfo::ERROR_NO_CLASS_METHOD,
00308                           'internal_error_class_name' => $className,
00309                           'internal_error_class_method_name' => $methodName );
00310         }
00311         $parameterArray = array();
00312         foreach ( $functionParameterDefinitions as $functionParameterDefinition )
00313         {
00314             $parameterName = $functionParameterDefinition['name'];
00315             if ( isset( $functionParameters[$parameterName] ) )
00316             {
00317                 // Do type checking
00318                 $parameterArray[] = $functionParameters[$parameterName];
00319             }
00320             else
00321             {
00322                 if ( $functionParameterDefinition['required'] )
00323                 {
00324                     return array( 'internal_error' => eZModuleFunctionInfo::ERROR_MISSING_PARAMETER,
00325                                   'internal_error_parameter_name' => $parameterName );
00326                 }
00327                 else if ( isset( $functionParameterDefinition['default'] ) )
00328                 {
00329                     $parameterArray[] = $functionParameterDefinition['default'];
00330                 }
00331                 else
00332                 {
00333                     $parameterArray[] = null;
00334                 }
00335             }
00336         }
00337 
00338         return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
00339     }
00340 
00341     /*!
00342      \deprecated use call_user_func_array() instead
00343     */
00344     function callClassMethod( $methodName, $classObject, $parameterArray )
00345     {
00346 /*        echo "*********** fetching START **************** <br>";
00347         var_dump( $methodName );
00348         var_dump( $classObject );
00349         var_dump( $parameterArray );
00350         echo "*********** fetching END ******************<br><br><br>";*/
00351 
00352         return call_user_func_array( array( $classObject, $methodName ), $parameterArray );
00353     }
00354 
00355 
00356     /// \privatesection
00357     public $ModuleName;
00358     public $FunctionList;
00359     public $IsValid;
00360 }
00361 
00362 ?>