You are here

function DrupalExtensionUnderscoreFinderPlugin::findFile in X Autoload 7.4

Same name and namespace in other branches
  1. 7.5 src/ClassFinder/Plugin/DrupalExtensionUnderscoreFinderPlugin.php \Drupal\xautoload\ClassFinder\Plugin\DrupalExtensionUnderscoreFinderPlugin::findFile()

Find the file for a class that in PSR-0 or PEAR would be in $psr_0_root . '/' . $path_fragment . $path_suffix

E.g.:

  • The class we look for is Some\Namespace\Some\Class
  • The file is actually in "exotic/location.php". This is not following PSR-0 or PEAR standard, so we need a plugin.

-> The class finder will transform the class name to "Some/Namespace/Some/Class.php"

  • The plugin was registered for the namespace "Some\Namespace". This is because all those exotic classes all begin with Some\Namespace\

-> The arguments will be: ($api = the API object, see below) $path_fragment = "Some/Namespace/" $path_suffix = "Some/Class.php" $api->getClass() gives the original class name, if we still need it. -> We are supposed to: if ($api->suggestFile('exotic/location.php')) { return TRUE; }

Parameters

InjectedApiInterface $api: An object with a suggestFile() method. We are supposed to suggest files until suggestFile() returns TRUE, or we have no more suggestions.

string $path_fragment: The key that this plugin was registered with. With trailing '/'.

string $path_suffix: Second part of the canonical path, ending with '.php'.

Return value

bool|NULL TRUE, if the file was found. FALSE, otherwise.

Overrides DrupalExtensionNamespaceFinderPlugin::findFile

File

lib/ClassFinder/Plugin/DrupalExtensionUnderscoreFinderPlugin.php, line 10

Class

DrupalExtensionUnderscoreFinderPlugin

Namespace

Drupal\xautoload\ClassFinder\Plugin

Code

function findFile($api, $logical_base_path, $relative_path, $extension_name = NULL) {
  $extension_file = $this->system
    ->drupalGetFilename($this->type, $extension_name);
  if (empty($extension_file)) {

    // Extension does not exist, or is not installed.
    return FALSE;
  }
  $nspath = 'Drupal/' . $extension_name . '/';
  $testpath = $nspath . 'Tests/';
  $uspath = $extension_name . '/';
  $lib = dirname($extension_file) . '/lib/';
  $lib_psr0 = $lib . 'Drupal/' . $extension_name . '/';

  // Try PEAR-Flat.
  if ($api
    ->guessPath($lib . $relative_path)) {

    // Register PEAR-Flat.
    $this->prefixMap
      ->registerDeepPath($uspath, $lib, $this->defaultBehavior);

    // Unregister the lazy plugins.
    $this->namespaceMap
      ->unregisterDeepPath($nspath, $extension_name);
    $this->prefixMap
      ->unregisterDeepPath($uspath, $extension_name);

    // See if there are PSR-0 or PSR-4 test classes.
    if (is_dir($lib_psr0 . 'Tests/')) {
      $this->namespaceMap
        ->registerDeepPath($testpath, $lib_psr0 . 'Tests/', $this->psr0Behavior);
    }
    if (is_dir($lib . 'Tests/')) {
      $this->namespaceMap
        ->registerDeepPath($testpath, $lib . 'Tests/', $this->defaultBehavior);
    }

    // The class was found, so return TRUE.
    return TRUE;
  }
}