Skip to content

Commit

Permalink
Merge pull request #26 from DarkGhostHunter/master
Browse files Browse the repository at this point in the history
Fixed default method.
  • Loading branch information
DarkGhostHunter authored Mar 10, 2020
2 parents ec163fe + 5478073 commit a7aa8ab
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 304 deletions.
39 changes: 36 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ This package generates a [PHP 7.4 preloading](https://www.php.net/manual/en/opca
* [Compilation](#compilation)
+ [`writeTo()`](#writeto)
+ [`getList()`](#getlist)
- [Safe Preloader](#safe-preloader)
- [Example](#example)
- [Security](#security)
- [License](#license)
Expand Down Expand Up @@ -112,7 +113,7 @@ This is handy if you can combine the condition with your own application logic,

#### `whenOneIn()`

This is method is just a helper to allows you to quickly make generate a Preloader list in one of a given number of random chances.
This is method is just a helper to allows you to quickly generate a Preloader script in one of a given number of random chances.

```php
<?php
Expand Down Expand Up @@ -272,9 +273,41 @@ Preloader::make()->getList();

This may become handy if you have your own script, or you just want to tinker around it.

## Safe Preloader

This packages comes with a handy Safe Preloader, located in `helpers/safe_preloader.php`.

What it does is very simple: it registers a shutdown function for PHP that is executed after the preload script finishes, and registers any error the script may have returned so you can debug it.

To use it, copy the file into an accessible path for PHP, and along with the real preloader script, reference it in your `php.ini`:

```ini
opcache.preload=/www/app/safe_preloader.php
```

```php
<?php
// /www/app/safe_preloader.php

register_shutdown_function(function (): void {
$error = error_get_last();
if (!$error) {
return;
}
echo 'Preloader Script has stopped with an error:' . \PHP_EOL;
echo 'Message: ' . $error['message'] . \PHP_EOL;
echo 'File: ' . $error['file'] . \PHP_EOL;
});

// require_once /* Path to your preload script */
require_once '/www/app/preloader.php';
```

Technically speaking, the Opcache preloads the files in a different process, so there shouldn't be a problem using this safe-preloader.

## Example

Okay. Let's say we have a codebase with thousand of files. We don't know any metrics, so we will make generate a preloader script if the request hits the lottery 1 on 100, with a memory limit of 64MB.
Okay. Let's say we have a codebase with thousand of files. We don't know any metrics, so we will generate a preloader script if the request hits the lottery 1 on 100, with a memory limit of 64MB.

```php
<?php
Expand All @@ -293,7 +326,7 @@ $response->sendToBrowser();

Preloader::make()
->whenOneIn(100)
->memory(64)
->memoryLimit(64)
->writeTo(PHP_LOCALSTATEDIR . '/preload.php'); // put it in /var.;
```

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"symfony/finder": "^5.0.5"
"symfony/finder": "^4.3||^5.0.5"
},
"autoload": {
"psr-4": {
Expand Down
2 changes: 2 additions & 0 deletions src/Preloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,8 @@ protected function prepareCompiler(string $path)
$this->compiler->contents = file_get_contents(static::STUB_LOCATION);
$this->compiler->opcacheConfig = $this->getOpcacheConfig();
$this->compiler->preloaderConfig = $this->getPreloaderConfig();
$this->compiler->useRequire = $this->useRequire;
$this->compiler->autoloader = $this->autoloader;
$this->compiler->writeTo = $path;

return $this->compiler;
Expand Down
2 changes: 1 addition & 1 deletion src/PreloaderCompiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class PreloaderCompiler
*
* @var bool
*/
public bool $useRequire = true;
public bool $useRequire;

/**
* The file list to include.
Expand Down
Loading

0 comments on commit a7aa8ab

Please sign in to comment.