Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176

This commit is contained in:
Pantheon Automation 2015-08-17 17:00:26 -07:00 committed by Greg Anderson
commit 9921556621
13277 changed files with 1459781 additions and 0 deletions

View file

@ -0,0 +1,71 @@
<?php
/**
* @file
* Contains \Drupal\Component\Annotation\AnnotationBase.
*/
namespace Drupal\Component\Annotation;
/**
* Provides a base class for classed annotations.
*/
abstract class AnnotationBase implements AnnotationInterface {
/**
* The annotated class ID.
*
* @var string
*/
public $id;
/**
* The class used for this annotated class.
*
* @var string
*/
protected $class;
/**
* The provider of the annotated class.
*
* @var string
*/
protected $provider;
/**
* {@inheritdoc}
*/
public function getProvider() {
return $this->provider;
}
/**
* {@inheritdoc}
*/
public function setProvider($provider) {
$this->provider = $provider;
}
/**
* {@inheritdoc}
*/
public function getId() {
return $this->id;
}
/**
* {@inheritdoc}
*/
public function getClass() {
return $this->class;
}
/**
* {@inheritdoc}
*/
public function setClass($class) {
$this->class = $class;
}
}

View file

@ -0,0 +1,55 @@
<?php
/**
* @file
* Contains \Drupal\Component\Annotation\AnnotationInterface.
*/
namespace Drupal\Component\Annotation;
/**
* Defines a common interface for classed annotations.
*/
interface AnnotationInterface {
/**
* Gets the value of an annotation.
*/
public function get();
/**
* Gets the name of the provider of the annotated class.
*
* @return string
*/
public function getProvider();
/**
* Sets the name of the provider of the annotated class.
*
* @param string $provider
*/
public function setProvider($provider);
/**
* Gets the unique ID for this annotated class.
*
* @return string
*/
public function getId();
/**
* Gets the class of the annotated class.
*
* @return string
*/
public function getClass();
/**
* Sets the class of the annotated class.
*
* @param string $class
*/
public function setClass($class);
}

View file

@ -0,0 +1,117 @@
<?php
/**
* @file
* Contains \Drupal\Component\Annotation\Plugin.
*/
namespace Drupal\Component\Annotation;
use Drupal\Component\Utility\NestedArray;
/**
* Defines a Plugin annotation object.
*
* Annotations in plugin classes can use this class in order to pass various
* metadata about the plugin through the parser to
* DiscoveryInterface::getDefinitions() calls. This allows the metadata
* of a class to be located with the class itself, rather than in module-based
* info hooks.
*
* @ingroup plugin_api
*
* @Annotation
*/
class Plugin implements AnnotationInterface {
/**
* The plugin definition read from the class annotation.
*
* @var array
*/
protected $definition;
/**
* Constructs a Plugin object.
*
* Builds up the plugin definition and invokes the get() method for any
* classed annotations that were used.
*/
public function __construct($values) {
$reflection = new \ReflectionClass($this);
// Only keep actual default values by ignoring NULL values.
$defaults = array_filter($reflection->getDefaultProperties(), function ($value) {
return $value !== NULL;
});
$parsed_values = $this->parse($values);
$this->definition = NestedArray::mergeDeep($defaults, $parsed_values);
}
/**
* Parses an annotation into its definition.
*
* @param array $values
* The annotation array.
*
* @return array
* The parsed annotation as a definition.
*/
protected function parse(array $values) {
$definitions = array();
foreach ($values as $key => $value) {
if ($value instanceof AnnotationInterface) {
$definitions[$key] = $value->get();
}
elseif (is_array($value)) {
$definitions[$key] = $this->parse($value);
}
else {
$definitions[$key] = $value;
}
}
return $definitions;
}
/**
* {@inheritdoc}
*/
public function get() {
return $this->definition;
}
/**
* {@inheritdoc}
*/
public function getProvider() {
return isset($this->definition['provider']) ? $this->definition['provider'] : FALSE;
}
/**
* {@inheritdoc}
*/
public function setProvider($provider) {
$this->definition['provider'] = $provider;
}
/**
* {@inheritdoc}
*/
public function getId() {
return $this->definition['id'];
}
/**
* {@inheritdoc}
*/
public function getClass() {
return $this->definition['class'];
}
/**
* {@inheritdoc}
*/
public function setClass($class) {
$this->definition['class'] = $class;
}
}

View file

