[limb-svn] r6031 - in 3.x/trunk/limb/core: src tests/cases
svn at limb-project.com
svn at limb-project.com
Mon Jul 2 01:58:53 MSD 2007
Author: pachanga
Date: 2007-07-02 01:58:53 +0400 (Mon, 02 Jul 2007)
New Revision: 6031
URL: http://fisheye.limb-project.com/changelog/limb/?cs=6031
Added:
3.x/trunk/limb/core/src/lmbReflectionHelper.class.php
Removed:
3.x/trunk/limb/core/src/lmbReflection.class.php
Modified:
3.x/trunk/limb/core/src/lmbDecoratorGenerator.class.php
3.x/trunk/limb/core/tests/cases/lmbDecoratorTest.class.php
Log:
-- renaming lmbReflection into lmbReflectionHelper and making it static
Modified: 3.x/trunk/limb/core/src/lmbDecoratorGenerator.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbDecoratorGenerator.class.php 2007-07-01 19:17:44 UTC (rev 6030)
+++ 3.x/trunk/limb/core/src/lmbDecoratorGenerator.class.php 2007-07-01 21:58:53 UTC (rev 6031)
@@ -1,91 +1,88 @@
-<?php
+<?php
/*
* Limb PHP Framework
*
* @link http://limb-project.com
* @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
* @license LGPL http://www.gnu.org/copyleft/lesser.html
- */
-//code is based on MockGenerator class from SimpleTest test suite
-lmb_require('limb/core/src/lmbReflection.class.php');
+ */
+//code is based on MockGenerator class from SimpleTest test suite
+lmb_require('limb/core/src/lmbReflectionHelper.class.php');
-/**
- * class lmbDecoratorGenerator.
- *
- * @package core
- * @version $Id$
+/**
+ * class lmbDecoratorGenerator.
+ *
+ * @package core
+ * @version $Id$
*/
-class lmbDecoratorGenerator
-{
- protected $_class;
- protected $_decorator_class;
- protected $_decorator_base;
- protected $_reflection;
-
- function generate($class, $decorator_class = null, $decorator_base = 'lmbDecorator')
- {
- $this->_class = $class;
-
- if(is_null($decorator_class))
- $this->_decorator_class = $class . 'Decorator';
- else
- $this->_decorator_class = $decorator_class;
-
- $this->_decorator_base = $decorator_base;
-
- if(class_exists($this->_decorator_class))
- return false;
-
- $this->_reflection = new lmbReflection($this->_class);
-
- $methods = array();
-
- return eval($this->_createClassCode() . " return true;");
- }
-
- protected function _createClassCode()
- {
- $implements = '';
- $interfaces = $this->_reflection->getInterfaces();
- if(function_exists('spl_classes'))
- $interfaces = array_diff($interfaces, array('Traversable'));
-
- if(count($interfaces) > 0)
- $implements = 'implements ' . implode(', ', $interfaces);
-
- $code = "class " . $this->_decorator_class . " extends " . $this->_decorator_base . " $implements {\n";
- $code .= " function __construct(\$original) {\n";
- $code .= " parent :: __construct(\$original);\n";
- $code .= " }\n";
- $code .= $this->_createHandlerCode();
- $code .= "}\n";
- return $code;
- }
-
- protected function _createHandlerCode()
- {
- $code = '';
- $methods = $this->_reflection->getMethods();
- $base_reflection = new lmbReflection($this->_decorator_base);
- foreach($methods as $method)
- {
- if($this->_isMagicMethod($method))
- continue;
-
- if(in_array($method, $base_reflection->getMethods()))
- continue;
-
- $code .= " " . $this->_reflection->getSignature($method) . " {\n";
- $code .= " \$args = func_get_args();\n";
- $code .= " return \$this->___invoke(\"$method\", \$args);\n";
- $code .= " }\n";
- }
- return $code;
- }
-
- protected function _isMagicMethod($method)
- {
- return in_array(strtolower($method), array('__construct', '__destruct', '__clone'));
- }
-}
-?>
+class lmbDecoratorGenerator
+{
+ protected $_class;
+ protected $_decorator_class;
+ protected $_decorator_base;
+
+ function generate($class, $decorator_class = null, $decorator_base = 'lmbDecorator')
+ {
+ $this->_class = $class;
+
+ if(is_null($decorator_class))
+ $this->_decorator_class = $class . 'Decorator';
+ else
+ $this->_decorator_class = $decorator_class;
+
+ $this->_decorator_base = $decorator_base;
+
+ if(class_exists($this->_decorator_class))
+ return false;
+
+ $methods = array();
+
+ return eval($this->_createClassCode() . " return true;");
+ }
+
+ protected function _createClassCode()
+ {
+ $implements = '';
+ $interfaces = lmbReflectionHelper :: getInterfaces($this->_class);
+ if(function_exists('spl_classes'))
+ $interfaces = array_diff($interfaces, array('Traversable'));
+
+ if(count($interfaces) > 0)
+ $implements = 'implements ' . implode(', ', $interfaces);
+
+ $code = "class " . $this->_decorator_class . " extends " . $this->_decorator_base . " $implements {\n";
+ $code .= " function __construct(\$original) {\n";
+ $code .= " parent :: __construct(\$original);\n";
+ $code .= " }\n";
+ $code .= $this->_createHandlerCode();
+ $code .= "}\n";
+ return $code;
+ }
+
+ protected function _createHandlerCode()
+ {
+ $code = '';
+ $methods = lmbReflectionHelper :: getMethods($this->_class);
+ $base_methods = lmbReflectionHelper :: getMethods($this->_decorator_base);
+ foreach($methods as $method)
+ {
+ if($this->_isMagicMethod($method))
+ continue;
+
+ if(in_array($method, $base_methods))
+ continue;
+
+ $code .= " " . lmbReflectionHelper :: getSignature($this->_class, $method) . " {\n";
+ $code .= " \$args = func_get_args();\n";
+ $code .= " return \$this->___invoke(\"$method\", \$args);\n";
+ $code .= " }\n";
+ }
+ return $code;
+ }
+
+ protected function _isMagicMethod($method)
+ {
+ return in_array(strtolower($method), array('__construct', '__destruct', '__clone'));
+ }
+}
+?>
Deleted: 3.x/trunk/limb/core/src/lmbReflection.class.php
===================================================================
--- 3.x/trunk/limb/core/src/lmbReflection.class.php 2007-07-01 19:17:44 UTC (rev 6030)
+++ 3.x/trunk/limb/core/src/lmbReflection.class.php 2007-07-01 21:58:53 UTC (rev 6031)
@@ -1,165 +0,0 @@
-<?php
-/*
- * Limb PHP Framework
- *
- * @link http://limb-project.com
- * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
- * @license LGPL http://www.gnu.org/copyleft/lesser.html
- */
-//code is based on SimpleReflection class from SimpleTest test suite
-
-/**
- * class lmbReflection.
- *
- * @package core
- * @version $Id$
- */
-class lmbReflection
-{
- protected $_interface;
-
- function __construct($interface)
- {
- $this->_interface = $interface;
- }
-
- function classExists()
- {
- if(!class_exists($this->_interface))
- return false;
-
- $reflection = new ReflectionClass($this->_interface);
- return ! $reflection->isInterface();
- }
-
- function classOrInterfaceExists()
- {
- return $this->_classOrInterfaceExistsWithAutoload($this->_interface, true);
- }
-
- protected function _classOrInterfaceExistsWithAutoload($interface, $autoload)
- {
- if(function_exists('interface_exists'))
- {
- if(interface_exists($this->_interface, $autoload))
- return true;
- }
- return class_exists($this->_interface, $autoload);
- }
-
- function getMethods()
- {
- return array_unique(get_class_methods($this->_interface));
- }
-
- function getInterfaces()
- {
- $reflection = new ReflectionClass($this->_interface);
- if($reflection->isInterface())
- return array($this->_interface);
-
- return $this->_onlyParents($reflection->getInterfaces());
- }
-
- function getInterfaceMethods()
- {
- $methods = array();
- foreach($this->getInterfaces() as $interface)
- $methods = array_merge($methods, get_class_methods($interface));
- return array_unique($methods);
- }
-
- protected function _isInterfaceMethod($method)
- {
- return in_array($method, $this->getInterfaceMethods());
- }
-
- function getParent()
- {
- $reflection = new ReflectionClass($this->_interface);
- $parent = $reflection->getParentClass();
- if($parent)
- return $parent->getName();
- return false;
- }
-
- function isAbstract()
- {
- $reflection = new ReflectionClass($this->_interface);
- return $reflection->isAbstract();
- }
-
- protected function _onlyParents($interfaces)
- {
- $parents = array();
- foreach($interfaces as $interface)
- {
- foreach($interfaces as $possible_parent)
- {
- if($interface->getName() == $possible_parent->getName())
- continue;
-
- if($interface->isSubClassOf($possible_parent))
- break;
- }
- $parents[] = $interface->getName();
- }
- return $parents;
- }
-
- function getSignature($name)
- {
- if($name == '__get')
- return 'function __get($key)';
-
- if($name == '__set')
- return 'function __set($key, $value)';
-
- if(!is_callable(array($this->_interface, $name)))
- return "function $name()";
-
- if($this->_isInterfaceMethod($name))
- return $this->_getFullSignature($name);
-
- return "function $name()";
- }
-
- protected function _getFullSignature($name)
- {
- $interface = new ReflectionClass($this->_interface);
- $method = $interface->getMethod($name);
- $reference = $method->returnsReference() ? '&' : '';
- return "function $reference$name(" .
- implode(', ', $this->_getParameterSignatures($method)) .
- ")";
- }
-
- protected function _getParameterSignatures($method)
- {
- $signatures = array();
- foreach($method->getParameters() as $parameter)
- {
- $type = $parameter->getClass();
- $signatures[] =
- (! is_null($type) ? $type->getName() . ' ' : '') .
- ($parameter->isPassedByReference() ? '&' : '') .
- '$' . $this->_suppressSpurious($parameter->getName()) .
- ($this->_isOptional($parameter) ? ' = null' : '');
- }
- return $signatures;
- }
-
- protected function _suppressSpurious($name)
- {
- return str_replace(array('[', ']', ' '), '', $name);
- }
-
- protected function _isOptional($parameter)
- {
- if(method_exists($parameter, 'isOptional'))
- return $parameter->isOptional();
-
- return false;
- }
-}
-?>
\ No newline at end of file
Copied: 3.x/trunk/limb/core/src/lmbReflectionHelper.class.php (from rev 6029, 3.x/trunk/limb/core/src/lmbReflection.class.php)
===================================================================
--- 3.x/trunk/limb/core/src/lmbReflectionHelper.class.php (rev 0)
+++ 3.x/trunk/limb/core/src/lmbReflectionHelper.class.php 2007-07-01 21:58:53 UTC (rev 6031)
@@ -0,0 +1,125 @@
+<?php
+/*
+ * Limb PHP Framework
+ *
+ * @link http://limb-project.com
+ * @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
+ * @license LGPL http://www.gnu.org/copyleft/lesser.html
+ */
+
+/**
+ * class lmbReflectionHelper.
+ *
+ * @package core
+ * @version $Id$
+ */
+class lmbReflectionHelper
+{
+ static function getMethods($name)
+ {
+ return array_unique(get_class_methods($name));
+ }
+
+ static function getInterfaces($name)
+ {
+ $reflection = new ReflectionClass($name);
+ if($reflection->isInterface())
+ return array($name);
+
+ return self :: _onlyParents($reflection->getInterfaces());
+ }
+
+ static function getInterfaceMethods($name)
+ {
+ $methods = array();
+ foreach(self :: getInterfaces($name) as $interface)
+ $methods = array_merge($methods, get_class_methods($interface));
+ return array_unique($methods);
+ }
+
+ protected function _isInterfaceMethod($name, $method)
+ {
+ return in_array($method, self :: getInterfaceMethods($name));
+ }
+
+ static function getParent($name)
+ {
+ $reflection = new ReflectionClass($name);
+ $parent = $reflection->getParentClass();
+ if($parent)
+ return $parent->getName();
+ return false;
+ }
+
+ static function isAbstract($name)
+ {
+ $reflection = new ReflectionClass($name);
+ return $reflection->isAbstract();
+ }
+
+ protected static function _onlyParents($interfaces)
+ {
+ $parents = array();
+ foreach($interfaces as $interface)
+ {
+ foreach($interfaces as $possible_parent)
+ {
+ if($interface->getName() == $possible_parent->getName())
+ continue;
+
+ if($interface->isSubClassOf($possible_parent))
+ break;
+ }
+ $parents[] = $interface->getName();
+ }
+ return $parents;
+ }
+
+ static function getSignature($name, $method)
+ {
+ if($method == '__get')
+ return 'function __get($key)';
+
+ if($method == '__set')
+ return 'function __set($key, $value)';
+
+ if(!is_callable(array($name, $method)))
+ return "function $method()";
+
+ if(self :: _isInterfaceMethod($name, $method))
+ return self :: _getFullSignature($name, $method);
+
+ return "function $method()";
+ }
+
+ static protected function _getFullSignature($name, $method_name)
+ {
+ $interface = new ReflectionClass($name);
+ $method = $interface->getMethod($method_name);
+ $reference = $method->returnsReference() ? '&' : '';
+ return "function $reference $method_name(" .
+ implode(', ', self :: _getParameterSignatures($method)) .
+ ")";
+ }
+
+ static protected function _getParameterSignatures($method)
+ {
+ $signatures = array();
+ foreach($method->getParameters() as $parameter)
+ {
+ $type = $parameter->getClass();
+ $signatures[] =
+ (! is_null($type) ? $type->getName() . ' ' : '') .
+ ($parameter->isPassedByReference() ? '&' : '') .
+ '$' . self :: _suppressSpurious($parameter->getName()) .
+ ($parameter->isOptional() ? ' = null' : '');
+ }
+ return $signatures;
+ }
+
+ protected function _suppressSpurious($name)
+ {
+ return str_replace(array('[', ']', ' '), '', $name);
+ }
+}
+?>
Modified: 3.x/trunk/limb/core/tests/cases/lmbDecoratorTest.class.php
===================================================================
--- 3.x/trunk/limb/core/tests/cases/lmbDecoratorTest.class.php 2007-07-01 19:17:44 UTC (rev 6030)
+++ 3.x/trunk/limb/core/tests/cases/lmbDecoratorTest.class.php 2007-07-01 21:58:53 UTC (rev 6031)
@@ -1,75 +1,75 @@
-<?php
+<?php
/*
* Limb PHP Framework
*
* @link http://limb-project.com
* @copyright Copyright © 2004-2007 BIT(http://bit-creative.com)
* @license LGPL http://www.gnu.org/copyleft/lesser.html
- */
-lmb_require('limb/core/src/lmbDecorator.class.php');
-
-interface DecoratorTestInterface
-{
- function set($value);
- function get();
- function typehint(DecoratorTestStub $value);
-}
-
-class DecoratorTestStub implements DecoratorTestInterface
-{
- var $value;
-
- function set($value)
- {
- $this->value = $value;
- }
-
- function get()
- {
- return $this->value;
- }
-
- function typehint(DecoratorTestStub $value){}
-}
-
-lmbDecorator :: generate('DecoratorTestStub', 'DecoratorTestStubDecorator');
-
-class lmbDecoratorTest extends UnitTestCase
-{
- function testDoubleDeclaration()
- {
- lmbDecorator :: generate('DecoratorTestStub', 'DecoratorTestStubDecorator');
- lmbDecorator :: generate('DecoratorTestStub', 'DecoratorTestStubDecorator');
- }
-
- function testImplementsInterface()
- {
- $refl = new ReflectionClass('DecoratorTestStubDecorator');
- $this->assertTrue($refl->implementsInterface('DecoratorTestInterface'));
- }
-
- function testHasMethods()
- {
- $decorator = new DecoratorTestStubDecorator(new DecoratorTestStub());
-
- foreach(get_class_methods('DecoratorTestStub') as $method)
- $this->assertTrue(method_exists($decorator, $method));
- }
-
- function testMethodArgumentsTypehinting()
- {
- $refl = new ReflectionClass('DecoratorTestStubDecorator');
- $params = $refl->getMethod('typehint')->getParameters();
- $this->assertEqual(sizeof($params), 1);
- $this->assertEqual($params[0]->getClass()->getName(), 'DecoratorTestStub');
- }
-
- function testCallsPassedToDecorated()
- {
- $decorator = new DecoratorTestStubDecorator(new DecoratorTestStub());
- $decorator->set('foo');
- $this->assertEqual($decorator->get(), 'foo');
- }
-}
-
-?>
\ No newline at end of file
+ */
+lmb_require('limb/core/src/lmbDecorator.class.php');
+
+interface DecoratorTestInterface
+{
+ function set($value);
+ function get();
+ function typehint(DecoratorTestStub $value);
+}
+
+class DecoratorTestStub implements DecoratorTestInterface
+{
+ var $value;
+
+ function set($value)
+ {
+ $this->value = $value;
+ }
+
+ function get()
+ {
+ return $this->value;
+ }
+
+ function typehint(DecoratorTestStub $value){}
+}
+
+lmbDecorator :: generate('DecoratorTestStub', 'DecoratorTestStubDecorator');
+
+class lmbDecoratorTest extends UnitTestCase
+{
+ function testDoubleDeclaration()
+ {
+ lmbDecorator :: generate('DecoratorTestStub', 'DecoratorTestStubDecorator');
+ lmbDecorator :: generate('DecoratorTestStub', 'DecoratorTestStubDecorator');
+ }
+
+ function testImplementsInterface()
+ {
+ $refl = new ReflectionClass('DecoratorTestStubDecorator');
+ $this->assertTrue($refl->implementsInterface('DecoratorTestInterface'));
+ }
+
+ function testHasMethods()
+ {
+ $decorator = new DecoratorTestStubDecorator(new DecoratorTestStub());
+
+ foreach(get_class_methods('DecoratorTestStub') as $method)
+ $this->assertTrue(method_exists($decorator, $method));
+ }
+
+ function testMethodArgumentsTypehinting()
+ {
+ $refl = new ReflectionClass('DecoratorTestStubDecorator');
+ $params = $refl->getMethod('typehint')->getParameters();
+ $this->assertEqual(sizeof($params), 1);
+ $this->assertEqual($params[0]->getClass()->getName(), 'DecoratorTestStub');
+ }
+
+ function testCallsPassedToDecorated()
+ {
+ $decorator = new DecoratorTestStubDecorator(new DecoratorTestStub());
+ $decorator->set('foo');
+ $this->assertEqual($decorator->get(), 'foo');
+ }
+}
+
+?>
More information about the limb-svn
mailing list