You are here

class xautoload_InjectedAPI_findFile in X Autoload 7.3

Same name and namespace in other branches
  1. 7.2 lib/InjectedAPI/findFile.php \xautoload_InjectedAPI_findFile

To help testability, we use an injected API instead of just a return value. The injected API can be mocked to provide a mocked file_exists(), and to monitor all suggested candidates, not just the correct return value.

Hierarchy

Expanded class hierarchy of xautoload_InjectedAPI_findFile

1 string reference to 'xautoload_InjectedAPI_findFile'
_xautoload_register in ./xautoload.early.inc
Create and register the xautoload class loader. Register the xautoload prefix, but don't register any Drupal-specific stuff yet.

File

lib/InjectedAPI/findFile.php, line 9

View source
class xautoload_InjectedAPI_findFile {

  /**
   * @var string
   *   The file that was found.
   */
  protected $file;

  /**
   * @var string
   *   The class name to look for. Set in the constructor.
   */
  protected $className;

  /**
   * @param $class_name
   *   Name of the class or interface we are trying to load.
   */
  function __construct($class_name) {
    $this->className = $class_name;
  }

  /**
   * This is done in the injected api object, so we can easily provide a mock
   * implementation.
   */
  function is_dir($dir) {
    return is_dir($dir);
  }

  /**
   * Get the name of the class we are looking for.
   *
   * @return string
   *   The class we are looking for.
   */
  function getClass() {
    return $this->className;
  }

  /**
   * Suggest a file that, if the file exists,
   * has to declare the class we are looking for.
   * Only keep the class on success.
   *
   * @param string $file
   *   The file that is supposed to declare the class.
   *
   * @return bool
   *   TRUE, if the file exists.
   *   FALSE, otherwise.
   */
  function suggestFile($file) {
    if (file_exists($file)) {
      $this->file = $file;
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * Same as suggestFile(), but skip the file_exists(),
   * assuming that we already know the file exists.
   *
   * This could make sense if a plugin already did the file_exists() check.
   *
   * @param string $file
   *   The file that is supposed to declare the class.
   *
   * @return bool
   *   TRUE, if the file was found - which is always.
   */
  function suggestFile_skipFileExists($file) {
    $this->file = $file;
    return TRUE;
  }

  /**
   * Same as suggestFile(), but assume that file_exists() returns TRUE.
   *
   * @param string $file
   *   The file that is supposed to declare the class.
   *
   * @return bool
   *   TRUE, if the file was found - which is always.
   */
  function suggestFile_checkNothing($file) {
    $this->file = $file;
    return TRUE;
  }

  /**
   * Same as suggestFile(), but check the full PHP include path.
   *
   * @param string $file
   *   The file that is supposed to declare the class.
   *
   * @return bool
   *   TRUE, if the file exists.
   *   FALSE, otherwise.
   */
  function suggestFile_checkIncludePath($file) {
    if ($this
      ->_fileExists_checkIncludePath($file)) {
      $this->file = $file;
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * When the process has finished, use this to return the result.
   *
   * @return string
   *   The file that is supposed to declare the class.
   */
  function getFile() {
    return $this->file;
  }

  /**
   * Check if a file exists, considering the full include path.
   *
   * @param string $file
   *   The filepath
   * @return boolean
   *   TRUE, if the file exists somewhere in include path.
   */
  protected function _fileExists_checkIncludePath($file) {
    if (function_exists('stream_resolve_include_path')) {

      // Use the PHP 5.3.1+ way of doing this.
      return FALSE !== stream_resolve_include_path($file);
    }
    elseif ($file[0] === DIRECTORY_SEPARATOR) {

      // That's an absolute path already.
      return file_exists($file);
    }
    else {

      // Manually loop all candidate paths.
      foreach (explode(PATH_SEPARATOR, get_include_path()) as $base_dir) {
        if (file_exists($base_dir . DIRECTORY_SEPARATOR . $file)) {
          return TRUE;
        }
      }
      return FALSE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
xautoload_InjectedAPI_findFile::$className protected property The class name to look for. Set in the constructor.
xautoload_InjectedAPI_findFile::$file protected property The file that was found.
xautoload_InjectedAPI_findFile::getClass function Get the name of the class we are looking for.
xautoload_InjectedAPI_findFile::getFile function When the process has finished, use this to return the result.
xautoload_InjectedAPI_findFile::is_dir function This is done in the injected api object, so we can easily provide a mock implementation.
xautoload_InjectedAPI_findFile::suggestFile function Suggest a file that, if the file exists, has to declare the class we are looking for. Only keep the class on success.
xautoload_InjectedAPI_findFile::suggestFile_checkIncludePath function Same as suggestFile(), but check the full PHP include path.
xautoload_InjectedAPI_findFile::suggestFile_checkNothing function Same as suggestFile(), but assume that file_exists() returns TRUE.
xautoload_InjectedAPI_findFile::suggestFile_skipFileExists function Same as suggestFile(), but skip the file_exists(), assuming that we already know the file exists.
xautoload_InjectedAPI_findFile::_fileExists_checkIncludePath protected function Check if a file exists, considering the full include path.
xautoload_InjectedAPI_findFile::__construct function