Skip to content

Commit

Permalink
Merge pull request #29 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixed appending and excluding with an empty array
  • Loading branch information
DarkGhostHunter committed Mar 25, 2020
2 parents a7aa8ab + 58951f9 commit 30fd806
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 13 deletions.
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This package generates a [PHP 7.4 preloading](https://www.php.net/manual/en/opca
+ [`selfExclude()`](#selfexclude)
* [Generation](#generation)
+ [`memoryLimit()`](#memorylimit)
+ [`shouldRequire()`](#shouldrequire)
+ [`useRequire()`](#userequire)
* [Compilation](#compilation)
+ [`writeTo()`](#writeto)
+ [`getList()`](#getlist)
Expand Down Expand Up @@ -161,7 +161,7 @@ Preloader::make()->append(function (Finder $find) {
#### `exclude()`

This method excludes a file or list of files from Opcache list of files, which later end up in the Preload list. Excluding these files will free up the memory of the compiled list, leaving space for others files to be included.
This method excludes files from inside directories from Opcache list, which later end up in the Preload list. Excluding files may free up the memory of the compiled list, leaving space for others to be included.

You can pass an array of paths, which is good if you already have a list ready to exclude.

Expand Down Expand Up @@ -209,7 +209,7 @@ Preloader::make()->selfExclude();

#### `memoryLimit()`

By default, Preloader defaults a memory limit of 32MB, which is enough for *most* applications. The The Preloader will generate a list of files until that memory limit is reached.
By default, Preloader defaults a memory limit of 32MB, which is enough for *most* applications. TThehe Preloader will generate a list of files until that memory limit is reached.

You can set your own memory limit in **MB**.

Expand All @@ -225,20 +225,20 @@ This takes into account the `memory_consumption` key of each script cached in Op

> This limit doesn't have any relation with Opcache memory limit.
To disable any memory limit, use `memoryLimit(0)`. This will list **ALL** available files from Opcache.
To disable the limit, use `memoryLimit(0)`. This will list **ALL** available files from Opcache.

#### `shouldRequire()`
#### `useRequire()`

By default, the Preloader will upload the file to Opcache using `opcache_compile_file()`. This avoids executing any file in your project, but no links (traits, interfaces, extended classes, ...) will be resolved from the files compiled, but you may have some warnings of unresolved links when preloading (nothing too dangerous).
By default, the Preloader will upload the files to Opcache using `opcache_compile_file()`. This avoids executing any file in your project, but no links (traits, interfaces, extended classes, ...) will be resolved from the files compiled. You may have some warnings of unresolved links when preloading (nothing too dangerous).

You can change this using `shouldRequire()`, which changes to `require_once`, along the path the Composer Autoloader (usually at `vendor/autoload.php`) to resolve the links.
You can change this using `useRequire()`, which changes to `require_once`, along the path the Composer Autoloader (usually at `vendor/autoload.php`) to resolve the links.

```php
<?php

use DarkGhostHunter\Preloader\Preloader;

Preloader::make()->shouldRequire(__DIR__ . '/../vendor/autoload.php');
Preloader::make()->useRequire(__DIR__ . '/../vendor/autoload.php');
```

> You may get some warnings when compiling a file with unresolved links. These are not critical, since these files usually are the least requested in your application.
Expand Down
20 changes: 15 additions & 5 deletions src/ManagesFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace DarkGhostHunter\Preloader;

use LogicException;
use Symfony\Component\Finder\Finder;

trait ManagesFiles
Expand Down Expand Up @@ -75,7 +76,8 @@ protected function findFiles($files) : Finder
{
if (is_callable($files)) {
$files($finder = new Finder());
} else {
}
else {
$finder = (new Finder())->in($files);
}

Expand All @@ -92,10 +94,18 @@ protected function getFilesFromFinder(Finder $finder) : array
{
$paths = [];

foreach ($finder as $file) {
$paths[] = $file->getRealPath();
}
try {
foreach ($finder as $file) {
$paths[] = $file->getRealPath();
}

return $paths;
return $paths;
}
// If the developer used an empty array for the Finder instance, we will just
// catch the exception thrown by having no directories to find and return an
// empty array. Otherwise the Preloader will fail when retrieving the list.
catch (LogicException $exception) {
return $paths;
}
}
}
24 changes: 24 additions & 0 deletions tests/PreloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,30 @@ public function test_helper_creates_instance()
$this->assertInstanceOf(Preloader::class, Preloader::make());
}

public function test_accepts_empty_array_for_excluding()
{
$this->mockOpcache();

$preloader = new Preloader(new PreloaderCompiler, new PreloaderLister, $this->opcache);

$list = $preloader->exclude([])->getList();

$this->assertIsArray($list);
$this->assertCount(5, $list);
}

public function test_accepts_empty_array_for_appending()
{
$this->mockOpcache();

$preloader = new Preloader(new PreloaderCompiler, new PreloaderLister, $this->opcache);

$list = $preloader->append([])->getList();

$this->assertIsArray($list);
$this->assertCount(5, $list);
}

protected function tearDown() : void
{
parent::tearDown();
Expand Down

0 comments on commit 30fd806

Please sign in to comment.