PHPackages                             phine/phar - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. phine/phar

Abandoned → [box-project/box2](/?search=box-project%2Fbox2)ArchivedLibrary[File &amp; Storage](/categories/file-storage)

phine/phar
==========

A PHP library for creating and reading (without the phar extension) PHP archives.

1.0.2(12y ago)4792.3k↓32.1%1[1 issues](https://github.com/kherge-archive/lib-phar/issues)[1 PRs](https://github.com/kherge-archive/lib-phar/pulls)7MITPHPPHP &gt;=5.3.3

Since Dec 7Pushed 12y agoCompare

[ Source](https://github.com/kherge-archive/lib-phar)[ Packagist](https://packagist.org/packages/phine/phar)[ Docs](https://github.com/phine/lib-phar)[ RSS](/packages/phine-phar/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (2)Dependencies (5)Versions (4)Used By (7)

Phar
====

[](#phar)

[![Build Status](https://camo.githubusercontent.com/03cf4b9f88a0b932ebd27c9e66597d06f5a908b090c1b65f7207a421b21527e3/68747470733a2f2f7472617669732d63692e6f72672f7068696e652f6c69622d706861722e706e673f6272616e63683d6d6173746572)](https://travis-ci.org/phine/lib-phar)[![Coverage Status](https://camo.githubusercontent.com/bc044d9d1b47b9fce944bfb4fa71fac421770922ab1d88a78ca727c502107dc5/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f7068696e652f6c69622d706861722f62616467652e706e67)](https://coveralls.io/r/phine/lib-phar)[![Latest Stable Version](https://camo.githubusercontent.com/41378e0709db4533ad246580f59cf867ffb45b9394bcce99ff6bee12e106ae52/68747470733a2f2f706f7365722e707567782e6f72672f7068696e652f706861722f762f737461626c652e706e67)](https://packagist.org/packages/phine/phar)[![Total Downloads](https://camo.githubusercontent.com/f0ee6123ace4ea356a7926bab26f408454704561d593c672eaff15480e92d85f/68747470733a2f2f706f7365722e707567782e6f72672f7068696e652f706861722f646f776e6c6f6164732e706e67)](https://packagist.org/packages/phine/phar)

A PHP library for creating and reading (without the phar extension) PHP archives.

Requirement
-----------

[](#requirement)

- PHP &gt;= 5.3.3
- [Phine Exception](https://github.com/phine/lib-exception) &gt;= 1.0.0
- [Phine Observer](https://github.com/phine/lib-observer) &gt;= 2.0.0
- [Phine Path](https://github.com/phine/lib-path) &gt;= 1.0.0

Installation
------------

[](#installation)

Via [Composer](http://getcomposer.org/):

```
$ composer require "phine/phar=~1.0"

```

Usage
-----

[](#usage)

### Building an Archive

[](#building-an-archive)

To create a new archive, you will need to create a new `Builder` instance. There are two ways of doing this: using an existing `Phar` instance, or by creating a new one.

```
use Phine\Phar\Builder;

// using an existing Phar instance
$builder = new Builder($phar);

// create a new Phar instance
$builder = Builder::create('/path/to/archive.phar');
```

With the new `Builder` instance, you will now have access to a few methods that appear to be identical to that of the `Phar` class:

- `addEmptyDir()`
- `addFile()`
- `addFromString()`
- `buildFromDirectory()`
- `buildFromIterator()`
- `setStub()`

Truth be told, they have the exact same end result as their `Phar` counterparts. The difference being that each method can be observed, and the arguments passed to each method can be altered before the actual action (adding an empty directory, adding a file from disk, etc) is performed. The simplest example is performing a search and replace to all content added using the `addFromString()` method.

#### Observing an Action

[](#observing-an-action)

The `Builder` class is based on the [phine/observer](https://github.com/phine/lib-observer) library, so it may benefit you to read up on the documentation provided by that library. To observe an action (aka "subject"), you will need to create your own implementation of `Phine\Observer\ObserverInterface`.

```
use Phine\Observer\SubjectInterface;
use Phine\Observer\ObserverInterface;

/**
 * Replaces occurrences of "{name}" with "world".
 */
class Replace implements ObserverInterface
{
    /**
     * {@inheritDoc}
     */
    public function receiveUpdate(SubjectInterface $subject)
    {
        // get the arguments for the addFromString() method
        $arguments = $subject->getArguments();

        // replace "{name}" with "world"
        $arguments['contents'] = str_replace(
            '{name}',
            'world',
            $arguments['contents']
        );
    }
}
```

Now that we have our observer, we will need to register an instance of it with a builder event. In particular, we are interested in the `Builder::ADD_STRING`event, which is the event used by the builder for the `addFromString()` method.

```
// register our observer
$builder->observe(Builder::ADD_STRING, new Replace());
```

With the observer registered to the `addFromString()` method, whenever we call it all occurrences of `{name}` will be replaced with the string `world`. So, if we add the following:

```
$builder->addFromString(
    'hello.php',
