Skip to content
This repository has been archived by the owner on Sep 6, 2020. It is now read-only.

How to build phar? #135

Open
flip111 opened this issue Mar 6, 2016 · 7 comments
Open

How to build phar? #135

flip111 opened this issue Mar 6, 2016 · 7 comments

Comments

@flip111
Copy link
Contributor

flip111 commented Mar 6, 2016

I'm trying to make a phar from this library https://github.com/symfony/var-dumper

My box.json

{
    "output": "var-dumper.phar",
    "stub": true,
    "finder": [{"in": "."}],
    "main": "VarDumper.php"
}

error

» php test.php
#!/usr/bin/env php
PHP Fatal error:  Class 'Symfony\Component\VarDumper\Cloner\VarCloner' not found in phar:///home/flip101/php/var-dumper/var-dumper.phar/VarDumper.php on line 31

for file

<?php
require 'var-dumper.phar';
dump('test');

Also if i use the default stub (by not setting stub to true) i can actually see the phar contents plain in my text editor and i see that the VarCloner class is there. However that phar can not be run for the same reason as this issue #39

I would also like to exclude the test and tests folders.

@kherge
Copy link
Contributor

kherge commented Mar 10, 2016

Your varDumper.php script needs to require the Composer autoloader.

@flip111
Copy link
Contributor Author

flip111 commented Mar 10, 2016

The composer autoloader is not in the library, i also don't need lazy loading, it's okay if all classes get loaded with the phar.

@kherge
Copy link
Contributor

kherge commented Mar 10, 2016

You will then need to manually require all of the classes you need in your VarDumper.php script.

@flip111
Copy link
Contributor Author

flip111 commented Mar 10, 2016

Is it me not understanding phar? I assumed since all the classes are in the same file they all get loaded by just including the phar file. If that's not true i would have to rethink my approach on this one.

Hold on .. what do you mean actually by "requiring a class" .. in php only files can be required or included.

@kherge
Copy link
Contributor

kherge commented Mar 10, 2016

It sounds like you believe that a .phar file contains bytecode that is loaded by PHP and then executed. A .phar file is basically an archive with a PHP script at the beginning of that file. PHP will only do whatever is in that script. This particular script is known as a stub.

For Box, you have stub set to true which only means that Box will generate a basically script for you. What this script does depends on the rest of the settings you provide. Since you have the main setting defined, you are telling Box that you want VarDumper.php to be executed when your run php var-dumper.phar.

In this case, you are asking that PHP execute the class file which in of itself does nothing but define a class. However, since there is no class loader set, it can't lazy load other classes or interfaces that VarDumper will need.

@flip111
Copy link
Contributor Author

flip111 commented Mar 10, 2016

Let's say i want to manually load (include/require) this class https://github.com/symfony/var-dumper/blob/master/Dumper/HtmlDumper.php how would i refer to that file? Or rather how would i refer to that class now that it is inside the phar archive? Will the archive keep the same directory structure?

Is there a box configuration option that generate an autoloader?

Actually what would even be better is that if i don't have to include an autoloader in the phar, but i can keep the autoloader outside. Otherwise each phar will have it's own autoloader and it will get pretty messy

@kherge
Copy link
Contributor

kherge commented Mar 10, 2016

Box does preserve directory structure. To manually load a file, you will want to do something like require 'phar://path/to/var-dumper.phar/path/to/Class.php. Otherwise, you will want to include and setup a class loader as Box will not do that for you.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants