Skip to content

Commit

Permalink
add two Reflection util methods: propSet & propGet
Browse files Browse the repository at this point in the history
add @method tags for plugin-provided methods
move varDump method to Plugin/Method/Basic with other logging methods
unit tests: remove ModifyTests .. move to EVENT_STREAM_WRAP subscriber  (never writes to file)
  • Loading branch information
bkdotcom committed Aug 22, 2023
1 parent a57af22 commit 28d6e3c
Show file tree
Hide file tree
Showing 63 changed files with 455 additions and 440 deletions.
5 changes: 2 additions & 3 deletions src/Debug/AbstractDebug.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,15 @@ public function __call($methodName, $args)
/**
* Magic method to allow us to call instance methods statically
*
* Prefix the instance method with an underscore ie
* \bdk\Debug::_log('logged via static method');
*
* @param string $methodName Inaccessible method name
* @param array $args Arguments passed to method
*
* @return mixed
*/
public static function __callStatic($methodName, $args)
{
// prior to v3.1 it was required to have underscore prefix to disambiguate from instance method
// as of v3.1, all methodss provided via plugin
$methodName = \ltrim($methodName, '_');
if (!self::$instance && $methodName === 'setCfg') {
/*
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/CurlHttpMessageMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct($cfg = array(), Debug $debug = null)
{
$this->setCfg($cfg);
if (!$debug) {
$debug = Debug::_getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon']));
$debug = Debug::getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon']));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon']));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/DoctrineLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class DoctrineLogger implements SQLLogger
public function __construct(Connection $connection = null, Debug $debug = null)
{
if (!$debug) {
$debug = Debug::_getChannel('Doctrine', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('Doctrine', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('Doctrine', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/GuzzleMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public function __construct($cfg = array(), Debug $debug = null)
{
$this->setCfg($cfg);
if (!$debug) {
$debug = Debug::_getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon']));
$debug = Debug::getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon']));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel($this->cfg['label'], array('channelIcon' => $this->cfg['icon']));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/MySqli.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function __construct($host = null, $username = null, $passwd = null, $dbn
: array();
$this->doConstruct($params);
if (!$debug) {
$debug = Debug::_getChannel('MySqli', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('MySqli', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('MySqli', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/OAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function __construct($consumerKey, $consumerSecret, $signatureMethod = OA
{
parent::__construct($consumerKey, $consumerSecret, $signatureMethod, $authType);
if ($debug === null) {
$debug = Debug::_getChannel('OAuth', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('OAuth', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('OAuth', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/Pdo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Pdo extends PdoBase
public function __construct(PdoBase $pdo, Debug $debug = null)
{
if (!$debug) {
$debug = Debug::_getChannel('PDO', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('PDO', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('PDO', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/PhpCurlClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function __construct($options = array(), Debug $debug = null)
{
$this->debugOptions = \array_merge($this->debugOptions, $options);
if (!$debug) {
$debug = Debug::_getChannel($this->debugOptions['label'], array('channelIcon' => $this->icon));
$debug = Debug::getChannel($this->debugOptions['label'], array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel($this->debugOptions['label'], array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/SimpleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class SimpleCache implements CacheInterface
public function __construct(CacheInterface $cache, Debug $debug = null)
{
if (!$debug) {
$debug = Debug::_getChannel('SimpleCache', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('SimpleCache', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('SimpleCache', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/SoapClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class SoapClient extends SoapClientBase
public function __construct($wsdl, $options = array(), Debug $debug = null)
{
if (!$debug) {
$debug = Debug::_getChannel('Soap', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('Soap', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('Soap', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/SwiftMailerLogger.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class SwiftMailerLogger implements Swift_Events_CommandListener, Swift_Events_Re
public function __construct(Debug $debug = null)
{
if (!$debug) {
$debug = Debug::_getChannel('SwiftMailer', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('SwiftMailer', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('SwiftMailer', array('channelIcon' => $this->icon));
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Collector/TwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TwigExtension extends ProfilerExtension
public function __construct(Debug $debug = null, Profile $profile = null)
{
if (!$debug) {
$debug = Debug::_getChannel('Twig', array('channelIcon' => $this->icon));
$debug = Debug::getChannel('Twig', array('channelIcon' => $this->icon));
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('Twig', array('channelIcon' => $this->icon));
}
Expand Down
13 changes: 7 additions & 6 deletions src/Debug/ComposerScripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,28 @@ public static function updatePhpcsXml($inclSlevomat = true)
*/
private static function installDependencies()
{
$composer = $GLOBALS['argv'][0];
$info = array(
'haveSlevomat' => false,
);
$isCi = \filter_var(\getenv('CI'), FILTER_VALIDATE_BOOLEAN);
PHP_VERSION_ID >= 80000
// need a newer version to avoid ReturnTypeWillChange fatal
// v 2.0 requires php 7.0
? \exec('composer require twig/twig ~3.1 --dev --no-scripts')
: \exec('composer require twig/twig ~1.42 --dev --no-scripts');
? \exec($composer . ' require twig/twig ~3.1 --dev --no-scripts')
: \exec($composer . ' require twig/twig ~1.42 --dev --no-scripts');
if (PHP_VERSION_ID >= 50500) {
\exec('composer require guzzlehttp/guzzle --dev --no-scripts');
\exec($composer . ' require guzzlehttp/guzzle --dev --no-scripts');
}
if (PHP_VERSION_ID >= 70000) {
\exec('composer require psr/http-server-middleware --dev --no-scripts');
\exec('composer require mindplay/middleman --dev --no-scripts');
\exec($composer . ' require psr/http-server-middleware --dev --no-scripts');
\exec($composer . ' require mindplay/middleman --dev --no-scripts');
}
if ($isCi) {
return $info;
}
if (PHP_VERSION_ID >= 70200) {
\exec('composer require slevomat/coding-standard ^8.9.0 --dev --no-scripts');
\exec($composer . ' require slevomat/coding-standard ^8.9.0 --dev --no-scripts');
$info['haveSlevomat'] = true;
}
return $info;
Expand Down
27 changes: 27 additions & 0 deletions src/Debug/Debug.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,33 @@
/**
* Web-browser/javascript like console class for PHP
*
* @method $this alert(string $message, string $level = error, bool $dismissible = false)
* @method $this assert(bool $assertion, mixed ...$msg = null)
* @method $this clear(int $bitmask = bdk\Debug::CLEAR_LOG)
* @method $this count($label = null, int $flags = null)
* @method $this countReset(mixed $label = null, int $flags = null)
* @method $this error(mixed ...$arg)
* @method $this group(mixed ...$arg)
* @method $this groupCollapsed(mixed ...$arg)
* @method $this groupEnd(mixed $value = bdk\Debug\Abstraction\Abstracter::UNDEFINED)
* @method $this groupSummary(int $priority = 0)
* @method $this groupUncollapse()
* @method $this info(mixed ...$arg)
* @method $this log(mixed ...$arg)
* @method $this profile(string $name = null)
* @method $this profileEnd(string $name = null)
* @method $this table(mixed ...$arg)
* @method $this time(string $label = null, float $duration = null)
* @method $this timeEnd(string $label = null, bool $log = true, bool $return = auto)
* @method $this|float|false timeGet(string $label = null, bool $log = true, bool $return = auto)
* @method $this timeLog(string $label = null, mixed ...$args = null)
* @method $this trace(bool $inclContext = false, string $caption = trace)
* @method $this warn(mixed ...$arg)
* @method string output()
*
* @method Abstraction|string prettify(string $string, string $contentType)
* @method bool email($toAddr, $subject, $body)
* @method array errorStats()
* @method string getInterface()
* @method string getResponseCode()
* @method array|string getResponseHeader($header = 'Content-Type', $delimiter = ', ')
Expand All @@ -31,9 +56,11 @@
* @method bool hasLog()
* @method void obEnd()
* @method void obStart()
* @method Abstraction|string prettify($string, $contentType)
* @method mixed redact($val, $key = null)
* @method string requestId()
* @method void setErrorCaller(array $callerInfo)
* @method void varDump(mixed ...$arg)
*
* @property Abstracter $abstracter lazy-loaded Abstracter instance
* @property \bdk\Debug\Utility\ArrayUtil $arrayUtil lazy-loaded array utilitys
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Framework/Laravel/CacheEventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function __construct($options = array(), Debug $debug = null)
'channelShow' => false,
);
if (!$debug) {
$debug = Debug::_getChannel('cache', $channelOptions);
$debug = Debug::getChannel('cache', $channelOptions);
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('cache', $channelOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Framework/Laravel/EventsSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(Debug $debug = null)
'channelShow' => false,
);
if (!$debug) {
$debug = Debug::_getChannel('events', $channelOptions);
$debug = Debug::getChannel('events', $channelOptions);
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('events', $channelOptions);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Framework/Slim2.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Slim2
public function __construct(Debug $debug = null, $prevWriter = null)
{
if (!$debug) {
$debug = Debug::_getChannel('Slim');
$debug = Debug::getChannel('Slim');
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('Slim');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Framework/Yii1_1/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ public function init()
return;
}
/*
Since Yii doesn't use namespaces, we can usually use Debug::_log()
Since Yii doesn't use namespaces, we can usually use Debug::log()
*/
if (\class_exists('Debug') === false) {
\class_alias('bdk\Debug', 'Debug');
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Framework/Yii1_1/LogRoute.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class LogRoute extends CLogRoute
public function __construct(Debug $debug = null, $opts = array())
{
if (!$debug) {
$debug = Debug::_getChannel('Yii');
$debug = Debug::getChannel('Yii');
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('Yii');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Framework/Yii2/LogTarget.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class LogTarget extends Target
public function __construct(Debug $debug = null, $config = array())
{
if (!$debug) {
$debug = Debug::_getChannel('Yii');
$debug = Debug::getChannel('Yii');
} elseif ($debug === $debug->rootInstance) {
$debug = $debug->getChannel('Yii');
}
Expand Down
2 changes: 1 addition & 1 deletion src/Debug/Plugin/ConfigEvents.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ private function onCfgEnableProfiling($val, $key, Event $event)
static::$profilingEnabled = true;
FileStreamWrapper::setEventManager($this->debug->eventManager);
FileStreamWrapper::setPathsExclude(array(
\realpath(__DIR__ . '/../'),
__DIR__ . '/../',
));
FileStreamWrapper::register();
}
Expand Down
64 changes: 64 additions & 0 deletions src/Debug/Plugin/Method/Basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use bdk\Debug\LogEntry;
use bdk\Debug\Plugin\CustomMethodTrait;
use bdk\ErrorHandler\Error;
use bdk\PubSub\Event;
use bdk\PubSub\SubscriberInterface;

/**
Expand All @@ -25,11 +26,15 @@ class Basic implements SubscriberInterface
{
use CustomMethodTrait;

private $cliOutputStream = null;
private $isCli = false;

protected $methods = array(
'assert',
'error',
'info',
'log',
'varDump',
'warn',
);

Expand Down Expand Up @@ -104,6 +109,17 @@ public function info()
return $this->debug;
}

/**
* {@inheritDoc}
*/
public function getSubscriptions()
{
return array(
Debug::EVENT_BOOTSTRAP => 'onBootstrap',
Debug::EVENT_CUSTOM_METHOD => 'onCustomMethod',
);
}

/**
* Log general information
*
Expand Down Expand Up @@ -134,6 +150,54 @@ public function log()
return $this->debug;
}

/**
* Debug::EVENT_BOOTSTRAP subscriber
*
* @param Event $event Debug::EVENT_BOOTSTRAP Event instance
*
* @return void
*/
public function onBootstrap(Event $event)
{
$debug = $event->getSubject();
$this->isCli = $debug->isCli(false); // are we a cli app? (disregard PSR7 ServerRequest obj)
if ($this->isCli) {
$this->cliOutputStream = STDERR;
}
}

/**
* Dump values to output
*
* Similar to php's `var_dump()`. Dump values immediately
*
* @param mixed $arg,... message / values
*
* @return void
*/
public function varDump()
{
$isCli = $this->isCli;
$dumper = $this->debug->getDump($isCli ? 'textAnsi' : 'text');
$args = \array_map(static function ($val) use ($dumper, $isCli) {
$new = $dumper->valDumper->dump($val);
if ($isCli) {
$dumper->valDumper->escapeReset = "\e[0m";
}
$dumper->valDumper->setValDepth(0);
return $new;
}, \func_get_args());
$glue = \func_num_args() > 2
? ', '
: ' = ';
$outStr = \implode($glue, $args);
if ($isCli) {
\fwrite($this->cliOutputStream, $outStr . "\n");
return;
}
echo '<pre style="margin:.25em;">' . $outStr . '</pre>' . "\n";
}

/**
* Log a warning
*
Expand Down
4 changes: 2 additions & 2 deletions src/Debug/Plugin/Method/Count.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __construct()
*
* @param mixed $label Label. If omitted, logs the number of times `count()` has been called at this particular line.
* @param int $flags (optional) A bitmask of
* \bdk\Debug::COUNT_NO_INC` : don't increment the counter
* `\bdk\Debug::COUNT_NO_INC` : don't increment the counter
* (ie, just get the current count)
* \bdk\Debug::COUNT_NO_OUT` : don't output/log
* `\bdk\Debug::COUNT_NO_OUT` : don't output/log
*
* @return int The new count (or current count when using `COUNT_NO_INC`)
*/
Expand Down
Loading

0 comments on commit 28d6e3c

Please sign in to comment.