简体   繁体   English

在Composer中将名称空间添加到导入的第三方程序包

[英]Adding namespace to imported third-party package in Composer

I am importing a third-party package into my project using composer. 我正在使用composer将第三方程序包导入到我的项目中。

The composer.json of the package autoloads its classes using "classmap" : 包的composer.json使用"classmap"自动加载其类:

{
  ...
  "name"=>"vendor/project",
  ...
  "require": {
    "php": ">=5.2.0"
  },
  "type": "library",
  "include-path":["src/"],
  "classmap": [
    "src/path/to/lib1",
    "src/path/to/lib2"
  ]
  ...
}

My project composer.json pulls the package in using "require" . 我的项目composer.json使用"require"拉包。

{
  ...
  "require": {
    "vendor/project": "m.n.*",
  }
  ...
}

I'd like to add a namespace that can prefix all the classes of that package when I use it in my project, can I do this in composer? 我想添加一个可以在我的项目中使用该包的所有类作为前缀的名称空间,我可以在composer中这样做吗?

I am aware I can use autoload at the level of my project, but presumably these classes don't need loading again and where do I point it? 我知道我可以在项目级别使用自动加载功能,但是大概这些类不需要再次加载,我该在哪里指向它?

You cannot add a namespace to a project without editing every individual file in the project and adding a namespace .. declaration at its top. 如果不编辑项目中的每个文件并在其顶部添加namespace ..声明,则无法将名称空间添加到项目中。 This is likely infeasible. 这可能是不可行的。

If you namespace your own code, there should be no possible problem of a name clash. 如果您为自己的代码命名空间,则应该不会出现名称冲突的问题。
If the library clashes with yet another non-namespaced third party library which you also cannot feasibly namespace, then you're in trouble. 如果该库与另一个没有命名空间的第三方库(您也无法切实可行地命名空间)发生冲突,那么您将遇到麻烦。 Unless this is the case, there's no real reason to worry about it. 除非是这种情况,否则没有真正的理由担心。

If composer's definition is set up properly, all you should need to do is simply use the class: 如果正确地设置了作曲家的定义,那么您只需要做的就是使用该类:

$foo = new \VendorClass;

Composer's autoloading will take care of loading the class, a missing namespace is of no concern (see above). Composer的自动加载将负责加载类,而缺少名称空间则无关紧要(请参见上文)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM