From e73b7e31b95924a464823ae5b72aa1fc2f8811e8 Mon Sep 17 00:00:00 2001 From: Mark Armendariz Date: Thu, 1 Sep 2016 06:01:15 -0500 Subject: [PATCH] Basic tools I use from project to project --- .gitignore | 4 ++ composer.json | 20 ++++++ composer.lock | 135 ++++++++++++++++++++++++++++++++++++++++ src/Log.php | 136 +++++++++++++++++++++++++++++++++++++++++ src/Timer.php | 79 ++++++++++++++++++++++++ src/Tools.php | 166 ++++++++++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 540 insertions(+) create mode 100644 .gitignore create mode 100644 composer.json create mode 100644 composer.lock create mode 100644 src/Log.php create mode 100644 src/Timer.php create mode 100644 src/Tools.php diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3dec35a --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +node_modules/ +*.iml +.idea/ +vendor/ diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..0a3db0f --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "enobrev/tools", + "description": "General Tools", + "type": "library", + "license": "MIT", + "authors": [ + { + "name": "Mark Armendariz", + "email": "src@enobrev.com" + } + ], + "require": { + "monolog/monolog": "^1.21" + }, + "autoload": { + "psr-0" : { + "Enobrev" : "src" + } + } +} diff --git a/composer.lock b/composer.lock new file mode 100644 index 0000000..d4de258 --- /dev/null +++ b/composer.lock @@ -0,0 +1,135 @@ +{ + "_readme": [ + "This file locks the dependencies of your project to a known state", + "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", + "This file is @generated automatically" + ], + "hash": "a7b9d92b8641d97fe45c1ebffa07f440", + "content-hash": "19e61080c065070bde1a905d1b15a650", + "packages": [ + { + "name": "monolog/monolog", + "version": "1.21.0", + "source": { + "type": "git", + "url": "https://github.com/Seldaek/monolog.git", + "reference": "f42fbdfd53e306bda545845e4dbfd3e72edb4952" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/Seldaek/monolog/zipball/f42fbdfd53e306bda545845e4dbfd3e72edb4952", + "reference": "f42fbdfd53e306bda545845e4dbfd3e72edb4952", + "shasum": "" + }, + "require": { + "php": ">=5.3.0", + "psr/log": "~1.0" + }, + "provide": { + "psr/log-implementation": "1.0.0" + }, + "require-dev": { + "aws/aws-sdk-php": "^2.4.9", + "doctrine/couchdb": "~1.0@dev", + "graylog2/gelf-php": "~1.0", + "jakub-onderka/php-parallel-lint": "0.9", + "php-amqplib/php-amqplib": "~2.4", + "php-console/php-console": "^3.1.3", + "phpunit/phpunit": "~4.5", + "phpunit/phpunit-mock-objects": "2.3.0", + "ruflin/elastica": ">=0.90 <3.0", + "sentry/sentry": "^0.13", + "swiftmailer/swiftmailer": "~5.3" + }, + "suggest": { + "aws/aws-sdk-php": "Allow sending log messages to AWS services like DynamoDB", + "doctrine/couchdb": "Allow sending log messages to a CouchDB server", + "ext-amqp": "Allow sending log messages to an AMQP server (1.0+ required)", + "ext-mongo": "Allow sending log messages to a MongoDB server", + "graylog2/gelf-php": "Allow sending log messages to a GrayLog2 server", + "mongodb/mongodb": "Allow sending log messages to a MongoDB server via PHP Driver", + "php-amqplib/php-amqplib": "Allow sending log messages to an AMQP server using php-amqplib", + "php-console/php-console": "Allow sending log messages to Google Chrome", + "rollbar/rollbar": "Allow sending log messages to Rollbar", + "ruflin/elastica": "Allow sending log messages to an Elastic Search server", + "sentry/sentry": "Allow sending log messages to a Sentry server" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Monolog\\": "src/Monolog" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Jordi Boggiano", + "email": "j.boggiano@seld.be", + "homepage": "http://seld.be" + } + ], + "description": "Sends your logs to files, sockets, inboxes, databases and various web services", + "homepage": "http://github.com/Seldaek/monolog", + "keywords": [ + "log", + "logging", + "psr-3" + ], + "time": "2016-07-29 03:23:52" + }, + { + "name": "psr/log", + "version": "1.0.0", + "source": { + "type": "git", + "url": "https://github.com/php-fig/log.git", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/php-fig/log/zipball/fe0936ee26643249e916849d48e3a51d5f5e278b", + "reference": "fe0936ee26643249e916849d48e3a51d5f5e278b", + "shasum": "" + }, + "type": "library", + "autoload": { + "psr-0": { + "Psr\\Log\\": "" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "PHP-FIG", + "homepage": "http://www.php-fig.org/" + } + ], + "description": "Common interface for logging libraries", + "keywords": [ + "log", + "psr", + "psr-3" + ], + "time": "2012-12-21 11:40:51" + } + ], + "packages-dev": [], + "aliases": [], + "minimum-stability": "stable", + "stability-flags": [], + "prefer-stable": false, + "prefer-lowest": false, + "platform": [], + "platform-dev": [] +} diff --git a/src/Log.php b/src/Log.php new file mode 100644 index 0000000..401176c --- /dev/null +++ b/src/Log.php @@ -0,0 +1,136 @@ +setFormatter($oFormatter); + self::$oLog->pushHandler($oSyslog); + } + + return self::$oLog; + } + + /** + * Adds a log record at the designated level + * + * @param int $iLevel The logging level + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + private static function addRecord($iLevel, $sMessage, array $aContext = array()) { + return self::init()->addRecord($iLevel, $sMessage, array_merge(['action' => $sMessage], $aContext)); + } + + /** + * @param string $sName + */ + public static function setName(string $sName) { + self::$sName = $sName; + } + + /** + * Adds a log record at the DEBUG level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function d($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::DEBUG, $sMessage, $aContext); + } + + /** + * Adds a log record at the INFO level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function i($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::INFO, $sMessage, $aContext); + } + + /** + * Adds a log record at the NOTICE level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function n($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::NOTICE, $sMessage, $aContext); + } + + /** + * Adds a log record at the WARNING level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function w($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::WARNING, $sMessage, $aContext); + } + + /** + * Adds a log record at the ERROR level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function e($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::ERROR, $sMessage, $aContext); + } + + /** + * Adds a log record at the CRITICAL level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function c($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::CRITICAL, $sMessage, $aContext); + } + + /** + * Adds a log record at the ALERT level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function a($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::CRITICAL, $sMessage, $aContext); + } + + /** + * Adds a log record at the EMERGENCY level. + * + * @param string $sMessage The log message + * @param array $aContext The log context + * @return Boolean Whether the record has been processed + */ + public static function em($sMessage, array $aContext = array()) { + self::addRecord(Monolog\Logger::EMERGENCY, $sMessage, $aContext); + } + } \ No newline at end of file diff --git a/src/Timer.php b/src/Timer.php new file mode 100644 index 0000000..20b32bd --- /dev/null +++ b/src/Timer.php @@ -0,0 +1,79 @@ +aTimers = array(); + } + + /** + * @param $sLabel + */ + public function init($sLabel) { + $this->aTimers[$sLabel] = array( + 'start' => 0, + 'stop' => 0 + ); + } + + /** + * @param string $sLabel + * @return array + */ + public function get($sLabel) { + if (isset($this->aTimers[$sLabel]) + && isset($this->aTimers[$sLabel]['start'])) { + $this->aTimers[$sLabel]['range'] = $this->aTimers[$sLabel]['stop'] - $this->aTimers[$sLabel]['start']; + $this->aTimers[$sLabel]['range_human'] = sprintf("%01.2f", $this->aTimers[$sLabel]['range']); + + return $this->aTimers[$sLabel]; + } + } + + /** + * @return array + */ + public function getAll() { + $aReturn = array(); + $aReturn['__total__'] = array( + 'range' => 0, + 'range_human' => "0.00", + 'average' => 0 + ); + foreach (array_keys($this->aTimers) as $sLabel) { + $aReturn[$sLabel] = $this->get($sLabel); + $aReturn['__total__']['range'] += $aReturn[$sLabel]['range']; + } + + $aReturn['__total__']['range_human'] = sprintf("%01.2f", $aReturn['__total__']['range']); + $aReturn['__total__']['average'] = sprintf("%01.2f", $aReturn['__total__']['range'] / count($this->aTimers)); + + return $aReturn; + } + + /** + * @param string $sLabel + */ + public function start($sLabel) { + $this->init($sLabel); + $this->aTimers[$sLabel]['start'] = $this->getTime(); + $this->aTimers[$sLabel]['stop'] = 0; + } + + /** + * @param string $sLabel + */ + public function stop($sLabel) { + $this->aTimers[$sLabel]['stop'] = $this->getTime(); + } + + /** + * @return mixed + */ + private function getTime() { + return microtime(true) * 1000; + } + } +?> \ No newline at end of file diff --git a/src/Tools.php b/src/Tools.php new file mode 100644 index 0000000..8cbb221 --- /dev/null +++ b/src/Tools.php @@ -0,0 +1,166 @@ +', "\n", $sMessage); + $sMessage = str_replace('
',  '', $sMessage);
+            $sMessage = str_replace('
', '', $sMessage); + } + + echo $sMessage; + } + + function dbg(...$aArgs) { + $sTitle = ''; + + if (count($aArgs) > 1) { + $sTitle = array_shift($aArgs) . ': '; + } + + if (strlen($sTitle)) { + if (is_object($aArgs[0]) + || is_array($aArgs[0])) { + _output($sTitle . "
"); + } else { + _output($sTitle); + } + } + + foreach ($aArgs as $mArg) { + if (is_object($mArg) + || is_array($mArg)) { + _output('
' . print_r($mArg, 1) . '
'); + } else { + _output($mArg . "
"); + } + } + } + + /** + * @param bool $bShort + * @param bool $bReturn + * @return array|void + */ + function trace($bShort = true, $bReturn = false) { + $oBacktrace = debug_backtrace(); + + if ($bShort) { + $aResponse = array(); + + foreach ($oBacktrace as $oTrace) { + $sResponse = ''; + + if (isset($oTrace['line'])) { + $sResponse .= $oTrace['line']; + } + + if (isset($oTrace['file'])) { + $sResponse .= ': ' . str_replace(dirname(dirname(dirname(__FILE__))), '', $oTrace['file']); + } + + $sResponse .= ': ' . $oTrace['function']; + $aResponse[] = $sResponse; + } + + if ($bReturn) { + return $aResponse; + } else { + dbg($aResponse); + } + } else { + foreach ($oBacktrace as &$oTrace) { + if (isset($oTrace['object'])) { + $oTrace['object'] = '[OBJECT]'; + } + if (isset($oTrace['args'])) { + foreach ($oTrace['args'] as &$mArg) { + if (is_object($mArg)) { + $mArg = '[OBJECT]'; + } else if (is_array($mArg)) { + $mArg = '[ARRAY]'; + } + } + } + } + + if ($bReturn) { + return $oBacktrace; + } else { + dbg($oBacktrace); + } + } + } + + /** + * @return string + */ + function get_ip() { + if (isset($_SERVER["HTTP_X_REAL_IP"]) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { + $sIP = $_SERVER["HTTP_X_REAL_IP"]; // explicitly set in nginx load balancer + } else if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) { + $sIP = getenv("HTTP_CLIENT_IP"); + } else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) { + $sIP = getenv("HTTP_X_FORWARDED_FOR"); + } else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) { + $sIP = getenv("REMOTE_ADDR"); + } else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) { + $sIP = $_SERVER['REMOTE_ADDR']; + } else { + $sIP = "unknown"; + } + + return $sIP; + } + + /** + * @param string $word + * @return string + */ + function depluralize($word){ + $rules = array( + 'ss' => false, + 'os' => 'o', + 'ies' => 'y', + 'xes' => 'x', + 'oes' => 'o', + 'ves' => 'f', + 's' => '' + ); + + foreach(array_keys($rules) as $key){ + if(substr($word, (strlen($key) * -1)) != $key) + continue; + if($key === false) + return $word; + return substr($word, 0, strlen($word) - strlen($key)) . $rules[$key]; + } + + return $word; + } \ No newline at end of file