Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Metadata for PHP4 #16

Closed
thekid opened this issue Oct 17, 2011 · 2 comments
Closed

Metadata for PHP4 #16

thekid opened this issue Oct 17, 2011 · 2 comments

Comments

@thekid
Copy link
Member

thekid commented Oct 17, 2011

Scope of Change

Annotations (metadata) may be embedded within the sourcecode using
the @ sign to prefix them.

Rationale

This feature will be useful to mark test methods in the Unit Test API
(instead of relying on the "test" prefix), the webservices APIs (XML-RPC
and SOAP) for marking methods as remotely callable as well as the new
and yet unimplemented application server (using annotations there
instead of implementing marker interfaces such as "SessionBean" or
"EntityBean" - see also the EJB 3.0 specs).

Functionality

The syntax will be almost the same as in the patched PHP (see related
documents below for definitions) but will make use of the "#" sign (so
that effectively, the annotations are comments).

Example #1: Webservices API

<?php
  #[@webservice(name= 'Customer')]
  class CustomerHandler extends Object {

    #[@webmethod]
    function getByCustomerId($customerId) {
      // ...
    }

    #[@webmethod, @restricted(role= 'admin')]
    function sendMessage($customerId, &$message) {
      // ...
    }
  }
?>

Example #2: Unit test API

<?php
  class ParserTest extends TestCase {

    #[@test]
    function tokenOrder() {
      // ...
    }
  }
?>

Syntax definitions

  • All annotations begin with the hash sign ("#") (so that the PHP4 parser
    ignores them) and are then enclosed in square brackets ("[" and "]").

  • Annotation tag names may be made up of any character from a-z and A-Z
    and the underscore ("_").

  • Annotation tag names are case-sensitive.

  • Simple definition:
    In the simple annotation only a simple annotation tag exists. Example:
    #[@test]

  • String value:
    An annotation may have a string value associated to it. Example:
    #[@deprecated('Use foo() instead')]

  • Hash key/value pairs:
    An annotation may have a hash map associated to it: Example:
    #[@fromxml(xpath= '/root/element[position() = 3]/@id')]

    Hash keys may consist of any character from a-z and A-Z and the
    underscore ("_"). Key names are case sensitive.

    Multiple elements in a hash are separated by commas. Example:
    #[@inject(type= 'dbconnection', name= 'news'))]

    Values may have an array associated with them:
    #[@restricted(roles = array('admin', 'root'))]

Reflection

The reflection API offers the following ways to retrieve these
annotations:

Method annotations

  • bool lang.reflect.Method::hasAnnotations()
    Allows you to check whether any annotation exists for this method

  • array lang.reflect.Method::getAnnotations()
    Returns an associative array of annotation names and their
    values.

    Example (for CustomerHandler::sendMessage):

  array(2) {
    ["webmethod"]=>
    NULL
    ["remote"]=>
    array(1) {
      ["role"]=>
      string(5) "admin"
    }
  }

  • mixed lang.reflect.Method::getAnnotation($name [, $key])
    Returns the annotation's value specified by name. Throws a
    lang.ElementNotFoundException in case the annotation is not found
  • bool lang.reflect.Method::hasAnnotation($name [, $key])
    Allows you to check if a annotation exists

Class annotations

  • bool lang.XPClass::hasAnnotations()
    Allows you to check whether any annotation exists for this class
  • array lang.XPClass::getAnnotations()
    Returns an associative array of annotation names and their
    values.
  • mixed lang.XPClass::getAnnotation($name [, $key])
    Returns the annotation's value specified by name. Throws a
    lang.ElementNotFoundException in case the annotation is not found
  • bool lang.XPClass::hasAnnotation($name [, $key])
    Allows you to check if a annotation exists

Dependencies

The APIdoc parser and the reflection API will need to be changed to
recognize these comments.

Related documents

@thekid thekid closed this as completed Oct 17, 2011
@thekid
Copy link
Member Author

thekid commented Oct 17, 2011

The reflection API will need to tokenize the class file in order to
read the annotations. This is slow but already done there.

friebe, Thu, 10 Feb 2005 22:34:14 +0100

@thekid
Copy link
Member Author

thekid commented Oct 17, 2011

In future versions, we might want to support annotations for classes,
too.

friebe, Sat, 12 Feb 2005 12:49:25 +0100

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

No branches or pull requests

1 participant