mtymek/class-dumper

Creates single file containing multiple classes, to speed up application bootstrap.

0.4.0 2016-06-26 12:09 UTC

This package is auto-updated.

Last update: 2024-12-05 04:27:08 UTC


README

Creates single file PHP containing multiple classes, to speed up application bootstrap.

Scrutinizer Code Quality Build Status Code Coverage Latest Stable Version Total Downloads License

Usage

Commandline script

First, create configuration file that lists all files you want to merge. You don't need to worry about class order, nor about adding required interfaces or parent classes - they will be added automatically to merged file.

Config file is simple PHP file, returning array of class names:

// config/classes-to-cache.php
return [
    // ZF2 classes
    Zend\Mvc\Application::class,
    Zend\Mvc\ApplicationInterface::class,
    Zend\EventManager\EventsCapableInterface::class,
   
    // custom classes
    Foo\Application::class,
    Foo\Listener\Auth::class    
]

Next, use dump-classes.php script to generate cached file:

php ./vendor/bin/dump-classes.php config/classes-to-cache.php data/cache/classes.php.cache

When class cache is generated, you can include it in your application entry point:

// index.php
include 'vendor/autoload.php';
include 'data/cache/classes.php.cache';

You can automate generation using composer by adding post-install and post-update hooks to composer.json file:

{
    "scripts": {
        "post-install-cmd": [
            "php ./vendor/bin/dump-classes.php config/classes-to-cache.php data/cache/classes.php.cache-raw",
        ],
        "post-update-cmd": [
            "php ./vendor/bin/dump-classes.php config/classes-to-cache.php data/cache/classes.php.cache-raw",
        ]
    }
}

PHP

Alternatively, cached class file can be generated in your PHP script:

$dumper = new ClassDumper();
$cache = $dumper->dump([
    Foo::class,
    Bar::class,
]);
file_put_contents('data/cache/class_cache', "<?php\n" . $cache);

Minifing merged file

ClassDumper can reduce size of emitted file by stripping all whitespace and comments.

It can be triggered from commandline by adding --strip switch:

php ./vendor/bin/dump-classes.php config/classes-to-cache.php classes.php.cache --strip

Using in PHP:

$cache = $dumper->dump([ /* ... */ ], true);

Generating config files

You can easily generate configuration file based on currently included files in your application. In order to do it, add following lines after your application bootstrap (ideally somewhere before routing starts):

$configGenerator = new \ClassDumper\ConfigGenerator;
$configGenerator->dumpIncludedClasses('config.php');

This will save list of all included classes into config.php file. You may want to edit configuration file manually before using it in your application - see "Limitations" section below. After creating the config file, remove above lines.

Limitations

Not every class can be cached using Class Dumper.

  • class dump will end up in different directory than merged classes. Classes using constants like __DIR__or __FILE__ will likely not work correctly.
  • when using with "--strip" options, all comments - including annotations - will be stripped out. This will prevent annotation parser from working, if used on cached classes.

TODO

  • throw exception when class does not exist
  • warn if class contains __DIR__ or __FILE__ constants
  • output/log statistics
  • fix __DIR__ constants