An open API service indexing awesome lists of open source software.

https://github.com/dflydev/dflydev-embedded-composer

Embed Composer into another application
https://github.com/dflydev/dflydev-embedded-composer

Last synced: 10 months ago
JSON representation

Embed Composer into another application

Awesome Lists containing this project

README

          

Embedded Composer
=================

Embed [Composer][1] into another application.

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

Through [Composer][1] as [dflydev/embedded-composer][2].

Why Would I Want To Embed Composer?
-----------------------------------

Imagine a console application shipped as a phar. If it is desired for the
application to be extensible based on which directory it is in (say one set
of plugins should be used in one directory but an entirely different set of
plugins used in another directory) one cannot simply define a `composer.json`
in both directories and run `composer install`.

Why not? Because the application shipped with a specific set of dependencies.
Composer cannot add more dependencies without running the risk of introducing
conflicts. The answer is to embed Composer into the application so that
Composer can merge the dependencies already installed for the application
with the dependencies defined in a specific directory's `composer.json`.

The end result is a set of dependencies that satisfy the directory specific
requirements while taking into account the dependencies *already installed*
for the console application.

While this is required for a phar distributed application this technique can
be applied to any globally installed application that needs to be runtime
extensible.

Usage
-----

### Basics

The following is an example `bin/myapp` style script that can be used either
installed via Composer (`vendor/bin/myapp`) or installed globally
(`/usr/local/bin/myapp`).

#### myapp.php (bin)

A shared block of code to initialize Embedded Composer from an application.

```php
getParameterOption('--project-dir') ?: '.';

$embeddedComposerBuilder = new EmbeddedComposerBuilder(
$classLoader,
$projectDir
);

$embeddedComposer = $embeddedComposerBuilder
->setComposerFilename('myapp.json')
->setVendorDirectory('.myapp')
->build();

$embeddedComposer->processAdditionalAutoloads();

// application is now ready to be run taking both the embedded
// dependencies and directory specific dependencies into account.
```

#### myapp (bin)

Example bin script (`bin/myapp`) that requires the shared block of code
after it locates the correct autoloader.

```php
#!/usr/bin/env php
findPackage('acme/myapp');
```

Composer does not currently install the root package in the `installed.json`
that represents the local repository. There is a PR out for this to be added
to Composer core but until then the following workaround can be used.

Add the following `post-autoload-dump` script to the root package's
`composer.json`:

```json
{
"scripts": {
"post-autoload-dump": "Dflydev\\EmbeddedComposer\\Core\\Script::postAutoloadDump"
}
}
```

This will write an additional repository to
`vendor/dflydev/embedded-composer/.root_package.json` and Embedded Composer
will automatically use it if it can be found. It is important to ensure
that this file is a part of any phar built if the root package needs to be
included in the distribution.

#### Create a Composer Installer instance

The Installer instance is suitable for processing `install` and `update`
operations against the external configuration. It will take the internal
(embedded) configuration into account when solving dependencies.

```php
createInstaller($io);
```

#### Create a vanilla Composer instance

```php
createComposer($io);
```

License
-------

MIT, see LICENSE.

Community
---------

If you have questions or want to help out, join us in the **#dflydev** channel
on **irc.freenode.net**.

Not Invented Here
-----------------

Much of the work here has been supported by the Composer team and many
people in `#composer-dev`.

The actual code started its life as a part of [Sculpin][3] and was spun
out into a standalone project.

[1]: http://getcomposer.org
[2]: https://packagist.org/packages/dflydev/embedded-composer
[3]: https://sculpin.io