Skip to content

Commit

Permalink
Callable via get,post or cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Gundars committed Nov 12, 2016
0 parents commit 46e0bce
Show file tree
Hide file tree
Showing 4 changed files with 203 additions and 0 deletions.
122 changes: 122 additions & 0 deletions Heartbeat.php
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");
}
}
52 changes: 52 additions & 0 deletions README.md
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);
```
20 changes: 20 additions & 0 deletions composer.json
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"
}
}
9 changes: 9 additions & 0 deletions load.php
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);

0 comments on commit 46e0bce

Please sign in to comment.