Author: fmenge
Date: Tue Dec 25 22:40:17 2007
New Revision: 7036

Log:
- Dependency injection mechanism introduced in ezcReflection class
- First parent methods reimplemented to query external reflection object
- Documentation improved
- Test case for the dependency injection (inherits the existing test case)

Added:
    experimental/Reflection/tests/class_external_test.php
Modified:
    experimental/Reflection/src/class.php
    experimental/Reflection/tests/suite.php

Modified: experimental/Reflection/src/class.php
==============================================================================
--- experimental/Reflection/src/class.php [iso-8859-1] (original)
+++ experimental/Reflection/src/class.php [iso-8859-1] Tue Dec 25 22:40:17 2007
@@ -25,34 +25,103 @@
     protected $docParser;
 
     /**
-     * @param string $name
-     */
-    public function __construct($name) {
-        try {
-            parent::__construct($name);
-        }
-        catch (Exception $e) {
-            return;
-        }
+     * @var string|object|ReflectionClass
+     *      name, instance or ReflectionClass object of the class to be
+     *      reflected
+     */
+    protected $class;
+
+    /**
+     * @param string|object|ReflectionClass $argument
+     *        name, instance or ReflectionClass object of the class to be
+     *        reflected
+     */
+    public function __construct( $argument )
+    {
+        if ( !$argument instanceof ReflectionClass )
+        {
+            parent::__construct($argument);
+        }
+        $this->class = $argument;
         $this->docParser = ezcReflectionApi::getDocParserInstance();
         $this->docParser->parse($this->getDocComment());
     }
-
-    /**
-     * @param string $name
+    
+    /**
+     * Use overloading to call additional methods
+     * of the reflection instance given to the constructor
+     *
+     * @param string $method Method to be called
+     * @param array<integer, mixed> $arguments Arguments that were passed
+     * @return mixed
+     */
+    public function __call( $method, $arguments )
+    {
+        if ( !$this->class instanceof ReflectionClass )
+        {
+            // query external reflection object
+            return call_user_method( $this->class, $method, $arguments );
+        } else {
+            throw new Exception( 'Call to undefined method ' . __CLASS__ . 
'::' . $method );
+        }
+    }
+
+    /**
+     * Returns the name of the class.
+     *
+     * @return string Classname
+     */
+    public function getName() {
+        if ( $this->class instanceof ReflectionClass )
+        {
+            // query external reflection object
+            $name = $this->class->getName();
+        } else {
+            $name = parent::getName();
+        }
+        return $name;
+    }
+
+    /**
+     * Returns the doc comment for the class.
+     *
+     * @return string Doc comment
+     */
+    public function getDocComment() {
+        if ( $this->class instanceof ReflectionClass )
+        {
+            // query external reflection object
+            $comment = $this->class->getDocComment();
+        } else {
+            $comment = parent::getDocComment();
+        }
+        return $comment;
+    }
+
+    /**
+     * Returns an ezcReflectionMethod object of the method specified by $name.
+     *
+     * @param string $name Name of the method
      * @return ezcReflectionMethod
      */
     public function getMethod($name) {
-        return new ezcReflectionMethod($this->getName(), $name);
-    }
-
-    /**
+        return new ezcReflectionMethod($this->class, $name);
+    }
+
+    /**
+     * Returns an ezcReflectionMethod object of the constructor method.
+     *
      * @return ezcReflectionMethod
      */
     public function getConstructor() {
-        $con = parent::getConstructor();
-        if ($con != null) {
-            $extCon = new ezcReflectionMethod($this->getName(), 
$con->getName());
+        if ($this->class instanceof ReflectionClass) {
+            // query external reflection object
+            $constructorName = $this->class->getConstructor();
+        } else {
+            $constructorName = parent::getConstructor();
+        }
+        if ($constructor != null) {
+            $extCon = new ezcReflectionMethod($this->class, 
$constructor->getName());
             return $extCon;
         }
         else {
@@ -61,9 +130,16 @@
     }
 
     /**
-     * @param integer $filter a combination of ReflectionMethod::IS_STATIC,
-     * ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_PROTECTED, 
ReflectionMethod::IS_PRIVATE,
-     * ReflectionMethod::IS_ABSTRACT and ReflectionMethod::IS_FINAL
+     * Returns the methods as an array of ezcReflectionMethod objects.
+     *
+     * @param integer $filter
+     *        A combination of
+     *        ReflectionMethod::IS_STATIC,
+     *        ReflectionMethod::IS_PUBLIC,
+     *        ReflectionMethod::IS_PROTECTED,
+     *        ReflectionMethod::IS_PRIVATE,
+     *        ReflectionMethod::IS_ABSTRACT and
+     *        ReflectionMethod::IS_FINAL
      * @return ezcReflectionMethod[]
      */
     public function getMethods($filter = 0) {
@@ -82,8 +158,16 @@
     /**
      * @return ezcReflectionClassType
      */
-    public function getParentClass() {
-        $class = parent::getParentClass();
+    public function getParentClass()
+    {
+        if ( $this->class instanceof ReflectionClass )
+        {
+            // query external reflection object
+            $parentClass = $this->class->getParentClass();
+        } else {
+            $parentClass = parent::getParentClass();
+        }
+        //TODO: continue work with external reflection object
         if (is_object($class)) {
             return new ezcReflectionClassType($class->getName());
         }
@@ -98,6 +182,7 @@
      * @throws RelectionException if property doesn't exists
      */
     public function getProperty($name) {
+        //FIXME: unused variable $pro
         $pro = parent::getProperty($name);
         return new ezcReflectionProperty($this->getName(), $name);
     }

Added: experimental/Reflection/tests/class_external_test.php
==============================================================================
--- experimental/Reflection/tests/class_external_test.php (added)
+++ experimental/Reflection/tests/class_external_test.php [iso-8859-1] Tue Dec 
25 22:40:17 2007
@@ -1,0 +1,33 @@
+<?php
+/**
+ * @copyright Copyright (C) 2005, 2006 eZ systems as. All rights reserved.
+ * @license http://ez.no/licenses/new_bsd New BSD License
+ * @version //autogentag//
+ * @filesource
+ * @package Reflection
+ * @subpackage Tests
+ */
+
+class ezcReflectionClassExternalTest extends ezcReflectionClassTest
+{
+    /**
+     * @var ezcReflectionClass
+     */
+    protected $class;
+
+    public function setUp()
+    {
+        $this->class = new ezcReflectionClass( new ReflectionClass( 
'ezcReflectionClass' ) );
+    }
+
+    public function tearDown()
+    {
+        unset($this->class);
+    }
+
+    public static function suite()
+    {
+         return new PHPUnit_Framework_TestSuite( 
"ezcReflectionClassExternalTest" );
+    }
+}
+?>

Modified: experimental/Reflection/tests/suite.php
==============================================================================
--- experimental/Reflection/tests/suite.php [iso-8859-1] (original)
+++ experimental/Reflection/tests/suite.php [iso-8859-1] Tue Dec 25 22:40:17 
2007
@@ -15,6 +15,7 @@
 require_once 'function_test.php';
 require_once 'parameter_test.php';
 require_once 'class_test.php';
+require_once 'class_external_test.php';
 require_once 'method_test.php';
 require_once 'property_test.php';
 require_once 'extension_test.php';
@@ -50,6 +51,7 @@
         $this->addTest( ezcReflectionFunctionTest::suite() );
         $this->addTest( ezcReflectionParameterTest::suite() );
         $this->addTest( ezcReflectionClassTest::suite() );
+        $this->addTest( ezcReflectionClassExternalTest::suite() );
         $this->addTest( ezcReflectionMethodTest::suite() );
         $this->addTest( ezcReflectionPropertyTest::suite() );
         $this->addTest( ezcReflectionExtensionTest::suite() );


-- 
svn-components mailing list
svn-components@lists.ez.no
http://lists.ez.no/mailman/listinfo/svn-components

Reply via email to