Location: PHPKode > projects > EUrl the Easy URL Generator > EUrl/src/PhpCompiler.class.php
<?php

define( "START_TAG" , "/"."*NODEBUG{*"."/" );
define( "END_TAG"   , "/"."*}NODEBUG*"."/" );

/**
 * This is a set of functions designed to compress and compile PHP
 * files for faster loading and smaller through-put.  You'd be amazed
 * how much memory this can save!
 *
 * @brief Compilers to shrink PHP code
 *
 * @author Jason Schmidt
 */
class PhpCompiler
{
    /**
     * @brief Boolean optionally use this component
     *
     * @private
     *
     * @author Jason Schmidt
     */
    var $_UseMakeLibrary;

    /**
     * @brief Boolean optionally use this component
     *
     * @private
     *
     * @author Jason Schmidt
     */
    var $_UseNoDebugCode;

    /**
     * @brief Boolean optionally use this component
     *
     * @private
     *
     * @author Jason Schmidt
     */
    var $_UsePhpScrub;

    /**
     * @brief Constructor should set default modes
     *
     * @return Reference to self
     *
     * @public
     *
     * @author Jason Schmidt
     */
    function & PhpCompiler()
    {
        $this->_UseMakeLibrary = FALSE;
        $this->_UseNoDebugCode = TRUE;
        $this->_UsePhpScrub    = TRUE;
        return($this);
    }

    /**
     * @brief Set engine choices
     *
     * @see PhpCompiler::GetModes()
     *
     * @param $NewModes Array(ModeName=>Value)
     *
     * @return Boolean success
     *
     * @author Jason Schmidt
     *
     * @public
     */
    function ChangeMode($NewModes=array())
    {
        $name_conversion = array(
            "library"    => "_UseMakeLibrary",
            "nodebug"    => "_UseNoDebugCode",
            "whitespace" => "_UsePhpScrub",
            );

        if(!is_array($NewModes))
        {
            return(FALSE);
        }

        foreach($NewModes as $Name => $Value)
        {
            if(!isset($name_conversion[$Name]))
            {
                return(FALSE);
            }

            if(!is_bool($Value))
            {
                return(FALSE);
            }

            $Name = $name_conversion[$Name];
            $this->$Name = $Value;
        }

        return(TRUE);
    }

    /**
     * @brief Get the modes for ChangeMode()
     *
     * @return Array( Array( "Name"=>name, "Default"=>default,
     * "Description"=>description, ) )
     *
     * @public
     *
     * @author Jason Schmidt
     */
    function GetModes()
    {
        return(array(
            array(
                "Name"        => "library",
                "Default"     => FALSE,
                "Description" =>
                    "Remove extra newlines and php tags"
                ),
            array(
                "Name"        => "nodebug",
                "Default"     => TRUE,
                "Description" =>
                    "Remove /*NODEBUG{*/.../*}NODEBUG*/ blocks"
                ),
            array(
                "Name"        => "whitespace",
                "Default"     => TRUE,
                "Description" =>
                    "Pass through 'php -w'"
                ),
            ));
    }

    /**
     * @brief Stub function for returning errors
     *
     * @return Array(String...) Error list
     *
     * @author Jason Schmidt
     *
     * @public
     */
    function GetErrors()
    {
        return(array());
    }

    /**
     * @brief Perform actual compile stage
     *
     * @see PhpCompiler documentation
     *
     * @param $Input STRING input to mash about the head
     *
     * @return String contents of $Input corrected as requested
     *
     * @author Jason Schmidt
     *
     * @public
     */
    function Compile($Input)
    {
        if(!is_string($Input))
        {
            return("");
        }

        if($this->_UseNoDebugCode)
        {
            $Input = $this->NoDebugCode($Input);
        }
        if($this->_UsePhpScrub)
        {
            $Input = $this->PhpScrub($Input);
        }
        if($this->_UseMakeLibrary)
        {
            $Input = $this->MakeLibrary($Input);
        }

        return($Input);
    }

    /**
     * So you have a bunch of PHP files which contain class
     * definitions, and you need them in a library file so that
     * there's only one file to include to get all your class
     * definitions.  Easily done!  Just concatenate ALL the files
     * together through this, and they'll be smushed down into a
     * single PHP block.
     *
     * @warning This ASSUMES your files contain NOTHING OTHER THAN PHP
     * CODE.  If you mix PHP and HTML, this is NOT FOR YOU.  You have
     * been warned.
     *
     * @warning This removes ALL newlines, including newlines inside
     * of strings, which may not be what you want.
     *
     * @param $Input String input to smush into even more
     * unrecognizable form
     *
     * @return String $Input with open and close tags removed (except
     * first and last) and ALL newlines removed
     *
     * @brief Combine multiple concatenated files into a single
     * logical file by removing opening and closing PHP tags (except
     * for first and last) and removing ALL newlines
     *
     * @author Jason Schmidt
     *
     * @public
     */
    function MakeLibrary($Input)
    {
        $replace = array(
            '/\n/s'         => ''          , # Remove all newlines
            '/\<\?'.'php/s' => ''          , # Remove all open  tags
            '/\?\>/s'       => ''          , # Remove all close tags
            '/^/s'          => '<?'.'php ' , # Add beginning open  tag
            '/$/s'          => ' ?'.'>'    , # Add ending    close tag
            );

        $Input = preg_replace(
                array_keys($replace),
                array_values($replace),
                $Input
                );

        return($Input);
    }

    /**
     * So you have a file which you want to put debug code in, but
     * don't want that very same code going to a production server.
     * The solution is simple!  Just put small comment tags around the
     * debug code, and pass it through a simple script which will
     * remove such blocks.  That's what this does!
     *
     * @warning If you have an open-ended or missing-beginning NODEBUG
     * block, the behaviour will be unexpected.  With both, the tag
     * (and code supposed to be enclosed) will be left behind.
     *
     * @param $Input String to be "fixed"
     *
     * @return String $Input without NODEBUG blocks
     *
     * @brief Strip out NODEBUG blocks from code
     *
     * @author Jason Schmidt
     *
     * @public
     */
    function NoDebugCode($Input)
    {
        $strlen = strlen($Input);
        $inblock = FALSE;
        $return = "";
        $block = "";
        for($x = 0; $x < $strlen; $x++)
        {
            if(START_TAG == substr($Input, $x, strlen(START_TAG)))
            {
                if($block)
                {
                    $return .= $block;
                    $block = "";
                }

                $inblock = TRUE;
            }
            if(END_TAG == substr($Input, $x, strlen(END_TAG)))
            {
                if($inblock)
                {
                    $inblock = FALSE;
                    $block = "";
                    $x += strlen(END_TAG);
                }
            }
            if(!$inblock)
            {
                $return .= $Input[$x];
            }
            else
            {
                $block .= $Input[$x];
            }
        }

        if($return !== $Input)
        {
            return($this->NoDebugCode($return));
        }

        return($return);
    }

    /**
     * Have a PHP file with a lot of comments, white space, and other junk
     * that you'd like to strip out?  Use this!  Admittedly, it's just a
     * system call to php -w, but in the future this may be updated to
     * take advantage of PHP 5 code parsing functions.
     *
     * @param $Input String to be "fixed"
     *
     * @return Cleaned up string
     *
     * @brief Strip out white-space and comments from PHP code
     *
     * @author Jason Schmidt
     */
    function PhpScrub($Input)
    {
        $Input = escapeshellarg($Input);
        $Command = "echo $Input | php -w ";
        $Return = shell_exec($Command);
        return($Return);
    }
}

return("PhpCompiler");

?>
Return current item: EUrl the Easy URL Generator