Skip to content

Commit

Permalink
[php7] Type.getInstanceFields()
Browse files Browse the repository at this point in the history
  • Loading branch information
RealyUniqueName committed Sep 16, 2016
1 parent 5ce1317 commit f0f7e95
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 26 deletions.
5 changes: 5 additions & 0 deletions std/php7/Global.hx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ extern class Global {
**/
static function array_merge( array:Rest<NativeArray> ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-diff.php
**/
static function array_diff( array:Rest<NativeArray> ) : NativeArray ;

/**
@see http://php.net/manual/en/function.array-pop.php
**/
Expand Down
1 change: 0 additions & 1 deletion std/php7/_std/Array.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import php7.NativeIndexedArray;

using php7.Global;


// @:coreApi
@:final
class Array<T> implements php7.ArrayAccess<Int,T> {
Expand Down
2 changes: 1 addition & 1 deletion std/php7/_std/List.hx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
@:coreApi class List<T> implements php.IteratorAggregate<T> {
@:coreApi class List<T> implements php7.IteratorAggregate<T> {

@:ifFeature("List.iterator") private var h : ArrayAccess<Dynamic>;
@:ifFeature("List.iterator") private var q : ArrayAccess<Dynamic>;
Expand Down
43 changes: 27 additions & 16 deletions std/php7/_std/Type.hx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import php7.Global;
import php7.Boot;
import php7.reflection.ReflectionClass;
import php7.reflection.ReflectionMethod;
import php7.reflection.ReflectionProperty;
import haxe.extern.EitherType;

enum ValueType {
Expand Down Expand Up @@ -144,24 +146,26 @@ enum ValueType {
}
}

@:access(Array)
public static function getInstanceFields( c : Class<Dynamic> ) : Array<String> {
if(untyped c.__qname__ == 'String') return ['substr', 'charAt', 'charCodeAt', 'indexOf', 'lastIndexOf', 'split', 'toLowerCase', 'toUpperCase', 'toString', 'length'];
if(untyped c.__qname__ == 'Array') return ['push', 'concat', 'join', 'pop', 'reverse', 'shift', 'slice', 'sort', 'splice', 'toString', 'copy', 'unshift', 'insert', 'remove', 'iterator', 'length'];
untyped __php__("
$rfl = $c->__rfl__();
if($rfl === null) return new _hx_array(array());
$r = array();
$internals = array('__construct', '__call', '__get', '__set', '__isset', '__unset', '__toString');
$ms = $rfl->getMethods();
while(list(, $m) = each($ms)) {
$n = $m->getName();
if(!$m->isStatic() && !in_array($n, $internals)) $r[] = $n;
if (c == null) return null;
if (c == String) {
return [
'substr', 'charAt', 'charCodeAt', 'indexOf',
'lastIndexOf', 'split', 'toLowerCase',
'toUpperCase', 'toString', 'length'
];
}
$ps = $rfl->getProperties();
while(list(, $p) = each($ps))
if(!$p->isStatic() && ($name = $p->getName()) !== '__dynamics') $r[] = $name;
");
return untyped __php__("new _hx_array(array_values(array_unique($r)))");

var phpName = getPhpName(c);
if (phpName == null) return null;

var reflection = new ReflectionClass(phpName);
var methods = [for(m in reflection.getMethods()) (m:ReflectionMethod).getName()].filter(isNotServiceName);
var properties = [for(p in reflection.getProperties()) (p:ReflectionProperty).getName()].filter(isNotServiceName);
properties.arr = Global.array_diff(properties.arr, methods.arr);

return properties.concat(methods);
}

public static function getClassFields( c : Class<Dynamic> ) : Array<String> {
Expand Down Expand Up @@ -269,5 +273,12 @@ enum ValueType {

return (haxeName == null ? null : Boot.getPhpName(haxeName));
}

/**
Ensure specified `name` is not a special field name generated by compiler.
**/
static function isNotServiceName(name:String) : Bool {
return (name != '__construct' && name.indexOf('__hx__') != 0);
}
}

16 changes: 8 additions & 8 deletions std/php7/reflection/ReflectionClass.hx
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ extern class ReflectionClass implements Reflector {
public function new( argument:Dynamic ) : Void;
public function getConstant( name:String ) : Dynamic;
public function getConstants() : NativeAssocArray<Dynamic>;
// public function getConstructor() : ReflectionMethod;
public function getConstructor() : ReflectionMethod;
public function getDefaultProperties() : NativeAssocArray<Dynamic>;
public function getDocComment() : String;
public function getEndLine() : Int;
// public function getExtension() : ReflectionExtension;
public function getExtensionName() : String;
public function getFileName() : String;
public function getInterfaceNames() : NativeIndexedArray<String>;
// public function getInterfaces() : NativeIndexedArray<ReflectionClass>;
// public function getMethod( name:String ) : ReflectionMethod;
// public function getMethods( ?filter:Int ) : NativeIndexedArray<ReflectionMethod>;
public function getInterfaces() : NativeIndexedArray<ReflectionClass>;
public function getMethod( name:String ) : ReflectionMethod;
public function getMethods( ?filter:Int ) : NativeIndexedArray<ReflectionMethod>;
public function getModifiers() : Int;
public function getName() : String;
public function getNamespaceName() : String;
// public function getParentClass() : Null<ReflectionClass>;
// public function getProperties( ?filter:Int ) : NativeIndexedArray<ReflectionProperty>;
// public function getProperty( name:String ) : ReflectionProperty;
public function getParentClass() : Null<ReflectionClass>;
public function getProperties( ?filter:Int ) : NativeIndexedArray<ReflectionProperty>;
public function getProperty( name:String ) : ReflectionProperty;
public function getShortName() : String;
public function getStartLine() : Int;
public function getStaticProperties() : NativeAssocArray<Dynamic>;
public function getStaticPropertyValue( name:String, ?def_value:Ref<Dynamic> ) : Dynamic;
public function getTraitAliases() : NativeAssocArray<String>;
public function getTraitNames() : NativeIndexedArray<String>;
// public function getTraits() : NativeIndexedArray<ReflectionClass>;
public function getTraits() : NativeIndexedArray<ReflectionClass>;
public function hasConstant( name:String ) : Bool;
public function hasMethod( name:String ) : Bool;
public function hasProperty( name:String ) : Bool;
Expand Down
35 changes: 35 additions & 0 deletions std/php7/reflection/ReflectionFunctionAbstract.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package php7.reflection;


@:native('ReflectionFunctionAbstract')
extern class ReflectionFunctionAbstract implements Reflector {

public var name : String;

public function getClosureScopeClass() : ReflectionClass;
public function getClosureThis() : Dynamic;
public function getDocComment() : String;
public function getEndLine() : Int;
// public function getExtension() : ReflectionExtension;
public function getExtensionName() : String;
public function getFileName() : String;
public function getName() : String;
public function getNamespaceName() : String;
public function getNumberOfParameters() : Int;
public function getNumberOfRequiredParameters() : Int;
// public function getParameters() : NativeIndexedArray<ReflectionParameter>;
// public function getReturnType() : ReflectionType;
public function getShortName() : String;
public function getStartLine() : Int;
public function getStaticVariables() : NativeAssocArray<Dynamic>;
public function hasReturnType() : Bool;
public function inNamespace() : Bool;
public function isClosure() : Bool;
public function isDeprecated() : Bool;
public function isGenerator() : Bool;
public function isInternal() : Bool;
public function isUserDefined() : Bool;
public function isVariadic() : Bool;
public function returnsReference() : Bool;
public function __toString() : String;
}
35 changes: 35 additions & 0 deletions std/php7/reflection/ReflectionMethod.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package php7.reflection;

import haxe.Constraints;
import haxe.extern.Rest;

@:native('ReflectionMethod')
extern class ReflectionMethod extends ReflectionFunctionAbstract {
// const integer IS_STATIC = 1 ;
// const integer IS_PUBLIC = 256 ;
// const integer IS_PROTECTED = 512 ;
// const integer IS_PRIVATE = 1024 ;
// const integer IS_ABSTRACT = 2 ;
// const integer IS_FINAL = 4 ;

// public var class : String;

public static function export( className:String, name:String, ?returnValue:Bool) : String;

public function new( cls:Dynamic, name:String ) : Void;
public function getClosure( object:{} ) : Function;
public function getDeclaringClass() : ReflectionClass;
public function getModifiers() : Int;
public function getPrototype() : ReflectionMethod;
public function invoke( object:{}, args:Rest<Dynamic> ) : Dynamic;
public function invokeArgs( object:{}, args:NativeIndexedArray<Dynamic> ) : Dynamic;
public function isAbstract() : Bool;
public function isConstructor() : Bool;
public function isDestructor() : Bool;
public function isFinal() : Bool;
public function isPrivate() : Bool;
public function isProtected() : Bool;
public function isPublic() : Bool;
public function isStatic() : Bool;
public function setAccessible( accessible:Bool ) : Void;
}
29 changes: 29 additions & 0 deletions std/php7/reflection/ReflectionProperty.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package php7.reflection;


@:native('ReflectionProperty')
extern class ReflectionProperty implements Reflector {

// const integer IS_STATIC = 1 ;
// const integer IS_PUBLIC = 256 ;
// const integer IS_PROTECTED = 512 ;
// const integer IS_PRIVATE = 1024 ;

public var name : String;

public static function export( className:Dynamic, name:String, ?returnValue:Bool ) : String;

public function new ( cls:Dynamic, name:String ) : Void;
public function getDeclaringClass() : ReflectionClass;
public function getDocComment() : String;
public function getModifiers() : Int;
public function getName() : String;
public function getValue( ?object:{} ) : Dynamic;
public function isPrivate() : Bool;
public function isProtected() : Bool;
public function isPublic() : Bool;
public function isStatic() : Bool;
public function setAccessible ( accessible:Bool ) : Void;
public function setValue( object:{}, value:Dynamic ) : Void;
public function __toString() : String;
}

0 comments on commit f0f7e95

Please sign in to comment.