phpbb/phpBB/phpbb/db/log_wrapper_migrator_output_handler.php
MateBartus 4bdef6fd21 [ticket/13697] Moving filesystem related functions to filesystem service
* Moving filesystem service to \phpbb\filesystem namespace
 * Wraping Symfony's Filesystem component
 * Moving filesystem related functions from includes/functions.php
   into \phpbb\filesystem\filesystem
   Functions moved (and deprecated):
     - phpbb_chmod
     - phpbb_is_writable
     - phpbb_is_absolute
     - phpbb_own_realpath
     - phpbb_realpath
 * Adding interface for filesystem service

PHPBB3-13697
2015-04-16 13:24:10 +02:00

102 lines
2.1 KiB
PHP

<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\db;
use phpbb\user;
class log_wrapper_migrator_output_handler implements migrator_output_handler_interface
{
/**
* User object.
*
* @var user
*/
protected $user;
/**
* A migrator output handler
*
* @var migrator_output_handler_interface
*/
protected $migrator;
/**
* Log file handle
* @var resource
*/
protected $file_handle = false;
/**
* @var \phpbb\filesystem\filesystem_interface
*/
protected $filesystem;
/**
* Constructor
*
* @param user $user User object
* @param migrator_output_handler_interface $migrator Migrator output handler
* @param string $log_file File to log to
* @param \phpbb\filesystem\filesystem_interface phpBB filesystem object
*/
public function __construct(user $user, migrator_output_handler_interface $migrator, $log_file, \phpbb\filesystem\filesystem_interface $filesystem)
{
$this->user = $user;
$this->migrator = $migrator;
$this->filesystem = $filesystem;
$this->file_open($log_file);
}
/**
* Open file for logging
*
* @param string $file File to open
*/
protected function file_open($file)
{
if ($this->filesystem->is_writable(dirname($file)))
{
$this->file_handle = fopen($file, 'w');
}
else
{
throw new \RuntimeException('Unable to write to migrator log file');
}
}
/**
* {@inheritdoc}
*/
public function write($message, $verbosity)
{
$this->migrator->write($message, $verbosity);
if ($this->file_handle !== false)
{
$translated_message = call_user_func_array(array($this->user, 'lang'), $message) . "\n";
if ($verbosity <= migrator_output_handler_interface::VERBOSITY_NORMAL)
{
$translated_message = '[INFO] ' . $translated_message;
}
else
{
$translated_message = '[DEBUG] ' . $translated_message;
}
fwrite($this->file_handle, $translated_message);
fflush($this->file_handle);
}
}
}