From 46e0bce0737a787d5c5e7b9dbadc8b22ad226e54 Mon Sep 17 00:00:00 2001 From: SB0M0OYR Date: Sat, 12 Nov 2016 03:16:19 +0000 Subject: [PATCH] Callable via get,post or cli --- Heartbeat.php | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++ README.md | 52 +++++++++++++++++++++ composer.json | 20 +++++++++ load.php | 9 ++++ 4 files changed, 203 insertions(+) create mode 100644 Heartbeat.php create mode 100644 README.md create mode 100644 composer.json create mode 100644 load.php diff --git a/Heartbeat.php b/Heartbeat.php new file mode 100644 index 0000000..5ef1db1 --- /dev/null +++ b/Heartbeat.php @@ -0,0 +1,122 @@ +verbose = $verbose; + + return $this; + } + + /** + * @param string $path + * @param string $vendor + */ + public function load($path = '', $die = false) + { + $timerStart = microtime(true); + $path = realpath(rtrim($path, '\/\\') ?: static::DEFAULT_ROOT_PATH); + + if (!$path) { + echo "Unable to find path: {$path}\n"; + die("Exiting\n"); + } else { + echo "Scanning $path\n"; + } + $this->scanDir($path); + + echo " in: " + . $this->secondsToTime(round(microtime(true) - $timerStart, 0)) + . "\n"; + + if ($die) { + die(); + } + } + + /** + * @param $path + */ + private function scanDir($path) + { + if (!$this->isDir($path)) { + die('Error: NOT A DIR: ' . $path); + } + + return $this->includeFiles($path); + } + + /** + * @param $path + * @param bool $verbose + * @return bool|string + */ + private function isDir($path, $verbose = false) + { + if ($verbose) { + return is_dir($path) ? 'Dir' : 'NOT A DIR'; + } + + return is_dir($path); + } + + /** + * @verbose string $path + * @return bool + */ + private function includeFiles($path) + { + $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path)); + + $count = 0; + foreach ($objects as $name => $object) { + if ($object->getFileName() === '.' || $object->getFileName() === '..') { + continue; + } + $count++; + if ($this->verbose) { + echo str_pad($count, 10) . " Require: $name\n"; + } + $read = file_get_contents($name); + } + + echo "$count files loaded"; + + return ($count > 0); + } + + /** + * @param $seconds + * @return string + */ + private function secondsToTime($seconds) + { + $dtF = new DateTime('@0'); + $dtT = new DateTime("@$seconds"); + + return $dtF->diff($dtT)->format("%hh %im %ss"); + } +} diff --git a/README.md b/README.md new file mode 100644 index 0000000..f343df5 --- /dev/null +++ b/README.md @@ -0,0 +1,52 @@ +# Heartbeat +###### Includes - cache warms - all files found under specified path + +##### Install +``` +composer require gundars/heartbeat ^0.1 +``` + +##### Call in CLI + +``` +> php vendor/gundars/heartbeat/load.php + +Scanning /var/www/public/zend/approot +10102 files loaded in: 0h 2m 49s +``` + +##### Call with input parameter +Paste this code in your index.php: + +``` +use Heartbeat\Heartbeat; +``` +``` +if (array_key_exists('heartbeat', $_REQUEST)) { + $heartBeat = new Heartbeat(); + $heartBeat->load(__DIR__); +} +``` +and call via http(s) `http://example.com/?heartbeat=true` + +##### Manual Loading +``` +->load(__DIR__ . '/../../../'); +``` + +##### Verbose +Prints all included files +``` +$heartBeat->verbose()->load(__DIR__); +``` + +##### Die +stops script execution after file include is finished +``` +$heartBeat->verbose()->load(__DIR__, true); +``` diff --git a/composer.json b/composer.json new file mode 100644 index 0000000..8026e12 --- /dev/null +++ b/composer.json @@ -0,0 +1,20 @@ +{ + "name": "gundars/heartbeat", + "description": "Cache warms all files found under specified path", + "authors": [ + { + "name": "Gundars", + "email": "me@gundars.me" + } + ], + "license": "MIT", + "autoload": { + "psr-4": { + "Heartbeat\\": "/" + } + }, + "scripts": { + "post-update-cmd": "composer dumpautoload -o", + "post-install-cmd": "composer dumpautoload -o" + } +} diff --git a/load.php b/load.php new file mode 100644 index 0000000..13176b4 --- /dev/null +++ b/load.php @@ -0,0 +1,9 @@ +/*verbose()*/->load(__DIR__ . '/../../../', true);