Format.class.php

Go to the documentation of this file.
00001 <?php
00002 /***************************************************************************
00003  *   Copyright (C) 2006-2007 by Konstantin V. Arkhipov                     *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU Lesser General Public License as        *
00007  *   published by the Free Software Foundation; either version 3 of the    *
00008  *   License, or (at your option) any later version.                       *
00009  *                                                                         *
00010  ***************************************************************************/
00011 /* $Id: Format.class.php 4687 2007-12-09 18:57:18Z voxus $ */
00012 
00016     final class Format extends StaticFactory
00017     {
00018         // dumb and straigh beautifier
00019         public static function indentize($data)
00020         {
00021             $out    = null;
00022             
00023             $indent = 0;
00024             $chain  = 1;
00025             $first  = true; // for schema.php-like files
00026             
00027             foreach (explode("\n", $data) as $string) {
00028                 $string = preg_replace('~^[\t]+~', null, rtrim($string))."\n";
00029                 
00030                 if ($string == "}\n") {
00031                     $indent -= $chain;
00032                     $chain = 1;
00033                 } elseif ($string == ")->\n")
00034                     --$indent;
00035                 elseif ($string == ")\n")
00036                     --$indent;
00037                 elseif ($string == ");\n")
00038                     --$indent;
00039                 elseif ($string == "),\n")
00040                     --$indent;
00041                 elseif ($string == "?>\n")
00042                     $indent = 0;
00043                 elseif ($string[0] == '?')
00044                     ++$indent;
00045                 
00046                 if ($string <> "\n") {
00047                     if ($indent > 0)
00048                         $out .= str_pad(null, $indent, "\t", STR_PAD_LEFT).$string;
00049                     else
00050                         $out .= $string;
00051                 }
00052 
00053                 if (substr($string, -2 ,2) == "{\n")
00054                     ++$indent;
00055                 elseif (
00056                     substr_count($string, "'") == 2
00057                     && substr($string, -3, 3) == "=>\n"
00058                 ) {
00059                     ++$indent;
00060                     ++$chain;
00061                 } elseif (
00062                     $string[0] == '$'
00063                     && (
00064                         substr($string, -2, 2) == "=\n"
00065                         || substr($string, -3, 3) == "->\n"
00066                     )
00067                 ) {
00068                     ++$indent;
00069                     ++$chain;
00070                 } elseif (substr($string, -2, 2) == "(\n")
00071                     ++$indent;
00072                 elseif ($string == "\n" && $indent == 0) {
00073                     ++$indent;
00074                 } elseif ($string == "return\n") {
00075                     ++$indent;
00076                     ++$chain;
00077                 } elseif ($string == "\n" && $chain > 1) {
00078                     $indent -= $chain - 1;
00079                     $chain = 1;
00080                 } elseif ($string[0] == ':') {
00081                     --$indent;
00082                 } elseif ($string == "),\n")
00083                     --$indent;
00084                 
00085                 if ($string == "\n") {
00086                     if (!$first && ($indent > 0)) {
00087                         $out .= str_pad(null, $indent, "\t", STR_PAD_LEFT).$string;
00088                     } else {
00089                         $out .= $string;
00090                         $first = false;
00091                     }
00092                 }
00093             }
00094             
00095             return $out;
00096         }
00097     }
00098 ?>

Generated on Sun Dec 9 21:56:24 2007 for onPHP by  doxygen 1.5.4