Upgrade to Pro — share decks privately, control downloads, hide ads and more …

PHP ARCHIVE (Phar)

PHP ARCHIVE (Phar)

Christian Münch

July 21, 2017
Tweet

More Decks by Christian Münch

Other Decks in Programming

Transcript

  1. PHP ARCHIVE
    Christian Münch / @cmuench 1

    View Slide

  2. Was ist ein Phar Archiv?
    Phar archives are best characterized as a convenient way to group several files
    into a single file. As such, a phar archive provides a way to distribute a complete
    PHP application in a single file and run it from that file without the need to extract it
    to disk. Additionally, phar archives can be executed by PHP as easily as any other
    file, both on the commandline and from a web server. Phar is kind of like a thumb
    drive for PHP applications.
    http://www.php.net/manual/en/intro.phar.php
    Christian Münch / @cmuench 2

    View Slide

  3. Was ist in einer Phar Datei?
    ein Stub
    Ein Manifest, welches den Inhalt beschreibt
    Inhalte
    Eine Signatur zum Prüfen ob die Datei valide ist (optional)
    Christian Münch / @cmuench 3

    View Slide

  4. Historie
    PEAR Paket "PHP_Archive"
    25 . Januar 2005
    Damals aktuelle PHP Version -> 4.3.7
    Registrierte phar:// Stream
    Reiner PHP Code
    Christian Münch / @cmuench 4

    View Slide

  5. Historie / Phar Reloaded
    28.03.2007
    PECL Package
    Implementierung in C
    SPL Integration
    APC Kompatibilität
    Damals aktuelle PHP Version 5.2
    Christian Münch / @cmuench 5

    View Slide

  6. Status Quo
    Ab PHP 5.3 (30. Juni 2009) ist das
    Phar Modul in der Standard
    Distribution enthalten.
    Christian Münch / @cmuench 6

    View Slide

  7. Datei Formate
    Phar
    Tar
    Zip
    Bzip
    Bzip2
    Christian Münch / @cmuench 7

    View Slide

  8. Feature Matrix
    Christian Münch / @cmuench 8

    View Slide

  9. Phar Datei erstellen
    $phar = new Phar('test.phar', 0, 'test.phar');
    $phar->buildFromIterator(
    new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator('src')
    ),
    '.'
    );
    $phar->setStub(
    $phar->createDefaultStub(
    'src/cli/index.php',
    'src/web/index.php'
    )
    );
    Christian Münch / @cmuench 9

    View Slide

  10. Include
    echo include 'phar://test.phar/src/a.php';
    Christian Münch / @cmuench 10

    View Slide

  11. CLI Stub
    echo include 'phar://test.phar/src/a.php';
    __HALT_COMPILER();
    Kann z.B. einen Autoloader enthalten.
    Christian Münch / @cmuench 11

    View Slide

  12. Metadaten
    try {
    $pharInfo = new \Phar('phar://test.phar');
    var_dump($pharInfo->getMetadata());
    $pharInfo->setMetadata(array('Created at' => 'PHPUG Rheinhessen'));
    } catch (\Exception $e) {
    echo $e->getMessage();
    }
    Christian Münch / @cmuench 12

    View Slide

  13. Metadaten (pro Datei)
    try {
    $pharFileInfo = new \PharFileInfo('phar://test.phar/src/a.php');
    var_dump($pharFileInfo->getContent());
    var_dump($pharFileInfo->getMetadata());
    var_dump($pharFileInfo->isCompressed());
    $pharFileInfo->setMetadata(array('Created at' => 'PHPUG Rheinhessen'));
    } catch (\Exception $e) {
    echo $e->getMessage();
    }
    Christian Münch / @cmuench 13

    View Slide

  14. Komfortables Paketieren
    Box
    Phing
    Christian Münch / @cmuench 14

    View Slide

  15. Box
    http://box-project.org
    {
    "chmod": "0755",
    "directories": ["src"],
    "alias": "test.phar",
    "output": "test.phar",
    "stub": "src/cli/index.php"
    }
    Christian Münch / @cmuench 15

    View Slide

  16. Phing
    http://www.phing.info/
    Christian Münch / @cmuench 16

    View Slide

  17. Phing build.xml



    compression="none"
    alias="test.phar"
    destfile="test.phar"
    clistub="src/cli/index.php"
    webstub="src/web/index.php"
    signature="true">






    Christian Münch / @cmuench 17

    View Slide

  18. Praxis Beispiele
    Composer
    n98-magerun
    pestl
    PHPUnit
    Christian Münch / @cmuench 18

    View Slide

  19. Danke!
    Christian Münch / @cmuench 19

    View Slide