Skip to content

Commit

Permalink
Make use of wecodemore/wordpress-early-hook
Browse files Browse the repository at this point in the history
  • Loading branch information
gmazzap committed Mar 1, 2023
1 parent 1ce492e commit 6038a2d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 36 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@
}
],
"require": {
"php": ">=7.2",
"php": ">=7.2 < 8.3",
"psr/log": "^1.1.4",
"wecodemore/wordpress-early-hook": "^1.0.0",
"monolog/monolog": "^2.3.5"
},
"require-dev": {
Expand Down
42 changes: 9 additions & 33 deletions inc/bootstrap.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?php // phpcs:disable PSR1.Files.SideEffects.FoundWithSymbols
<?php

/**
* This file is part of the Wonolog package.
Expand All @@ -13,7 +13,9 @@

namespace Inpsyde\Wonolog;

use Psr\Log\LoggerInterface;
use Psr\Log\{LoggerInterface, NullLogger};

use function WeCodeMore\earlyAddAction;

// We want to load this file just once.
// Being loaded by Composer autoload, and being in WP context, we have to put special care on this.
Expand All @@ -35,45 +37,19 @@ function makeLogger(?string $forChannel = null): LoggerInterface
return $loggerFactory($forChannel);
}

if (!$actionAdded && function_exists('add_action')) {
$actionAdded = add_action(
if (!$actionAdded) {
earlyAddAction(
Configurator::ACTION_LOADED,
/** @param callable(?string):LoggerInterface $factory */
static function (callable $factory) use (&$loggerFactory): void {
$loggerFactory = $factory;
},
PHP_INT_MIN
);
$actionAdded = true;
}

return new \Psr\Log\NullLogger();
return new NullLogger();
}

(static function () {
$hook = 'muplugins_loaded';
$priority = PHP_INT_MIN;
$callback = [Configurator::new(), 'setup'];

$addActionExists = function_exists('add_action');
if ($addActionExists || defined('ABSPATH')) {
if (!$addActionExists) {
require_once ABSPATH . 'wp-includes/plugin.php';
}

add_action($hook, $callback, $priority);

return;
}
/**
* If here, this file is loaded very early, probably _too_ early, before ABSPATH is defined.
* Only option we have is to "manually" write in global `$wp_filter` array.
*/
global $wp_filter;
is_array($wp_filter) or $wp_filter = [];
is_array($wp_filter[$hook] ?? null) or $wp_filter[$hook] = [];
/** @psalm-suppress MixedArrayAssignment */
is_array($wp_filter[$hook][$priority] ?? null) or $wp_filter[$hook][$priority] = [];
$callbackId = spl_object_hash($callback[0]) . 'setup';
/** @psalm-suppress MixedArrayAssignment */
$wp_filter[$hook][$priority][$callbackId] = ['function' => $callback, 'accepted_args' => 0];
})();
earlyAddAction('muplugins_loaded', [Configurator::new(), 'setup'], PHP_INT_MIN, 0);
4 changes: 4 additions & 0 deletions phpcs.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
</properties>
</rule>

<rule ref="PSR1.Files.SideEffects.FoundWithSymbols">
<exclude-pattern>./inc/*.php</exclude-pattern>
</rule>

<rule ref="Inpsyde.CodeQuality.Psr4">
<properties>
<property
Expand Down
4 changes: 2 additions & 2 deletions src/Configurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -813,9 +813,9 @@ public function setup(): void
* Fires right after Wonolog has been set up.
*
* Passes a factory that creates an PSR-3 LoggerInterface that can be injected into any
* object that can make use of it, e. g. any object implementing `LoggerAwareInterface`.
* object that can make use of it, e.g. any object implementing `LoggerAwareInterface`.
* This hook can only be used in MU plugins.
* Plugins/themes/etc can use `makeLogger()` if they need a PSR-3 compliant logger.
* Plugins/themes/etc. can use `makeLogger()` if they need a PSR-3 compliant logger.
*/
do_action(self::ACTION_LOADED, $psr3Factory);
remove_all_actions(self::ACTION_LOADED);
Expand Down

0 comments on commit 6038a2d

Please sign in to comment.