Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build Libraries? #53

Closed
mcordingley opened this issue Nov 6, 2015 · 8 comments
Closed

Build Libraries? #53

mcordingley opened this issue Nov 6, 2015 · 8 comments

Comments

@mcordingley
Copy link

It looks like this project is meant to build an executable phar version of a project, like the Boris REPL. Can it also be used to build includable libraries?

It would be neat to be able to build a non-executable phar, with dependencies, for some given library and then be able to include that into a small or legacy project. The only rub I see is if multiple libraries are included this way that have overlapping dependency trees.

@clue
Copy link
Owner

clue commented Nov 7, 2015

This is an excellent question and I agree that it would be nice to support this 👍

This project currently focuses on the bin configuration in your composer.json, which is commonly not set for library projects. FWIW, ticket #29 discusses adding an explicit CLI option to pass this.

Has anybody already looked into using phar-composer for library projects? Any input/feedback would be appreciated.

One possible use case should probably work right now:

require 'demolibrary.phar';

$demo = new Demo();
$demo->run();

We may also want to look into supporting the following use case:

$demo = require 'demolibrary.phar';

$demo->run();

For this to work we would need to implement a mechanisms to allow the library author to specify what this should return. Again, #29 might be a good starting point here.

if multiple libraries are included this way that have overlapping dependency trees

Indeed, and there's probably little we can do about this. IMO it would be sufficient to add some documentation (warnings) and leave this up the consumers of this project.

@halaei
Copy link

halaei commented Jan 28, 2016

@clue

Has anybody already looked into using phar-composer for library projects?

I has! There are programmers who know a little about composer and want to use my package to , let say send SMS with. I thought your package can help.

@halaei
Copy link

halaei commented Jan 28, 2016

I used you package with a bit of hack to accomplish that. Let say you want to make a phar lib of foo/bar package.

  1. Make a local foo/bar-bin project with this composer.json:
{
  "require": {
    "foo/bar": "*"
  },
  "bin": ["autoload.php"]
}
  1. Make an autoload.php file:

  2. composer install your foo/bar-bin project

  3. git clone and composer install clue/phar-composer

  4. Find the first line of Herrera\Box\StubGenerator::generate() function:

    public function generate()
    {
    $stub = array($this->shebang, '<?php');
    and replace it with:

    public function generate()
    {
    $stub = array('<?php');

  5. run the build command:

    phar-composer/bin/phar-composer build {path-to foo/bar-bin project}

This gives you a phar file that can be included and used as a library! There may be a simpler process, by the way.

@RauchF
Copy link

RauchF commented Mar 16, 2016

As far as I can tell this will lead to issues when you have more than one .phar built with phar-composer that you want to include, due to classes in the .phar being redeclared (for ex. the class Extract).

@halaei
Copy link

halaei commented Mar 16, 2016

@radford that is true.

@RauchF
Copy link

RauchF commented Mar 16, 2016

I don't know if it's the nicest way, but one possible solution would be to generate the class names at the time the file is built, with a suffix unique to each specific .phar.

@helhum
Copy link

helhum commented Jun 8, 2016

If you change the code from

require 'demolibrary.phar';

to

require 'phar://' . __DIR__ . '/demolibrary.phar/vendor/autoload.php';

It works perfectly fine with multiple phar files and with phar files that do not have a script (e.g. libraries)

I use this successfully here:
https://github.com/helhum/typo3_console/blob/master/Classes/Core/ConsoleBootstrap.php#L250

@helhum
Copy link

helhum commented Jun 8, 2016

@clue Maybe this might be an easy task to add this to the documentation. No code changes required.

Oh, and thanks a lot for this project. It helps me a lot easily creating other open source tools :)

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

No branches or pull requests

5 participants