PHP code example of phine / phar

1. Go to this page and download the library: Download phine/phar library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

phine / phar example snippets


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');

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']
        );
    }
}

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

$builder->addFromString(
    'hello.php',
    <<<CODE


echo "Hello, {name}!\n";
CODE
);

use Phine\Phar\Stub;

$banner = <<<BANNER
This stub has been licensed under blah blah blah.

Copyright (c) 2199 Hulk Smash
BANNER
;

$builder->setStub(
    Stub::create()
        ->setBanner($banner)
        ->mapPhar('alias.phar')
        ->addRequire('src/hello.php')
        ->selfExtracting()
        ->getStub()
);

#!/usr/bin/env php


/*
 * This stub has been licensed under blah blah blah.
 *
 * Copyright (c) 2199 Hulk Smash
 */

if (class_exists('Phar')) {
$include = 'phar://' . __FILE__;
Phar::mapPhar('alias.phar');
} else {
$include = Extract::from(__FILE__)->to();
set_include_path($include . PATH_SEPARATOR . get_include_path());
}


use Phine\Phar\Archive;
use Phine\Phar\Extract;

$archive = Archive::create('/path/to/archive.phar');

$extract = new Extract($archive);
$extract->extractTo('/path/to/output/dir');

use Phine\Phar\Manifest\Entry;

$extract->extractTo(
    '/path/to/output/dir',
    function (Entry $entry) {
        if ('.php' !== substr($entry->getName(), -3, 3)) {
            return true; // skip this file
        }
    }
);

use Phine\Phar\Signature;

if (Signature::create('/path/to/archive.phar')->verifySignature()) {
    // the signature was successfully verified
} else {
    // the verification failed!
}