You are here

class ClassFinderAdapter in X Autoload 7.5

Same name and namespace in other branches
  1. 7.4 lib/Adapter/ClassFinderAdapter.php \Drupal\xautoload\Adapter\ClassFinderAdapter

An instance of this class is passed around to implementations of hook_xautoload(). It acts as a wrapper around the ClassFinder, to register stuff.

Hierarchy

Expanded class hierarchy of ClassFinderAdapter

7 files declare their use of ClassFinderAdapter
ComposerDir.php in src/Discovery/ComposerDir.php
ComposerJson.php in src/Discovery/ComposerJson.php
ComposerJsonTargetDir.php in src/Discovery/ComposerJsonTargetDir.php
ComposerJsonTest.php in tests/src/ComposerJsonTest.php
hookXautoload.php in legacy/lib/InjectedAPI/hookXautoload.php

... See full list

File

src/Adapter/ClassFinderAdapter.php, line 21

Namespace

Drupal\xautoload\Adapter
View source
class ClassFinderAdapter implements ClassFinderAdapterInterface {

  /**
   * @var ExtendedClassFinderInterface
   */
  protected $finder;

  /**
   * @var GenericPrefixMap
   */
  protected $prefixMap;

  /**
   * @var GenericPrefixMap
   */
  protected $namespaceMap;

  /**
   * @var ClassMapGeneratorInterface
   */
  protected $classMapGenerator;

  /**
   * @param ExtendedClassFinderInterface $finder
   *
   * @return self
   */
  static function create($finder) {
    return new self($finder, new ClassMapGenerator());
  }

  /**
   * @param ExtendedClassFinderInterface $finder
   *   The class finder object.
   * @param ClassMapGeneratorInterface $classmap_generator
   */
  function __construct($finder, $classmap_generator) {
    $this->finder = $finder;
    $this->prefixMap = $finder
      ->getPrefixMap();
    $this->namespaceMap = $finder
      ->getNamespaceMap();
    $this->defaultBehavior = new DefaultDirectoryBehavior();
    $this->psr0Behavior = new Psr0DirectoryBehavior();
    $this->classMapGenerator = $classmap_generator;
  }

  /**
   * @return \Drupal\xautoload\ClassFinder\GenericPrefixMap
   */
  function getNamespaceMap() {
    return $this->namespaceMap;
  }

  /**
   * @return GenericPrefixMap
   */
  function getPrefixMap() {
    return $this->prefixMap;
  }

  /**
   * @return ClassMapGeneratorInterface
   */
  function getClassmapGenerator() {
    return $this->classMapGenerator;
  }

  /**
   * @return ClassMapGeneratorInterface
   */
  function getFinder() {
    return $this->finder;
  }

  //                                                                   Discovery
  // ---------------------------------------------------------------------------

  /**
   * {@inheritdoc}
   */
  function addClassmapSources($paths) {
    $map = $this->classMapGenerator
      ->wildcardPathsToClassmap($paths);
    $this
      ->addClassMap($map);
  }

  //                                                              Composer tools
  // ---------------------------------------------------------------------------

  /**
   * {@inheritdoc}
   */
  function composerJson($file) {
    $json = ComposerJson::createFromFile($file);
    $json
      ->writeToAdapter($this);
  }

  /**
   * {@inheritdoc}
   */
  function composerDir($dir) {
    $dir = ComposerDir::create($dir);
    $dir
      ->writeToAdapter($this);
  }

  //                                                      multiple PSR-0 / PSR-4
  // ---------------------------------------------------------------------------

  /**
   * {@inheritdoc}
   */
  function addMultiplePsr0(array $prefixes) {
    $namespace_map = array();
    $prefix_map = array();
    foreach ($prefixes as $prefix => $paths) {
      if (FALSE === strpos($prefix, '\\')) {
        $logical_base_path = Util::prefixLogicalPath($prefix);
        foreach ((array) $paths as $root_path) {
          $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' . $logical_base_path : $logical_base_path;
          $prefix_map[$logical_base_path][$deep_path] = $this->defaultBehavior;
        }
      }
      $logical_base_path = Util::namespaceLogicalPath($prefix);
      foreach ((array) $paths as $root_path) {
        $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' . $logical_base_path : $logical_base_path;
        $namespace_map[$logical_base_path][$deep_path] = $this->psr0Behavior;
      }
    }
    if (!empty($prefix_map)) {
      $this->prefixMap
        ->registerDeepPaths($prefix_map);
    }
    $this->namespaceMap
      ->registerDeepPaths($namespace_map);
  }

  /**
   * {@inheritdoc}
   */
  function addMultiplePsr4(array $map) {
    $namespace_map = array();
    foreach ($map as $namespace => $paths) {
      $logical_base_path = Util::namespaceLogicalPath($namespace);
      foreach ($paths as $root_path) {
        $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' : '';
        $namespace_map[$logical_base_path][$deep_path] = $this->defaultBehavior;
      }
    }
    $this->namespaceMap
      ->registerDeepPaths($namespace_map);
  }

  //                                                        Composer ClassLoader
  // ---------------------------------------------------------------------------

  /**
   * {@inheritdoc}
   */
  function addClassMap(array $classMap) {
    $this->finder
      ->registerClasses($classMap);
  }

