thadafinser/speed-loader

This package is abandoned and no longer maintained. No replacement package was suggested.

Load classes faster

v1.0.0 2015-04-13 06:37 UTC

This package is not auto-updated.

Last update: 2022-02-01 12:45:12 UTC


README

Build Status Code Coverage Scrutinizer Code Quality

One file to load all your needed classes at once! Since autoloading takes more and more time, it has become very important to have an efficient autoloading. SpeedLoader aims to improve your autoloading experience

Install

Get it with composer:

composer require thadafinser/speed-loader

Create a seperate file

// composer autoloading
require 'vendor/autoload.php';

//since composer is always needed, exclude all classes loaded until here
$classesNoLoad = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());

//execute your app part you want to cache
$app = MyApplication::init();

//find all loaded files until here
$classes = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());
//remove the classes loaded by composer
$classes = array_diff($classes, $classesNoLoad);

//cache it now
$cache = new SpeedLoader\BuildCache();
$cache->setClasses($classes);
//$cache->setNewLine("\n");
//$cache->setCompressionLevel(SpeedLoader\BuildClass::COMPRESS_HIGH);

file_put_contents('data/cache/classes.php.cache', '<?php ' . "\n" . $cache->getCachedString());

Add the cache to your application

if (file_exists('data/cache/classes.php.cache')) {
    require_once 'data/cache/classes.php.cache';
}

Is this something new?

No it's nothing completely new. There are a couple of solutions around, but all of them are having some problems, that's why i "reinvited the wheel".

Features

  • independent
    • can be used with all packages or frameworks around
    • or only with your code
  • different compression modes
    • compression for production
    • normal for development
  • save the cache how you want
    • you get the complete cache as a string
    • e.g. save it as a file or memory

Example

Generate

Just execute the part of your application you want to cache. Dont do this in your normal process - warm up your cache with a cronjob or similar

// composer autoloading
require 'vendor/autoload.php';

//since composer is always needed, exclude all classes loaded until here
$classesNoLoad = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());

//execute your app...(only the bootstrap)
$app = Zend\Mvc\Application::init($appConfig);

//find all loaded files
$classes = array_merge(get_declared_interfaces(), get_declared_traits(), get_declared_classes());
$classes = array_diff($classes, $classesNoLoad);

//cache it
$cache = new SpeedLoader\BuildCache();
$cache->setClasses($classes);
//$cache->setNewLine("\n");
//$cache->setCompressionLevel(SpeedLoader\BuildClass::COMPRESS_HIGH);

file_put_contents('data/cache/classes.php.cache', '<?php ' . "\n" . $cache->getCachedString());

Include the file in your final application

if (file_exists('data/cache/classes.php.cache')) {
    require_once 'data/cache/classes.php.cache';
}

Why concat classes

Finding and opening a lot of files on the filesystem is expensive. (similar reason why you should combine JS or CSS files...but there its HTTP)

Alternatives

EdpSuperluminal

  • not maintained
  • only for ZF2
  • only compressed possible

Symfony

  • only output direct to a file
  • no compression possible

ClassPreloader

  • class hierarchy can be wrong (e.g. a class requires an interface and the interface comes later in the file...but in the meantime autoloader have loaded the interface -> "cannot redeclare error")

Benchmarks

http://stackoverflow.com/questions/8240726/are-there-performance-downsides-while-using-autoloading-classes-in-php https://mwop.net/blog/245-Autoloading-Benchmarks.html