如何创建要由作曲家自动加载使用的库? [英] How to create a library to be used by composer autoloading?
问题描述
我要让Composer自动加载此软件包。
此软件包位于 Packagist 上
我意识到我需要向 composer.json
添加一些内容,我需要一个 autoload.php
某处。
应该自动加载的唯一类是 Webbot.php
。
$
$ b
返回的Google搜索结果是自动加载库的说明。
我需要有关如何编写自动加载库的说明。
您的封装结构为PSR-0或PSR-4。我还没有开始使用PSR-4,因为它只是刚刚被接受为一个标准。 Composer将在很长时间内支持PSR-0。
这意味着您必须遵守以下规则:
- 完全限定的命名空间和类必须具有以下
结构< Vendor Name> \(< Namespace> \)*<类名称>
- 每个命名空间必须有顶层命名空间(供应商名称)。
- 当从文件系统加载
时,每个命名空间分隔符将转换为DIRECTORY_SEPARATOR。 - CLASS NAME中的每个_字符都将转换为
DIRECTORY_SEPARATOR。 - 当
从文件系统加载时,完全限定的命名空间和类后缀为.php。 / li>
- 供应商名称,命名空间和类名称中的字母字符
可以是小写和大写的任意组合。
这意味着你的软件包应该放在你的github仓库中,如下所示:
-src
-Simkimsia
-Webbot
-Webbot.php
-composer.json
-license.md
- {任何其他基础文件}
Webbot.php将在命名空间中:<$ c $
然后...因为这是一个github包,你可以添加它到您的项目composer.json使用repositories属性。
{
name:'test',
description:'Test',
keywords:['test'],
repositories:[
{
type:vcs
url:https://github.com/simkimsia/webbot.git
}
],
require:{
simkimsia / webbot:dev-master
}
}
可以从Composers自动加载获取,可以实例化为:
$ webbot = new Simkimsia\Webbot\Webbot
注意:
Composers autoload.php一旦您已运行 composer install
:
/ vendor / composer / autoload .php
只需在PHP脚本的开头包含此文件, / p>
I want to make this package to be autoloaded by Composer.
This package is available on Packagist
I realized I need to add something to composer.json
and I need to have a autoload.php
somewhere.
The only class that should be autoloaded is the Webbot.php
.
Can someone give me the step by step breakdown to accomplish this?
Google search results returned are instructions to autoload libraries.
I need instructions on how to write autoloadable libraries.
First, you need to have your package structured in either PSR-0 or PSR-4. I haven't started using PSR-4 yet as it has only just been accepted as a standard. Composer will still support PSR-0 for a long time to come.
This means that you MUST follow these rules:
- A fully-qualified namespace and class must have the following
structure
<Vendor Name>\(<Namespace>\)*<Class Name>
- Each namespace must have a top-level namespace ("Vendor Name").
- Each namespace can have as many sub-namespaces as it wishes.
- Each namespace separator is converted to a DIRECTORY_SEPARATOR when loading from the file system.
- Each _ character in the CLASS NAME is converted to a DIRECTORY_SEPARATOR. The _ character has no special meaning in the namespace.
- The fully-qualified namespace and class is suffixed with .php when loading from the file system.
- Alphabetic characters in vendor names, namespaces, and class names may be of any combination of lower case and upper case.
This would mean that your package should be laid out in your github repository as follows:
-src
-Simkimsia
-Webbot
-Webbot.php
-composer.json
-license.md
-{any other base level files}
Webbot.php would be in the namespace : Simkimsia\Webbot
as dicated by the directory structure.
Then... As this is a github package, you can add it to your projects composer.json using the repositories property.
{
"name" : 'test',
"description" : 'Test',
"keywords" : ['test'],
"repositories" : [
{
"type": "vcs",
"url": "https://github.com/simkimsia/webbot.git"
}
],
"require" : {
"simkimsia/webbot" : "dev-master"
}
}
The package will be available from Composers autoload and can be instantiated as :
$webbot = new Simkimsia\Webbot\Webbot();
Note:
Composers autoload.php will be available in once you have run composer install
:
/vendor/composer/autoload.php
Just include this file at the start of your PHP script and your classes will be available.
这篇关于如何创建要由作曲家自动加载使用的库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!