Skip to content
This repository has been archived by the owner on May 19, 2021. It is now read-only.
kherge edited this page Mar 8, 2013 · 8 revisions

The Herrera\Box\Box API.

__construct(Phar $phar, string $file)

Sets the Phar that Box will be working on, as well as the file path for that Phar.

<?php $box = new Herrera\Box\Box(new Phar('test.phar'), 'test.phar');

addCompactor(CompactorInterface $compactor)

Adds a file contents compactor.

Please see CompactorInterface for implementing a file contents compactor.

<?php

use Herrera\Box\Compactor\Composer;

$box->addCompactor(new Composer());

addFile(string $file, string $local = null)

Adds a file to the Phar, with its contents run through compactContents() and replaceValues().

  • If the $local file name is not provided, $file will be used.
<?php $box->addFile('/path/to/my/file.php', 'my/file.php');

addFromString(string $local, string $contents)

Adds the file contents to the Phar, with the contents run through compactContents() and replaceValues().

<?php $box->addFromString('my/file.php', '<?php echo "Hello, world!\n";');

buildFromDirectory(string $dir, string $regex = null)

Recursively adds the directory to the Phar, with each file's contents run through compactContents() and replaceValues().

If a regular expression is provided, all files not matching the regular expression will not be added.

<?php $box->buildFromDirectory('/path/to/dir', '/\.php$/');

buildFromIterator(Traversable $iterator, string $base = null)

Adds the files returned from the iterator to the Phar, with each file's contents run through compactContents() and replaceValues().

Please see the PHP documentation for Phar::buildFromIterator() for details about what kind of data is expected from the iterator.

<?php

use Symfony\Component\Finder\Finder;

$finder = new Finder();
$finder->name('*.php')->in('/path/to/dir');

$box->buildFromIterator($finder);

string compactContents(string $file, string $contents)

Runs the contents through each compactor added by addCompactor() and returns the results.

If no compactor supports the $file, the contents will be returned unchanged.

Before:

<?php

use Herrera\Box\Compactor\Composer;

$box->addCompactor(new Composer());

$contents = $box->compactContents('my/file.php', <<<CONTENTS
<?php

/**
 * My class.
 */
class MyClass
{
    /**
     * My method.
     */
     public function myMethod()
     {
     }
}
CONTENTS
);

After:

<?php




class MyClass
{



public function myMethod()
{
}
}

Box create(string $file, integer $flags = null, string $alias = null)

Returns a new instance of Phar and Box.

The given arguments are used for the Phar::__construct() method.

<?php $box = Box::create('test.phar');

Phar getPhar()

Returns the Phar instance provided in __construct().

<?php $phar = $box->getPhar();

string replaceValues(string $contents)

Searches for the placeholder values and replaces them with the values specified by setValues().

<?php

$box->setValues(array('@name@' => 'world'));

$contents = $box->replaceValues('Hello, @name@!'); // "Hello, world!"

setStubUsingFile($file, $replace = false)

Reads the file and sets the Phar stub using its contents.

  • If $replace is set to true, the contents will be run through replaceValues().
<?php $box->setStubUsingFile('/path/to/stub.php');

setValues(array $values)

Sets the search and replace values used by replaceValues().

  • The array key is the search term.
  • The array value is value the search term is replaced with.
<?php $box->setValues(array('@name@' => 'Name'));

sign($key, $password = null)

Signs the Phar using the given private key and password.

<?php $box->sign($privateKey, 'privateKeyPassword');

signUsingFile($file, $password = null)

Signs the Phar using the given private key file and password.

<?php $box->signUsingFile('/path/to/private.key', 'privateKeyPassword');