Log close method. Fixes unit tests.

This commit is contained in:
Mike Jolley 2016-04-19 16:56:41 +01:00
parent 37f03f34e8
commit a60b03baa9
1 changed files with 23 additions and 9 deletions

View File

@ -35,21 +35,20 @@ class WC_Logger {
*/ */
public function __destruct() { public function __destruct() {
foreach ( $this->_handles as $handle ) { foreach ( $this->_handles as $handle ) {
@fclose( $handle ); if ( is_resource( $handle ) ) {
fclose( $handle );
}
} }
} }
/** /**
* Open log file for writing. * Open log file for writing.
* *
* @access private
*
* @param string $handle * @param string $handle
* @param string $mode * @param string $mode
*
* @return bool success * @return bool success
*/ */
private function open( $handle, $mode = 'a' ) { protected function open( $handle, $mode = 'a' ) {
if ( isset( $this->_handles[ $handle ] ) ) { if ( isset( $this->_handles[ $handle ] ) ) {
return true; return true;
} }
@ -61,6 +60,23 @@ class WC_Logger {
return false; return false;
} }
/**
* Close a handle.
*
* @param string $handle
* @return bool success
*/
protected function close( $handle ) {
$result = false;
if ( is_resource( $this->_handles[ $handle ] ) ) {
$result = fclose( $this->_handles[ $handle ] );
unset( $this->_handles[ $handle ] );
}
return $result;
}
/** /**
* Add a log entry to chosen file. * Add a log entry to chosen file.
* *
@ -74,7 +90,7 @@ class WC_Logger {
if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) { if ( $this->open( $handle ) && is_resource( $this->_handles[ $handle ] ) ) {
$time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time $time = date_i18n( 'm-d-Y @ H:i:s -' ); // Grab Time
$result = @fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" ); $result = fwrite( $this->_handles[ $handle ], $time . " " . $message . "\n" );
} }
do_action( 'woocommerce_log_add', $handle, $message ); do_action( 'woocommerce_log_add', $handle, $message );
@ -93,9 +109,7 @@ class WC_Logger {
$result = false; $result = false;
// Close the file if it's already open. // Close the file if it's already open.
if ( is_resource( $this->_handles[ $handle ] ) ) { $this->close( $handle );
@fclose( $handle );
}
/** /**
* $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file, * $this->open( $handle, 'w' ) == Open the file for writing only. Place the file pointer at the beginning of the file,