Skip to content
This repository was archived by the owner on Jul 1, 2024. It is now read-only.

Commit 9e5eaac

Browse files
committed
Add phar dir issue docs
1 parent 07fb5a0 commit 9e5eaac

File tree

4 files changed

+241
-0
lines changed

4 files changed

+241
-0
lines changed

docs/en/develop/structure.md

+121
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,124 @@ supports automatic loading of the PSR-4 standard, and contains the following sub
5757
If you have read the source code, you may find that there is also a `src/globals/` directory,
5858
which is used to store some global variables, global methods,
5959
and non-PSR-4 standard PHP source code that is relied upon during the build process, such as extension sanity check code etc.
60+
61+
## Phar application directory issue
62+
63+
Like other php-cli projects, spc itself has additional considerations for paths.
64+
Because spc can run in multiple modes such as `php-cli directly`, `micro SAPI`, `php-cli with Phar`, `vendor with Phar`, etc.,
65+
there are ambiguities in various root directories. A complete explanation is given here.
66+
This problem is generally common in the base class path selection problem of accessing files in PHP projects, especially when used with `micro.sfx`.
67+
68+
Note that this may only be useful for you when developing Phar projects or PHP frameworks.
69+
70+
> Next, we will treat `static-php-cli` (that is, spc) as a normal `php` command line program. You can understand spc as any of your own php-cli applications for reference.
71+
72+
There are three basic constant theoretical values below. We recommend that you introduce these three constants when writing PHP projects:
73+
74+
- `WORKING_DIR`: the working directory when executing PHP scripts
75+
76+
- `SOURCE_ROOT_DIR` or `ROOT_DIR`: the root directory of the project folder, generally the directory where `composer.json` is located
77+
78+
- `FRAMEWORK_ROOT_DIR`: the root directory of the framework used, which may be used by self-developed frameworks. Generally, the framework directory is read-only
79+
80+
You can define these constants in your framework entry or cli applications to facilitate the use of paths in your project.
81+
82+
The following are PHP built-in constant values, which have been defined inside the PHP interpreter:
83+
84+
- `__DIR__`: the directory where the file of the currently executed script is located
85+
86+
- `__FILE__`: the file path of the currently executed script
87+
88+
### Git project mode (source)
89+
90+
Git project mode refers to a framework or program itself stored in plain text in the current folder, and running through `php path/to/entry.php`.
91+
92+
Assume that your project is stored in the `/home/example/static-php-cli/` directory, or your project is the framework itself,
93+
which contains project files such as `composer.json`:
94+
95+
```
96+
composer.json
97+
src/App/MyCommand.app
98+
vendor/*
99+
bin/entry.php
100+
```
101+
102+
We assume that the above constants are obtained from `src/App/MyCommand.php`:
103+
104+
| Constant | Value |
105+
|----------------------|------------------------------------------------------|
106+
| `WORKING_DIR` | `/home/example/static-php-cli` |
107+
| `SOURCE_ROOT_DIR` | `/home/example/static-php-cli` |
108+
| `FRAMEWORK_ROOT_DIR` | `/home/example/static-php-cli` |
109+
| `__DIR__` | `/home/example/static-php-cli/src/App` |
110+
| `__FILE__` | `/home/example/static-php-cli/src/App/MyCommand.php` |
111+
112+
In this case, the values of `WORKING_DIR`, `SOURCE_ROOT_DIR`, and `FRAMEWORK_ROOT_DIR` are exactly the same: `/home/example/static-php-cli`.
113+
114+
The source code of the framework and the source code of the application are both in the current path.
115+
116+
### Vendor library mode (vendor)
117+
118+
The vendor library mode generally means that your project is a framework or is installed into the project as a composer dependency by other applications,
119+
and the storage location is in the `vendor/author/XXX` directory.
120+
121+
Suppose your project is `crazywhalecc/static-php-cli`, and you or others install this project in another project using `composer require`.
122+
123+
We assume that static-php-cli contains all files except the `vendor` directory with the same `Git mode`, and get the constant value from `src/App/MyCommand`,
124+
Directory constant should be:
125+
126+
| Constant | Value |
127+
|----------------------|--------------------------------------------------------------------------------------|
128+
| `WORKING_DIR` | `/home/example/another-app` |
129+
| `SOURCE_ROOT_DIR` | `/home/example/another-app` |
130+
| `FRAMEWORK_ROOT_DIR` | `/home/example/another-app/vendor/crazywhalecc/static-php-cli` |
131+
| `__DIR__` | `/home/example/another-app/vendor/crazywhalecc/static-php-cli/src/App` |
132+
| `__FILE__` | `/home/example/another-app/vendor/crazywhalecc/static-php-cli/src/App/MyCommand.php` |
133+
134+
Here `SOURCE_ROOT_DIR` refers to the root directory of the project using `static-php-cli`.
135+
136+
### Git project Phar mode (source-phar)
137+
138+
Git project Phar mode refers to the mode of packaging the project directory of the Git project mode into a `phar` file. We assume that `/home/example/static-php-cli` will be packaged into a Phar file, and the directory has the following files:
139+
140+
```
141+
composer.json
142+
src/App/MyCommand.app
143+
vendor/*
144+
bin/entry.php
145+
```
146+
147+
When packaged into `app.phar` and stored in the `/home/example/static-php-cli` directory, `app.phar` is executed at this time. Assuming that the `src/App/MyCommand` code is executed, the constant is obtained in the file:
148+
149+
| Constant | Value |
150+
|----------------------|----------------------------------------------------------------------|
151+
| `WORKING_DIR` | `/home/example/static-php-cli` |
152+
| `SOURCE_ROOT_DIR` | `phar:///home/example/static-php-cli/app.phar/` |
153+
| `FRAMEWORK_ROOT_DIR` | `phar:///home/example/static-php-cli/app.phar/` |
154+
| `__DIR__` | `phar:///home/example/static-php-cli/app.phar/src/App` |
155+
| `__FILE__` | `phar:///home/example/static-php-cli/app.phar/src/App/MyCommand.php` |
156+
157+
Because the `phar://` protocol is required to read files in the phar itself, the project root directory and the framework directory will be different from `WORKING_DIR`.
158+
159+
### Vendor Library Phar Mode (vendor-phar)
160+
161+
Vendor Library Phar Mode means that your project is installed as a framework in other projects and stored in the `vendor` directory.
162+
163+
We assume that your project directory structure is as follows:
164+
165+
```
166+
composer.json # Composer configuration file of the current project
167+
box.json # Configuration file for packaging Phar
168+
another-app.php # Entry file of another project
169+
vendor/crazywhalecc/static-php-cli/* # Your project is used as a dependent library
170+
```
171+
172+
When packaging these files under the directory `/home/example/another-app/` into `app.phar`, the value of the following constant for your project should be:
173+
174+
| Constant | Value |
175+
|----------------------|------------------------------------------------------------------------------------------------------|
176+
| `WORKING_DIR` | `/home/example/another-app` |
177+
| `SOURCE_ROOT_DIR` | `phar:///home/example/another-app/app.phar/` |
178+
| `FRAMEWORK_ROOT_DIR` | `phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli` |
179+
| `__DIR__` | `phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli/src/App` |
180+
| `__FILE__` | `phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli/src/App/MyCommand.php` |

