[PHP] Add support for specifying any PHP interfaces a wrapped class

implements, e.g.: %typemap("phpinterfaces") MyIterator "Iterator";
This commit is contained in:
Olly Betts 2014-09-12 12:48:37 -03:00
commit 1a99212c2c
7 changed files with 79 additions and 0 deletions

View file

@ -5,6 +5,10 @@ See the RELEASENOTES file for a summary of changes in each release.
Version 3.0.3 (in progress)
===========================
2014-09-12: olly
[PHP] Add support for specifying any PHP interfaces a wrapped class
implements, e.g.: %typemap("phpinterfaces") MyIterator "Iterator";
2014-09-11: olly
[PHP] Fix throwing a PHP exception through C++ from a subclassed
director method - PHP NULL gets returned by the subclassed method

View file

@ -1416,6 +1416,7 @@
<li><a href="Php.html#Php_nn2_6_2">Constructors and Destructors</a>
<li><a href="Php.html#Php_nn2_6_3">Static Member Variables</a>
<li><a href="Php.html#Php_nn2_6_4">Static Member Functions</a>
<li><a href="Php.html#Php_nn2_6_5">Specifying Implemented Interfaces</a>
</ul>
<li><a href="Php.html#Php_nn2_7">PHP Pragmas, Startup and Shutdown code</a>
</ul>

View file

@ -29,6 +29,7 @@
<li><a href="#Php_nn2_6_2">Constructors and Destructors</a>
<li><a href="#Php_nn2_6_3">Static Member Variables</a>
<li><a href="#Php_nn2_6_4">Static Member Functions</a>
<li><a href="#Php_nn2_6_5">Specifying Implemented Interfaces</a>
</ul>
<li><a href="#Php_nn2_7">PHP Pragmas, Startup and Shutdown code</a>
</ul>
@ -770,6 +771,23 @@ Ko::threats();
</pre></div>
<H4><a name="Php_nn2_6_5"></a>34.2.6.5 Specifying Implemented Interfaces</H4>
<p>
PHP supports the concept of abstract interfaces which a class can implement.
Since SWIG 3.0.3, you can tell SWIG that a wrapped class (for example
<code>MyIterator</code>) implements the <code>Iterator</code> interface like
so:
</p>
<div class="code"><pre>
%typemap("phpinterfaces") MyIterator "Iterator";
</pre></div>
<p>
If there are multiple interfaces, just list them separated by commas.
</p>
<H3><a name="Php_nn2_7"></a>34.2.7 PHP Pragmas, Startup and Shutdown code</H3>

View file

@ -10,6 +10,7 @@ top_srcdir = @top_srcdir@
top_builddir = @top_builddir@
CPP_TEST_CASES += \
php_iterator \
php_namewarn_rename \
include $(srcdir)/../common.mk

View file

@ -0,0 +1,24 @@
<?php
require "tests.php";
require "php_iterator.php";
check::functions(array(myiterator_rewind,myiterator_key,myiterator_current,myiterator_next,myiterator_valid));
check::classes(array(MyIterator));
// No new global variables.
check::globals(array());
$s = '';
foreach (new MyIterator(1, 6) as $i) {
$s .= $i;
}
check::equal($s, '12345', 'Simple iteration failed');
$s = '';
foreach (new MyIterator(2, 5) as $k => $v) {
$s .= "($k=>$v)";
}
check::equal($s, '(0=>2)(1=>3)(2=>4)', 'Simple iteration failed');
check::done();
?>

View file

@ -0,0 +1,20 @@
/* php_iterator.i - PHP-specific testcase for wrapping to a PHP Iterator */
%module php_iterator
%typemap("phpinterfaces") MyIterator "Iterator";
%inline %{
class MyIterator {
int i, from, to;
public:
MyIterator(int from_, int to_)
: i(from_), from(from_), to(to_) { }
void rewind() { i = from; }
bool valid() const { return i != to; }
int key() const { return i - from; }
int current() const { return i; }
void next() { ++i; }
};
%}

View file

@ -2045,6 +2045,17 @@ done:
} else if (GetFlag(n, "feature:exceptionclass")) {
Append(s_phpclasses, "extends Exception ");
}
{
Node *node = NewHash();
Setattr(node, "type", Getattr(n, "name"));
Setfile(node, Getfile(n));
Setline(node, Getline(n));
String * interfaces = Swig_typemap_lookup("phpinterfaces", node, "", 0);
if (interfaces) {
Printf(s_phpclasses, "implements %s ", interfaces);
}
Delete(node);
}
Printf(s_phpclasses, "{\n\tpublic $%s=null;\n", SWIG_PTR);
if (!baseclass) {
// Only store this in the base class (NB !baseclass means we *are*