Download the PHP package diego-ninja/preloader without Composer

On this page you can find all versions of the php package diego-ninja/preloader. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package preloader


Braden Collum - Unsplash (UL) #9HI8UJMSdZA

Latest Version on Packagist Total Downloads PHP Version License Composer Coverage Status PHPStan Level 8

Opcache Preloader

Get the best options to keep your application fast as ever, with just one line.

This package generates a PHP preloading script from your Opcache statistics automatically. No need to hack your way in. This package is a fork of darkghosthunter/preloader with the only target of update code and dependencies to fit with php 8.x and Symfony 6 components. It doesn't add, at least at the moment, new functionality.

If you need php 7.4 support please, use the aforementioned package.

If you're looking for preloading your Laravel project, check Laragear PReload.

Table of Contents

Requirements

Installation

Require this using Composer into your project

composer require diego-ninja/preloader

This package doesn't require ext-opcache to install. Just be sure to have it enabled in your application server.

Usage

Anywhere in your application, where Opcache is enabled and running, call Preloader with where to output the compiled script:

This will automatically gather Opcache statistics, and write an optimized preload.php file. In this case, the file will be created in the same directory the Preloader was called.

www
└── app
    ├── PreloaderCall.php
    └── preload.php

Once generated, tell PHP to use this file as a preloader at start up in your php.ini.

Once the script is generated, you're encouraged to restart your PHP process (or server, in some cases) to pick up the generated preload script. Only generating the script is not enough.

If you use Preloader when Opcache is disabled or without hits, you will get an Exception.

How it works

This package asks Opcache only for the most requested files and compiles a list from it. You can check this article in Medium about that preload.

Since the best statistics are those you get after your application has been running for a while, you can use your own mechanisms to compile the list only after certain conditions are met.

Don't worry, you can configure what and how compile the list.

Configuration

You can configure the Preloader to run when a condition is met, limit the file list, among what other things.

Conditions

when()

This method executes the given callable and checks if the preloader should compile the list or not based on what the callable returning value evaluates.

This is handy if you can combine the condition with your own application logic, like a given number of requests, or an external signal.

whenOneIn()

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.

For example, the above makes the Preloader generate a compiled list one in fifty chances.

Listing

append()

You can add a list of directories to the compiled list. The files inside them will be appended to the compiled list, and won't account for memory restrictions.

If the files you're adding are already in the compiled list, these will be removed from the included files to avoid effective duplicates.

This packages includes Symfony Finder, so as an alternative you can pass a closure that receives the Finder instance along with the files you want to append.

The exclude() method take precedence over append(). If you exclude a file that is later appended, you won't exclude it at all.

exclude()

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.

This packages includes Symfony Finder, so as an alternative you can pass a Closure receiving the Finder instance along with the files you want to exclude.

The exclude() method take precedence over append(). If you exclude a file that is later appended, you won't exclude it at all.

selfExclude()

Automatically excludes the Package files from the Preload list.

By default, the package is not excluded, since it may be part of the most requested files. It's recommended to exclude the package only if you have total confidence it won't be called once Opcache Preloading is enabled.

Generation

memoryLimit()

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

You can set your own memory limit in MB.

This takes into account the memory_consumption key of each script cached in Opcache, not the real file size.

This limit doesn't have any relation with Opcache memory limit.

To disable the limit, use memoryLimit(0). This will list ALL available files from Opcache.

useRequire()

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 useRequire(), which changes to require_once, along the path the Composer Autoloader (usually at vendor/autoload.php) to resolve the links.

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.

ignoreNotFound()

Some applications may create files at runtime that are genuinely cached by Opcache, but doesn't exist when the application is firstly deployed.

To avoid this problem, you can use the ignoreNotFound(), which will compile a script that ignore files not found or are unreadable.

If the file is readable but its preloading returns an error, it will throw an exception nonetheless.

Compilation

writeTo()

This will automatically create a PHP-ready script to preload your application. It will return true on success, and false when the when the conditions are not met.

You can issue false as second parameter to not overwrite any existing file in the write path. If a file is found, no preload logic will be run.

getList()

You can retrieve the raw list of files that should be included as an array using getList().

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

Safe Preloader

This package 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:

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 thousands 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.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

License

The MIT License (MIT). Please see License File for more information.


All versions of preloader with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
symfony/finder Version ^6 || ^7
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package diego-ninja/preloader contains the following files

Loading the files please wait ....