Skip to content

Instantly share code, notes, and snippets.

@Berdir
Last active December 14, 2015 06:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Berdir/5042373 to your computer and use it in GitHub Desktop.
Save Berdir/5042373 to your computer and use it in GitHub Desktop.
<?php
/**
* @file
* Definition of Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery.
*/
namespace Drupal\Core\Plugin\Discovery;
use Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery as ComponentAnnotatedClassDiscovery;
use Drupal\Core\Extension\ModuleHandlerInterface;
/**
* Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces.
*/
class AnnotatedClassDiscovery extends ComponentAnnotatedClassDiscovery {
/**
* Module handler.
*
* @var \Drupal\Core\Extension\ModuleHandlerInterface
*/
protected $moduleHandler;
/**
* Constructs an AnnotatedClassDiscovery object.
*
* @param string $owner
* The plugin type owner.
* @param string $type
* The plugin type name.
* @param Drupal\Core\Extension\ModuleHandlerInterfac $module_handler
* Module handler
* @param array $plugin_namespaces
* An array of paths keyed by it's corresponding namespaces.
*/
function __construct($owner, $type, ModuleHandlerInterface $module_handler, array $plugin_namespaces = array()) {
$this->owner = $owner;
$this->type = $type;
$this->moduleHandler = $module_handler;
$annotation_namespaces = array(
'Drupal\Component\Annotation' => DRUPAL_ROOT . '/core/lib',
'Drupal\Core\Annotation' => DRUPAL_ROOT . '/core/lib',
);
parent::__construct($plugin_namespaces, $annotation_namespaces, 'Drupal\Core\Annotation\Plugin');
}
/**
* Overrides Drupal\Component\Plugin\Discovery\AnnotatedClassDiscovery::getPluginNamespaces().
*
* This is overridden rather than set in the constructor, because Drupal
* modules can be enabled (and therefore, namespaces registered) during the
* lifetime of a plugin manager.
*/
protected function getPluginNamespaces() {
$plugin_namespaces = parent::getPluginNamespaces();
$plugin_namespaces += array(
"Drupal\\Component\\Plugin\\{$this->owner}\\{$this->type}" => array(DRUPAL_ROOT . '/core/lib'),
"Drupal\\Core\\Plugin\\{$this->owner}\\{$this->type}" => array(DRUPAL_ROOT . '/core/lib'),
);
foreach ($this->moduleHandler->getModuleList() as $filename => $module) {
$plugin_namespaces["Drupal\\$module\\Plugin\\{$this->owner}\\{$this->type}"] = array(dirname($filename) . '/lib');
}
return $plugin_namespaces;
}
}
@sun
Copy link

sun commented Feb 26, 2013

This is overridden rather than set in the constructor, because Drupal
modules can be enabled (and therefore, namespaces registered) during the
lifetime of a plugin manager.

When modules get enabled or disabled, we're invoking DrupalKernel::updateModules()... Perhaps we should/need to trigger an event there?

Recalculating the list on every invocation looks bad to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment