diff --git a/core/lib/Drupal/Core/Entity/AnnotatedClassDiscovery.php b/core/lib/Drupal/Core/Entity/AnnotatedClassDiscovery.php index d32c571..76b34b2 100644 --- a/core/lib/Drupal/Core/Entity/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Core/Entity/AnnotatedClassDiscovery.php @@ -7,84 +7,21 @@ namespace Drupal\Core\Entity; -use DirectoryIterator; -use Drupal\Component\Plugin\Discovery\DiscoveryInterface; -use Drupal\Component\Reflection\MockFileFinder; -use Drupal\Core\Annotation\Plugin; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Annotations\AnnotationRegistry; -use Doctrine\Common\Reflection\StaticReflectionParser; +use Drupal\Core\Plugin\Discovery\AnnotatedClassDiscoveryBase; /** * Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces. */ -class AnnotatedClassDiscovery implements DiscoveryInterface { +class AnnotatedClassDiscovery extends AnnotatedClassDiscoveryBase { /** - * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). + * Overrides \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscoveryBase::getDirectoryStructure(). */ - public function getDefinition($plugin_id) { - $plugins = $this->getDefinitions(); - return isset($plugins[$plugin_id]) ? $plugins[$plugin_id] : NULL; - } - - /** - * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions(). - */ - public function getDefinitions() { - $definitions = array(); - $reader = new AnnotationReader(); - // Prevent @endlink from being parsed as an annotation. - $reader->addGlobalIgnoredName('endlink'); - - // Register the namespace of classes that can be used for annotations. - AnnotationRegistry::registerAutoloadNamespace('Drupal\Core\Annotation', array(DRUPAL_ROOT . '/core/lib')); - // Get all PSR-0 namespaces. - $namespaces = drupal_classloader()->getNamespaces(); - foreach ($namespaces as $ns => $namespace_dirs) { - - // OS-Safe directory separators. - $ns = str_replace('\\', DIRECTORY_SEPARATOR, $ns); - - foreach ($namespace_dirs as $dir) { - // Check for the pre-determined directory structure to find plugins. - $prefix = implode(DIRECTORY_SEPARATOR, array( - $ns, - 'EntityType', - )); - $dir .= DIRECTORY_SEPARATOR . $prefix; - - // If the directory structure exists, look for classes. - if (file_exists($dir)) { - $directories = new DirectoryIterator($dir); - foreach ($directories as $fileinfo) { - // @todo Once core requires 5.3.6, use $fileinfo->getExtension(). - if (pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION) == 'php') { - $class = str_replace( - DIRECTORY_SEPARATOR, - '\\', - $prefix . DIRECTORY_SEPARATOR . $fileinfo->getBasename('.php') - ); - - // The filename is already known, so there is no need to find the - // file. However, StaticReflectionParser needs a finder, so use a - // mock version. - $finder = MockFileFinder::create($fileinfo->getPathName()); - $parser = new StaticReflectionParser($class, $finder); - - if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), 'Drupal\Core\Annotation\Plugin')) { - // AnnotationInterface::get() returns the array definition - // instead of requiring us to work with the annotation object. - $definition = $annotation->get(); - $definition['class'] = $class; - $definitions[$definition['id']] = $definition; - } - } - } - } - } - } - return $definitions; + protected function getDirectoryStructure($namespace) { + return array( + $namespace, + 'EntityType', + ); } } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php index bd89a92..a51758c 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php @@ -7,18 +7,10 @@ namespace Drupal\Core\Plugin\Discovery; -use DirectoryIterator; -use Drupal\Component\Plugin\Discovery\DiscoveryInterface; -use Drupal\Component\Reflection\MockFileFinder; -use Drupal\Core\Annotation\Plugin; -use Doctrine\Common\Annotations\AnnotationReader; -use Doctrine\Common\Annotations\AnnotationRegistry; -use Doctrine\Common\Reflection\StaticReflectionParser; - /** * Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces. */ -class AnnotatedClassDiscovery implements DiscoveryInterface { +class AnnotatedClassDiscovery extends AnnotatedClassDiscoveryBase { /** * Constructs an AnnotatedClassDiscovery object. @@ -29,72 +21,15 @@ function __construct($owner, $type) { } /** - * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinition(). - */ - public function getDefinition($plugin_id) { - $plugins = $this->getDefinitions(); - return isset($plugins[$plugin_id]) ? $plugins[$plugin_id] : NULL; - } - - /** - * Implements Drupal\Component\Plugin\Discovery\DiscoveryInterface::getDefinitions(). + * Overrides \Drupal\Core\Plugin\Discovery\AnnotatedClassDiscoveryBase::getDirectoryStructure(). */ - public function getDefinitions() { - $definitions = array(); - $reader = new AnnotationReader(); - // Prevent @endlink from being parsed as an annotation. - $reader->addGlobalIgnoredName('endlink'); - - // Register the namespace of classes that can be used for annotations. - AnnotationRegistry::registerAutoloadNamespace('Drupal\Core\Annotation', array(DRUPAL_ROOT . '/core/lib')); - // Get all PSR-0 namespaces. - $namespaces = drupal_classloader()->getNamespaces(); - foreach ($namespaces as $ns => $namespace_dirs) { - - // OS-Safe directory separators. - $ns = str_replace('\\', DIRECTORY_SEPARATOR, $ns); - - foreach ($namespace_dirs as $dir) { - // Check for the pre-determined directory structure to find plugins. - $prefix = implode(DIRECTORY_SEPARATOR, array( - $ns, - 'Plugin', - $this->owner, - $this->type - )); - $dir .= DIRECTORY_SEPARATOR . $prefix; - - // If the directory structure exists, look for classes. - if (file_exists($dir)) { - $directories = new DirectoryIterator($dir); - foreach ($directories as $fileinfo) { - // @todo Once core requires 5.3.6, use $fileinfo->getExtension(). - if (pathinfo($fileinfo->getFilename(), PATHINFO_EXTENSION) == 'php') { - $class = str_replace( - DIRECTORY_SEPARATOR, - '\\', - $prefix . DIRECTORY_SEPARATOR . $fileinfo->getBasename('.php') - ); - - // The filename is already known, so there is no need to find the - // file. However, StaticReflectionParser needs a finder, so use a - // mock version. - $finder = MockFileFinder::create($fileinfo->getPathName()); - $parser = new StaticReflectionParser($class, $finder); - - if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), 'Drupal\Core\Annotation\Plugin')) { - // AnnotationInterface::get() returns the array definition - // instead of requiring us to work with the annotation object. - $definition = $annotation->get(); - $definition['class'] = $class; - $definitions[$definition['id']] = $definition; - } - } - } - } - } - } - return $definitions; + protected function getDirectoryStructure($namespace) { + return array( + $namespace, + 'Plugin', + $this->owner, + $this->type, + ); } } diff --git a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscoveryBase.php similarity index 85% copy from core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php copy to core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscoveryBase.php index bd89a92..f9ce4e2 100644 --- a/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscovery.php +++ b/core/lib/Drupal/Core/Plugin/Discovery/AnnotatedClassDiscoveryBase.php @@ -2,7 +2,7 @@ /** * @file - * Definition of Drupal\Core\Plugin\Discovery\AnnotatedClassDiscovery. + * Definition of Drupal\Core\Plugin\Discovery\AnnotatedClassDiscoveryBase. */ namespace Drupal\Core\Plugin\Discovery; @@ -18,14 +18,21 @@ /** * Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces. */ -class AnnotatedClassDiscovery implements DiscoveryInterface { +abstract class AnnotatedClassDiscoveryBase implements DiscoveryInterface { /** - * Constructs an AnnotatedClassDiscovery object. + * Determines the directory structure under which annotated classes are found. + * + * @param string $namespace + * The base namespace for this annotated class. + * + * @return array + * An array whose values are parts of the directory structure. */ - function __construct($owner, $type) { - $this->owner = $owner; - $this->type = $type; + protected function getDirectoryStructure($namespace) { + return array( + $namespace, + ); } /** @@ -54,14 +61,9 @@ public function getDefinitions() { // OS-Safe directory separators. $ns = str_replace('\\', DIRECTORY_SEPARATOR, $ns); + $prefix = implode(DIRECTORY_SEPARATOR, $this->getDirectoryStructure($ns)); foreach ($namespace_dirs as $dir) { // Check for the pre-determined directory structure to find plugins. - $prefix = implode(DIRECTORY_SEPARATOR, array( - $ns, - 'Plugin', - $this->owner, - $this->type - )); $dir .= DIRECTORY_SEPARATOR . $prefix; // If the directory structure exists, look for classes.