定制 nanbando/embedded-composer 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

nanbando/embedded-composer

Composer 安装命令:

composer require nanbando/embedded-composer

包简介

Embed Composer into another application

README 文档

README

Embed Composer into another application.

Installation

Through Composer as dflydev/embedded-composer.

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
// assume $classLoader is somehow defined prior to this block of
// code and contains the Composer class loader from the command
//
// see next two blocks of code

use Dflydev\EmbeddedComposer\Core\EmbeddedComposerBuilder;
use Symfony\Component\Console\Input\ArgvInput;

$input = new ArgvInput;

$projectDir = $input->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.

#!/usr/bin/env php
<?php

if (
    // Check where autoload would be if this is myapp included
    // as a dependency.
    (!$classLoader = @include __DIR__.'/../../../autoload.php') and

    // Check where autoload would be if this is a development version
    // of myapp. (based on actual file)
    (!$classLoader = @include __DIR__.'/../vendor/autoload.php')
) {
    die('You must set up the project dependencies, run the following commands:

    composer install

');
}

include('myapp.php');

myapp-phar-stub (bin)

Example phar stub (bin/myapp-phar-stub) that can be used to bootstrap a phar application prior to requiring the shared block of code.

#!/usr/bin/env php
<?php

if (!$classLoader = @include __DIR__.'/../vendor/autoload.php') {
    die ('There is something terribly wrong with your archive.
Try downloading it again?');
}

include('myapp.php');

What else ...

Find installed package by name

One can search for any package that Composer has installed by using the findPackage method:

<?php
$package = $embeddedComposer->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:

{
    "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
// requires creating an IOInterface instance
$installer = $embeddedComposer->createInstaller($io);

Create a vanilla Composer instance

<?php
// requires creating an IOInterface instance
$composer = $embeddedComposer->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 and was spun out into a standalone project.

nanbando/embedded-composer 适用场景与选型建议

nanbando/embedded-composer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 4 次下载、GitHub Stars 达 0, 最近一次更新时间为 2021 年 06 月 23 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「composer」 「embedded」 「extensibility」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 nanbando/embedded-composer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 nanbando/embedded-composer 我们能提供哪些服务?
定制开发 / 二次开发

基于 nanbando/embedded-composer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 8
  • 依赖项目数: 1
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2021-06-23