You are here

class CollectFilesInjectedApi in X Autoload 7.4

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

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 CollectFilesInjectedApi

1 file declares its use of CollectFilesInjectedApi
XAutoloadUnitTestCase.php in lib/Drupal/xautoload/Tests/XAutoloadUnitTestCase.php

File

lib/ClassFinder/InjectedApi/CollectFilesInjectedApi.php, line 10

Namespace

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

  /**
   * @var string
   *   The method that, if called with $this->file, will return TRUE.
   */
  protected $methodName;

  /**
   * @var string
   *   The file where $this->$method($this->file) will return TRUE.
   */
  protected $file;

  /**
   * @var array[]
   *   All files that were suggested.
   */
  protected $suggestions;

  /**
   * @param string $class_name
   * @var string $method
   *   The method that, if called with $this->file, will return TRUE.
   * @param string $file
   *   The file where $this->$method($this->file) will return TRUE.
   */
  function __construct($class_name, $method_name, $file) {
    $this->methodName = $method_name;
    $this->file = $file;
    parent::__construct($class_name);
  }

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

  /**
   * 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) {
    $this->suggestions[] = array(
      __FUNCTION__,
      $file,
    );
    return __FUNCTION__ === $this->methodName && $file === $this->file;
  }

  /**
   * 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->suggestions[] = array(
      __FUNCTION__,
      $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->suggestions[] = array(
      __FUNCTION__,
      $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) {
    $this->suggestions[] = array(
      __FUNCTION__,
      $file,
    );
    return __FUNCTION__ == $this->methodName && $file === $this->file;
  }

  /**
   * {@inheritdoc}
   */
  function guessFile($file) {
    $this->suggestions[] = array(
      __FUNCTION__,
      $file,
    );
    return __FUNCTION__ == $this->methodName && $file === $this->file;
  }

  /**
   * {@inheritdoc}
   */
  function guessPath($file) {
    $this->suggestions[] = array(
      __FUNCTION__,
      $file,
    );
    return __FUNCTION__ == $this->methodName && $file === $this->file;
  }

  /**
   * {@inheritdoc}
   */
  function claimFile($file) {
    $this->suggestions[] = array(
      __FUNCTION__,
      $file,
    );
    return TRUE;
  }

  /**
   * {@inheritdoc}
   */
  function claimPath($file) {
    $this->suggestions[] = array(
      __FUNCTION__,
      $file,
    );
    return __FUNCTION__ == $this->methodName && $file === $this->file;
  }

}

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
CollectFilesInjectedApi::$file protected property The file where $this->$method($this->file) will return TRUE.
CollectFilesInjectedApi::$methodName protected property The method that, if called with $this->file, will return TRUE.
CollectFilesInjectedApi::$suggestions protected property All files that were suggested.
CollectFilesInjectedApi::claimFile function Suggest a file that MUST exist, and if so, MUST declare the class we are looking for. Overrides InjectedApiInterface::claimFile
CollectFilesInjectedApi::claimPath function Suggest a file that MAY exist, and if so, MUST declare the class we are looking for. Overrides InjectedApiInterface::claimPath
CollectFilesInjectedApi::getSuggestions function When the process has finished, use this to return the result.
CollectFilesInjectedApi::guessFile function Suggest a file that MUST exists, and that MAY declare the class we are looking for. Overrides InjectedApiInterface::guessFile
CollectFilesInjectedApi::guessPath function Suggest a file that MAY exist, and that MAY declare the class we are looking for. Overrides InjectedApiInterface::guessPath
CollectFilesInjectedApi::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
CollectFilesInjectedApi::suggestFile_checkIncludePath function Same as suggestFile(), but check the full PHP include path. Overrides InjectedApiInterface::suggestFile_checkIncludePath
CollectFilesInjectedApi::suggestFile_checkNothing function Same as suggestFile(), but assume that file_exists() returns TRUE. Overrides InjectedApiInterface::suggestFile_checkNothing
CollectFilesInjectedApi::suggestFile_skipFileExists function Same as suggestFile(), but skip the file_exists(), assuming that we already know the file exists. Overrides InjectedApiInterface::suggestFile_skipFileExists
CollectFilesInjectedApi::__construct function The method that, if called with $this->file, will return TRUE. Overrides AbstractInjectedApi::__construct