]> granicus.if.org Git - php/commitdiff
Added support for namespaces
authorDmitry Stogov <dmitry@php.net>
Wed, 25 Jun 2008 12:33:46 +0000 (12:33 +0000)
committerDmitry Stogov <dmitry@php.net>
Wed, 25 Jun 2008 12:33:46 +0000 (12:33 +0000)
ext/reflection/php_reflection.c
ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt [new file with mode: 0644]
ext/reflection/tests/reflectionClass_getNamespaceName.phpt [new file with mode: 0644]

index a9c87c2d7fcac625672344972adaa5ebc33cb62d..fa3e1a2880fe8ec17f1dbe9dc0e0750f5edb2643 100644 (file)
@@ -2550,6 +2550,66 @@ ZEND_METHOD(reflection_function, isDeprecated)
 }
 /* }}} */
 
+/* {{{ proto public bool ReflectionFunction::inNamespace()
+   Returns whether this function is defined in namespace */
+ZEND_METHOD(reflection_function, inNamespace)
+{
+       zval **name;
+       char *colon;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0);
+       if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) {
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(name) == IS_STRING &&
+           (colon = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) &&
+           colon > Z_STRVAL_PP(name) && *(colon-1) == ':') {
+               RETURN_TRUE;
+    }
+       RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ proto public string ReflectionFunction::getNamespaceName()
+   Returns the name of namespace where this function is defined */
+ZEND_METHOD(reflection_function, getNamespaceName)
+{
+       zval **name;
+       char *colon;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0);
+       if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) {
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(name) == IS_STRING &&
+           (colon = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) &&
+           colon > Z_STRVAL_PP(name) && *(colon-1) == ':') {
+               RETURN_STRINGL(Z_STRVAL_PP(name), colon - Z_STRVAL_PP(name) - 1, 1);
+    }
+       RETURN_EMPTY_STRING();
+}
+/* }}} */
+
+/* {{{ proto public string ReflectionFunction::getShortName()
+   Returns the short name of the function (without namespace part) */
+ZEND_METHOD(reflection_function, getShortName)
+{
+       zval **name;
+       char *colon;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_function_abstract_ptr, 0);
+       if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) {
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(name) == IS_STRING &&
+           (colon = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) &&
+           colon > Z_STRVAL_PP(name) && *(colon-1) == ':') {
+               RETURN_STRINGL(colon + 1, Z_STRLEN_PP(name) - (colon - Z_STRVAL_PP(name) + 1), 1);
+    }
+    RETURN_ZVAL(*name, 1, 0);
+}
+/* }}} */
+
 /* {{{ proto public bool ReflectionMethod::isConstructor()
    Returns whether this method is the constructor */
 ZEND_METHOD(reflection_method, isConstructor)
@@ -3760,6 +3820,66 @@ ZEND_METHOD(reflection_class, getExtensionName)
 }
 /* }}} */
 
