On 9/17/10 12:49 PM, Stas Malyshev wrote:
userspace) 2) adding syntax (probably can't be done in userspace unless
somebody thinks of a clever way to do it).
I've thought of it, and I guess I'll give it a go, I could make this a
proposal if it makes sense to.
The idea is to make the Zend Engine aware that annotations exist without
the worry of what the syntax is. Syntax can then be deferred to the
consumer. How to accomplish? Well:
* Zend Engine should have a new token T_ANNOTATION that is demarcated by
an opening [ and a closing ] (with necessary escaping for \] along the
way if need be) that should exists before PHP declarations (func,
property, method, class, const, etc). Multiple annotations per
declaration are ok.
* Add new methods to Reflection APIs: getAnnotations() and
getParsedAnnotations().
* Add a new extension called annotation responsible for registering and
implementing annotation parsers:
annotation_register_parser($name, AnnotationParser $p|$validcallback);
** and an interface for AnnotationParser
interface AnnotationParser
{
public function tokenize($string); // array of tokens
public function execute(array $tokens);
}
(if AnnotationParser is used, Reflection can "byte-code" cache the
tokens from tokenize())
* Add real implementations of syntax:
// class to handle proposed doctrine syntax, similar to c#
class AnnotationAttributeParser { /** ... */ }
// perhaps a simple key value based parser
class AnnotationKeyValueParser { /** .. */ }
// perhaps a PHP include parser
class AnnotationPHPIncludeParser { /** .. */ }
* Usage:
annotation_register_parser('doctrine', new AnnotationAttributeParser);
// callbacks can be used as wel
annotation_register_parser('zfservice', array('MyClass',
'myCallbackMethod'));
How would this work?
When Reflection<>::getParsedAnnotations() is called, it will check
for a registered key in the annotation (of one does not exist, exception
is thrown).. code would look like this:
[doctrine:ClassMap('foo' => 'bar', 'rest of c# nested syntax')]
public $foo;
This also allows other frameworks to propose and use their own syntax
that makes the most sense to them:
[symfonyDi:arg1=Foo,arg2=Bar]
public function setStuff(Foo $arg1, Bar $arg2) {...}
or
[zendservice:param1=string;return=int]
public function doFooForWebService() { /* .. */ return $someInt; }
you could even use url based annotations:
annotation_register_parser('wordpress', 'parse_str');
in code:
[wordpress:first=value&arr[]=foo+bar&arr[]=baz]
function wp_plugin_handle_something() {}
Since annotation formats and knowing annotations might be required are a
requirement of the framework/system the consumer has adopted, that
framework can specify the format of the string used inside the annotation.
Also, multiple annotations can be attached to various declarations,
hence the getParsedAnnotations(), perhaps getParsedAnnotation($index)
would be valid as well.
thoughts?
-ralph
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php