Skip to content

Commit

Permalink
Updated tests and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
eldadfux committed Oct 23, 2020
1 parent dcbe373 commit b68d767
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 20 deletions.
90 changes: 80 additions & 10 deletions src/Preloader/Preloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ class Preloader
*/
protected $included = [];

/**
* Constructor
*
* @param string $paths
*/
public function __construct(string ...$paths)
{
$this->paths = $paths;
Expand All @@ -37,6 +42,15 @@ public function __construct(string ...$paths)
);
}

/**
* Paths
*
* Path to load
*
* @param string $paths
*
* @return $this
*/
public function paths(string ...$paths): self
{
$this->paths = \array_merge(
Expand All @@ -47,6 +61,15 @@ public function paths(string ...$paths): self
return $this;
}

/**
* Ignore
*
* Ignore a given path or file
*
* @param string $names
*
* @return $this
*/
public function ignore(string ...$names): self
{
foreach ($names as $name) {
Expand All @@ -60,29 +83,52 @@ public function ignore(string ...$names): self
return $this;
}

/**
* Load
*
* Loads all preloader preconfigured paths and files
*/
public function load(): void
{
$this->included = get_included_files();

foreach ($this->paths as $path) {
$this->loadPath(\rtrim($path, '/'));
}

$already = count($this->included);

$this->count = $already;

//echo "[Preloader] Preloaded {$already} files.".PHP_EOL;
}

/**
* Get Count
*
* Get the total number of loaded files.
*
* @return int
*/
public function getCount(): int
{
return $this->count;
}

/**
* Get List
*
* Get a list of all included paths.
*
* @return array
*/
public function getList(): array
{
return $this->included;
}

/**
* Load Path
*
* Load a specific file or folder and nested folders.
*
* @param string $path
* @return void
*/
private function loadPath(string $path): void
{
if (\is_dir($path)) {
Expand All @@ -94,6 +140,15 @@ private function loadPath(string $path): void
$this->loadFile($path);
}


/**
* Load Directory
*
* Load a specific folder and nested folders.
*
* @param string $path
* @return void
*/
private function loadDir(string $path): void
{
$handle = \opendir($path);
Expand All @@ -109,19 +164,25 @@ private function loadDir(string $path): void
\closedir($handle);
}

/**
* Load File
*
* Load a specific file.
*
* @param string $path
* @return void
*/
private function loadFile(string $path): void
{
if ($this->shouldIgnore($path)) {
return;
}

if (\in_array(\realpath($path), $this->included)) {
// echo "[Preloader] Skiped `{$path}`".PHP_EOL;
echo "[Preloader] Skiped `{$path}`".PHP_EOL;
return;
}

// echo "[Preloader] Preloaded `{$path}`".PHP_EOL;

try {
// opcache_compile_file($path);
require $path;
Expand All @@ -130,9 +191,18 @@ private function loadFile(string $path): void
return;
}

$this->included = array_merge(get_included_files(), [realpath($path)]);
$this->included[] = $path;
$this->count++;
}

/**
* Should Ignore
*
* Should a given path be ignored or not?
*
* @param string $path
* @return bool
*/
private function shouldIgnore(?string $path): bool
{
if ($path === null) {
Expand Down
22 changes: 12 additions & 10 deletions tests/Preloader/PreloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,30 @@ public function testTest()
{
$preloader = new Preloader();

$preloader
//->paths(realpath(__DIR__ . '/../resources'))
// ->paths(realpath(__DIR__ . '/../resources/nested'))
->load();

$preloader->load();

$autoloaded = $preloader->getCount();

$this->assertGreaterThan(30, $autoloaded);
$this->assertEquals(0, $autoloaded);
$this->assertCount(0, $preloader->getList());

$preloader = new Preloader();

$preloader
->paths(realpath(__DIR__ . '/../resources'))
->load();

$this->assertEquals($autoloaded + 3 + 4, $preloader->getCount());


$this->assertEquals(3, $preloader->getCount());
$this->assertCount(3, $preloader->getList());

$preloader = new Preloader();

$preloader
->paths(realpath(__DIR__ . '/../resources'))
->ignore(realpath(__DIR__ . '/../resources/nested'))
->load();

$this->assertEquals($autoloaded + 2 + 5, $preloader->getCount());
$this->assertEquals(2, $preloader->getCount());
$this->assertCount(2, $preloader->getList());
}
}

0 comments on commit b68d767

Please sign in to comment.