vagrant 打包box
In this tutorial, we’ll use Box to package a PHP application into a Phar, in order to make it easily distributable and globally installable via Composer.
在本教程中,我们将使用Box将PHP应用程序打包到Phar中,以使其易于通过Composer分发并在全球范围内安装。
We need a project for this, and packaging up yet another “Hello World” script would be pointless. We’ll use Webiny’s FolderBuilder, a tool which you can use to plan out the folder structure for your project, interactively drag and drop files and folders, and even export and share these structures with others.
为此,我们需要一个项目,而打包另一个“ Hello World”脚本将毫无意义。 我们将使用Webiny的FolderBuilder ,该工具可用于计划项目的文件夹结构,以交互方式拖放文件和文件夹,甚至与他人导出并共享这些结构。
But that’s almost entirely a JavaScript tool, why would we be using Phars for it? Yes, that GUI is JS, but if you look at the repo you’ll notice a PHP script inside which you can run on any folder and get the required JSON generated. This means you can easily generate FolderBuilder compatible structures from existing projects, too, which is super handy for planning a project’s restructuring.
但这几乎完全是一个JavaScript工具,为什么我们要使用Phars? 是的,该GUI 是 JS,但是如果您查看存储库,您会注意到一个PHP脚本,您可以在其中运行任何文件夹并生成所需的JSON。 这意味着您也可以轻松地从现有项目中生成与FolderBuilder兼容的结构,这对于计划项目的重组非常方便。
While it’s very simple to just download and run the script from any folder you’d like to parse into JSON, why not make the whole project Phar-distributable and allow for something like:
从您想解析为JSON的任何文件夹中下载并运行脚本非常简单,但为什么不将整个项目分配为Phar并允许以下操作:
composer global require webiny/folderbuilder
folderbuilder . > structure.json
Let’s bootstrap a Box project. First, we need to install Box if it’s not already present on our system. I am, of course, using Homestead Improved as usual to keep things isolated from my host OS.
让我们引导一个Box项目。 首先,如果我们的系统上尚未安装Box,则需要安装它。 我当然会像往常一样使用Homestead Improvement来使事情与主机OS隔离。
composer global require kherge/box
We could download the phar
of Box and use that, but we’re a Composer crowd here, we don’t use those outdated platform-specific methods ;)
我们可以下载Box的phar
并使用它,但是我们这里是Composer的人群 ,我们不使用那些过时的平台特定方法;)
Now that’s done, the box
command is accessible from anywhere on our machine:
现在完成了,可以从我们机器上的任何位置访问box
命令:
To package an app with Box, one needs to include a box.json
file in the project’s folder. The simplest of these can be seen in the example application:
要使用Box打包应用程序,需要在项目的文件夹中包含box.json
文件。 这些最简单的可以在示例应用程序中看到:
{
"files": ["src/Put.php"],
"main": "bin/main",
"output": "example.phar",
"stub": true
}
The project we’ll be packaging has a lot of files, but only one we actually need for the Phar to be useful, so to keep it light, we’ll only package that one (and another, but more on that later). In that regard, our json
file won’t be all that different (though if you’d like to see a more complex one, see the from Box itself here). Let’s begin!
我们将要打包的项目有很多文件,但实际上只有一个文件才能使Phar有用,因此为了保持轻巧,我们将只打包一个(另一个,以后再打包)。 在这方面,我们的json
文件不会有什么不同(尽管如果您想看到一个更复杂的文件,请在此处查看from Box本身)。 让我们开始!
Note for Vagrant users: when using Vagrant boxes such as Homestead Improved the Phar extension won’t be able to write files for some reason. The solution is to either change the “output” property to a folder outside the shared one, or to do everything outside a shared folder altogether. We’ll take the latter approach in this case and just do everything inside the Vagrant user’s “home” folder.
给Vagrant用户的注意事项 :当使用诸如Homestead Enhanced之类的Vagrant框时,由于某种原因,Phar扩展名将无法写入文件 。 解决方案是将“输出”属性更改为共享文件夹之外的文件夹,或者完全执行共享文件夹之外的所有操作。 在这种情况下,我们将采用后一种方法,只需在Vagrant用户的“ home”文件夹中进行所有操作即可。
The first thing we do is, of course, clone the repo:
我们要做的第一件事当然是克隆仓库:
cd ~
git clone https://github.com/webiny/folderbuilder
cd folderbuilder
git checkout 74b234fa33bd69690a2c26df38ef7d188c4e69eb
The last command is necessary so that you end up in a state of the project before I applied the fixes outlined in this post.
最后一条命令是必需的,这样您才能在我应用本文中概述的修补程序之前进入项目状态。
Then, we create the file box.json
and populate it with:
然后,我们创建文件box.json
并填充:
{
"files": ["structure.php"],
"output": "bin/wfb.phar",
"stub": true,
"main": "structure.php"
}
So what does this mean?
那么这是什么意思?
The “files” property lists all the files we want to include in the Phar. “output” is the product of our build, and “main” indicates the entry file. “stub” is required when using CLI apps. The help file says:
“文件”属性列出了我们要包含在Phar中的所有文件。 “输出”是我们构建的产品,“ main”表示入口文件。 使用CLI应用程序时需要“存根”。 帮助文件显示:
“The stub (string, boolean) setting is used to specify the location of a stub file, or if one should be generated. If a path is provided, the stub file will be used as is inside the Phar. If true is provided, a new stub will be generated. If false (or nothing) is provided, the default stub used by the Phar class will be used.” and further explanation can be found here.
“存根(字符串,布尔值)设置用于指定存根文件的位置,或者是否应生成一个存根文件。 如果提供了路径,则将按原样使用Phar中的存根文件。 如果提供true,则将生成一个新的存根。 如果提供false(或不提供任何值),则将使用Phar类使用的默认存根。” 进一步的解释可以在这里找到。
We make a bin folder because it’s a common place to put built/compiled resources.
我们创建一个bin文件夹是因为它是放置构建/编译资源的常见位置。
Then, we run:
然后,我们运行:
box build -v
Box will automatically detect the box.json
file in the folder and produce output not unlike this one:
Box将自动检测文件夹中的box.json
文件,并产生与以下内容不同的输出:
? Output path: /home/vagrant/folderbuilder/bin/wfb.phar
? Adding files...
+ /home/vagrant/folderbuilder/structure.php
? Adding main file: /home/vagrant/folderbuilder/structure.php
? Generating new stub...
* Done.
If we now execute the command:
如果现在执行命令:
php bin/wfb.phar
The current directory’s structure will be printed on screen in JSON format, just like if we ran php structure.php
directly.
当前目录的结构将以JSON格式显示在屏幕上,就像我们直接运行php structure.php
。
But how can we make it run without needing to specify either php
or .phar
? If you look inside the contents of wfb.phar
, the first two lines will read:
但是,如何在不指定php
或.phar
情况下使其运行? 如果您查看wfb.phar
的内容,前两行将显示为:
#!/usr/bin/env php
This means “When this file is executed, use the PHP environment to chew it through”. To be able to do this, though, we need to make the file executable, and we do this by adding a “chmod” flag to our box.json
file. While we’re at it, we can also make sure the files we include in the phar are compacted by using the two default compressors included with Box:
这意味着“执行此文件时,请使用PHP环境进行检查”。 为了能够做到这一点,我们需要使文件可执行,并通过在box.json
文件中添加“ chmod”标志来做到这一点。 在使用它的同时,我们还可以通过使用Box随附的两个默认压缩器来确保压缩包含在phar中的文件:
{
"files": ["structure.php", "bin/stub.php"],
"output": "bin/wfb.phar",
"stub": true,
"main": "bin/stub.php",
"chmod": "0755",
"compactors": [
"Herrera\\Box\\Compactor\\Json",
"Herrera\\Box\\Compactor\\Php"
]
}
Now, rerun box build
and after it’s done, try running:
现在,重新运行box build
,完成后,尝试运行:
bin/wfb.phar
It should work. We still have to specify the extension though, and just removing it from box.json
will cause Box to throw errors at us. What if we just removed .phar
from the filename and hoped for the best?
它应该工作。 不过,我们仍然必须指定扩展名,仅将其从box.json
删除会导致Box向我们抛出错误。 如果我们只是从文件名中删除.phar
并希望达到最佳效果,该怎么办?
mv bin/wfb.phar bin/wfb
bin/wfb
Lo and behold, it works! Okay, now how do we distribute this?
瞧,它起作用了! 好的,现在我们该如何分发?
If your project already has a composer.json
file, then all you need to do is add a bin
field for vendor binaries:
如果您的项目已经有一个composer.json
文件,那么您要做的就是为供应商的二进制文件添加一个bin
字段:
"bin": ["bin/wfb"]
In the case of FolderBuilder, there was no composer.json
file at all, so I created one from scratch with composer init
. The final version is here.
就FolderBuilder而言,根本没有composer.json
文件,因此我使用composer init
从头开始创建了一个文件。 最终版本在这里 。
The project also needed to be put onto Packagist. Following the instructions in this post, that’s a 5-minute endeavor.
该项目也需要放在Packagist上。 按照这篇文章中的说明进行操作需要5分钟。
With everything done, let’s try it out. I’m going to fire up a new Homestead Improved instance for that, just so I’m 100% sure I’m starting with a fresh environment, and so that I don’t have to uninstall anything from the environment we’ve built this tutorial on.
完成所有操作后,让我们尝试一下。 为此,我将启动一个新的Homestead Improvementd实例,以确保我100%确信我要从一个新鲜的环境开始,这样我就不必从我们构建的环境中卸载任何东西了。本教程。
Starting a new HI instance for testing is literally 5 lines of shell commands:
启动一个新的HI实例进行测试实际上是5行shell命令:
git clone https://github.com/swader/homestead_improved hi_fbtest
cd hi_fbtest
sed -i '' "s@map\: \.@map\: $PWD@g” Homestead.yaml
vagrant up
vagrant ssh
Now inside my new VM, I run:
现在在我的新VM中,运行:
composer global require webiny/folderbuilder
That should be enough. Let’s see if it works. I want to map out the Code folder (i.e. the root of the Homestead Improved project, should produce the scripts folder, Homestead.yaml
, and other files and folders).
那应该足够了。 让我们看看它是否有效。 我想映射出Code文件夹(即Homestead Improvement项目的根,应生成脚本文件夹Homestead.yaml
以及其他文件和文件夹)。
wfb ~/Code > out.json
And indeed, the file is there! Pasting its contents into FolderBuilder, we get exactly what we asked for.
确实,文件在那里! 将其内容粘贴到FolderBuilder中 ,就可以得到我们所需要的。
In this tutorial, we looked at boxing up PHP code into Phars with the Box project. We discussed making them executable and we explained how to distribute these Phars during Composer installation, so that they become immediately accessible from everywhere once globally required. We could talk about signing the Phars or building their auto-update scripts, but that was already done better by someone else – check that post out if you’d like to learn more advanced aspects.
在本教程中,我们研究了通过Box项目将PHP代码装箱到Phars中。 我们讨论了使其成为可执行文件,并解释了如何在Composer安装期间分发这些Phar,以便一旦全局需要它们就可以从任何地方立即访问它们。 我们可以讨论签署Phars或构建其自动更新脚本的方法,但是其他人已经做得更好–如果您想了解更多高级方面,请检查发布。
As for Webiny’s folder builder, it’s open source and welcoming contributions. Have ideas on how to improve it further? Maybe add tree creation into the mix, reading a JSON file and creating what was developed in the GUI? Go for it, submit a PR!
至于Webiny的文件夹生成器,它是开源的,也很受欢迎。 是否有进一步改进的想法? 也许将树创建添加到混合中,读取JSON文件并创建在GUI中开发的内容? 去吧,提交一份公关!
Did you see any missteps in our process? Do you follow the same workflow or do you package your Phars differently? Let us know in the comments!
您在我们的流程中看到任何失误吗? 您遵循相同的工作流程还是打包不同的Phars? 让我们在评论中知道!
翻译自: https://www.sitepoint.com/boxing-apps-phars-quickly-easily-box/
vagrant 打包box