{"id":14985443,"url":"https://github.com/dflydev/dflydev-embedded-composer","last_synced_at":"2025-08-09T22:12:03.489Z","repository":{"id":5939950,"uuid":"7160371","full_name":"dflydev/dflydev-embedded-composer","owner":"dflydev","description":"Embed Composer into another application","archived":false,"fork":false,"pushed_at":"2018-04-18T14:50:05.000Z","size":52,"stargazers_count":71,"open_issues_count":5,"forks_count":17,"subscribers_count":6,"default_branch":"master","last_synced_at":"2025-05-09T03:54:08.600Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dflydev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-12-14T05:16:12.000Z","updated_at":"2023-06-24T23:52:55.000Z","dependencies_parsed_at":"2022-09-11T02:00:27.241Z","dependency_job_id":null,"html_url":"https://github.com/dflydev/dflydev-embedded-composer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/dflydev/dflydev-embedded-composer","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dflydev%2Fdflydev-embedded-composer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dflydev%2Fdflydev-embedded-composer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dflydev%2Fdflydev-embedded-composer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dflydev%2Fdflydev-embedded-composer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dflydev","download_url":"https://codeload.github.com/dflydev/dflydev-embedded-composer/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dflydev%2Fdflydev-embedded-composer/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269645821,"owners_count":24452809,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-08-09T02:00:10.424Z","response_time":111,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-09-24T14:10:58.832Z","updated_at":"2025-08-09T22:12:03.462Z","avatar_url":"https://github.com/dflydev.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Embedded Composer\n=================\n\nEmbed [Composer][1] into another application.\n\n\nInstallation\n------------\n\nThrough [Composer][1] as [dflydev/embedded-composer][2].\n\n\nWhy Would I Want To Embed Composer?\n-----------------------------------\n\nImagine a console application shipped as a phar. If it is desired for the\napplication to be extensible based on which directory it is in (say one set\nof plugins should be used in one directory but an entirely different set of\nplugins used in another directory) one cannot simply define a `composer.json`\nin both directories and run `composer install`.\n\nWhy not? Because the application shipped with a specific set of dependencies.\nComposer cannot add more dependencies without running the risk of introducing\nconflicts. The answer is to embed Composer into the application so that\nComposer can merge the dependencies already installed for the application\nwith the dependencies defined in a specific directory's `composer.json`.\n\nThe end result is a set of dependencies that satisfy the directory specific\nrequirements while taking into account the dependencies *already installed*\nfor the console application.\n\nWhile this is required for a phar distributed application this technique can\nbe applied to any globally installed application that needs to be runtime\nextensible.\n\n\nUsage\n-----\n\n### Basics\n\nThe following is an example `bin/myapp` style script that can be used either\ninstalled via Composer (`vendor/bin/myapp`) or installed globally\n(`/usr/local/bin/myapp`).\n\n#### myapp.php (bin)\n\nA shared block of code to initialize Embedded Composer from an application.\n\n```php\n\u003c?php\n// assume $classLoader is somehow defined prior to this block of\n// code and contains the Composer class loader from the command\n//\n// see next two blocks of code\n\nuse Dflydev\\EmbeddedComposer\\Core\\EmbeddedComposerBuilder;\nuse Symfony\\Component\\Console\\Input\\ArgvInput;\n\n$input = new ArgvInput;\n\n$projectDir = $input-\u003egetParameterOption('--project-dir') ?: '.';\n\n$embeddedComposerBuilder = new EmbeddedComposerBuilder(\n    $classLoader,\n    $projectDir\n);\n\n$embeddedComposer = $embeddedComposerBuilder\n    -\u003esetComposerFilename('myapp.json')\n    -\u003esetVendorDirectory('.myapp')\n    -\u003ebuild();\n\n$embeddedComposer-\u003eprocessAdditionalAutoloads();\n\n// application is now ready to be run taking both the embedded\n// dependencies and directory specific dependencies into account.\n```\n\n\n#### myapp (bin)\n\nExample bin script (`bin/myapp`) that requires the shared block of code\nafter it locates the correct autoloader.\n\n```php\n#!/usr/bin/env php\n\u003c?php\n\nif (\n    // Check where autoload would be if this is myapp included\n    // as a dependency.\n    (!$classLoader = @include __DIR__.'/../../../autoload.php') and\n\n    // Check where autoload would be if this is a development version\n    // of myapp. (based on actual file)\n    (!$classLoader = @include __DIR__.'/../vendor/autoload.php')\n) {\n    die('You must set up the project dependencies, run the following commands:\n\n    composer install\n\n');\n}\n\ninclude('myapp.php');\n```\n\n#### myapp-phar-stub (bin)\n\nExample phar stub (`bin/myapp-phar-stub`) that can be used to bootstrap\na phar application prior to requiring the shared block of code.\n\n```php\n#!/usr/bin/env php\n\u003c?php\n\nif (!$classLoader = @include __DIR__.'/../vendor/autoload.php') {\n    die ('There is something terribly wrong with your archive.\nTry downloading it again?');\n}\n\ninclude('myapp.php');\n```\n\n### What else ...\n\n#### Find installed package by name\n\nOne can search for any package that Composer has installed by using\nthe `findPackage` method:\n\n```php\n\u003c?php\n$package = $embeddedComposer-\u003efindPackage('acme/myapp');\n```\n\nComposer does not currently install the root package in the `installed.json`\nthat represents the local repository. There is a PR out for this to be added\nto Composer core but until then the following workaround can be used.\n\nAdd the following `post-autoload-dump` script to the root package's\n`composer.json`:\n\n```json\n{\n    \"scripts\": {\n        \"post-autoload-dump\": \"Dflydev\\\\EmbeddedComposer\\\\Core\\\\Script::postAutoloadDump\"\n    }\n}\n```\n\nThis will write an additional repository to\n`vendor/dflydev/embedded-composer/.root_package.json` and Embedded Composer\nwill automatically use it if it can be found. It is important to ensure\nthat this file is a part of any phar built if the root package needs to be\nincluded in the distribution.\n\n\n#### Create a Composer Installer instance\n\nThe Installer instance is suitable for processing `install` and `update`\noperations against the external configuration. It will take the internal\n(embedded) configuration into account when solving dependencies.\n\n```php\n\u003c?php\n// requires creating an IOInterface instance\n$installer = $embeddedComposer-\u003ecreateInstaller($io);\n```\n\n#### Create a vanilla Composer instance\n\n```php\n\u003c?php\n// requires creating an IOInterface instance\n$composer = $embeddedComposer-\u003ecreateComposer($io);\n```\n\n\nLicense\n-------\n\nMIT, see LICENSE.\n\n\nCommunity\n---------\n\nIf you have questions or want to help out, join us in the **#dflydev** channel\non **irc.freenode.net**.\n\n\nNot Invented Here\n-----------------\n\nMuch of the work here has been supported by the Composer team and many\npeople in `#composer-dev`.\n\nThe actual code started its life as a part of [Sculpin][3] and was spun\nout into a standalone project.\n\n\n[1]: http://getcomposer.org\n[2]: https://packagist.org/packages/dflydev/embedded-composer\n[3]: https://sculpin.io\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdflydev%2Fdflydev-embedded-composer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdflydev%2Fdflydev-embedded-composer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdflydev%2Fdflydev-embedded-composer/lists"}