web/lib/Zend/Reflection/Method.php
changeset 64 162c1de6545a
parent 19 1c2f13fd785c
child 68 ecaf28ffe26e
equal deleted inserted replaced
63:5b37998e522e 64:162c1de6545a
       
     1 <?php
       
     2 /**
       
     3  * Zend Framework
       
     4  *
       
     5  * LICENSE
       
     6  *
       
     7  * This source file is subject to the new BSD license that is bundled
       
     8  * with this package in the file LICENSE.txt.
       
     9  * It is also available through the world-wide-web at this URL:
       
    10  * http://framework.zend.com/license/new-bsd
       
    11  * If you did not receive a copy of the license and are unable to
       
    12  * obtain it through the world-wide-web, please send an email
       
    13  * to license@zend.com so we can send you a copy immediately.
       
    14  *
       
    15  * @category   Zend
       
    16  * @package    Zend_Reflection
       
    17  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    18  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    19  * @version    $Id: Method.php 20096 2010-01-06 02:05:09Z bkarwin $
       
    20  */
       
    21 
       
    22 /**
       
    23  * @see Zend_Reflection_Class
       
    24  */
       
    25 require_once 'Zend/Reflection/Class.php';
       
    26 
       
    27 /**
       
    28  * @see Zend_Reflection_Docblock
       
    29  */
       
    30 require_once 'Zend/Reflection/Docblock.php';
       
    31 
       
    32 /**
       
    33  * @see Zend_Reflection_Parameter
       
    34  */
       
    35 require_once 'Zend/Reflection/Parameter.php';
       
    36 
       
    37 /**
       
    38  * @category   Zend
       
    39  * @package    Zend_Reflection
       
    40  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
       
    41  * @license    http://framework.zend.com/license/new-bsd     New BSD License
       
    42  */
       
    43 class Zend_Reflection_Method extends ReflectionMethod
       
    44 {
       
    45     /**
       
    46      * Retrieve method docblock reflection
       
    47      *
       
    48      * @return Zend_Reflection_Docblock
       
    49      * @throws Zend_Reflection_Exception
       
    50      */
       
    51     public function getDocblock($reflectionClass = 'Zend_Reflection_Docblock')
       
    52     {
       
    53         if ('' == $this->getDocComment()) {
       
    54             require_once 'Zend/Reflection/Exception.php';
       
    55             throw new Zend_Reflection_Exception($this->getName() . ' does not have a docblock');
       
    56         }
       
    57 
       
    58         $instance = new $reflectionClass($this);
       
    59         if (!$instance instanceof Zend_Reflection_Docblock) {
       
    60             require_once 'Zend/Reflection/Exception.php';
       
    61             throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Docblock');
       
    62         }
       
    63         return $instance;
       
    64     }
       
    65 
       
    66     /**
       
    67      * Get start line (position) of method
       
    68      *
       
    69      * @param  bool $includeDocComment
       
    70      * @return int
       
    71      */
       
    72     public function getStartLine($includeDocComment = false)
       
    73     {
       
    74         if ($includeDocComment) {
       
    75             if ($this->getDocComment() != '') {
       
    76                 return $this->getDocblock()->getStartLine();
       
    77             }
       
    78         }
       
    79 
       
    80         return parent::getStartLine();
       
    81     }
       
    82 
       
    83     /**
       
    84      * Get reflection of declaring class
       
    85      *
       
    86      * @param  string $reflectionClass Name of reflection class to use
       
    87      * @return Zend_Reflection_Class
       
    88      */
       
    89     public function getDeclaringClass($reflectionClass = 'Zend_Reflection_Class')
       
    90     {
       
    91         $phpReflection  = parent::getDeclaringClass();
       
    92         $zendReflection = new $reflectionClass($phpReflection->getName());
       
    93         if (!$zendReflection instanceof Zend_Reflection_Class) {
       
    94             require_once 'Zend/Reflection/Exception.php';
       
    95             throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Class');
       
    96         }
       
    97         unset($phpReflection);
       
    98         return $zendReflection;
       
    99     }
       
   100 
       
   101     /**
       
   102      * Get all method parameter reflection objects
       
   103      *
       
   104      * @param  string $reflectionClass Name of reflection class to use
       
   105      * @return array of Zend_Reflection_Parameter objects
       
   106      */
       
   107     public function getParameters($reflectionClass = 'Zend_Reflection_Parameter')
       
   108     {
       
   109         $phpReflections  = parent::getParameters();
       
   110         $zendReflections = array();
       
   111         while ($phpReflections && ($phpReflection = array_shift($phpReflections))) {
       
   112             $instance = new $reflectionClass(array($this->getDeclaringClass()->getName(), $this->getName()), $phpReflection->getName());
       
   113             if (!$instance instanceof Zend_Reflection_Parameter) {
       
   114                 require_once 'Zend/Reflection/Exception.php';
       
   115                 throw new Zend_Reflection_Exception('Invalid reflection class provided; must extend Zend_Reflection_Parameter');
       
   116             }
       
   117             $zendReflections[] = $instance;
       
   118             unset($phpReflection);
       
   119         }
       
   120         unset($phpReflections);
       
   121         return $zendReflections;
       
   122     }
       
   123 
       
   124     /**
       
   125      * Get method contents
       
   126      *
       
   127      * @param  bool $includeDocblock
       
   128      * @return string
       
   129      */
       
   130     public function getContents($includeDocblock = true)
       
   131     {
       
   132         $fileContents = file($this->getFileName());
       
   133         $startNum = $this->getStartLine($includeDocblock);
       
   134         $endNum = ($this->getEndLine() - $this->getStartLine());
       
   135 
       
   136         return implode("\n", array_splice($fileContents, $startNum, $endNum, true));
       
   137     }
       
   138 
       
   139     /**
       
   140      * Get method body
       
   141      *
       
   142      * @return string
       
   143      */
       
   144     public function getBody()
       
   145     {
       
   146         $lines = array_slice(
       
   147             file($this->getDeclaringClass()->getFileName(), FILE_IGNORE_NEW_LINES),
       
   148             $this->getStartLine(),
       
   149             ($this->getEndLine() - $this->getStartLine()),
       
   150             true
       
   151         );
       
   152 
       
   153         $firstLine = array_shift($lines);
       
   154 
       
   155         if (trim($firstLine) !== '{') {
       
   156             array_unshift($lines, $firstLine);
       
   157         }
       
   158 
       
   159         $lastLine = array_pop($lines);
       
   160 
       
   161         if (trim($lastLine) !== '}') {
       
   162             array_push($lines, $lastLine);
       
   163         }
       
   164 
       
   165         // just in case we had code on the bracket lines
       
   166         return rtrim(ltrim(implode("\n", $lines), '{'), '}');
       
   167     }
       
   168 }