Skip to content

php annotations #85

Closed
wants to merge 5 commits into from

6 participants

@FabioBatSilva
Doctrine member

Hello all,

This patch add support for php annotations, I think that is very useful to collect class metadata.
This patch support just 3 annotations @var, @param, @return. but any annotation could be added.

@schmittjoh can you take a look please ?

Any suggestion are welcome.

Thanks

@stof stof commented on an outdated diff Dec 15, 2011
lib/Doctrine/Common/Annotations/DocParser.php
@@ -582,7 +617,13 @@ private function Annotation()
}
if (!$found) {
- if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
+ if (!$this->ignorePhpAnnotations && isset(self::$phpAnnotations[$loweredAlias])) {
+ $className = self::$phpAnnotations[$loweredAlias];
+ if (preg_match("/$name\s+([^\s]+)/", substr($this->input, $this->lexer->lookahead['position']-1), $matches)) {
+ return new $className(array('value'=>$matches[1]));
+ }
+ return new $className();
+ } else if ($this->ignoreNotImportedAnnotations || isset($this->ignoredAnnotationNames[$name])) {
@stof
Doctrine member
stof added a note Dec 15, 2011

you should simply use if here instead of elseif as the previous if returns

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

As a reference, I have also played with adding support for parsing PHPDoc annotations here:
https://github.com/doctrine/common/tree/phpdoc

I think we have to schedule this for 2.3 anyway since we already have entered the release cycle for 2.2.

@FabioBatSilva
Doctrine member

Sorry @schmittjoh, I didn't notice the phpdoc branch.

@asm89 asm89 commented on the diff Dec 21, 2011
...ne/Common/Annotations/Annotation/ReturnAnnotation.php
+ * This software consists of voluntary contributions made by many individuals
+ * and is licensed under the LGPL. For more information, see
+ * <http://www.doctrine-project.org>.
+ */
+
+namespace Doctrine\Common\Annotations\Annotation;
+
+/**
+ * Annotation that represent php annotations @return.
+ *
+ * @since 2.2
+ * @author Fabio B. Silva <fabio.bat.silva@gmail.com>
+ *
+ * @Annotation
+ */
+final class ReturnAnnotation implements PhpAnnotation
@asm89
Doctrine member
asm89 added a note Dec 21, 2011

Shouldn't it be PhpDocAnnotation? @return, @var etc are phpdoc annotations if I'm correct. Same question holds for all the other vars/methods that are called php annotations.

@FabioBatSilva
Doctrine member

Are you right @asm89,

I was thinking about it, and we'll have lots of annotations phpdoc,
maybe should be a good idea create a new package for these annotations.

@schmittjoh and @asm89 that you think about the following structure ? :

Doctrine/Common/Annotations/Annotation/PhpDoc/PhpDocReturn.php
Doctrine/Common/Annotations/Annotation/PhpDoc/PhpDocVar.php
... other annotations
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jonathaningram jonathaningram commented on the diff Jan 15, 2012
lib/Doctrine/Common/Annotations/DocParser.php
@@ -203,6 +215,17 @@
);
/**
+ * Hash-map for native annotations
+ *
+ * @var array
+ */
+ private static $phpAnnotations = array(
+ 'var' => 'Doctrine\Common\Annotations\Annotation\VarAnnotation',
+ 'param' => 'Doctrine\Common\Annotations\Annotation\ParamAnnotation',
+ 'return' => 'Doctrine\Common\Annotations\Annotation\ReturnAnnotation',

Should you make var, param and return constants rather than magic strings?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@jonathaningram jonathaningram commented on the diff Jan 15, 2012
lib/Doctrine/Common/Annotations/DocParser.php
@@ -203,6 +215,17 @@
);
/**
+ * Hash-map for native annotations
+ *
+ * @var array
+ */
+ private static $phpAnnotations = array(

Should it be named $phpDocAnnotations?

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

i'm closing this one.
i'll do some changes and create another PR.

@BenMorel

@FabioBatSilva Have you worked on this feature since?

@FabioBatSilva
Doctrine member

Hi @BenMorel,

I haven't worked on this branch for along time..
Are you interested in maintaining this feature ?

Maybe we can work on this one..

@BenMorel

I might be interested when I'll get some time for it!
Have there been discussions about what kind of API we want exactly?

We can keep a class-based approach such as the one you proposed, but because AFAIK phpDoc annotations have just one (or no) parameter, that's maybe some weight we could avoid.

An alternative could be to introduce something like:

$annotationReader->getPropertyPhpDocAnnotations(); // array('var' => 'int', 'since' => '2.2')
$annotationReader->getPropertyPhpDocAnnotation('var'); // 'int'
@FabioBatSilva
Doctrine member

Awesome !!

But i'd like to keep classes,

it allows more complex parsing for each annotation class.
eq : @author Ben Morel <ben.morel@yourdomain.com> could be parsed to :

class Doctrine\Common\Annotations\Annotation\PhpDoc\PhpDocAuthor (#) {
  public $value => string(36) "Ben Morel <ben.morel@yourdomain.com>"
  public $name  => string(9)  "Ben Morel"
  public $email => string(25) "ben.morel@yourdomain.com"
}
@BenMorel

It could be interesting indeed! I'll have a look as soon as I get some time for it.

@wesnick wesnick added a commit to wesnick/annotations that referenced this pull request Dec 13, 2013
@wesnick wesnick Port of doctrine/common#85 Add PhpDoc annotation support for classmet…
…adata
1b98636
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.