From d1f670a8a82a03f04c11d5b2cc4d0c8baffa6e72 Mon Sep 17 00:00:00 2001 From: Abel Date: Thu, 17 Jan 2019 17:05:19 +0100 Subject: [PATCH] Initial Commit --- .gitattributes | 2 + .gitignore | 22 ++ LICENSE | 21 ++ README.md | 60 +++++ composer.json | 29 +++ src/FuzeWorks/GitTracyBridge.php | 261 ++++++++++++++++++++++ src/FuzeWorks/LoggerTracyBridge.php | 95 ++++++++ src/FuzeWorks/TracyComponent.php | 183 +++++++++++++++ src/FuzeWorks/layout.tracyloggerpanel.php | 82 +++++++ src/FuzeWorks/layout.tracyloggertab.php | 43 ++++ 10 files changed, 798 insertions(+) create mode 100644 .gitattributes create mode 100644 .gitignore create mode 100644 LICENSE create mode 100644 README.md create mode 100644 composer.json create mode 100644 src/FuzeWorks/GitTracyBridge.php create mode 100644 src/FuzeWorks/LoggerTracyBridge.php create mode 100644 src/FuzeWorks/TracyComponent.php create mode 100644 src/FuzeWorks/layout.tracyloggerpanel.php create mode 100644 src/FuzeWorks/layout.tracyloggertab.php diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..d1564d7 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,2 @@ +.gitattributes export-ignore +.gitignore export-ignore \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..86166a3 --- /dev/null +++ b/.gitignore @@ -0,0 +1,22 @@ +# Ignore Coffee/Espresso. We only want chocolate +FuzeWorks.esproj/* + +# Ignore PHP Storm +.idea/ + +# Development files +test.php + +# Generic Files +*~ +*.lock +*.DS_Store +*.swp +*.out + +# Build Files +vendor/ +build/ +doc +nbproject +._* diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..82fedfb --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2013-2019 TechFuze + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5c1b8bd --- /dev/null +++ b/README.md @@ -0,0 +1,60 @@ +FuzeWorks - Readme +=================== + +Version 1.2.0 + +A versatile PHP Framework built to perform. + +https://techfuze.net/fuzeworks + +Summary +------- + +FuzeWorks is a flexible PHP Framework made for the requirements of todays web. +For a summary of features, list of requirements, and installation instructions, +please see the documentation in the ./doc/ folder or at http://techfuze.net/fuzeworks + +Copyright +--------- + +Copyright © 2013 onwards -- TechFuze + +Certain libraries are copyrighted by their respective authors; +see the full copyright list for details. + +For full copyright information, please see ./doc/copyright.html + +License +------- + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +Licensing of current contributions +---------------------------------- + +Beginning on 2018-04-17, new contributions to this codebase are all licensed +under terms compatible with the MIT license. FuzeWorks is currently +transitioning older code to the MIT License, but work is not yet complete. + +Enjoy! +------ + +TechFuze \ No newline at end of file diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..dc16a50 --- /dev/null +++ b/composer.json @@ -0,0 +1,29 @@ +{ + "name": "fuzeworks/tracycomponent", + "description": "FuzeWorks Framework Tracy Debugger Component", + "homepage": "https://techfuze.net/fuzeworks", + "license": ["MIT"], + "authors": [ + { + "name": "TechFuze", + "homepage": "https://techfuze.net" + }, + { + "name": "FuzeWorks Community", + "homepage": "https://techfuze.net/fuzeworks/contributors" + } + ], + "require": { + "php": ">=7.1.0", + "tracy/tracy": "2.5.*", + "fuzeworks/core": "dev-development" + }, + "require-dev": { + "phpunit/phpunit": "^7" + }, + "autoload": { + "psr-4": { + "FuzeWorks\\": "src/FuzeWorks/" + } + } +} \ No newline at end of file diff --git a/src/FuzeWorks/GitTracyBridge.php b/src/FuzeWorks/GitTracyBridge.php new file mode 100644 index 0000000..9ae1f8d --- /dev/null +++ b/src/FuzeWorks/GitTracyBridge.php @@ -0,0 +1,261 @@ +addPanel($class); + } + + /** + * Renders HTML code for custom tab. + * + * @return string + */ + public function getTab(): string + { + $style = ''; + if ($this->getBranchName() === 'master' || $this->getBranchName() === 'staging') { + $style = 'background:#dd4742;color:white;padding:3px 4px 4px'; + } + $icon = ''; + $label = ''.$this->getBranchName().''; + + return $icon.$label; + } + + /** + * Renders HTML code for custom panel. + * + * @return string + */ + public function getPanel(): string + { + if ($this->isUnderVersionControl()) { + $title = '

