Я написал MVC-фреймворк и использую ob_start ('error_handler'), чтобы иметь возможность также перехватывать фатальные ошибки. Это прекрасно работает!
В boot.php
// Start the error-logging
ob_start( 'error_logging' );
// Run the request
Request::run( URL::getInstance()->init()->formatURL() );
// Flush and disable output-buffering
ob_end_flush();
функция журнала ошибок:
function error_logging( $output )
{
// Get the last error
$trace = error_get_last();
// Initialize our final array of fields explaining the error
$final = array();
// Check if the error does not exist, return the original output unedited
if( !isset( $trace ) ) return $output;
// Loop through or error and build our array
foreach( $trace as $info => $value ) $final[$info] = $value;
// Initialize our Error-class
$error = Error::factory();
// Handle the error with the fields and build our error-message
$newOutput = $error->handle( $final['type'] , $final['message'], $final['file'], $final['line'] );
// Check if the new output is null, if yes set output to the original output, otherwise set it to the new output
$output = ( $newOutput === null ) ? $output : $newOutput;
// Return the output
return $output;
}
Опять же, это прекрасно работает и прекрасно справляется со всеми ошибками! Я не использую какой-либо сброс или что-либо внутри класса Error.
Теперь у меня есть Контроллер, который использует ob_start, так как это довольно длительное время работы определенного метода, и я хочу получить обратную связь с пользователем, что происходит и что делает скрипт. Для этого я использую ob_flush ().
Однако, после реализации этого ob_start ('error_logging'); кажется, что ob_flush не работает. Они ждут, пока весь сценарий не будет завершен (это займет около 8 минут).
Почему?
Пример того, как я смываю, выглядит следующим образом:
В методе Controller:
$this->login->execute();
В логине :: выполнить:
public function execute( $site = 'mobile' )
{
ob_start();
$this->debug->log('--------------------------------------------<br />');
$this->debug->log( 'Logging into site: ' . ucfirst( $site ) . '<br />' );
// Initiate cookies
if( $site == 'full' )
$this->_initCookies();
// Set up the URL
$url = Config::get( $site ) . Config::get( 'url_login' );
curl_setopt( $this->curl->get(), CURLOPT_URL, $url );
// CURL-values
curl_setopt( $this->curl->get(), CURLOPT_FOLLOWLOCATION, 1 );
curl_setopt( $this->curl->get(), CURLOPT_RETURNTRANSFER, 1 );
curl_setopt( $this->curl->get(), CURLOPT_POST, 1 );
curl_setopt( $this->curl->get(), CURLOPT_POSTFIELDS, Config::get( 'postfields' ) );
curl_setopt( $this->curl->get(), CURLOPT_COOKIEFILE, 'resources/tmp/cookies.txt');
curl_setopt( $this->curl->get(), CURLOPT_CONNECTTIMEOUT, 10 );
curl_setopt( $this->curl->get(), CURLOPT_TIMEOUT, 40 );
curl_setopt( $this->curl->get(), CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:6.0.1) Gecko/20100101 Firefox/6.0.1');
// Get the page returned after we've logged in
$this->login_html = str_get_html( curl_exec( $this->curl->get() ) );
// Validate the page returned to see if we successfully logged in
$this->_validateLogin( $site );
$this->debug->log('--------------------------------------------<br />');
ob_end_flush();
// Return $this
return $this;
}
Каждый вызов $ this-> debug-> log () сбрасывает вывод следующим образом:
public function log( $string )
{
if( $this->active() || $this->logging() )
{
echo str_repeat("<!-- AGENT SMITH -->", 500);
echo $string;
ob_flush();
flush();
}
}
Есть идеи, почему он не корректно очищается?
Скажите, если вам нужно просмотреть еще какой-нибудь код, и я предоставлю его !!
Спасибо
:::::::: РЕШЕНИЕ ::::::::::
Мне пришлось сохранить первый буфер (error_logging), завершить его, запустить новый, выполнить мои действия, а затем завершить его, снова запустить первый буфер и повторить сохраненный вывод.
сделал класс для него:
class Tanaxia_Buffer
{
public static $instance = null;
private $main = null;
private $previousOutput = "";
public static function getInstance()
{
if( Buffer::$instance === null )
Buffer::$instance = new Buffer();
return Buffer::$instance;
}
public function set_main( $ob )
{
$this->main = $ob;
}
public function start( $main = null )
{
if( $main !== null )
$this->set_main( $main );
ob_start( $this->main );
}
public function end( $type = 'clean' )
{
switch( $type )
{
case 'flush': ob_end_flush(); break;
case 'clean': ob_end_clean(); break;
}
}
public function start_separate()
{
$this->previousOutput = ob_get_contents();
ob_end_clean();
ob_start();
}
public function end_separate( $type = 'flush' )
{
switch( $type )
{
case 'flush': ob_end_flush(); break;
case 'clean': ob_end_clean(); break;
}
ob_start( $this->main );
echo $this->previousOutput;
empty( $this->previousOutput );
}
}
Использование:
boot.php
// Start the error-logging
Buffer::getInstance()->start( 'error_logging' );
// Run the request
Request::run( URL::getInstance()->init()->formatURL() );
// Flush and disable output-buffering
Buffer::getInstance()->end( 'flush' );
Вход :: Execute ():
public function execute( $site = 'mobile' )
{
Buffer::getInstance()->start_separate();
// Everything in between
Buffer::getInstance()->end_separate();
// Return $this
return $this;
}
Работает отлично! Не будет поддерживать несколько вложенных ob_start, поскольку он сохраняет только 1 предыдущий вывод. Может быть исправлено, но не обязательно прямо сейчас!