@ -0,0 +1,151 @@
<?php
/**
* @file
* Contains \Drupal\Component\Annotation\Plugin\Discovery\AnnotatedClassDiscovery.
*/
namespace Drupal\Component\Annotation\Plugin\Discovery;
use Drupal\Component\Annotation\AnnotationInterface;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Annotation\Reflection\MockFileFinder;
use Doctrine\Common\Annotations\SimpleAnnotationReader;
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\Common\Reflection\StaticReflectionParser;
use Drupal\Component\Plugin\Discovery\DiscoveryTrait;
/**
* Defines a discovery mechanism to find annotated plugins in PSR-0 namespaces.
*/
class AnnotatedClassDiscovery implements DiscoveryInterface {
use DiscoveryTrait;
/**
* The namespaces within which to find plugin classes.
*
* @var string[]
*/
protected $pluginNamespaces;
/**
* The name of the annotation that contains the plugin definition.
*
* The class corresponding to this name must implement
* \Drupal\Component\Annotation\AnnotationInterface.
*
* @var string
*/
protected $pluginDefinitionAnnotationName;
/**
* The doctrine annotation reader.
*
* @var \Doctrine\Common\Annotations\Reader
*/
protected $annotationReader;
/**
* Constructs a new instance.
*
* @param string[] $plugin_namespaces
* (optional) An array of namespace that may contain plugin implementations.
* Defaults to an empty array.
* @param string $plugin_definition_annotation_name
* (optional) The name of the annotation that contains the plugin definition.
* Defaults to 'Drupal\Component\Annotation\Plugin'.
*/
function __construct($plugin_namespaces = array(), $plugin_definition_annotation_name = 'Drupal\Component\Annotation\Plugin') {
$this->pluginNamespaces = $plugin_namespaces;
$this->pluginDefinitionAnnotationName = $plugin_definition_annotation_name;
}
/**
* Gets the used doctrine annotation reader.
*
* @return \Doctrine\Common\Annotations\Reader
* The annotation reader.
*/
protected function getAnnotationReader() {
if (!isset($this->annotationReader)) {
$this->annotationReader = new SimpleAnnotationReader();
// Add the namespaces from the main plugin annotation, like @EntityType.
$namespace = substr($this->pluginDefinitionAnnotationName, 0, strrpos($this->pluginDefinitionAnnotationName, '\\'));
$this->annotationReader->addNamespace($namespace);
}
return $this->annotationReader;
}
/**
* {@inheritdoc}
*/
public function getDefinitions() {
$definitions = array();
$reader = $this->getAnnotationReader();
// Clear the annotation loaders of any previous annotation classes.
AnnotationRegistry::reset();
// Register the namespaces of classes that can be used for annotations.
AnnotationRegistry::registerLoader('class_exists');
// Search for classes within all PSR-0 namespace locations.
foreach ($this->getPluginNamespaces() as $namespace => $dirs) {
foreach ($dirs as $dir) {
if (file_exists($dir)) {
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($dir, \RecursiveDirectoryIterator::SKIP_DOTS)
);
foreach ($iterator as $fileinfo) {
if ($fileinfo->getExtension() == 'php') {
$sub_path = $iterator->getSubIterator()->getSubPath();
$sub_path = $sub_path ? str_replace(DIRECTORY_SEPARATOR, '\\', $sub_path) . '\\' : '';
$class = $namespace . '\\' . $sub_path . $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, TRUE);
/** @var $annotation \Drupal\Component\Annotation\AnnotationInterface */
if ($annotation = $reader->getClassAnnotation($parser->getReflectionClass(), $this->pluginDefinitionAnnotationName)) {
$this->prepareAnnotationDefinition($annotation, $class);
$definitions[$annotation->getId()] = $annotation->get();
}
}
}
}
}
}
// Don't let annotation loaders pile up.
AnnotationRegistry::reset();
return $definitions;
}
/**
* Prepares the annotation definition.
*
* @param \Drupal\Component\Annotation\AnnotationInterface $annotation
* The annotation derived from the plugin.
* @param string $class
* The class used for the plugin.
*/
protected function prepareAnnotationDefinition(AnnotationInterface $annotation, $class) {
$annotation->setClass($class);
}
/**
* Gets an array of PSR-0 namespaces to search for plugin classes.
*
* @return string[]
*/
protected function getPluginNamespaces() {
return $this->pluginNamespaces;
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* @file
* Contains \Drupal\Component\Annotation\PluginID.
*/
namespace Drupal\Component\Annotation;
/**
* Defines a Plugin annotation object that just contains an ID.
*
* @Annotation
*/
class PluginID extends AnnotationBase {
/**
* The plugin ID.
*
* When an annotation is given no key, 'value' is assumed by Doctrine.
*
* @var string
*/
public $value;
/**
* {@inheritdoc}
*/
public function get() {
return array(
'id' => $this->value,
'class' => $this->class,
'provider' => $this->provider,
);
}
/**
* {@inheritdoc}
*/
public function getId() {
return $this->value;
}
}

View file

@ -0,0 +1,44 @@
<?php
/**
* @file
* Contains \Drupal\Component\Annotation\Reflection\MockFileFinder.
*/
namespace Drupal\Component\Annotation\Reflection;
use Doctrine\Common\Reflection\ClassFinderInterface;
/**
* Defines a mock file finder that only returns a single filename.
*
* This can be used with Doctrine\Common\Reflection\StaticReflectionParser if
* the filename is known and inheritance is not a concern (for example, if
* only the class annotation is needed).
*/
class MockFileFinder implements ClassFinderInterface {
/**
* The only filename this finder ever returns.
*
* @var string
*/
protected $filename;
/**
* Implements Doctrine\Common\Reflection\ClassFinderInterface::findFile().
*/
public function findFile($class) {
return $this->filename;
}
/**
* Creates new mock file finder objects.
*/
static public function create($filename) {
$object = new static();
$object->filename = $filename;
return $object;
}
}