0

I am trying to write a simple template parsing object and my code works great. The problem is any echo commands in the main page or erors should get buffered by ob_start. Trigger the error handling routing on destruct and die on line 52. However this does not run ever. any ideas?

<?php

define('ERROR_FOLDER', 'includes/error/');
define('HTML_FOLDER', 'includes/pages/');
class HTML{
    /* 
        Takes a template file and replaces all instances of <!--VARNAME--> with the value inputed.

        All values should be strings.   
    */

    private $template;
    private $vars;          //array of user defined variables

    function __construct($filePath=''){
        //stop writing anything to the page-html-output
        ob_start();

        //read contents of template
        $this->template=file_get_contents(HTML_FOLDER.$filePath);
    }

    function __destruct() {
        echo $this->execute();
    }

    function getCode() {
        return $this->execute();
    }

    public function get($var) {
        return $this->vars[$var];
    }

    public function set($var,$value) {
        $this->vars[$var]=$value;
    }

    public function append($var,$value) {
        $this->vars[$var].=$value;
    }


    /* ********************************************************************************************************
    *                                            Private Functions                                            *
    ******************************************************************************************************** */

    private function execute() {
        //handel any errors
        $errors=ob_get_contents();
        if (strlen($errors>0)) {
            echo $errors;die();
            //create error file
            //name: date_time_random_pageName
            $fileName=DateTime::format("Y-m-d_H-i-s-u").'_'.rand(10000,99999).'_'.strtok($_SERVER["REQUEST_URI"],'?').'.txt';
            $errorMessage='Error For: ' . $_SERVER[REQUEST_URI] . "\r\n" . $errors;
            file_put_contents(ERROR_FOLDER.$fileName, $errorMessage);
        }       

        //erase any error messages
        ob_end_clean();


        //get template
        $html=$this->template;

        //create search arrays
        $from=array();
        $to=array();
        foreach ($this->vars as $key=>$value) {
            $from[]='<!!--'.$key.'--!!>';
            $to[]=$value;
        }       

        //replace any vars added
        $html=str_replace($from,$to,$html);

        //minimize html text                            adapted from http://stackoverflow.com/questions/6225351/how-to-minify-php-page-html-output
        $from = array(
            '/\>[^\S ]+/s',                             // strip whitespaces after tags, except space
            '/[^\S ]+\</s',                             // strip whitespaces before tags, except space
            '/(\s)+/s',                                 // shorten multiple whitespace sequences
            '/<!--.*?-->|\t|(?:\r?\n[ \t]*)+/s',        // removes comments
            '/<!!--.*?--!!>|\t|(?:\r?\n[ \t]*)+/s'      // removes unused vars
        );
        $to = array(
            '>',
            '<',
            '\\1',
            '',
            ''
        );
        $html = preg_replace($from, $to, $html);


        return $html;       
    }
}   
?>

here is a test script

<?php
    require_once "includes/HTML.php";

    $page=new HTML('template-main.html');
    $page->set('list','Hello World');
    echo "should get saved to file and never show up on screen.";   
?>

template-main.html should have

<!!--list--!!>

What I expect to see is: should get saved to file and never show up on screen. since this should be stored in $errors after line 51 of object.

What I get is: Hello World

2 Answers2

0

Try to surround your echo with ob_end_flush(); and ob_start();

example :

ob_end_flush();

# CODE THAT NEEDS IMMEDIATE FLUSHING

ob_start();

source : How to flush output after each `echo` call?

Community
  • 1
  • 1
SamyQc
  • 365
  • 2
  • 10
  • I don't want anything to be written to user until destruct of the object. Any Echo commands by main program should be treated as errors. – Matthew Cornelisse Nov 19 '15 at 19:25
  • What if you change your ob_end_clean by ob_end_flush ? EDIT : ob_end_clean only clears the buffers, it nevers does anything with it , while ob_end_flush prints it before flushing it. – SamyQc Nov 19 '15 at 19:45
  • ob_get_contents() a few lines higher is suppose to get it but doesn't. I don't want it printed. – Matthew Cornelisse Nov 19 '15 at 19:48
0

if (strlen($errors>0)) {

should be

if (strlen($errors)>0) {