示例#1
0
 /**
  * Update an existing docblock
  * @param string $file File in which the docblock should be updated
  * @param DocBlock $doc DocBlock instance that contains the text to insert
  * @param int $line Code line where the existing docblock should be located
  */
 function UpdateDocBlock($file, $doc, $line)
 {
     // Read file...
     $source = @file_get_contents($file);
     if (!$source) {
         die("ERR3");
     }
     // error 3 => can't read file
     // Now, we process the file, and make sure the original comment is still at the right location, or it's been moved
     $tokens = token_get_all($source);
     $found = false;
     $oldcomment = "";
     foreach ($tokens as $token) {
         if ($token[0] == T_DOC_COMMENT && $token[2] == $line) {
             $found = true;
             $oldcomment = $token[1];
         }
     }
     if (!$found) {
         die("ERR4");
     }
     // Set indent
     $code = $_POST['Code'];
     if (preg_match('@^([\\t\\040]*).*$@', $code, $matches)) {
         $doc->indent = $matches[1];
     }
     // Which line breaks does the source use?
     $lbtype = "\n";
     if (substr_count($source, "\r\n") > 0) {
         $lbtype = "\r\n";
     }
     $final = implode($lbtype, $doc->Output(isUtf8($source)));
     // Simply search & replace old docblock
     $source = str_replace($oldcomment, trim($final), $source);
     //rename($file,$file.".bak");
     file_put_contents($file, $source);
 }