Skip to content

Commit

Permalink
Support for namespaces.
Browse files Browse the repository at this point in the history
  • Loading branch information
realityking committed Feb 2, 2013
1 parent ca6be75 commit 4966fce
Show file tree
Hide file tree
Showing 4 changed files with 117 additions and 5 deletions.
19 changes: 19 additions & 0 deletions CodeGen/PECL/Element.php
Expand Up @@ -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;
}
}
}

41 changes: 40 additions & 1 deletion CodeGen/PECL/Element/Class.php
Expand Up @@ -77,6 +77,39 @@ function getName()
return $this->name;
}

/**
* 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
*
Expand Down Expand Up @@ -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";
Expand Down
44 changes: 42 additions & 2 deletions CodeGen/PECL/Element/Interface.php
Expand Up @@ -45,7 +45,7 @@ class CodeGen_PECL_Element_Interface
implements CodeGen_PECL_Element_ObjectInterface
{
/**
* The class name
* The interface name
*
* @var string
*/
Expand Down Expand Up @@ -77,6 +77,39 @@ function getName()
return $this->name;
}

/**
* 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
*
Expand Down Expand Up @@ -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) {
Expand Down
18 changes: 16 additions & 2 deletions CodeGen/PECL/ExtensionParser.php
Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand Down Expand Up @@ -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;
}
Expand All @@ -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)) {
Expand Down

0 comments on commit 4966fce

Please sign in to comment.