作曲备忘单

This article was sponsored by AppDynamics. Thank you for supporting the sponsors who make SitePoint possible!

本文由AppDynamics赞助。 感谢您支持使SitePoint成为可能的赞助商!

Unless you’ve been living under a rock, today’s PHP isn’t your grandmother’s PHP; it’s an entirely different, much more elegant and mature language with countless improvements and additions.

除非您一直生活在一块石头上,否则今天PHP并不是您祖母PHP。 它是一门完全不同,更加优雅和成熟的语言,具有无数的改进和补充。

One of the key additions is Composer, the de facto standard for managing PHP project dependencies which, by default, gives you access to hundreds of ready-made libraries, via Packagist.org.

其中最重要的一项功能是Composer ,它是用于管理PHP项目依赖关系的事实上的标准,默认情况下,通过Packagist.org ,您可以访问数百个现成的库。

I’m not going to go over how to use Composer, as it’s been covered so well here on SitePoint already; especially by this article, by Alexander Cogneau.

我不会讨论如何使用Composer,因为在SitePoint上已经对此进行了很好的介绍。 特别是亚历山大·科尼奥(Alexander Cogneau) 的这篇文章

Instead, I’m approaching it from a different angle, taking you through the excellent Composer cheat sheet, which I came across recently.

取而代之的是,我从另一个角度研究了它,并带您浏览了我最近遇到的出色的Composer备忘单

If you’d rather watch a quick video overview, play the embedded one below. Otherwise, continue reading. I encourage you to do both.

如果您想观看快速视频概述,请播放下面的嵌入式视频。 否则,请继续阅读。 我鼓励您同时做。

Like any complex, feature rich, tool there’s so much that you can do with Composer. For example, you can use the composer.phar file to:

像任何复杂的,功能丰富的工具一样,Composer可以做很多事情。 例如,您可以使用composer.phar文件执行以下操作:

  • Add composer support to an existing project

    将作曲家支持添加到现有项目
  • Run scripts in response to various events, such as pre and post install, pre and post package updates

    运行脚本以响应各种事件 ,例如安装 前和 安装后,安装 前和 安装 后更新

  • Specify custom autoloaders

    指定自定义自动加载器
  • Differentiate dependencies, based on whether the project is in development, testing, live, or another specific environment

    根据项目是处于开发测试实时还是其他特定环境中来区分依赖项

  • Make use of the hundreds of readily available packages via Packagist.org

    通过Packagist.org利用数百个随时可用的软件包

Through these samples alone, you can see that there’s a lot of functionality, as well as a lot to remember.

仅通过这些示例,您就可以看到有很多功能,还有很多需要记住的地方

So whilst you use it in every project, do you know all that it has to offer? Or do you use the same set of command line switches and configuration options time and time again, without thinking about what else is available?

因此,当您在每个项目中使用它时,您知道它所提供的全部吗? 还是您一次又一次地使用同一组命令行开关和配置选项,而不考虑其他可用选项?

I’d suggest, if you’re anything like me, you’re in the latter camp. Today I want to help you rectify that and really get the most out of Composer, by going through the cheat sheet.

我建议,如果你像我一样,那就在后一个阵营。 今天,我想帮助您纠正这一点,并通过备忘单来真正从Composer中获得最大收益。

I’m sure you’ll see just why it’s so well deserving of the name; and before long you’ll start having it in a pinned tab, much like the PHP manual. It’s that good!

我敢肯定,您会明白它为什么如此配得上名字; 不久之后,您将开始在固定选项卡中使用它,就像PHP手册一样。 就是那样!

2个关键部位 (2 Key Parts)

Essentially, it’s comprised of two key parts:

从本质上讲,它包括两个关键部分:

  1. The composer.phar command line switches

    composer.phar命令行开关
  2. The composer.json file

    composer.json文件

composer.phar命令行开关 (The composer.phar Command Line Switches)

alt

This section lists all of the command line switches you can pass to composer.phar, at the command line. What’s more, by mousing over them, on the right hand side, you’ll see a fuller description of what each one does.

本节列出了您可以在命令行传递给composer.phar所有命令行开关。 此外,通过将鼠标悬停在它们的右侧,您将看到每个人的工作的完整描述。

It lists everything from the most common, regularly, used options to the more advanced. Here’s a sample:

它列出了从最常见,最常用的选项到更高级的所有内容。 这是一个示例:

  • install – which sets up the dependencies in the vendor directory

    安装 –在供应商目录中设置依赖项

  • self-update – which updates the composer.phar file to the latest version

    自我更新 –将composer.phar文件更新到最新版本

  • init – which sets up composer support in an existing, or new, project

    init –在现有或新项目中设置作曲家支持

  • validate – which valises an existing composer.json file

    验证 –评估现有的composer.json文件

  • diagnose – for performing automated checks

    诊断 –执行自动检查

  • archive – which creates an archive of the composer package.

    存档 –创建composer软件包的存档。