docs/en/guide/manual-build.md

+3
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ The ini injection of this command is achieved by appending a special structure a
342342
which is different from the function of inserting hard-coded INI during compilation.
343343
:::
344344

345+
If you want to package phar, just replace `a.php` with the packaged phar file.
346+
But please note that micro.sfx under phar needs extra attention to the path problem, see [Developing - Phar directory issue](../develop/structure#phar-application-directory-issue).
347+
345348
## Command - extract
346349

347350
Use the command `bin/spc extract` to unpack and copy the source code required for compilation,

docs/zh/develop/structure.md

+115
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,118 @@ static-php-cli 主要包含三种逻辑组件:资源、依赖库、扩展。
4646
- `src/SPC/ConsoleApplication.php`: 命令行程序入口文件。
4747

4848
如果你阅读过源码,你可能会发现还有一个 `src/globals/` 目录,它是用于存放一些全局变量、全局方法、构建过程中依赖的非 PSR-4 标准的 PHP 源码,例如测试扩展代码等。
49+
50+
## Phar 应用目录问题
51+
52+
和其他 php-cli 项目一样,spc 自身对路径有额外的考虑。
53+
因为 spc 可以在 `php-cli directly``micro SAPI``php-cli with Phar``vendor with Phar` 等多种模式下运行,各类根目录存在歧义。这里会进行一个完整的说明。
54+
此问题一般常见于 PHP 项目中存取文件的基类路径选择问题,尤其是在配合 `micro.sfx` 使用时容易出现路径问题。
55+
56+
注意,此处仅对你在开发 Phar 项目或 PHP 框架时可能有用。
57+
58+
> 接下来我们都将 `static-php-cli`(也就是 spc)当作一个普通的 `php` 命令行程序来看,你可以将 spc 理解为你自己的任何 php-cli 应用以参考。
59+
60+
下面主要有三个基本的常量理论值,我们建议你在编写 php 项目时引入这三种常量:
61+
62+
- `WORKING_DIR`:执行 PHP 脚本时的工作目录
63+
- `SOURCE_ROOT_DIR``ROOT_DIR`:项目文件夹的根目录,一般为 `composer.json` 所在目录
64+
- `FRAMEWORK_ROOT_DIR`:使用框架的根目录,自行开发的框架可能会用到,一般框架目录为只读
65+
66+
你可以在你的框架或者 cli 应用程序入口中定义这些常量,以方便在你的项目中使用路径。
67+
68+
下面是 PHP 内置的常量值,在 PHP 解释器内部已被定义:
69+
70+
- `__DIR__`:当前执行脚本的文件所在目录
71+
- `__FILE__`:当前执行脚本的文件路径
72+
73+
### Git 项目模式(source)
74+
75+
Git 项目模式指的是一个框架或程序本身在当前文件夹以纯文本形式存放,运行通过 `php path/to/entry.php` 方式。
76+
77+
假设你的项目存放在 `/home/example/static-php-cli/` 目录下,或你的项目就是框架本身,里面包含 `composer.json` 等项目文件:
78+
79+
```
80+
composer.json
81+
src/App/MyCommand.app
82+
vendor/*
83+
bin/entry.php
84+
```
85+
86+
我们假设从 `src/App/MyCommand.php` 中获取以上常量:
87+
88+
| Constant | Value |
89+
|----------------------|------------------------------------------------------|
90+
| `WORKING_DIR` | `/home/example/static-php-cli` |
91+
| `SOURCE_ROOT_DIR` | `/home/example/static-php-cli` |
92+
| `FRAMEWORK_ROOT_DIR` | `/home/example/static-php-cli` |
93+
| `__DIR__` | `/home/example/static-php-cli/src/App` |
94+
| `__FILE__` | `/home/example/static-php-cli/src/App/MyCommand.php` |
95+
96+
这种情况下,`WORKING_DIR``SOURCE_ROOT_DIR``FRAMEWORK_ROOT_DIR` 的值是完全一致的:`/home/example/static-php-cli`
97+
框架的源码和应用的源码都在当前路径下。
98+
99+
### Vendor 库模式(vendor)
100+
101+
Vendor 库模式一般是指你的项目为框架类或者被其他应用作为 composer 依赖项安装到项目中,存放位置在 `vendor/author/XXX` 目录。
102+
103+
假设你的项目是 `crazywhalecc/static-php-cli`,你或其他人在另一个项目使用 `composer require` 安装了这个项目。
104+
105+
我们假设 static-php-cli 中包含同 `Git 模式` 的除 `vendor` 目录外的所有文件,并从 `src/App/MyCommand` 中获取常量值,
106+
目录常量应该是:
107+
108+
| Constant | Value |
109+
|----------------------|--------------------------------------------------------------------------------------|
110+
| `WORKING_DIR` | `/home/example/another-app` |
111+
| `SOURCE_ROOT_DIR` | `/home/example/another-app` |
112+
| `FRAMEWORK_ROOT_DIR` | `/home/example/another-app/vendor/crazywhalecc/static-php-cli` |
113+
| `__DIR__` | `/home/example/another-app/vendor/crazywhalecc/static-php-cli/src/App` |
114+
| `__FILE__` | `/home/example/another-app/vendor/crazywhalecc/static-php-cli/src/App/MyCommand.php` |
115+
116+
117+
这里的 `SOURCE_ROOT_DIR` 就指的是使用 `static-php-cli` 的项目的根目录。
118+
119+
### Git 项目 Phar 模式(source-phar)
120+
121+
Git 项目 Phar 模式指的是将 Git 项目模式的项目目录打包为一个 `phar` 文件的模式。我们假设 `/home/example/static-php-cli` 将打包为一个 Phar 文件,目录有以下文件:
122+
123+
```
124+
composer.json
125+
src/App/MyCommand.app
126+
vendor/*
127+
bin/entry.php
128+
```
129+
130+
打包为 `app.phar` 并存放到 `/home/example/static-php-cli` 目录下时,此时执行 `app.phar`,假设执行了 `src/App/MyCommand` 代码,常量在该文件内获取:
131+
132+
| Constant | Value |
133+
|----------------------|----------------------------------------------------------------------|
134+
| `WORKING_DIR` | `/home/example/static-php-cli` |
135+
| `SOURCE_ROOT_DIR` | `phar:///home/example/static-php-cli/app.phar/` |
136+
| `FRAMEWORK_ROOT_DIR` | `phar:///home/example/static-php-cli/app.phar/` |
137+
| `__DIR__` | `phar:///home/example/static-php-cli/app.phar/src/App` |
138+
| `__FILE__` | `phar:///home/example/static-php-cli/app.phar/src/App/MyCommand.php` |
139+
140+
因为在 phar 内读取自身 phar 的文件需要 `phar://` 协议进行,所以项目根目录和框架目录将会和 `WORKING_DIR` 不同。
141+
142+
### Vendor 库 Phar 模式(vendor-phar)
143+
144+
Vendor 库 Phar 模式指的是你的项目作为框架安装在其他项目内,存储于 `vendor` 目录下。
145+
146+
我们假设你的项目目录结构如下:
147+
148+
```
149+
composer.json # 当前项目的 Composer 配置文件
150+
box.json # 打包 Phar 的配置文件
151+
another-app.php # 另一个项目的入口文件
152+
vendor/crazywhalecc/static-php-cli/* # 你的项目被作为依赖库
153+
```
154+
155+
将该目录 `/home/example/another-app/` 下的这些文件打包为 `app.phar` 时,对于你的项目而言,下面常量的值应为:
156+
157+
| Constant | Value |
158+
|----------------------|------------------------------------------------------------------------------------------------------|
159+
| `WORKING_DIR` | `/home/example/another-app` |
160+
| `SOURCE_ROOT_DIR` | `phar:///home/example/another-app/app.phar/` |
161+
| `FRAMEWORK_ROOT_DIR` | `phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli` |
162+
| `__DIR__` | `phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli/src/App` |
163+
| `__FILE__` | `phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli/src/App/MyCommand.php` |

docs/zh/guide/manual-build.md

+2
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,8 @@ memory_limit=1G
306306
该命令的注入 ini 是通过在 micro.sfx 后追加一段特殊的结构来实现的,和编译时插入硬编码 INI 的功能不同。
307307
:::
308308

309+
如果要打包 phar,只需要将 `a.php` 替换为打包好的 phar 文件即可。但要注意,phar 下的 micro.sfx 需要额外注意路径问题,见 [Developing - Phar 路径问题](../develop/structure#phar-应用目录问题)
310+
309311
## 命令 extract - 手动解压某个库
310312

311313
使用命令 `bin/spc extract` 可以解包和拷贝编译需要的源代码,包括 php-src 以及依赖的各种库的源码(需要自己指定要解包的库名)。

0 commit comments

Comments
 (0)