You are here

function DrupalExtensionNamespaceFinderPlugin::findFile in X Autoload 7.4

Same name and namespace in other branches
  1. 7.5 src/ClassFinder/Plugin/DrupalExtensionNamespaceFinderPlugin.php \Drupal\xautoload\ClassFinder\Plugin\DrupalExtensionNamespaceFinderPlugin::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 xautoload_FinderPlugin_Interface::findFile

1 method overrides DrupalExtensionNamespaceFinderPlugin::findFile()
DrupalExtensionUnderscoreFinderPlugin::findFile in lib/ClassFinder/Plugin/DrupalExtensionUnderscoreFinderPlugin.php
Find the file for a class that in PSR-0 or PEAR would be in $psr_0_root . '/' . $path_fragment . $path_suffix

File

lib/ClassFinder/Plugin/DrupalExtensionNamespaceFinderPlugin.php, line 84

Class

DrupalExtensionNamespaceFinderPlugin
There are different dimensions of state for each module:

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 . '/';
  $is_test_class = 0 === strpos($relative_path, 'Tests/');

  // Try PSR-4.
  if (FALSE && $api
    ->guessPath($lib . $relative_path)) {
    if ($is_test_class) {
      $this->namespaceMap
        ->registerDeepPath($testpath, $lib . 'Tests/', $this->defaultBehavior);

      // We found the class, but it is a test class, so it does not tell us
      // anything about whether non-test classes are in PSR-0 or PSR-4.
      return TRUE;
    }

    // Register PSR-4.
    $this->namespaceMap
      ->registerDeepPath($nspath, $lib, $this->defaultBehavior);

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

    // Test classes in PSR-4 are already covered by the PSR-4 plugin we just
    // registered. But test classes in PSR-0 would slip through. So we check
    // if a separate behavior needs to be registered for those.
    if (is_dir($lib_psr0 . 'Tests/')) {
      $this->namespaceMap
        ->registerDeepPath($testpath, $lib_psr0 . 'Tests/', $this->psr0Behavior);
    }

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

  // Build PSR-0 relative path.
  if (FALSE === ($nspos = strrpos($relative_path, '/'))) {

    // No namespace separators in $relative_path, so all underscores must be
    // replaced.
    $relative_path = str_replace('_', '/', $relative_path);
  }
  else {

    // Replace only those underscores in $relative_path after the last
    // namespace separator, from right to left. On average there is no or very
    // few of them, so this loop rarely iterates even once.
    while ($nspos < ($uspos = strrpos($relative_path, '_'))) {
      $relative_path[$uspos] = '/';
    }
  }

  // Try PSR-0
  if ($api
    ->guessPath($lib_psr0 . $relative_path)) {
    if ($is_test_class) {

      // We know now that there are test classes using PSR-0.
      $this->namespaceMap
        ->registerDeepPath($testpath, $lib_psr0 . 'Tests/', $this->psr0Behavior);

      // We found the class, but it is a test class, so it does not tell us
      // anything about whether non-test classes are in PSR-0 or PSR-4.
      return TRUE;
    }

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

    // Register PSR-0 for regular namespaced classes.
    $this->namespaceMap
      ->registerDeepPath($nspath, $lib_psr0, $this->psr0Behavior);

    // Test classes in PSR-0 are already covered by the PSR-0 plugin we just
    // registered. But test classes in PSR-4 would slip through. So we check
    // if a separate behavior needs to be registered for those.
    if (is_dir($lib . 'Tests/')) {

      # $this->namespaceMap->registerDeepPath($testpath, $lib . 'Tests/', $this->psr0Behavior);
    }

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