diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000000000000000000000000000000000000..116d0053603df29d4d87e1122a3220bfc3c5ce3d --- /dev/null +++ b/.editorconfig @@ -0,0 +1,54 @@ +# This file is for unifying the coding style for different editors and IDEs +# editorconfig.org + +# WordPress Coding Standards +# https://make.wordpress.org/core/handbook/coding-standards/ + +# Configuration for PhpStorm + +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = tab +indent_size = 4 +tab_width = 4 +ij_continuation_indent_size = 4 + +[{*.ctp,*.hphp,*.inc,*.module,*.php,*.php4,*.php5,*.phtml}] +ij_php_align_assignments = true +ij_php_align_class_constants = true +ij_php_align_key_value_pairs = true +ij_php_align_phpdoc_comments = true +ij_php_align_phpdoc_param_names = true +ij_php_class_brace_style = end_of_line +ij_php_comma_after_last_array_element = true +ij_php_else_if_style = combine +ij_php_force_short_declaration_array_style = true +ij_php_getters_setters_naming_style = snake_case +ij_php_if_brace_force = always +ij_php_lower_case_boolean_const = true +ij_php_lower_case_null_const = true +ij_php_method_brace_style = end_of_line +ij_php_phpdoc_blank_line_before_tags = true +ij_php_phpdoc_blank_lines_around_parameters = true +ij_php_phpdoc_keep_blank_lines = false +ij_php_phpdoc_wrap_long_lines = true +ij_php_space_after_type_cast = true +ij_php_space_after_unary_not = true +ij_php_space_before_unary_not = true +ij_php_spaces_around_var_within_brackets = true +ij_php_spaces_within_array_initializer_braces = true +ij_php_spaces_within_catch_parentheses = true +ij_php_spaces_within_for_parentheses = true +ij_php_spaces_within_if_parentheses = true +ij_php_spaces_within_method_call_parentheses = true +ij_php_spaces_within_method_parentheses = true +ij_php_spaces_within_parentheses = true +ij_php_spaces_within_switch_parentheses = true +ij_php_spaces_within_while_parentheses = true +ij_php_ternary_operation_signs_on_next_line = true +ij_php_variable_naming_style = snake_case diff --git a/.gitignore b/.gitignore index 5e97e71d832bc5817adcc213005eb6136c9f7f2d..87cf4deb550bb1e3ce4eb08ec13dc76818890ee7 100644 --- a/.gitignore +++ b/.gitignore @@ -2,4 +2,5 @@ .idea composer.lock build-coverage -docker-compose.yml \ No newline at end of file +docker-compose.yml +.phpcs-cache diff --git a/CHANGELOG.md b/CHANGELOG.md index d062e4db49794bbd16088468bee810a43632461d..4b9c27ef1272672cbd98d9ae02730d7c9be3fa65 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,10 @@ +## [1.7.0] - Unreleased +### Added +- Added `SimpleLoggerFactory` for basic logger use cases. + +### Changed +- Minimum PHP required is now >=7.0 + ## [1.6.2] - 2020-07-21 ### Fixed - Notice error when cannot create log file @@ -45,8 +52,8 @@ ### Changed - setDisableLog changes to disableLog -## [1.2.0] - 2018-10-29 -### Changed +## [1.2.0] - 2018-10-29 +### Changed - getWPDeskFileName renamed to getFileName - isWPDeskLogWorking renamed to isLogWorking ### Added @@ -65,4 +72,4 @@ ## [1.0.0] - 2018-10-28 ### Added - first stable version -- 80% coverage in integration tests \ No newline at end of file +- 80% coverage in integration tests diff --git a/composer.json b/composer.json index dcf1f22296a1438449c7701a14255d4bd70f2ce7..8181625c5f8faa79a76d8f41d3494d04c6de8e20 100644 --- a/composer.json +++ b/composer.json @@ -6,24 +6,28 @@ "email": "krzysiek@wpdesk.pl" } ], - "require": { - "php": ">=5.6", - "psr/log": "^1.0.1", + "config": { + "platform": { + "php": "7.0.19" + }, + "allow-plugins": { + "dealerdirect/phpcodesniffer-composer-installer": true + } + }, + "require": { + "php": ">=7.0|^8", + "psr/log": "^1", "monolog/monolog": "^1.23", "wpdesk/wp-notice": "^3.0" }, "require-dev": { "phpunit/phpunit": "^5", - "wp-coding-standards/wpcs": "^0.14.1", "squizlabs/php_codesniffer": "^3.0.2", - "wimg/php-compatibility": "^8" + "wpdesk/wp-code-sniffer": "^1.2.3" }, "autoload": { - "classmap": ["src/deprecated"], "psr-4": {"WPDesk\\Logger\\": "src/"} }, - "autoload-dev": { - }, "scripts": { "phpunit-unit": "phpunit --configuration phpunit-unit.xml --coverage-text --colors=never", "phpunit-unit-fast": "phpunit --configuration phpunit-unit.xml --no-coverage", diff --git a/phpcs.xml.dist b/phpcs.xml.dist new file mode 100644 index 0000000000000000000000000000000000000000..78b4b745393846d2e5a79ae9458f3c92a13048b6 --- /dev/null +++ b/phpcs.xml.dist @@ -0,0 +1,48 @@ +<?xml version="1.0"?> +<ruleset name="WordPress Coding Standards for WP Desk Plugin"> + + <!-- + ############################################################################# + COMMAND LINE ARGUMENTS + https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml + ############################################################################# + --> + + <!-- Scan all files. --> + <file>./src</file> + + <!-- Only check PHP files. --> + <arg name="extensions" value="php"/> + + <!-- Show progress, show the error codes for each message (source). --> + <arg value="sp"/> + + <!-- Check up to 8 files simultaneously. --> + <arg name="parallel" value="8"/> + + <!-- Cache outcomes for better performance. Remember to add the file to .gitignore. --> + <arg name="cache" value="./.phpcs-cache"/> + + <!-- + ############################################################################# + USE THE WPDeskCS RULESET + ############################################################################# + --> + + <!-- Define plugin text domain for i18n. --> + <config name="text_domain" value="wpdesk-helper"/> + + <!-- This value should be aligned with WordPress support version declared in plugin header --> + <config name="minimum_supported_wp_version" value="5.0"/> + + <!-- Set value aligned with supported PHP Version for PHPCompatibilityWP check. --> + <config name="testVersion" value="7.0-"/> + + <rule ref="WPDeskPlugin"/> + + <!-- + ############################################################################## + CUSTOM RULES + ############################################################################## + --> +</ruleset> diff --git a/src/Settings.php b/src/Settings.php new file mode 100644 index 0000000000000000000000000000000000000000..07fa9788b75581684c2f736db2c6da716100f3cb --- /dev/null +++ b/src/Settings.php @@ -0,0 +1,18 @@ +<?php + +namespace WPDesk\Logger; + +use Psr\Log\LogLevel; + +final class Settings { + + /** @var string */ + public $level = LogLevel::DEBUG; + + /** @var bool */ + public $use_wc_log = true; + + /** @var bool */ + public $use_wp_log = true; + +} diff --git a/src/SimpleLoggerFactory.php b/src/SimpleLoggerFactory.php new file mode 100644 index 0000000000000000000000000000000000000000..e815c8be23f8ab18f1ed8473f8e2a2f6c44ec9c5 --- /dev/null +++ b/src/SimpleLoggerFactory.php @@ -0,0 +1,63 @@ +<?php +declare(strict_types=1); + +namespace WPDesk\Logger; + +use Monolog\Handler\HandlerInterface; +use Monolog\Handler\NullHandler; +use Monolog\Logger; +use Monolog\Handler\ErrorLogHandler; +use WPDesk\Logger\WC\WooCommerceHandler; + +final class SimpleLoggerFactory implements LoggerFactory { + + /** @var Settings */ + private $options; + + /** @var string */ + private $channel; + + /** @var Logger */ + private $logger; + + public function __construct( string $channel, Settings $options = null ) { + $this->channel = $channel; + $this->options = $options ?? new Settings(); + } + + public function getLogger( string $channel, Settings $options = null ): Logger { + if ( $this->logger ) { + return $this->logger; + } + + $logger = new Logger( $this->channel ); + + $wc_handler = $this->get_wc_handler(); + if ( $this->options->use_wc_log ) { + $logger->pushHandler( $wc_handler ); + } + + if ( $this->options->use_wp_log || $wc_handler instanceof NullHandler ) { + $logger->pushHandler( $this->get_wp_handler() ); + } + + return $this->logger = $logger; + } + + private function get_wc_handler(): HandlerInterface { + if ( function_exists( 'wc_get_logger' ) ) { + return new WooCommerceHandler( wc_get_logger(), $this->options->level ); + } + + return new NullHandler(); + } + + private function get_wp_handler(): HandlerInterface { + if ( defined( 'WP_DEBUG_LOG' ) && WP_DEBUG_LOG ) { + return new ErrorLogHandler( ErrorLogHandler::OPERATING_SYSTEM, $this->options->level ); + } + + return new NullHandler(); + } + +}