-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 46e0bce
Showing
4 changed files
with
203 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<?php | ||
|
||
namespace Heartbeat; | ||
|
||
use DateTime; | ||
use RecursiveDirectoryIterator; | ||
use RecursiveIteratorIterator; | ||
|
||
/** | ||
* Class Heartbeat | ||
* | ||
* @package Heartbeat | ||
*/ | ||
class Heartbeat | ||
{ | ||
/** | ||
* By default can 1 level before vendor, or 3 levels up | ||
*/ | ||
const DEFAULT_ROOT_PATH = '/../../..'; | ||
|
||
private $verbose = false; | ||
|
||
/** | ||
* @param bool $verbose | ||
* @return $this | ||
*/ | ||
public function verbose($verbose = true) | ||
{ | ||
$this->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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
``` | ||
<?php | ||
use Heartbeat\Heartbeat; | ||
$heartBeat = new Heartbeat(); | ||
$heartBeat->->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); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "gundars/heartbeat", | ||
"description": "Cache warms all files found under specified path", | ||
"authors": [ | ||
{ | ||
"name": "Gundars", | ||
"email": "[email protected]" | ||
} | ||
], | ||
"license": "MIT", | ||
"autoload": { | ||
"psr-4": { | ||
"Heartbeat\\": "/" | ||
} | ||
}, | ||
"scripts": { | ||
"post-update-cmd": "composer dumpautoload -o", | ||
"post-install-cmd": "composer dumpautoload -o" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
#require_once __DIR__ . '/Heartbeat.php'; | ||
require __DIR__ . '/../../autoload.php'; | ||
|
||
use Heartbeat\Heartbeat; | ||
|
||
$heartBeat = new Heartbeat(); | ||
$heartBeat->/*verbose()*/->load(__DIR__ . '/../../../', true); |