2017 © Pedro Peláez
 

library classpreloader

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

image

classpreloader/classpreloader

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  • Sunday, December 10, 2017
  • by graham-campbell
  • Repository
  • 11 Watchers
  • 270 Stars
  • 25,463,484 Installations
  • PHP
  • 32 Dependents
  • 0 Suggesters
  • 30 Forks
  • 1 Open issues
  • 14 Versions
  • 3 % Grown

The README.md

Class Preloader for PHP

This tool is used to generate a single PHP script containing all of the classes required for a specific use case. Using a single compiled PHP script instead of relying on autoloading can help to improve the performance of specific use cases. For example, if your application executes the same bootstrap code on every request, then you could generate a preloader (the compiled output of this tool) to reduce the cost of autoloading the required classes over and over., (*1)

Banner, (*2)

What it actually does

This tool listens for each file that is autoloaded, creates a list of files, traverses the parsed PHP file using PHP Parser and any visitors of a Config object, wraps the code of each file in a namespace block if necessary, and writes the contents of every autoloaded file (in order) to a single PHP file., (*3)

Notice

This tool should only be used for specific use cases. There is a tradeoff between preloading classes and autoloading classes. The point at which it is no longer beneficial to generate a preloader is application specific. You'll need to perform your own benchmarks to determine if this tool will speed up your application., (*4)

Installation

Add ClassPreloader as a dependency to your composer.json file by adding "classpreloader/classpreloader": "^4.1" to your require block. Note that if you want to use the cli tool, then you need to also add "classpreloader/console": "^3.1" to the require block., (*5)

Using the tool

You use the ./vendor/bin/classpreloader compile command with a few command line flags to generate a preloader., (*6)

--config: A CSV containing a list of files to combine into a classmap, or the full path to a PHP script that returns an array of classes or a ClassPreloader\ClassLoader\Config object., (*7)

--output: The path to the file to store the compiled PHP code. If the directory does not exist, the tool will attempt to create it., (*8)

--skip_dir_file: (no value) Skip files with __DIR__ or __FILE__ to make the cache portable., (*9)

--fix_dir: (defaults to 1) Set to 0 to not replace __DIR__ constants with the actual directory of the original file., (*10)

--fix_file: (defaults to 1) Set to 0 to not replace __FILE__ constants with the actual location of the original file., (*11)

--strict_types: (defaults to 0) Set to 1 to enable strict types mode., (*12)

--strip_comments: (defaults to 0) Set to 1 to strip comments from each source file., (*13)

Writing a config file

Creating a PHP based configuration file is fairly simple. Just include the vendor/classpreloader/classpreloader/src/ClassLoader.php file and call the ClassPreloader\ClassLoader::getIncludes() method, passing a function as the only argument. This function should accept a ClassPreloader\ClassLoader object and register the passed in object's autoloader using $loader->register(). It is important to register the ClassPreloader\ClassLoader autoloader after all other autoloaders are registered., (*14)

An array or ClassPreloader\ClassLoader\Config must be returned from the config file. You can attach custom node visitors if you need to perform any sort of translation on each matching file before writing it to the output., (*15)

<?php

// Here's an example of creating a preloader for using the
// Amazon DynamoDB and the AWS SDK for PHP 2.

require __DIR__.'/src/Config.php';
require __DIR__.'/src/ClassNode.php';
require __DIR__.'/src/ClassList.php';
require __DIR__.'/src/ClassLoader.php';

use ClassPreloader\ClassLoader;

$config = ClassLoader::getIncludes(function (ClassLoader $loader) {
    require __DIR__.'/vendor/autoload.php';
    $loader->register();
    $aws = Aws\Common\Aws::factory([
        'key'    => '***',
        'secret' => '***',
        'region' => 'us-east-1'
    ]);
    $client = $aws->get('dynamodb');
    $client->listTables()->getAll();
});

// Add a regex filter that requires all classes to match the regex.
// $config->addInclusiveFilter('/Foo/');

// Add a regex filter that requires that a class does not match the filter.
// $config->addExclusiveFilter('/Foo/');

return $config;

You would then run the classpreloader script and pass in the full path to the above PHP script., (*16)

./vendor/bin/classpreloader compile --config="/path/to/the_example.php" --output="/tmp/preloader.php", (*17)

The above command will create a file in /tmp/preloader.php that contains every file that was autoloaded while running the snippet of code in the anonymous function. You would generate this file and include it in your production script., (*18)

Automating the process with Composer

You can automate the process of creating preloaders using Composer's script functionality. For example, if you wanted to automatically create a preloader each time the AWS SDK for PHP is installed, you could define a script like the following in your composer.json file:, (*19)

{
    "require": {
        "classpreloader/console": "^3.1"
    },
    "scripts": {
        "post-autoload-dump": "@php vendor/bin/classpreloader compile --config=/path/to/the_example.php --output=/path/to/preload.php"
    },
    "config": {
        "bin-dir": "bin"
    }
}

Using the above composer.json file, each time the project's autoloader is recreated using the install or update command, the classpreloader.php file will be executed. This script would generate a preload.php containing the classes required to run the previously demonstrated "the_example.php" configuration file., (*20)

Security

If you discover a security vulnerability within this package, please send an email to Graham Campbell at graham@alt-three.com. All security vulnerabilities will be promptly addressed. You may view our full security policy here., (*21)

License

Class Preloader is licensed under The MIT License (MIT)., (*22)

For Enterprise

Available as part of the Tidelift Subscription, (*23)

The maintainers of classpreloader/classpreloader and thousands of other packages are working with Tidelift to deliver commercial support and maintenance for the open source dependencies you use to build your applications. Save time, reduce risk, and improve code health, while paying the maintainers of the exact dependencies you use. Learn more., (*24)

The Versions

10/12 2017

dev-master

9999999-dev

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

10/12 2017

3.2.0

3.2.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

16/09 2016

3.1.0

3.1.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

09/11 2015

3.0.0

3.0.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

28/06 2015

2.0.x-dev

2.0.9999999.9999999-dev

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

28/06 2015

2.0.0

2.0.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

26/05 2015

1.4.x-dev

1.4.9999999.9999999-dev

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

26/05 2015

1.4.0

1.4.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

15/04 2015

1.3.0

1.3.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

26/01 2015

1.2.0

1.2.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

23/09 2014

1.1.0

1.1.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

The Development Requires

by Graham Campbell

class autoload preload

12/03 2014

1.0.2

1.0.2.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

class autoload preload

25/06 2013

1.0.1

1.0.1.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

class autoload preload

03/03 2013

1.0.0

1.0.0.0

Helps class loading performance by generating a single PHP file containing all of the autoloaded files for a specific use case

  Sources   Download

MIT

The Requires

 

class autoload preload