/**
  * {@inheritdoc}
  */
 public function getConfig()
 {
     // Initialize empty configuration.
     $config = ['psr-0' => [], 'psr-4' => [], 'class-location' => []];
     // Find the tokenized paths.
     $psrs = array('psr-0' => $this->classLoader->getPrefixes(), 'psr-4' => $this->classLoader->getPrefixesPsr4());
     // Get all the PSR-0 and PSR-0 and detect the ones that have Drupal tokens.
     foreach ($psrs as $psr_type => $namespaces) {
         $namespaces = $namespaces ?: [];
         foreach ($namespaces as $prefix => $paths) {
             $token_paths = array();
             if (!is_array($paths)) {
                 $paths = array($paths);
             }
             foreach ($paths as $path) {
                 $token_resolver = $this->tokenFactory->factory($path);
                 if (!$token_resolver->hasToken()) {
                     continue;
                 }
                 $path = $token_resolver->trimPath();
                 $token_paths[] = $path;
             }
             // If there were paths, add them to the config.
             if (!empty($token_paths)) {
                 $config[$psr_type][$prefix] = $token_paths;
             }
         }
     }
     // Get the drupal path configuration.
     $composer_config = json_decode(file_get_contents(static::COMPOSER_CONFIGURATION_NAME));
     $config['class-location'] = array();
     if (isset($composer_config->autoload->{'class-location'})) {
         $config['class-location'] = array_merge($config['class-location'], (array) $composer_config->autoload->{'class-location'});
     }
     if (isset($composer_config->{'autoload-dev'}->{'class-location'})) {
         $config['class-location'] = array_merge($config['class-location'], (array) $composer_config->{'autoload-dev'}->{'class-location'});
     }
     return $config;
 }
Example #2
0
 /**
  * {@inheritdoc}
  */
 public function registerPsr(\Composer\Autoload\ClassLoader $loader)
 {
     // Composer's autoloader uses a different method to add each PSR partial
     // namespace.
     $psrs = array('psr-0' => 'add', 'psr-4' => 'addPsr4');
     foreach ($psrs as $psr => $loader_method) {
         // The $psrClassMap contains an array for psr-0 and an array for psr-4.
         // Each one of those contains an array where the keys are the partial
         // namespace, and the value is an array of tokenized paths where those
         // partial namespaces can be found.
         // Ex:
         //  [
         //    'psr-0' => [
         //      [ 'Drupal\\plug\\' => ['DRUPAL_CONTRIB<plug>/lib', …] ],
         //    ],
         //  ]
         foreach ($this->psrClassMap[$psr] as $partial_namespace => $tokenized_paths) {
             if (!is_array($tokenized_paths)) {
                 // If a string was passed, then convert it to an array for
                 // consistency.
                 $tokenized_paths = array($tokenized_paths);
             }
             foreach ($tokenized_paths as $tokenized_path) {
                 try {
                     // Find the real path for the tokenized one.
                     $resolver = $this->tokenResolverFactory->factory($tokenized_path);
                     $finder = $resolver->resolve();
                     // Get the real path of the prefix.
                     $real_path = $finder->find($this->seed);
                     $loader->{$loader_method}($partial_namespace, $real_path);
                 } catch (ClassLoaderException $e) {
                 }
             }
         }
     }
 }