You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jul 1, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: docs/en/develop/structure.md
+121
Original file line number
Diff line number
Diff line change
@@ -57,3 +57,124 @@ supports automatic loading of the PSR-4 standard, and contains the following sub
57
57
If you have read the source code, you may find that there is also a `src/globals/` directory,
58
58
which is used to store some global variables, global methods,
59
59
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`:
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`,
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:
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:
Copy file name to clipboardExpand all lines: docs/en/guide/manual-build.md
+3
Original file line number
Diff line number
Diff line change
@@ -342,6 +342,9 @@ The ini injection of this command is achieved by appending a special structure a
342
342
which is different from the function of inserting hard-coded INI during compilation.
343
343
:::
344
344
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
+
345
348
## Command - extract
346
349
347
350
Use the command `bin/spc extract` to unpack and copy the source code required for compilation,
0 commit comments