+/* {{{ proto public bool ReflectionClass::inNamespace()
+   Returns whether this class is defined in namespace */
+ZEND_METHOD(reflection_class, inNamespace)
+{
+       zval **name;
+       char *colon;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_class_ptr, 0);
+       if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) {
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(name) == IS_STRING &&
+           (colon = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) &&
+           colon > Z_STRVAL_PP(name) && *(colon-1) == ':') {
+               RETURN_TRUE;
+    }
+       RETURN_FALSE;
+}
+/* }}} */
+
+/* {{{ proto public string ReflectionClass::getNamespaceName()
+   Returns the name of namespace where this class is defined */
+ZEND_METHOD(reflection_class, getNamespaceName)
+{
+       zval **name;
+       char *colon;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_class_ptr, 0);
+       if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) {
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(name) == IS_STRING &&
+           (colon = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) &&
+           colon > Z_STRVAL_PP(name) && *(colon-1) == ':') {
+               RETURN_STRINGL(Z_STRVAL_PP(name), colon - Z_STRVAL_PP(name) - 1, 1);
+    }
+       RETURN_EMPTY_STRING();
+}
+/* }}} */
+
+/* {{{ proto public string ReflectionClass::getShortName()
+   Returns the short name of the class (without namespace part) */
+ZEND_METHOD(reflection_class, getShortName)
+{
+       zval **name;
+       char *colon;
+
+       METHOD_NOTSTATIC_NUMPARAMS(reflection_class_ptr, 0);
+       if (zend_hash_find(Z_OBJPROP_P(getThis()), "name", sizeof("name"), (void **) &name) == FAILURE) {
+               RETURN_FALSE;
+       }
+       if (Z_TYPE_PP(name) == IS_STRING &&
+           (colon = zend_memrchr(Z_STRVAL_PP(name), ':', Z_STRLEN_PP(name))) &&
+           colon > Z_STRVAL_PP(name) && *(colon-1) == ':') {
+               RETURN_STRINGL(colon + 1, Z_STRLEN_PP(name) - (colon - Z_STRVAL_PP(name) + 1), 1);
+    }
+    RETURN_ZVAL(*name, 1, 0);
+}
+/* }}} */
+
 /* {{{ proto public static mixed ReflectionObject::export(mixed argument [, bool return]) throws ReflectionException
    Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
 ZEND_METHOD(reflection_object, export)
@@ -4510,6 +4630,9 @@ static const zend_function_entry reflection_function_abstract_functions[] = {
        ZEND_ME(reflection_function, getExtension, NULL, 0)
        ZEND_ME(reflection_function, getExtensionName, NULL, 0)
        ZEND_ME(reflection_function, isDeprecated, NULL, 0)
+       ZEND_ME(reflection_function, inNamespace, NULL, 0)
+       ZEND_ME(reflection_function, getNamespaceName, NULL, 0)
+       ZEND_ME(reflection_function, getShortName, NULL, 0)
        {NULL, NULL, NULL}
 };
 
@@ -4699,6 +4822,9 @@ static const zend_function_entry reflection_class_functions[] = {
        ZEND_ME(reflection_class, implementsInterface, arginfo_reflection_class_implementsInterface, 0)
        ZEND_ME(reflection_class, getExtension, NULL, 0)
        ZEND_ME(reflection_class, getExtensionName, NULL, 0)
+       ZEND_ME(reflection_class, inNamespace, NULL, 0)
+       ZEND_ME(reflection_class, getNamespaceName, NULL, 0)
+       ZEND_ME(reflection_class, getShortName, NULL, 0)
        {NULL, NULL, NULL}
 };
 
diff --git a/ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt b/ext/reflection/tests/ReflectionFunction_getNamespaceName.phpt
new file mode 100644 (file)
index 0000000..cd1f52c
--- /dev/null
@@ -0,0 +1,29 @@
+--TEST--
+ReflectionFunction::getNamespaceName()
+--FILE--
+<?php
+namespace A::B;
+function foo() {}
+
+$function = new ReflectionFunction('sort');
+var_dump($function->inNamespace());
+var_dump($function->getName());
+var_dump($function->getNamespaceName());
+var_dump($function->getShortName());
+
+$function = new ReflectionFunction('A::B::foo');
+var_dump($function->inNamespace());
+var_dump($function->getName());
+var_dump($function->getNamespaceName());
+var_dump($function->getShortName());
+?>
+--EXPECT--
+bool(false)
+string(4) "sort"
+string(0) ""
+string(4) "sort"
+bool(true)
+string(9) "A::B::foo"
+string(4) "A::B"
+string(3) "foo"
+
diff --git a/ext/reflection/tests/reflectionClass_getNamespaceName.phpt b/ext/reflection/tests/reflectionClass_getNamespaceName.phpt
new file mode 100644 (file)
index 0000000..4b51190
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+ReflectionClass::getNamespaceName()
+--FILE--
+<?php
+namespace A::B;
+class Foo {
+}
+
+$function = new ReflectionClass('stdClass');
+var_dump($function->inNamespace());
+var_dump($function->getName());
+var_dump($function->getNamespaceName());
+var_dump($function->getShortName());
+
+$function = new ReflectionClass('A::B::Foo');
+var_dump($function->inNamespace());
+var_dump($function->getName());
+var_dump($function->getNamespaceName());
+var_dump($function->getShortName());
+?>
+--EXPECT--
+bool(false)
+string(8) "stdClass"
+string(0) ""
+string(8) "stdClass"
+bool(true)
+string(9) "A::B::Foo"
+string(4) "A::B"
+string(3) "Foo"
+