本文整理汇总了PHP中Doctrine\Common\Annotations\DocParser::parse方法的典型用法代码示例。如果您正苦于以下问题:PHP DocParser::parse方法的具体用法?PHP DocParser::parse怎么用?PHP DocParser::parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Doctrine\Common\Annotations\DocParser
的用法示例。
在下文中一共展示了DocParser::parse方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: processDocComment
/**
* Dispatch annotations found in phpDoc with context given in second argument.
*
* @param string $phpDoc phpDoc
* @param mixed $context context
*/
private function processDocComment($phpDoc, $context = null)
{
$annotations = $this->docParser->parse($phpDoc);
foreach ($annotations as $annotation) {
$this->collection->dispatch(Events::TOKEN_PHP_ANNOTATION, new Context\PHP\Annotation($annotation, $context), $this->result);
}
}
示例2: getMethodAnnotations
/**
* {@inheritDoc}
*/
public function getMethodAnnotations(ReflectionMethod $method)
{
$class = $method->getDeclaringClass();
$context = 'method ' . $class->getName() . '::' . $method->getName() . '()';
$this->parser->setTarget(Target::TARGET_METHOD);
$this->parser->setImports($this->getMethodImports($method));
$this->parser->setIgnoredAnnotationNames($this->getIgnoredAnnotationNames($class));
return $this->parser->parse($method->getDocComment(), $context);
}
示例3: enterNode
/**
* @param Node $node
* @return void
*/
public function enterNode(Node $node)
{
if (!$node instanceof Node\Expr\MethodCall || !is_string($node->name) || !in_array(strtolower($node->name), array_map('strtolower', array_keys($this->methodsToExtractFrom)))) {
$this->previousNode = $node;
return;
}
$ignore = false;
$desc = $meaning = null;
if (null !== ($docComment = $this->getDocCommentForNode($node))) {
if ($docComment instanceof Doc) {
$docComment = $docComment->getText();
}
foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
if ($annot instanceof Ignore) {
$ignore = true;
} elseif ($annot instanceof Desc) {
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
}
}
}
if (!$node->args[0]->value instanceof String_) {
if ($ignore) {
return;
}
$message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
$id = $node->args[0]->value->value;
$index = $this->methodsToExtractFrom[strtolower($node->name)];
if (isset($node->args[$index])) {
if (!$node->args[$index]->value instanceof String_) {
if ($ignore) {
return;
}
$message = sprintf('Can only extract the translation domain from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
$domain = $node->args[$index]->value->value;
} else {
$domain = 'messages';
}
$message = new Message($id, $domain);
$message->setDesc($desc);
$message->setMeaning($meaning);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
}
示例4: testIssueWithNamespacesOrImports
public function testIssueWithNamespacesOrImports()
{
$docblock = "@Entity";
$parser = new DocParser();
$annots = $parser->parse($docblock);
$this->assertEquals(1, count($annots));
$this->assertInstanceOf("Entity", $annots[0]);
$this->assertEquals(1, count($annots));
}
示例5: testDocParsePerformance
/**
* @group performance
*/
public function testDocParsePerformance()
{
$imports = array('ignorephpdoc' => 'Annotations\\Annotation\\IgnorePhpDoc', 'ignoreannotation' => 'Annotations\\Annotation\\IgnoreAnnotation', 'route' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation\\Route', 'template' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation\\Template', '__NAMESPACE__' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures');
$ignored = array('access', 'author', 'copyright', 'deprecated', 'example', 'ignore', 'internal', 'link', 'see', 'since', 'tutorial', 'version', 'package', 'subpackage', 'name', 'global', 'param', 'return', 'staticvar', 'static', 'var', 'throws', 'inheritdoc');
$method = $this->getMethod();
$methodComment = $method->getDocComment();
$classComment = $method->getDeclaringClass()->getDocComment();
$time = microtime(true);
for ($i = 0, $c = 200; $i < $c; $i++) {
$parser = new DocParser();
$parser->setImports($imports);
$parser->setIgnoredAnnotationNames($ignored);
$parser->setIgnoreNotImportedAnnotations(true);
$parser->parse($methodComment);
$parser->parse($classComment);
}
$time = microtime(true) - $time;
$this->printResults('doc-parser', $time, $c);
}
示例6: parseItem
/**
* @param $item
* @param null $domain
*/
private function parseItem($item, $domain = null)
{
// get doc comment
$ignore = false;
$desc = $meaning = $docComment = null;
if ($item->key) {
$docComment = $item->key->getDocComment();
}
if (!$docComment) {
$docComment = $item->value->getDocComment();
}
$docComment = is_object($docComment) ? $docComment->getText() : null;
if ($docComment) {
if ($docComment instanceof Doc) {
$docComment = $docComment->getText();
}
foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $item->value->getLine()) as $annot) {
if ($annot instanceof Ignore) {
$ignore = true;
} elseif ($annot instanceof Desc) {
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
}
}
}
// check if the value is explicitly set to false => e.g. for FormField that should be rendered without label
$ignore = $ignore || !$item->value instanceof Node\Scalar\String_ || $item->value->value == false;
if (!$item->value instanceof Node\Scalar\String_ && !$item->value instanceof Node\Scalar\LNumber) {
if ($ignore) {
return;
}
$message = sprintf('Unable to extract translation id for form label/title/placeholder from non-string values, but got "%s" in %s on line %d. Please refactor your code to pass a string, or add "/** @Ignore */".', get_class($item->value), $this->file, $item->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
if ($domain === false) {
// Don't translate when domain is `false`
return;
}
$source = $this->fileSourceFactory->create($this->file, $item->value->getLine());
$id = $item->value->value;
if (null === $domain) {
$this->defaultDomainMessages[] = array('id' => $id, 'source' => $source, 'desc' => $desc, 'meaning' => $meaning);
} else {
$this->addToCatalogue($id, $source, $domain, $desc, $meaning);
}
}
示例7: scanForAnnotations
public function scanForAnnotations(string $docBlock, $context, array $imports) : Annotations
{
// Hack to ensure an attempt to autoload an annotation class is made
AnnotationRegistry::registerLoader(function ($class) {
return (bool) class_exists($class);
});
$imports = array_combine(array_map(function ($string) {
return strtolower($string);
}, array_keys($imports)), array_values($imports));
$parser = new DocParser();
$parser->setIgnoreNotImportedAnnotations(true);
$parser->setImports($imports);
return new Annotations($parser->parse($docBlock, $context));
}
示例8: fromComment
/**
* Use doctrine to parse the comment block and return the detected annotations.
*
* @param string $comment a T_DOC_COMMENT.
* @param Context $context
* @return array Annotations
*/
public function fromComment($comment, $context = null)
{
if ($context === null) {
$context = new Context(['comment' => $comment]);
} else {
$context->comment = $comment;
}
try {
self::$context = $context;
if ($context->is('annotations') === false) {
$context->annotations = [];
}
$comment = preg_replace_callback('/^[\\t ]*\\*[\\t ]+/m', function ($match) {
// Replace leading tabs with spaces.
// Workaround for http://www.doctrine-project.org/jira/browse/DCOM-255
return str_replace("\t", ' ', $match[0]);
}, $comment);
$annotations = $this->docParser->parse($comment, $context);
self::$context = null;
return $annotations;
} catch (Exception $e) {
self::$context = null;
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote($context, '/') . '\\.$/', $e->getMessage(), $matches)) {
$errorMessage = $matches[1];
$errorPos = $matches[2];
$atPos = strpos($comment, '@');
$context->line += substr_count($comment, "\n", 0, $atPos + $errorPos);
$lines = explode("\n", substr($comment, $atPos, $errorPos));
$context->character = strlen(array_pop($lines)) + 1;
// position starts at 0 character starts at 1
Logger::warning(new Exception($errorMessage . ' in ' . $context, $e->getCode(), $e));
} else {
Logger::warning($e);
}
return [];
}
}
示例9: collectParsingMetadata
/**
* Collects parsing metadata for a given class.
*
* @param \ReflectionClass $class
*/
private function collectParsingMetadata(ReflectionClass $class)
{
$ignoredAnnotationNames = self::$globalIgnoredNames;
$annotations = $this->preParser->parse($class->getDocComment(), 'class ' . $class->name);
foreach ($annotations as $annotation) {
if ($annotation instanceof IgnoreAnnotation) {
foreach ($annotation->names as $annot) {
$ignoredAnnotationNames[$annot] = true;
}
}
}
$name = $class->getName();
$this->imports[$name] = array_merge(self::$globalImports, $this->phpParser->parseClass($class), array('__NAMESPACE__' => $class->getNamespaceName()));
$this->ignoredAnnotationNames[$name] = $ignoredAnnotationNames;
}
示例10: enterNode
/**
* @param Node $node
*/
public function enterNode(Node $node)
{
if (!$node instanceof Node\Expr\New_ || !is_string($node->class) || strtolower($node->class) !== 'validationerror') {
$this->previousNode = $node;
return;
}
$ignore = false;
$desc = $meaning = null;
if (null !== ($docComment = $this->getDocCommentForNode($node))) {
if ($docComment instanceof Doc) {
$docComment = $docComment->getText();
}
foreach ($this->docParser->parse($docComment, 'file ' . $this->file . ' near line ' . $node->getLine()) as $annot) {
if ($annot instanceof Ignore) {
$ignore = true;
} elseif ($annot instanceof Desc) {
$desc = $annot->text;
} elseif ($annot instanceof Meaning) {
$meaning = $annot->text;
}
}
}
if (!$node->args[0]->value instanceof String_) {
if ($ignore) {
return;
}
$message = sprintf('Can only extract the translation id from a scalar string, but got "%s". Please refactor your code to make it extractable, or add the doc comment /** @Ignore */ to this code element (in %s on line %d).', get_class($node->args[0]->value), $this->file, $node->args[0]->value->getLine());
if ($this->logger) {
$this->logger->error($message);
return;
}
throw new RuntimeException($message);
}
$message = new Message($node->args[0]->value->value, $this->defaultDomain);
$message->setDesc($desc);
$message->setMeaning($meaning);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
// plural
if ($node->args[1]->value instanceof String_) {
$message = new Message($node->args[1]->value->value, $this->defaultDomain);
$message->setDesc($desc);
$message->setMeaning($meaning);
$message->addSource($this->fileSourceFactory->create($this->file, $node->getLine()));
$this->catalogue->add($message);
}
}
示例11: parseComment
/**
* @param RefComment $comment
*
* @return Annotation[]
*/
private function parseComment(RefComment $comment = null)
{
if ($comment === null) {
return [];
}
$annotations = $this->docParser->parse($comment->text);
foreach ($annotations as $key => $annotation) {
if (!$annotation instanceof Annotation) {
unset($annotations[$key]);
continue;
}
if ($annotation instanceof ContentInterface) {
$content = $this->extractContent($comment->text, get_class($annotation));
$annotation->setContent($content);
}
}
return $annotations;
}
示例12: parseContext
/**
*
* @param Context $context
* @return AbstractAnnotation[]
*/
protected function parseContext($context)
{
try {
self::$context = $context;
$annotations = $this->docParser->parse($context->comment, $context);
self::$context = null;
} catch (\Exception $e) {
self::$context = null;
if (preg_match('/^(.+) at position ([0-9]+) in ' . preg_quote($context, '/') . '\\.$/', $e->getMessage(), $matches)) {
$errorMessage = $matches[1];
$errorPos = $matches[2];
$atPos = strpos($context->comment, '@');
$context->line += substr_count($context->comment, "\n", 0, $atPos + $errorPos);
$lines = explode("\n", substr($context->comment, $atPos, $errorPos));
$context->character = strlen(array_pop($lines)) + 1;
// position starts at 0 character starts at 1
Logger::warning(new \Exception($errorMessage . ' in ' . $context, $e->getCode(), $e));
} else {
Logger::warning($e);
}
return array();
}
foreach ($annotations as $annotation) {
foreach ($this->processors as $processor) {
$processor->process($annotation, $context);
}
if ($annotation instanceof AbstractAnnotation) {
if ($annotation->hasPartialId()) {
if ($this->hasPartial($annotation->_partialId)) {
Logger::notice('partial="' . $annotation->_partialId . '" is not unique. another was found in ' . $annotation->_context);
}
$this->setPartial($annotation->_partialId, $annotation);
} elseif ($annotation instanceof Resource) {
$this->resources[] = $annotation;
} elseif ($annotation instanceof Model) {
$this->models[] = $annotation;
}
}
}
return $annotations;
}
示例13: collectAnnotationMetadata
/**
* Collects parsing metadata for a given annotation class
*
* @param string $name The annotation name
*/
private function collectAnnotationMetadata($name)
{
if (self::$metadataParser == null) {
self::$metadataParser = new self();
self::$metadataParser->setTarget(Target::TARGET_CLASS);
self::$metadataParser->setIgnoreNotImportedAnnotations(true);
self::$metadataParser->setImports(array('target' => 'Doctrine\\Common\\Annotations\\Annotation\\Target', 'attribute' => 'Doctrine\\Common\\Annotations\\Annotation\\Attribute', 'attributes' => 'Doctrine\\Common\\Annotations\\Annotation\\Attributes'));
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Target.php');
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Attribute.php');
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Attributes.php');
}
$class = new \ReflectionClass($name);
$docComment = $class->getDocComment();
// Sets default values for annotation metadata
$metadata = array('default_property' => null, 'has_constructor' => null !== ($constructor = $class->getConstructor()) && $constructor->getNumberOfParameters() > 0, 'properties' => array(), 'property_types' => array(), 'attribute_types' => array(), 'targets_literal' => null, 'targets' => Target::TARGET_ALL, 'is_annotation' => false !== strpos($docComment, '@Annotation'));
// verify that the class is really meant to be an annotation
if ($metadata['is_annotation']) {
foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) {
if ($annotation instanceof Target) {
$metadata['targets'] = $annotation->targets;
$metadata['targets_literal'] = $annotation->literal;
} elseif ($annotation instanceof Attributes) {
foreach ($annotation->value as $attrib) {
// handle internal type declaration
$type = isset(self::$typeMap[$attrib->type]) ? self::$typeMap[$attrib->type] : $attrib->type;
// handle the case if the property type is mixed
if ('mixed' !== $type) {
// Checks if the property has array<type>
if (false !== ($pos = strpos($type, '<'))) {
$arrayType = substr($type, $pos + 1, -1);
$type = 'array';
if (isset(self::$typeMap[$arrayType])) {
$arrayType = self::$typeMap[$arrayType];
}
$metadata['attribute_types'][$attrib->name]['array_type'] = $arrayType;
}
$metadata['attribute_types'][$attrib->name]['type'] = $type;
$metadata['attribute_types'][$attrib->name]['value'] = $attrib->type;
$metadata['attribute_types'][$attrib->name]['required'] = $attrib->required;
}
}
}
}
// if not has a constructor will inject values into public properties
if (false === $metadata['has_constructor']) {
// collect all public properties
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$metadata['properties'][$property->name] = $property->name;
// checks if the property has @var annotation
if (false !== ($propertyComment = $property->getDocComment()) && false !== strpos($propertyComment, '@var') && preg_match('/@var\\s+([^\\s]+)/', $propertyComment, $matches)) {
// literal type declaration
$value = $matches[1];
// handle internal type declaration
$type = isset(self::$typeMap[$value]) ? self::$typeMap[$value] : $value;
// handle the case if the property type is mixed
if ('mixed' !== $type) {
// Checks if the property has @var array<type> annotation
if (false !== ($pos = strpos($type, '<'))) {
$arrayType = substr($type, $pos + 1, -1);
$type = 'array';
if (isset(self::$typeMap[$arrayType])) {
$arrayType = self::$typeMap[$arrayType];
}
$metadata['attribute_types'][$property->name]['array_type'] = $arrayType;
}
$metadata['attribute_types'][$property->name]['type'] = $type;
$metadata['attribute_types'][$property->name]['value'] = $value;
$metadata['attribute_types'][$property->name]['required'] = false !== strpos($propertyComment, '@Required');
}
}
}
// choose the first property as default property
$metadata['default_property'] = reset($metadata['properties']);
}
}
self::$annotationMetadata[$name] = $metadata;
}
示例14: testAutoloadAnnotation
/**
* @group DCOM-56
*/
public function testAutoloadAnnotation()
{
$this->assertFalse(class_exists('Doctrine\\Tests\\Common\\Annotations\\Fixture\\Annotation\\Autoload', false), 'Pre-condition: Doctrine\\Tests\\Common\\Annotations\\Fixture\\Annotation\\Autoload not allowed to be loaded.');
$parser = new DocParser();
AnnotationRegistry::registerAutoloadNamespace('Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation', __DIR__ . '/../../../../');
$parser->setImports(array('autoload' => 'Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation\\Autoload'));
$annotations = $parser->parse('@Autoload');
$this->assertEquals(1, count($annotations));
$this->assertInstanceOf('Doctrine\\Tests\\Common\\Annotations\\Fixtures\\Annotation\\Autoload', $annotations[0]);
}
示例15: collectAnnotationMetadata
/**
* Collects parsing metadata for a given annotation class
*
* @param string $name The annotation name
*
* @return void
*/
private function collectAnnotationMetadata($name)
{
if (self::$metadataParser === null) {
self::$metadataParser = new self();
self::$metadataParser->setIgnoreNotImportedAnnotations(true);
self::$metadataParser->setIgnoredAnnotationNames($this->ignoredAnnotationNames);
self::$metadataParser->setImports(array('enum' => 'Doctrine\\Common\\Annotations\\Annotation\\Enum', 'target' => 'Doctrine\\Common\\Annotations\\Annotation\\Target', 'attribute' => 'Doctrine\\Common\\Annotations\\Annotation\\Attribute', 'attributes' => 'Doctrine\\Common\\Annotations\\Annotation\\Attributes'));
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Enum.php');
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Target.php');
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Attribute.php');
AnnotationRegistry::registerFile(__DIR__ . '/Annotation/Attributes.php');
}
$class = new \ReflectionClass($name);
$docComment = $class->getDocComment();
// Sets default values for annotation metadata
$metadata = array('default_property' => null, 'has_constructor' => null !== ($constructor = $class->getConstructor()) && $constructor->getNumberOfParameters() > 0, 'properties' => array(), 'property_types' => array(), 'attribute_types' => array(), 'targets_literal' => null, 'targets' => Target::TARGET_ALL, 'is_annotation' => false !== strpos($docComment, '@Annotation'));
// verify that the class is really meant to be an annotation
if ($metadata['is_annotation']) {
self::$metadataParser->setTarget(Target::TARGET_CLASS);
foreach (self::$metadataParser->parse($docComment, 'class @' . $name) as $annotation) {
if ($annotation instanceof Target) {
$metadata['targets'] = $annotation->targets;
$metadata['targets_literal'] = $annotation->literal;
continue;
}
if ($annotation instanceof Attributes) {
foreach ($annotation->value as $attribute) {
$this->collectAttributeTypeMetadata($metadata, $attribute);
}
}
}
// if not has a constructor will inject values into public properties
if (false === $metadata['has_constructor']) {
// collect all public properties
foreach ($class->getProperties(\ReflectionProperty::IS_PUBLIC) as $property) {
$metadata['properties'][$property->name] = $property->name;
if (false === ($propertyComment = $property->getDocComment())) {
continue;
}
$attribute = new Attribute();
$attribute->required = false !== strpos($propertyComment, '@Required');
$attribute->name = $property->name;
$attribute->type = false !== strpos($propertyComment, '@var') && preg_match('/@var\\s+([^\\s]+)/', $propertyComment, $matches) ? $matches[1] : 'mixed';
$this->collectAttributeTypeMetadata($metadata, $attribute);
// checks if the property has @Enum
if (false !== strpos($propertyComment, '@Enum')) {
$context = 'property ' . $class->name . "::\$" . $property->name;
self::$metadataParser->setTarget(Target::TARGET_PROPERTY);
foreach (self::$metadataParser->parse($propertyComment, $context) as $annotation) {
if (!$annotation instanceof Enum) {
continue;
}
$metadata['enum'][$property->name]['value'] = $annotation->value;
$metadata['enum'][$property->name]['literal'] = !empty($annotation->literal) ? $annotation->literal : $annotation->value;
}
}
}
// choose the first property as default property
$metadata['default_property'] = reset($metadata['properties']);
}
}
self::$annotationMetadata[$name] = $metadata;
}