You are here

class FindFileInjectedApi in X Autoload 7.4

Same name and namespace in other branches
  1. 7.5 src/ClassFinder/InjectedApi/FindFileInjectedApi.php \Drupal\xautoload\ClassFinder\InjectedApi\FindFileInjectedApi

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 FindFileInjectedApi

1 file declares its use of FindFileInjectedApi
Psr4FinderPlugin.php in lib/ClassFinder/Plugin/Psr4FinderPlugin.php

File

lib/ClassFinder/InjectedApi/FindFileInjectedApi.php, line 12

Namespace

Drupal\xautoload\ClassFinder\InjectedApi
View source
class FindFileInjectedApi extends AbstractInjectedApi {

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

  /**
   * 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;
  }

  /**
   * 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 (FALSE !== ($file = Util::findFileInIncludePath($file))) {
      $this->file = $file;
      return TRUE;
    }
    else {
      return FALSE;
    }
  }

  /**
   * {@inheritdoc}
   */
  function guessFile($file) {

    // The file must be included, or else we can't know if it defines the class.
    require_once $file;
    if (Util::classIsDefined($this->className)) {
      $this->file = $file;
      return TRUE;
    }
  }

  /**
   * {@inheritdoc}
   */
  function guessPath($file) {
    if (file_exists($file)) {

      // The file must be included, or else we can't know if it defines the class.
      require_once $file;
      if (Util::classIsDefined($this->className)) {
        $this->file = $file;
        return TRUE;
      }
    }
  }

  /**
   * {@inheritdoc}
   */
  function claimFile($file) {
    $this->file = $file;
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  function claimPath($file) {
    if (file_exists($file)) {
      $this->file = $file;
      return TRUE;
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractInjectedApi::$className protected property The class name to look for. Set in the constructor.
AbstractInjectedApi::forceAutoload static function Dummy method to force autoloading this class (or an ancestor).
AbstractInjectedApi::getClass function Get the name of the class we are looking for. Overrides InjectedApiInterface::getClass
AbstractInjectedApi::is_dir function This is done in the injected api object, so we can easily provide a mock implementation. Overrides InjectedApiInterface::is_dir
AbstractInjectedApi::__construct function 1
FindFileInjectedApi::$file protected property The file that was found.
FindFileInjectedApi::claimFile function Suggest a file that MUST exist, and if so, MUST declare the class we are looking for. Overrides InjectedApiInterface::claimFile 1
FindFileInjectedApi::claimPath function Suggest a file that MAY exist, and if so, MUST declare the class we are looking for. Overrides InjectedApiInterface::claimPath 1
FindFileInjectedApi::getFile function When the process has finished, use this to return the result.
FindFileInjectedApi::guessFile function Suggest a file that MUST exists, and that MAY declare the class we are looking for. Overrides InjectedApiInterface::guessFile 1
FindFileInjectedApi::guessPath function Suggest a file that MAY exist, and that MAY declare the class we are looking for. Overrides InjectedApiInterface::guessPath 1
FindFileInjectedApi::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. Overrides InjectedApiInterface::suggestFile 1
FindFileInjectedApi::suggestFile_checkIncludePath function Same as suggestFile(), but check the full PHP include path. Overrides InjectedApiInterface::suggestFile_checkIncludePath 1
FindFileInjectedApi::suggestFile_checkNothing function Same as suggestFile(), but assume that file_exists() returns TRUE. Overrides InjectedApiInterface::suggestFile_checkNothing 1
FindFileInjectedApi::suggestFile_skipFileExists function Same as suggestFile(), but skip the file_exists(), assuming that we already know the file exists. Overrides InjectedApiInterface::suggestFile_skipFileExists 1