[ Index ]

PHP Cross Reference of vtigercrm-6.1.0

title

Body

[close]

/includes/ -> Loader.php (source)

   1  <?php
   2  /*+**********************************************************************************
   3   * The contents of this file are subject to the vtiger CRM Public License Version 1.1
   4   * ("License"); You may not use this file except in compliance with the License
   5   * The Original Code is:  vtiger CRM Open Source
   6   * The Initial Developer of the Original Code is vtiger.
   7   * Portions created by vtiger are Copyright (C) vtiger.
   8   * All Rights Reserved.
   9   ************************************************************************************/
  10  
  11  global $LOADER_FILE_DIR;
  12  $LOADER_FILE_DIR = dirname(__FILE__);
  13  
  14  class Vtiger_Loader {
  15      protected static $includeCache = array();
  16      protected static $includePathCache = array();
  17  
  18      /**
  19       * Static function to resolve the qualified php filename to absolute path
  20       * @global <String> $LOADER_FILE_DIR
  21       * @param <String> $qualifiedName
  22       * @return <String> Absolute File Name
  23       */
  24  	static function resolveNameToPath($qualifiedName, $fileExtension='php') {
  25          global $LOADER_FILE_DIR;
  26          $allowedExtensions = array('php', 'js', 'css', 'less');
  27  
  28          $file = '';
  29          if(!in_array($fileExtension, $allowedExtensions)) {
  30              return '';
  31          }
  32          // TO handle loading vtiger files
  33          if (strpos($qualifiedName, '~~') === 0) {
  34              $file = str_replace('~~', '', $qualifiedName);
  35              $file = $LOADER_FILE_DIR . DIRECTORY_SEPARATOR .'..' . DIRECTORY_SEPARATOR . $file;
  36          } else if (strpos($qualifiedName, '~') === 0) {
  37              $file = str_replace('~', '', $qualifiedName);
  38              $file = $LOADER_FILE_DIR . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $file;
  39          } else {
  40              $file = str_replace('.', DIRECTORY_SEPARATOR, $qualifiedName) . '.' .$fileExtension;
  41              $file = $LOADER_FILE_DIR . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . $file;
  42          }
  43          return $file;
  44      }
  45  
  46      /**
  47       * Function to include a given php file through qualified file name
  48       * @param <String> $qualifiedName
  49       * @param <Boolean> $supressWarning
  50       * @return <Boolean>
  51       */
  52  	static function includeOnce($qualifiedName, $supressWarning=false) {
  53  
  54          if (isset(self::$includeCache[$qualifiedName])) {
  55              return true;
  56          }
  57  
  58          $file = self::resolveNameToPath($qualifiedName);
  59  
  60          if (!file_exists($file)) {
  61              return false;
  62          }
  63  
  64          // Check file inclusion before including it
  65          checkFileAccessForInclusion($file);
  66  
  67          $status = -1;
  68          if ($supressWarning) {
  69              $status = @include_once $file;
  70          } else {
  71              $status = include_once $file;
  72          }
  73  
  74          $success = ($status === 0)? false : true;
  75  
  76          if ($success) {
  77              self::$includeCache[$qualifiedName] = $file;
  78          }
  79  
  80          return $success;
  81      }
  82  
  83  	static function includePath($qualifiedName) {
  84          // Already included?
  85          if (isset(self::$includePathCache[$qualifiedName])) {
  86              return true;
  87          }
  88  
  89          $path = realpath(self::resolveNameToPath($qualifiedName));
  90          self::$includePathCache[$qualifiedName] = $path;
  91  
  92          // TODO Check if resolvedPath is already part of include path.
  93          set_include_path($path . PATH_SEPARATOR . get_include_path());
  94          return true;
  95      }
  96  
  97      /**
  98       * Function to get the class name of a given Component, of given Type, for a given Module
  99       * @param <String> $componentType
 100       * @param <String> $componentName
 101       * @param <String> $moduleName
 102       * @return <String> Required Class Name
 103       * @throws AppException
 104       */
 105  	public static function getComponentClassName($componentType, $componentName, $moduleName='Vtiger') {
 106          // Change component type from view to views, action to actions to navigate to the right path.
 107          $componentTypeDirectory = strtolower($componentType).'s';
 108          // Fall Back Directory & Fall Back Class
 109          $fallBackModuleDir = $fallBackModuleClassPath = 'Vtiger';
 110          // Intermediate Fall Back Directories & Classes, before relying on final fall back
 111          $firstFallBackModuleDir = $firstFallBackModuleClassPath = '';
 112          $secondFallBackDir = $secondFallBackClassPath = '';
 113          // Default module directory & class name
 114          $moduleDir = $moduleClassPath = $moduleName;
 115          // Change the Module directory & class, along with intermediate fall back directory and class, if module names has submodule as well
 116          if(strpos($moduleName, ':') > 0) {
 117              $moduleHierarchyParts = explode(':', $moduleName);
 118              $moduleDir = str_replace(':', '.', $moduleName);
 119              $moduleClassPath = str_replace(':', '_', $moduleName);
 120              $actualModule = $moduleHierarchyParts[count($moduleHierarchyParts)-1];
 121              $secondFallBackModuleDir= $secondFallBackModuleClassPath =  $actualModule;
 122              if($actualModule != 'Users') {
 123              $baseModule = $moduleHierarchyParts[0];
 124              if($baseModule == 'Settings')  $baseModule = 'Settings:Vtiger';
 125                  $firstFallBackDir = str_replace(':', '.', $baseModule);
 126                  $firstFallBackClassPath = str_replace(':', '_', $baseModule);
 127              }
 128          }
 129          // Build module specific file path and class name
 130          $moduleSpecificComponentFilePath = Vtiger_Loader::resolveNameToPath('modules.'.$moduleDir.'.'.$componentTypeDirectory.'.'.$componentName);
 131          $moduleSpecificComponentClassName = $moduleClassPath.'_'.$componentName.'_'.$componentType;
 132          if(file_exists($moduleSpecificComponentFilePath)) {
 133              return $moduleSpecificComponentClassName;
 134          }
 135  
 136  
 137          // Build first intermediate fall back file path and class name
 138          if(!empty($firstFallBackDir) && !empty($firstFallBackClassPath)) {
 139              $fallBackComponentFilePath = Vtiger_Loader::resolveNameToPath('modules.'.$firstFallBackDir.'.'.$componentTypeDirectory.'.'.$componentName);
 140              $fallBackComponentClassName = $firstFallBackClassPath.'_'.$componentName.'_'.$componentType;
 141  
 142              if(file_exists($fallBackComponentFilePath)) {
 143                  return $fallBackComponentClassName;
 144              }
 145          }
 146  
 147          // Build intermediate fall back file path and class name
 148          if(!empty($secondFallBackModuleDir) && !empty($secondFallBackModuleClassPath)) {
 149              $fallBackComponentFilePath = Vtiger_Loader::resolveNameToPath('modules.'.$secondFallBackModuleDir.'.'.$componentTypeDirectory.'.'.$componentName);
 150              $fallBackComponentClassName = $secondFallBackModuleClassPath.'_'.$componentName.'_'.$componentType;
 151  
 152              if(file_exists($fallBackComponentFilePath)) {
 153                  return $fallBackComponentClassName;
 154              }
 155          }
 156  
 157          // Build fall back file path and class name
 158          $fallBackComponentFilePath = Vtiger_Loader::resolveNameToPath('modules.'.$fallBackModuleDir.'.'.$componentTypeDirectory.'.'.$componentName);
 159          $fallBackComponentClassName = $fallBackModuleClassPath.'_'.$componentName.'_'.$componentType;
 160          if(file_exists($fallBackComponentFilePath)) {
 161              return $fallBackComponentClassName;
 162          }
 163          throw new AppException('Handler not found.');
 164      }
 165  
 166      /**
 167       * Function to auto load the required class files matching the directory pattern modules/xyz/types/Abc.php for class xyz_Abc_Type
 168       * @param <String> $className
 169       * @return <Boolean>
 170       */
 171  	public static function autoLoad($className) {
 172          $parts = explode('_', $className);
 173          $noOfParts = count($parts);
 174          if($noOfParts > 2) {
 175              $filePath = 'modules.';
 176              // Append modules and sub modules names to the path
 177              for($i=0; $i<($noOfParts-2); ++$i) {
 178                  $filePath .= $parts[$i]. '.';
 179              }
 180              $fileName = $parts[$noOfParts-2];
 181              $fileComponentName = strtolower($parts[$noOfParts-1]).'s';
 182              $filePath .= $fileComponentName. '.' .$fileName;
 183              return Vtiger_Loader::includeOnce($filePath);
 184          }
 185          return false;
 186      }
 187  }
 188  
 189  function vimport($qualifiedName) {
 190      return Vtiger_Loader::includeOnce($qualifiedName);
 191  }
 192  
 193  function vimport_try($qualifiedName) {
 194      return Vtiger_Loader::includeOnce($qualifiedName, true);
 195  }
 196  
 197  function vimport_path($qualifiedName) {
 198      return Vtiger_Loader::includePath($qualifiedName);
 199  }
 200  
 201  spl_autoload_register('Vtiger_Loader::autoLoad');


Generated: Fri Nov 28 20:08:37 2014 Cross-referenced by PHPXref 0.7.1