| 1 |
<?php |
| 2 |
declare(strict_types=1); |
| 3 |
|
| 4 |
namespace Packetery\Module; |
| 5 |
|
| 6 |
use Packetery\Module\Framework\WcAdapter; |
| 7 |
use Packetery\Tracy\Logger; |
| 8 |
|
| 9 |
class WcLogger { |
| 10 |
|
| 11 |
private const LEVEL_ERROR = 'error'; |
| 12 |
|
| 13 |
/** |
| 14 |
* @param string|\Throwable|mixed $message |
| 15 |
* @param string $level |
| 16 |
* @return void |
| 17 |
*/ |
| 18 |
private static function log( $message, string $level ): void { |
| 19 |
$logCallback = static function () use ( $level, $message ): void { |
| 20 |
$container = CompatibilityBridge::getContainer(); |
| 21 |
/** @var WcAdapter $wcAdapter */ |
| 22 |
$wcAdapter = $container->getByType( WcAdapter::class ); |
| 23 |
$wcLogger = $wcAdapter->getLogger(); |
| 24 |
|
| 25 |
$wcLogger->log( |
| 26 |
$level, |
| 27 |
Logger::formatMessage( $message ), |
| 28 |
[ 'source' => 'packeta' ] |
| 29 |
); |
| 30 |
}; |
| 31 |
|
| 32 |
if ( did_action( 'woocommerce_init' ) > 0 ) { |
| 33 |
$logCallback(); |
| 34 |
} else { |
| 35 |
add_action( 'woocommerce_init', $logCallback ); |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* @param string $method |
| 41 |
* @param string $param |
| 42 |
* @param string $expectedType |
| 43 |
* @param mixed $givenValue |
| 44 |
* @return void |
| 45 |
*/ |
| 46 |
public static function logArgumentTypeError( string $method, string $param, string $expectedType, $givenValue ): void { |
| 47 |
self::log( |
| 48 |
sprintf( |
| 49 |
'Method %s expects parameter "%s" to be type of "%s", type "%s" given. %s%s', |
| 50 |
$method, |
| 51 |
$param, |
| 52 |
$expectedType, |
| 53 |
is_object( $givenValue ) ? get_class( $givenValue ) : gettype( $givenValue ), |
| 54 |
PHP_EOL, |
| 55 |
( new \Exception() )->getTraceAsString() |
| 56 |
), |
| 57 |
self::LEVEL_ERROR |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|