mtymek / class-dumper
Creates single file containing multiple classes, to speed up application bootstrap.
Requires
- php: ^5.5 || ^7.0
- zendframework/zend-code: ~2.4
- zfcampus/zf-console: ~1.0
Requires (Dev)
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: 2.*
This package is auto-updated.
Last update: 2024-11-05 04:22:06 UTC
README
Creates single file PHP containing multiple classes, to speed up application bootstrap.
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