PHP:处理项目中的文件版本依赖关系

PHP:处理项目中的文件版本依赖关系,php,dependencies,version,Php,Dependencies,Version,我知道依赖注入模式,它似乎是处理需要其他类的实例才能正常运行的类的最佳解决方案。但是,当涉及到处理某个类存在但版本错误的场景时,DI或任何其他OOP模式显然都不会有任何帮助 在PHP中处理库的基于版本的依赖关系的好方法是什么? 下面是一个演示我的意思的方法(psuedo代码): 类支付模块基础{ var$baseVersion; 函数_u构造(){ $this->baseversion=12;//版本12 } //其他相关方法。。。 } 类别卡支付扩展支付模块基础{ 函数构造(){ 如果($th

我知道依赖注入模式,它似乎是处理需要其他类的实例才能正常运行的类的最佳解决方案。但是,当涉及到处理某个类存在但版本错误的场景时,DI或任何其他OOP模式显然都不会有任何帮助

在PHP中处理库的基于版本的依赖关系的好方法是什么?

下面是一个演示我的意思的方法(psuedo代码):

类支付模块基础{
var$baseVersion;
函数_u构造(){
$this->baseversion=12;//版本12
}
//其他相关方法。。。
}
类别卡支付扩展支付模块基础{
函数构造(){

如果($this->baseversion我只是不在PHP中处理它,而是在服务器、DI配置或自动加载程序中处理它。当我需要使用同一库的多个不同版本时,我通常设置我的目录结构如下:

/usr/share/php/SomeLibrary-1.0
/usr/Share/php/SomeLibrary-2.0
/usr/share/php/SomeLibrary --> SomeLibrary-2.0
每个版本都放在一个单独的版本化文件夹中。有一个指向最新版本的未版本化符号链接

在我的DI容器中(我经常使用symfony components中的容器),我只需将其配置为从正确的路径加载文件。或者,您可以设置自动加载程序,以便加载正确的版本

简而言之,要使用的库版本(以及最终的库路径)是应用程序配置的一部分,而不是应用程序代码

更新

当一个库需要另一个库的特定版本,而它只能找到不正确的版本时,我只需抛出一个异常并让应用程序处理它(通常意味着显示错误并退出)。我不尝试加载库的特定版本。我加载sfServiceContainer(我选择的依赖项注入解决方案)的配置文件中配置的任何内容。如果版本错误,则管理员必须更新配置设置


不要尝试自动搜索不同的库版本,而是尝试加载正确的库版本。只需让人工配置从哪个路径加载哪个库(并提供具有正常默认值的配置).

很多人都相信库文件顶部的定义。它很简单,性能也很好……可能太简单了

// -- At the top of the library, we define this ...
define('PAYMENT_LIBRARY_VER', 324);

// -- At some point in the code, we check it!
if( defined('PAYMENT_LIBRARY_VER') && PAYMENT_LIBRARY_VER <= 320 )
    throw new HissyFit("This is just an alias for an exception.");
此模型还支持构建依赖项标头

if( PaymentProcessorManager::LATEST_VERSION < 300
 || OtherModuleManager::LATEST_VERSION < 240 )
    throw new HissyFit("The minimum requirements for this functionality are not met.");

你好,桑德。谢谢你的回答。我想你的自动加载器然后会创建某种数组或对象,列出每个库及其版本,这样你就可以运行类似于
if(get::fileversion(2)>12)
(psuedo…)的程序来确保依赖性了?想了解更多信息吗?赏金开启了!
// -- In a file far far away ...
class PaymentProcessor_r324 extends PaymentProcessor
{
    ...
}

// -- Somewhere else ...
class PaymentProcessor_r325 extends PaymentProcessor
{
    ...
}

// -- In the main library class "holder"
class PaymentProcessorManager
{
    /**
     * The latest version of the library
     */
    const LATEST_VERSION = 324;

    /**
     * Initialise a new instance of the PaymentProcessor.
     * @param $ver
     * @return PaymentProcessor
     */
    public static function Initialise( $revision = null )
    {
        if( $revision === null )
            $revision = self::LATEST_VERSION;

        var $c = "PaymentProcessor_r{$revision}";

        return new $c();
    }
}

// -- Pretty much anywhere you like, and autoloading friendly to boot.
if( PaymentProcessorManager::LATEST_VERSION < 300 )
    throw new HissyFit("This is just an alias for an exception.");
// -- Even though the 'latest version' is 324, there's nothing to stop me
//    from loading my development version, #325.
var $paymentProcessor = PaymentProcessorManager::Initialise(325);
if( PaymentProcessorManager::LATEST_VERSION < 300
 || OtherModuleManager::LATEST_VERSION < 240 )
    throw new HissyFit("The minimum requirements for this functionality are not met.");
class_alias('PaymentProcessor_r' . PaymentProcessorManager::LATEST_VERSION, 'PaymentProcessorLatest');