GIT

'; + $warning = ''; + $cntTable = ''; + + if ($this->getBranchName() === 'master' || $this->getBranchName() === 'staging') { + $warning = '

You are working in '.$this->getBranchName().' branch

'; + } + + // commit message + if ($this->getLastCommitMessage() !== null) { + $cntTable .= 'Last commit '.$this->getLastCommitMessage().' '; + } + + // heads + if ($this->getHeads() !== null) { + $cntTable .= 'Branches '.$this->getHeads().' '; + } + + // remotes + if ($this->getRemotes() !== null) { + $cntTable .= 'Remotes '.$this->getRemotes().' '; + } + + // tags + if ($this->getTags() !== null && !empty($this->getTags())) { + $cntTable .= 'Tags '.$this->getTags().' '; + } + + $content = '
'. + $cntTable. + '
'; + + return $title.$warning.$content; + } + + return ""; + } + + protected function getBranchName(): string + { + $dir = $this->getDirectory(); + + $head = $dir.'/.git/HEAD'; + if ($dir && is_readable($head)) { + $branch = file_get_contents($head); + if (strpos($branch, 'ref:') === 0) { + $parts = explode('/', $branch); + + return substr($parts[2], 0, -1); + } + + return '('.substr($branch, 0, 7).'…)'; + } + + return 'not versioned'; + } + + protected function getLastCommitMessage() + { + $dir = $this->getDirectory(); + + $fileMessage = $dir.'/.git/COMMIT_EDITMSG'; + + if ($dir && is_readable($fileMessage)) { + $message = file_get_contents($fileMessage); + + return $message; + } + + return null; + } + + protected function getHeads() + { + $dir = $this->getDirectory(); + + $files = scandir($dir.'/.git/refs/heads'); + $message = ''; + + if ($dir && is_array($files)) { + foreach ($files as $file) { + if ($file !== '.' && $file !== '..') { + if ($file === $this->getBranchName()) { + $message .= ''.$file.' '; + } else { + $message .= $file.'
'; + } + } + } + + return $message; + } + + return null; + } + + protected function getRemotes() + { + $dir = $this->getDirectory(); + + try { + $files = scandir($dir.'/.git/refs/remotes'); + } catch (\ErrorException $e) { + return null; + } + + $message = ''; + + if ($dir && is_array($files)) { + foreach ($files as $file) { + if ($file !== '.' && $file !== '..') { + $message .= $file.' '; + } + } + + return $message; + } + + return null; + } + + protected function getTags() + { + $dir = $this->getDirectory(); + + $files = scandir($dir.'/.git/refs/tags'); + $message = ''; + + if ($dir && is_array($files)) { + foreach ($files as $file) { + if ($file !== '.' && $file !== '..') { + $message .= $file.' '; + } + } + + return $message; + } + + return null; + } + + private function getDirectory(): string + { + $scriptPath = $_SERVER['SCRIPT_FILENAME']; + + $dir = realpath(dirname($scriptPath)); + while ($dir !== false && !is_dir($dir.'/.git')) { + flush(); + $currentDir = $dir; + $dir .= '/..'; + $dir = realpath($dir); + + // Stop recursion to parent on root directory + if ($dir === $currentDir) { + break; + } + } + + return $dir; + } + + private function isUnderVersionControl(): bool + { + $dir = $this->getDirectory(); + $head = $dir.'/.git/HEAD'; + + if ($dir && is_readable($head)) { + return true; + } + + return false; + } + +} \ No newline at end of file diff --git a/src/FuzeWorks/LoggerTracyBridge.php b/src/FuzeWorks/LoggerTracyBridge.php new file mode 100644 index 0000000..85e7776 --- /dev/null +++ b/src/FuzeWorks/LoggerTracyBridge.php @@ -0,0 +1,95 @@ + + * @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net) + */ +class LoggerTracyBridge implements IBarPanel { + + /** + * Register the bar and register the event which will block the screen log + */ + public static function register() + { + // Register the bar object + $class = new self(); + $bar = Debugger::getBar(); + $bar->addPanel($class); + } + + public function getTab(): string + { + ob_start(function () {}); + require dirname(__FILE__) . DS . 'layout.tracyloggertab.php'; + return ob_get_clean(); + } + + public function getPanel(): string + { + // If an error is thrown, log it + $error = error_get_last(); + if ($error !== null) { + $errno = $error['type']; + $errfile = $error['file']; + $errline = $error['line']; + $errstr = $error['message']; + + // Log it! + Logger::errorHandler($errno, $errstr, $errfile, $errline); + } + + // Reverse the logs + $logs = array_reverse(Logger::$logs, true); + + // Parse the panel + ob_start(function () {}); + require dirname(__FILE__) . DS . 'layout.tracyloggerpanel.php'; + return ob_get_clean(); + } + +} \ No newline at end of file diff --git a/src/FuzeWorks/TracyComponent.php b/src/FuzeWorks/TracyComponent.php new file mode 100644 index 0000000..00d9d62 --- /dev/null +++ b/src/FuzeWorks/TracyComponent.php @@ -0,0 +1,183 @@ + + * @copyright Copyright (c) 2013 - 2019, TechFuze. (http://techfuze.net) + */ +class TracyComponent implements iComponent +{ + + /** + * Whether Tracy is enabled or not + * + * @var bool $enableTracy + */ + protected $enableTracy = true; + + public function getName(): string + { + return 'TracyComponent'; + } + + public function getClasses(): array + { + return []; + } + + public function onAddComponent(Configurator $configurator){} + + /** + * Enables Tracy when requested to do so. Disables FuzeWorks Logger. + * + * @param Factory $container + * @throws Exception\EventException + */ + public function onCreateContainer(Factory $container) + { + // If Tracy should not be enabled, escape + if ($this->enableTracy == false) + { + Logger::logInfo("TracyComponent added but not enabled."); + return; + } + + // Disable screenLog + Events::addListener(function($event){ + $event->setCancelled(true); + }, 'screenLogEvent'); + + // Enable Tracy. Use DEVELOPMENT mode when logger is enabled + if ($container->logger->isEnabled() == true) + Debugger::enable(Debugger::DEVELOPMENT, realpath(Core::$logDir)); + else + Debugger::enable(Debugger::PRODUCTION, realpath(Core::$logDir)); + + // Disable FuzeWorks Logger + $container->logger->disable(); + + // Enable bridges + GitTracyBridge::register(); + LoggerTracyBridge::register(); + } + + /** + * Calls a static method in the Debugger class + * + * @param $method + * @param $arguments + * @return mixed + * @throws InvalidArgumentException + */ + public function __call($method, $arguments) + { + if (!method_exists('\Tracy\Debugger', $method)) + throw new InvalidArgumentException("Could not invoke call on Tracy\Debugger. Method '".$method."' does not exist."); + + return call_user_func_array(['\Tracy\Debugger', $method], $arguments); + } + + /** + * Gets a property from the Debugger class + * + * @param $name + * @return mixed + * @throws InvalidArgumentException + */ + public function __get($name) + { + if (!property_exists('\Tracy\Debugger', $name)) + throw new InvalidArgumentException("Could not get property of Tracy\Debugger. Property does not exist."); + + return Debugger::$$name; + } + + /** + * Sets a property in the Debugger class + * + * @param $name + * @param $value + * @throws InvalidArgumentException + */ + public function __set($name, $value) + { + if (!property_exists('\Tracy\Debugger', $name)) + throw new InvalidArgumentException("Could not get property of Tracy\Debugger. Property does not exist."); + + Debugger::$$name = $value; + } + + /** + * Enable Tracy + * + * Has no effect after container is created + */ + public function enableTracy() + { + $this->enableTracy = true; + } + + /** + * Disable Tracy + * + * Has no effect after container is created + */ + public function disableTracy() + { + $this->enableTracy = false; + } + + /** + * Check whether Tracy will be enabled or not + * + * @return bool + */ + public function isEnabled(): bool + { + return $this->enableTracy; + } +} \ No newline at end of file diff --git a/src/FuzeWorks/layout.tracyloggerpanel.php b/src/FuzeWorks/layout.tracyloggerpanel.php new file mode 100644 index 0000000..6097d5d --- /dev/null +++ b/src/FuzeWorks/layout.tracyloggerpanel.php @@ -0,0 +1,82 @@ + + + +
+

Logger

+ +
+ + + + + + + + + + + + + + $log): ?> + + + + + + + + + + + +
#TypeMessageFileLineTiming
ms
+
+
diff --git a/src/FuzeWorks/layout.tracyloggertab.php b/src/FuzeWorks/layout.tracyloggertab.php new file mode 100644 index 0000000..034eab8 --- /dev/null +++ b/src/FuzeWorks/layout.tracyloggertab.php @@ -0,0 +1,43 @@ + + + + + +Logger + +