By using the cheat sheet, I learned that by passing the --optimize switch to dump-autoload, you can get anything up to a 20% performance boost in your projects.

通过使用备忘单,我了解到,通过将--optimize开关传递给dump-autoload ,您可以在项目中获得高达20%的性能提升

None too shabby, wouldn’t you say? Ok, I could have found this out through testing, or dug through the documentation, but the cheat sheet made it quick and easy to find.

没有一个太破旧,你不说吗? 好的,我可以通过测试找到它,也可以翻阅文档,但是备忘单使您可以快速,轻松地找到它。

composer.json文件配置 (The composer.json File Configuration)

alt

I’d suggest this is the section where you’ll spend most of your time with the cheat sheet. Whilst calling composer.phar can largely be scripted away, the per/project configurations you need may differ regularly enough to require a variety of different options each time.

我建议这是您将大部分时间都花在备忘单上的部分。 尽管可以大范围地编写脚本来调用composer.phar,但是您所需的每个项目配置可能经常有所不同,以至于每次都需要各种不同的选项。

So the composer.json section is definitely your friend in this regard. As with composer.phar, it displays an example, utilising every option. It lists all the options right from the most common, such as name, description, authors, and require which provide key information about the package, along with its dependencies.

因此,在这方面,composer.json部分绝对是您的朋友。 与composer.phar一样,它利用每个选项显示一个示例。 它列出了最常见的所有选项,例如namedescriptionauthorsrequire ,这些选项提供了有关软件包及其依赖项的关键信息。

Authors is a good example, showing the name, email, homepage, and role of 2 sample authors, which you can see below:

作者是一个很好的例子,其中显示了2个示例作者的姓名电子邮件主页角色 ,您可以在下面看到它们:

"authors": [
    {
        "name": "Xavier Lacot",
        "email": "xlacot@jolicode.com",
        "homepage": "http://www.lacot.org",
        "role": "Developer"
    },
    {
        "name": "Benjamin Clay",
        "email": "bclay@jolicode.com",
        "homepage": "http://ternel.net",
        "role": "Developer"
    }
],

Then there’s the more advanced ones which we likely use not as often.

然后是一些更高级的,我们可能不会经常使用。

  • support – where you find the list of support options, for when users need assistance

    支持 –当用户需要帮助时,可在其中找到支持选项列表

  • autoload – where you can set specific autoload mapping options for the PHP autoloader

    自动加载 –您可以在其中为PHP自动加载器设置特定的自动加载映射选项

  • repositories – allowing you to go beyond the standard Packagist backend, adding PEAR, use version control tools, such as GitHub, Bitbucket or a self-hosted, private, packages using Satis.

    存储库 –允许您超越标准Packagist后端,添加PEAR,使用版本控制工具,例如GitHub,Bitbucket或使用Satis的自托管私有软件包。

Here’s a sample of the scripts section. You can see that it lists how to use static class methods, of namespaced classes, to respond to several events.

这是脚本部分的示例。 您会看到它列出了如何使用命名空间类的静态类方法来响应多个事件。

"scripts": {
    "post-update-cmd": MyVendor\\MyClass::postUpdate",
    "post-package-install": [
        "MyVendor\\MyClass::postPackageInstall"
    ],
    "post-install-cmd": [
        "MyVendor\\MyClass::warmCache",
        "phpunit -c app/"
    ]
},

As with the composer.phar section, by mousing over them, you’ll see the section specifics, on the right hand side, as well as example uses.

与composer.phar部分一样,通过将它们悬停在它们上面,您会在右侧看到该部分的详细信息以及示例用法。

结论 ( Conclusion)

I believe it’s a great tool for getting the most out of Composer. Some might say that at one page long, it’s not sufficiently detailed; I disagree.

我相信这是充分利用Composer的绝佳工具。 有人可能会说一页纸不够详细。 我不同意

The way the cheat sheet is composed, showing sample usages of each command line option, and listing out a complete composer.json file example, combined with a detailed description and further usage examples of each aspect, is an elegant approach.

备忘单的编写方式(显示每个命令行选项的示例用法,并列出完整的composer.json文件示例)以及每个方面的详细描述和更多用法示例,是一种不错的方法。

You’re able to get as much information as you need, without being overloaded. What do you think? Do you think it could go further, and provide more information? Or is it just the right length? Share your thoughts in the comments.

您可以获取所需的尽可能多的信息,而不会超载。 你怎么看? 您认为它可以走得更远并提供更多信息吗? 还是长度合适? 在评论区分享你的观点。

As it’s Open Source, if you see something that needs improving, a typo, or generally want to add more information, get in there, fork it and send a Pull Request.

在开放源代码中,如果您发现需要改进的地方,打字错误,或者通常想添加更多信息,请进入那里,进行分叉并发送“拉取请求”

Otherwise, enjoy.

否则,请享受。

翻译自: https://www.sitepoint.com/composer-cheatsheet/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值