View source
<?php
namespace Drupal\xautoload\Adapter;
use Drupal\xautoload\DirectoryBehavior\DefaultDirectoryBehavior;
use xautoload_FinderPlugin_Interface;
use Drupal\xautoload\ClassFinder\ExtendedClassFinderInterface;
use Drupal\xautoload\ClassFinder\Plugin\DrupalExtensionNamespaceFinderPlugin;
use Drupal\xautoload\ClassFinder\Plugin\DrupalExtensionUnderscoreFinderPlugin;
class DrupalExtensionAdapter {
protected $system;
protected $finder;
protected $namespaceBehaviors = array();
protected $prefixBehaviors = array();
protected $namespaceMap;
protected $prefixMap;
protected $registered = array();
protected $defaultBehavior;
function __construct($system, $finder) {
$this->system = $system;
$this->finder = $finder;
$this->namespaceMap = $finder
->getNamespaceMap();
$this->prefixMap = $finder
->getPrefixMap();
foreach (array(
'module',
'theme',
) as $extension_type) {
$this->namespaceBehaviors[$extension_type] = new DrupalExtensionNamespaceFinderPlugin($extension_type, $this->namespaceMap, $this->prefixMap, $this->system);
$this->prefixBehaviors[$extension_type] = new DrupalExtensionUnderscoreFinderPlugin($extension_type, $this->namespaceMap, $this->prefixMap, $this->system);
}
$this->defaultBehavior = new DefaultDirectoryBehavior();
}
function registerExtensionsByName($extension_names) {
$this
->registerExtensions($this->system
->getExtensionTypes($extension_names));
}
function registerActiveExtensions() {
$this
->registerExtensions($this->system
->getActiveExtensions());
}
function registerExtensions(array $extensions) {
$prefix_map = array();
$namespace_map = array();
foreach ($extensions as $name => $type) {
if (empty($this->namespaceBehaviors[$type])) {
continue;
}
if (!empty($this->registered[$name])) {
continue;
}
$namespace_map['Drupal/' . $name . '/'][$name] = $this->namespaceBehaviors[$type];
$prefix_map[str_replace('_', '/', $name) . '/'][$name] = $this->prefixBehaviors[$type];
$this->registered[$name] = TRUE;
}
$this->namespaceMap
->registerDeepPaths($namespace_map);
$this->prefixMap
->registerDeepPaths($prefix_map);
}
function registerExtension($name, $type) {
if (!empty($this->registered[$name])) {
return;
}
$this->namespaceMap
->registerDeepPath('Drupal/' . $name . '/', $name, $this->namespaceBehaviors[$type]);
$this->prefixMap
->registerDeepPath(str_replace('_', '/', $name) . '/', $name, $this->prefixBehaviors[$type]);
$this->registered[$name] = TRUE;
}
function registerExtensionPsr4($name, $extension_dir, $subdir) {
if (!empty($this->registered[$name])) {
if ('psr-4' === $this->registered[$name]) {
return;
}
$this->namespaceMap
->unregisterDeepPath('Drupal/' . $name . '/', $name);
$this->prefixMap
->unregisterDeepPath(str_replace('_', '/', $name) . '/', $name);
}
$dir = strlen($subdir) ? $extension_dir . '/' . trim($subdir, '/') . '/' : $extension_dir . '/';
$this->namespaceMap
->registerDeepPath('Drupal/' . $name . '/', $dir, $this->defaultBehavior);
if (is_dir($psr0_tests_dir = $extension_dir . '/lib/Drupal/' . $name . '/Tests')) {
$this->namespaceMap
->registerDeepPath('Drupal/' . $name . '/Tests/', $psr0_tests_dir, $this->defaultBehavior);
}
}
}