Permalink
Browse files

Support for namespaces.

  • Loading branch information...
1 parent ca6be75 commit 4966fce69fea5e99e6a484f59eb7be2e665fe3a5 @realityking realityking committed Feb 2, 2013
Showing with 117 additions and 5 deletions.
  1. +19 −0 CodeGen/PECL/Element.php
  2. +40 −1 CodeGen/PECL/Element/Class.php
  3. +42 −2 CodeGen/PECL/Element/Interface.php
  4. +16 −2 CodeGen/PECL/ExtensionParser.php
View
@@ -161,5 +161,24 @@ function isKeyword($name)
return false;
}
+ /**
+ * Checks whether a string is a valid namespace
+ *
+ * @access public
+ * @param string name
+ * @return bool boolean false if a reserved keyword is used
+ */
+ function isNamespace($name)
+ {
+ $parts = explode('\\\\', $name);
+
+ foreach ($parts as $part) {
+ if (self::isKeyword($part)) {
+ return false;
+ }
+
+ return true;
+ }
+ }
}
@@ -78,6 +78,39 @@ function getName()
}
/**
+ * Namespace of the class
+ *
+ * @var string
+ */
+ protected $namespace = "";
+
+ /**
+ * class name setter
+ *
+ * @param string Classnamespace
+ */
+ function setNamespace($namespace)
+ {
+ if (!self::isNamespace($namespace)) {
+ return PEAR::raiseError("'$namespace' is not a valid namespace");
+ }
+
+ $this->namespace = $namespace;
+
+ return true;
+ }
+
+ /**
+ * class name getter
+ *
+ * @return string Classnamespace
+ */
+ function getNamespace()
+ {
+ return $this->namespace;
+ }
+
+ /**
* A short description
*
* @var string
@@ -511,7 +544,13 @@ function globalCode($extension)
echo " zend_class_entry ce;\n\n";
- echo " INIT_CLASS_ENTRY(ce, \"{$this->name}\", {$this->name}_methods);\n";
+
+ if (empty($this->namespace)) {
+ echo " INIT_CLASS_ENTRY(ce, \"{$this->name}\", {$this->name}_methods);\n";
+ }
+ else {
+ echo " INIT_NS_CLASS_ENTRY(ce, \"{$this->namespace}\", \"{$this->name}\", {$this->name}_methods);\n";
+ }
if ($this->payloadType) {
echo " ce.create_object = {$this->name}_obj_create;\n";
@@ -45,7 +45,7 @@ class CodeGen_PECL_Element_Interface
implements CodeGen_PECL_Element_ObjectInterface
{
/**
- * The class name
+ * The interface name
*
* @var string
*/
@@ -78,6 +78,39 @@ function getName()
}
/**
+ * namespace get()er
+ *
+ * @return string
+ */
+ function getNamespace()
+ {
+ return $this->namespace;
+ }
+
+ /**
+ * The namespace namespace
+ *
+ * @var string
+ */
+ protected $namespace = "";
+
+ /**
+ * namespace set()er
+ *
+ * @param string
+ */
+ function setNamespace($namespace)
+ {
+ if (!self::isNamespace($namespace)) {
+ return PEAR::raiseError("'$namespace' is not a valid namespace");
+ }
+
+ $this->namespace = $namespace;
+
+ return true;
+ }
+
+ /**
* A short description
*
* @var string
@@ -237,7 +270,14 @@ function globalCode($extension)
echo " zend_class_entry **parent_ce;\n";
}
echo "\n";
- echo " INIT_CLASS_ENTRY(ce, \"{$this->name}\", {$this->name}_methods);\n";
+
+ if (empty($this->namespace)) {
+ echo " INIT_CLASS_ENTRY(ce, \"{$this->name}\", {$this->name}_methods);\n";
+ }
+ else {
+ echo " INIT_NS_CLASS_ENTRY(ce, \"{$this->namespace}\", \"{$this->name}\", {$this->name}_methods);\n";
+ }
+
echo " {$this->name}_ce_ptr = zend_register_internal_interface(&ce TSRMLS_CC);\n";
if ($this->extends) {
@@ -909,7 +909,7 @@ function tagend_extension_tests($attr, $data) {
function tagstart_extension_class($attr)
{
- $err = $this->checkAttributes($attr, array("name", "extends", "final", "abstract", "if"));
+ $err = $this->checkAttributes($attr, array("name", "namespace", "extends", "final", "abstract", "if"));
if (PEAR::isError($err)) {
return $err;
}
@@ -927,6 +927,13 @@ function tagstart_extension_class($attr)
return PEAR::raiseError("name attribut for class missing");
}
+ if (isset($attr["namespace"])) {
+ $err = $class->setNamespace($attr["namespace"]);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+ }
+
if (isset($attr["extends"])) {
$err = $class->setExtends($attr["extends"]);
if (PEAR::isError($err)) {
@@ -1237,7 +1244,7 @@ function tagend_group_class($attr, $data)
function tagstart_extension_interface($attr)
{
- $err = $this->checkAttributes($attr, array("name", "extends", "if"));
+ $err = $this->checkAttributes($attr, array("name", "namespace", "extends", "if"));
if (PEAR::isError($err)) {
return $err;
}
@@ -1255,6 +1262,13 @@ function tagstart_extension_interface($attr)
return PEAR::raiseError("name attribut for class missing");
}
+ if (isset($attr["namespace"])) {
+ $err = $interface->setNamespace($attr["namespace"]);
+ if (PEAR::isError($err)) {
+ return $err;
+ }
+ }
+
if (isset($attr["extends"])) {
$err = $interface->setExtends($attr["extends"]);
if (PEAR::isError($err)) {

0 comments on commit 4966fce

Please sign in to comment.