  /**
   * {@inheritdoc}
   */
  function add($prefix, $paths) {
    if (FALSE === strpos($prefix, '\\')) {

      // Due to the ambiguity of PSR-0, this could be either PEAR-like or namespaced.
      $logical_base_path = Util::prefixLogicalPath($prefix);
      foreach ((array) $paths as $root_path) {
        $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' . $logical_base_path : $logical_base_path;
        $this->prefixMap
          ->registerDeepPath($logical_base_path, $deep_path, $this->defaultBehavior);
      }
    }

    // Namespaced PSR-0
    $logical_base_path = Util::namespaceLogicalPath($prefix);
    foreach ((array) $paths as $root_path) {
      $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' . $logical_base_path : $logical_base_path;
      $this->namespaceMap
        ->registerDeepPath($logical_base_path, $deep_path, $this->psr0Behavior);
    }
  }

  /**
   * {@inheritdoc}
   */
  function addPsr0($prefix, $paths) {
    $this
      ->add($prefix, $paths);
  }

  /**
   * {@inheritdoc}
   */
  function addPsr4($prefix, $paths) {

    // Namespaced PSR-4
    $logical_base_path = Util::namespaceLogicalPath($prefix);
    foreach ((array) $paths as $deep_path) {
      $deep_path = strlen($deep_path) ? rtrim($deep_path, '/') . '/' : '';
      $this->namespaceMap
        ->registerDeepPath($logical_base_path, $deep_path, $this->defaultBehavior);
    }
  }

  //                                                      More convenience stuff
  // ---------------------------------------------------------------------------

  /**
   * {@inheritdoc}
   */
  function addNamespacePsr0($prefix, $paths) {
    $logical_base_path = Util::namespaceLogicalPath($prefix);
    foreach ((array) $paths as $root_path) {
      $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' . $logical_base_path : $logical_base_path;
      $this->namespaceMap
        ->registerDeepPath($logical_base_path, $deep_path, $this->psr0Behavior);
    }
  }

  /**
   * {@inheritdoc}
   */
  function addPear($prefix, $paths) {
    $logical_base_path = Util::prefixLogicalPath($prefix);
    foreach ((array) $paths as $root_path) {
      $deep_path = strlen($root_path) ? rtrim($root_path, '/') . '/' . $logical_base_path : $logical_base_path;
      $this->prefixMap
        ->registerDeepPath($logical_base_path, $deep_path, $this->defaultBehavior);
    }
  }

  /**
   * {@inheritdoc}
   */
  function addPearFlat($prefix, $paths) {
    $logical_base_path = Util::prefixLogicalPath($prefix);
    foreach ((array) $paths as $deep_path) {
      $deep_path = strlen($deep_path) ? rtrim($deep_path, '/') . '/' : '';
      $this->prefixMap
        ->registerDeepPath($logical_base_path, $deep_path, $this->defaultBehavior);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ClassFinderAdapter::$classMapGenerator protected property
ClassFinderAdapter::$finder protected property
ClassFinderAdapter::$namespaceMap protected property
ClassFinderAdapter::$prefixMap protected property
ClassFinderAdapter::add function Adds a PSR-0 style prefix. Alias for ->addPsr0(). Overrides CommonRegistrationInterface::add
ClassFinderAdapter::addClassMap function Registers an array ("map") of classes to file paths. Overrides CommonRegistrationInterface::addClassMap
ClassFinderAdapter::addClassmapSources function Overrides ClassFinderAdapterInterface::addClassmapSources
ClassFinderAdapter::addMultiplePsr0 function Add multiple PSR-0 namespaces Overrides ClassFinderAdapterInterface::addMultiplePsr0
ClassFinderAdapter::addMultiplePsr4 function Add multiple PSR-4 namespaces Overrides ClassFinderAdapterInterface::addMultiplePsr4
ClassFinderAdapter::addNamespacePsr0 function Adds a PSR-0 style namespace. Overrides CommonRegistrationInterface::addNamespacePsr0
ClassFinderAdapter::addPear function Adds a PEAR-like prefix. Overrides CommonRegistrationInterface::addPear
ClassFinderAdapter::addPearFlat function Adds a prefix similar to PEAR, but with flat directories. Overrides CommonRegistrationInterface::addPearFlat
ClassFinderAdapter::addPsr0 function Adds a PSR-0 style prefix. Alias for ->add(). Overrides CommonRegistrationInterface::addPsr0
ClassFinderAdapter::addPsr4 function Adds a PSR-4 style namespace. Overrides CommonRegistrationInterface::addPsr4
ClassFinderAdapter::composerDir function Scan a directory containing Composer-generated autoload files. Overrides ClassFinderAdapterInterface::composerDir
ClassFinderAdapter::composerJson function Scan a composer.json file provided by a Composer package. Overrides ClassFinderAdapterInterface::composerJson
ClassFinderAdapter::create static function
ClassFinderAdapter::getClassmapGenerator function
ClassFinderAdapter::getFinder function
ClassFinderAdapter::getNamespaceMap function
ClassFinderAdapter::getPrefixMap function
ClassFinderAdapter::__construct function