Download the PHP package herrera-io/box without Composer
On this page you can find all versions of the php package herrera-io/box. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download herrera-io/box
More information about herrera-io/box
Files in herrera-io/box
Package box
Short Description A library for simplifying the PHAR build process.
License MIT
Homepage https://github.com/box-project/box2-lib
Informations about the package box
Box
Box is a library built on the Phar
class. It is designed to make it
easier to create new phars and modifying existing ones. Features include
compacting source files, better custom stub generation, and better OpenSSL
signing handling.
Example
Installation
Add it to your list of Composer dependencies:
Usage
The Box library includes many features and some are designed so that they can be used independently of each other. This is done to allow the phar builder better control of the phar building process.
Compacting Source Files
Box makes uses of source file "compactors". A compactor is simply a class that checks if the given file is supported, then manipulates its contents to make it smaller. I will later cover how to actually use them in Finally, Building Phars.
There are two ways of creating a compactor class: implement the
CompactorInterface
interface, or extend the Compactor
abstract class.
Implementing CompactorInterface
The CompactorInterface
interface only requires that you implement two
methods in your class: compact($contents)
and support($file)
. The
$contents
argument is the contents of the source file, and the $file
argument is the full path to the file. How you determine which file types
are supported is entirely up to you.
In this example, this custom compactor will only modify files that end in
.php
, and remove whitespace from the end of each line:
Extending Compactor
An abstract compactor class is included that handles file type checking for you. You simply need to provide the default list of file extensions supported. These extension can be overwritten later if necessary, by the developer using them.
This example is a variation of the previous example compactor:
Developers can later change the supported file extensions by calling the
Compactor->setExtensions()
method:
Bundled Compactors
The library has two compactors bundled for your convenience.
Compacting JavaScript
The JavaScript
compactor will minify JavaScript files, but requires the
tedivm/jshrink
packages to work. This is included when you install
the Box library.
Compacting JSON
The JSON
compactor is very simple to use as there are no options to
configure. However, the json
extension is required to use it. All extra
whitespace is removed from .json
files.
Compacting PHP
The PHP
compactor will strip all comments whitespace from .php
files.
Comments that are removed will be removed with an the same number of line
breaks as the original comment. This is done in order to preserve the line
number that is reported when errors occur in the phar.
If you make use of Doctrine formatted annotations, you can also make use
of a special feature within the Php
compactor. To compact comments and
preserve annotations, you will need to install the herrera-io/annotations
library and create an instance of Tokenizer
.
Both line count and annotation data is preserved.
Managing Signatures
The Phar
class provides an easy way of extracting and verifying a phar's
signature. Simply instantiating the class will verify the phar in question.
However, the phar
extension is required to do either task. The Box library
includes a way to extract and verify signatures without the use of the
extension.
The Signature::create()
method is an alias to Signature::__construct()
which allows for a shorthand version of the above example:
The purpose of being able to verify a phar without having the extension available is more prudent in nature. In sensitive environments without the extension available, a dev or sys admin may want to verify the integrity of a phar they are using before making it available on the system.
Extracting Phars
In addition to being able to verify a phar's signature without the extension, you can also extract its contents. This feature is primarily designed to be embedded as part of a custom stub, but it can also be used to extract any phar.
The first argument for the constructor is the path to the existing phar. The
second being the length of the stub. This second argument is required in order
for the Extract
class to know where the phar's manifest begins. Usually, this
value is generated by the Phar
class when the default stub is used.
If the value is unknown, the Extract
class can be used to make a best guess
effort by calling the Extract::findStubLength()
method. If the stub length
is incorrectly guessed, the Extract
class will thrown an exception at some
point during the extraction process.
By default, the Extract->go()
method will create a temporary directory path
and extract the contents of the phar there. The directory path specified in
the example is optional.
In order to reduce overhead, the Extract
class will not re-extract the phar
if a special file exists in the target directory. This is used to speed up the
execution process for phars that were executed without the phar extension.
Note that if any of the files within the phar were compressed using either
gzip or bzip2, their respective extensions will be required for decompression.
If the required extension is not installed, the Extract
class will throw an
exception.
Generating Stubs
If appropriate for the project, a custom stub can be generated by the Box library. You will have control over the following functions in the stub:
- Setting an alias.
- Setting a "banner" comment (such as a copyright statement).
- Embed the
Extract
class to support self-extraction. - Setting the CLI index script.
- Enabling the use of
Phar::interceptFileFuncs()
. - Setting the file mimetypes.
- Setting the list of server variables to "mung".
- Setting the 404 script.
- Setting the "shebang" line.
- Opting the user of
Phar::webPhar()
overPhar::mapPhar()
.
The following is an example of a stub generated with all of the settings used:
And the resulting stub:
For the sake of brevity, the embedded
Extract
class was replaced with "... snip ...".
The example stub is likely overkill for what you need. By not using the
extract()
method, you can easily shave a few hundred lines of code from
your stub, reducing its size, but you will lose the ability to execute the
phar in an environment without the phar
extension.
Finally, Building Phars
All these features are great, but they're even better when used together in
the Box
class. The Box
class is designed to automatically integrate all
of these features in a (hopefully) simple to use interface.
There are two ways of instantiating the class:
Note that the
Box::create()
method accepts the same arguments as thePhar::__construct()
method.
Registering Compactors
Whether you are using the bundled compactors or your own, you will need to
call the Box->addCompactor()
method to register your class with Box
.
All files added to the phar using Box
will be automatically run through
the supported compactors.
Using Placeholder Values
The Box
class offers the ability to search and replace placeholder values
in added files. Keep in mind that only scalar values are supported for any
replacements.
With the above value to match, the following code:
will be added to the phar as:
Adding Files
To actually make use of the registered compactors and set placeholder value
replacements, you will need to use the Box
class's methods for adding files.
The methods are identical to that of the Phar
class, but automatically apply
the appropriate compactors and replacements:
Box->addFile()
Box->addFromString()
Box->buildFromDirectory()
Box->buildFromIterator()
Note that if you need you need to add a file without any alterations (such as
a binary file), you may need to add the file directly using the Phar
instance:
Setting the Stub
The Box
class offers a simple way of adding a stub sourced from a file, and
also applying the placeholder replacements at the same time:
The second argument indicates that replacements should be performed on the
stub. If you leave it out, it will default to false
which means that no
replacements will be performed and the stub will be used as is.
Private Key Signing
The Box
class offers two simple ways of signing the phar using a private
key. Either method will, however, require the availability of the openssl
extension.
With a String
If you have already loaded the private key as a string variable, you can use
the Box->sign()
method.
The password is only required if the key was generated with a password.
With a File
You can also sign the phar using a private key contained with a file.