TYPO3 API  SVNRelease
DocCommentParser.php
Go to the documentation of this file.
00001 <?php
00002 /***************************************************************
00003 *  Copyright notice
00004 *
00005 *  (c) 2009 Christopher Hlubek <hlubek@networkteam.com>
00006 *  All rights reserved
00007 *
00008 *  This script is part of the TYPO3 project. The TYPO3 project is
00009 *  free software; you can redistribute it and/or modify
00010 *  it under the terms of the GNU General Public License as published by
00011 *  the Free Software Foundation; either version 2 of the License, or
00012 *  (at your option) any later version.
00013 *
00014 *  The GNU General Public License can be found at
00015 *  http://www.gnu.org/copyleft/gpl.html.
00016 *
00017 *  This script is distributed in the hope that it will be useful,
00018 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 *  GNU General Public License for more details.
00021 *
00022 *  This copyright notice MUST APPEAR in all copies of the script!
00023 ***************************************************************/
00024 
00025 /**
00026  * A little parser which creates tag objects from doc comments
00027  *
00028  * @package Extbase
00029  * @subpackage Reflection
00030  * @version $Id: DocCommentParser.php 1052 2009-08-05 21:51:32Z sebastian $
00031  */
00032 class Tx_Extbase_Reflection_DocCommentParser {
00033 
00034     /**
00035      * @var string The description as found in the doc comment
00036      */
00037     protected $description = '';
00038 
00039     /**
00040      * @var array An array of tag names and their values (multiple values are possible)
00041      */
00042     protected $tags = array();
00043 
00044     /**
00045      * Parses the given doc comment and saves the result (description and
00046      * tags) in the parser's object. They can be retrieved by the
00047      * getTags() getTagValues() and getDescription() methods.
00048      *
00049      * @param string $docComment A doc comment as returned by the reflection getDocComment() method
00050      * @return void
00051      */
00052     public function parseDocComment($docComment) {
00053         $this->description = '';
00054         $this->tags = array();
00055 
00056         $lines = explode(chr(10), $docComment);
00057         foreach ($lines as $line) {
00058             if (strlen($line) > 0 && strpos($line, '@') !== FALSE) {
00059                 $this->parseTag(substr($line, strpos($line, '@')));
00060             } elseif (count($this->tags) === 0) {
00061                 $this->description .= preg_replace('/\s*\\/?[\\\\*]*(.*)$/', '$1', $line) . chr(10);
00062             }
00063         }
00064         $this->description = trim($this->description);
00065     }
00066 
00067     /**
00068      * Returns the tags which have been previously parsed
00069      *
00070      * @return array Array of tag names and their (multiple) values
00071      */
00072     public function getTagsValues() {
00073         return $this->tags;
00074     }
00075 
00076     /**
00077      * Returns the values of the specified tag. The doc comment
00078      * must be parsed with parseDocComment() before tags are
00079      * available.
00080      *
00081      * @param string $tagName The tag name to retrieve the values for
00082      * @return array The tag's values
00083      */
00084     public function getTagValues($tagName) {
00085         if (!$this->isTaggedWith($tagName)) throw new RuntimeException('Tag "' . $tagName . '" does not exist.', 1169128255);
00086         return $this->tags[$tagName];
00087     }
00088 
00089     /**
00090      * Checks if a tag with the given name exists
00091      *
00092      * @param string $tagName The tag name to check for
00093      * @return boolean TRUE the tag exists, otherwise FALSE
00094      */
00095     public function isTaggedWith($tagName) {
00096         return (isset($this->tags[$tagName]));
00097     }
00098 
00099     /**
00100      * Returns the description which has been previously parsed
00101      *
00102      * @return string The description which has been parsed
00103      */
00104     public function getDescription() {
00105         return $this->description;
00106     }
00107 
00108     /**
00109      * Parses a line of a doc comment for a tag and its value.
00110      * The result is stored in the interal tags array.
00111      *
00112      * @param string $line A line of a doc comment which starts with an @-sign
00113      * @return void
00114      */
00115     protected function parseTag($line) {
00116         $tagAndValue = preg_split('/\s/', $line, 2);
00117         $tag = substr($tagAndValue[0], 1);
00118         if (count($tagAndValue) > 1) {
00119             $this->tags[$tag][] = trim($tagAndValue[1]);
00120         } else {
00121             $this->tags[$tag] = array();
00122         }
00123     }
00124 }
00125 
00126 ?>