diff --git a/404.html b/404.html index 8789e0c2..94b7038d 100644 --- a/404.html +++ b/404.html @@ -13,7 +13,7 @@
Skip to content

404

PAGE NOT FOUND

But if you don't change your direction, and if you keep looking, you may end up where you are heading.
- diff --git a/assets/en_develop_source-module.md.9402ba24.js b/assets/en_develop_source-module.md.faae4360.js similarity index 72% rename from assets/en_develop_source-module.md.9402ba24.js rename to assets/en_develop_source-module.md.faae4360.js index 9b5e2595..90dd8e85 100644 --- a/assets/en_develop_source-module.md.9402ba24.js +++ b/assets/en_develop_source-module.md.faae4360.js @@ -1,4 +1,4 @@ -import{_ as o,c as e,o as s,U as a}from"./chunks/framework.a73c7ff7.js";const F=JSON.parse('{"title":"Source module","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/source-module.md","filePath":"en/develop/source-module.md"}'),n={name:"en/develop/source-module.md"},l=a(`

Source module

The download source module of static-php-cli is a major module. It includes dependent libraries, external extensions, PHP source code download methods and file decompression methods. The download configuration file mainly involves the source.json file, which records the download method of all downloadable resources.

The main commands involved in the download function are bin/spc download and bin/spc extract. The download command is a downloader that downloads resources according to the configuration file, and the extract command is an extractor that extract sources from downloaded files.

Generally speaking, downloading resources may be slow because these resources come from various official websites, GitHub, and other different locations. At the same time, they also occupy a large space, so you can download the resources once and reuse them.

The configuration file of the downloader is source.json, which contains the download methods of all resources. You can add the resource download methods you need, or modify the existing resource download methods.

The download configuration structure of each resource is as follows. The following is the resource download configuration corresponding to the libevent extension:

json
{
+import{_ as o,c as e,o as s,U as a}from"./chunks/framework.a73c7ff7.js";const F=JSON.parse('{"title":"Source module","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/source-module.md","filePath":"en/develop/source-module.md"}'),n={name:"en/develop/source-module.md"},l=a(`

Source module

The download source module of static-php-cli is a major module. It includes dependent libraries, external extensions, PHP source code download methods and file decompression methods. The download configuration file mainly involves the source.json file, which records the download method of all downloadable sources.

The main commands involved in the download function are bin/spc download and bin/spc extract. The download command is a downloader that downloads sources according to the configuration file, and the extract command is an extractor that extract sources from downloaded files.

Generally speaking, downloading sources may be slow because these sources come from various official websites, GitHub, and other different locations. At the same time, they also occupy a large space, so you can download the sources once and reuse them.

The configuration file of the downloader is source.json, which contains the download methods of all sources. You can add the source download methods you need, or modify the existing source download methods.

The download configuration structure of each source is as follows. The following is the source download configuration corresponding to the libevent extension:

json
{
   "libevent": {
     "type": "ghrel",
     "repo": "libevent/libevent",
@@ -8,4 +8,4 @@ import{_ as o,c as e,o as s,U as a}from"./chunks/framework.a73c7ff7.js";const F=
       "path": "LICENSE"
     }
   }
-}

The most important field here is type. Currently, the types it supports are:

  • url: Directly use URL to download, for example: https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz.
  • ghrel: Use the GitHub Release API to download, download the artifacts uploaded from the latest version released by maintainers.
  • ghtar: Use the GitHub Release API to download. Different from ghrel, ghtar is downloaded from the source code (tar.gz) in the latest Release of the project.
  • ghtagtar: Use GitHub Release API to download. Compared with ghtar, ghtagtar can find the latest one from the tags list and download the source code in tar.gz format (because some projects only use tag release version).
  • bitbuckettag: Download using BitBucket API, basically the same as ghtagtar, except this one applies to BitBucket.
  • git: Clone the project directly from a Git address to download resources, applicable to any public Git repository.
  • filelist: Use a crawler to crawl the Web download site that provides file index, and get the latest version of the file name and download it.
  • custom: If none of the above download methods are satisfactory, you can write custom, create a new class under src/SPC/store/source/, inherit CustomSourceBase, and write the download script yourself.

(TODO)

`,10),t=[l];function c(p,r,d,i,u,D){return s(),e("div",null,t)}const h=o(n,[["render",c]]);export{F as __pageData,h as default}; +}

The most important field here is type. Currently, the types it supports are:

(TODO)

`,10),t=[l];function c(p,r,d,i,u,D){return s(),e("div",null,t)}const h=o(n,[["render",c]]);export{F as __pageData,h as default}; diff --git a/assets/en_develop_source-module.md.9402ba24.lean.js b/assets/en_develop_source-module.md.faae4360.lean.js similarity index 100% rename from assets/en_develop_source-module.md.9402ba24.lean.js rename to assets/en_develop_source-module.md.faae4360.lean.js diff --git a/assets/en_develop_structure.md.1a717dd0.js b/assets/en_develop_structure.md.1a717dd0.js new file mode 100644 index 00000000..ebc4004e --- /dev/null +++ b/assets/en_develop_structure.md.1a717dd0.js @@ -0,0 +1 @@ +import{_ as e,c as o,o as t,U as c}from"./chunks/framework.a73c7ff7.js";const b=JSON.parse('{"title":"Introduction to project structure","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/structure.md","filePath":"en/develop/structure.md"}'),d={name:"en/develop/structure.md"},n=c('

Introduction to project structure

static-php-cli mainly contains three logical components: sources, dependent libraries, and extensions. These three components also correspond to three configuration files: source.json, lib.json, and ext.json.

A complete process for building standalone static PHP is:

  1. Use the source download module Downloader to download specified or all source codes. These sources include PHP source code, dependent library source code, and extension source code.
  2. Use the source decompression module SourceExtractor to decompress the downloaded sources to the compilation directory.
  3. Use the dependency tool to calculate the dependent extensions and dependent libraries of the currently added extension, and then compile each library that needs to be compiled in the order of dependencies.
  4. After building each dependent library using Builder under the corresponding operating system, install it to the buildroot directory.
  5. If external extensions are included (the source code does not contain extensions within PHP), copy the external extensions to the source/php-src/ext/ directory.
  6. Use Builder to build the PHP source code and build target to the buildroot directory.

The project is mainly divided into several folders:

The operating principle is to start a ConsoleApplication of symfony/console, and then parse the commands entered by the user in the terminal.

Basic command line structure

bin/spc is an entry file, including the Unix common #!/usr/bin/env php, which is used to allow the system to automatically execute with the PHP interpreter installed on the system. After the project executes new ConsoleApplication(), the framework will automatically register them as commands.

The project does not directly use the Command registration method and command execution method recommended by Symfony. Here are small changes:

  1. Each command uses the #[AsCommand()] Attribute to register the name and description.
  2. Abstract execute() so that all commands are based on BaseCommand (which is based on Symfony\\Component\\Console\\Command\\Command), and the execution code of each command itself is written in the handle() method .
  3. Added variable $no_motd to BaseCommand, which is used to display the Figlet greeting when the command is executed.
  4. BaseCommand saves InputInterface and OutputInterface as member variables. You can use $this->input and $this->output within the command class.

Basic source code structure

The source code of the project is located in the src/SPC directory, supports automatic loading of the PSR-4 standard, and contains the following subdirectories and classes:

If you have read the source code, you may find that there is also a src/globals/ directory, which is used to store some global variables, global methods, and non-PSR-4 standard PHP source code that is relied upon during the build process, such as extension sanity check code etc.

',15),s=[n];function i(r,a,l,u,h,m){return t(),o("div",null,s)}const f=e(d,[["render",i]]);export{b as __pageData,f as default}; diff --git a/assets/en_develop_structure.md.39368a84.lean.js b/assets/en_develop_structure.md.1a717dd0.lean.js similarity index 100% rename from assets/en_develop_structure.md.39368a84.lean.js rename to assets/en_develop_structure.md.1a717dd0.lean.js diff --git a/assets/en_develop_structure.md.39368a84.js b/assets/en_develop_structure.md.39368a84.js deleted file mode 100644 index 79784d4b..00000000 --- a/assets/en_develop_structure.md.39368a84.js +++ /dev/null @@ -1 +0,0 @@ -import{_ as e,c as o,o as t,U as c}from"./chunks/framework.a73c7ff7.js";const b=JSON.parse('{"title":"Introduction to project structure","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/structure.md","filePath":"en/develop/structure.md"}'),d={name:"en/develop/structure.md"},n=c('

Introduction to project structure

static-php-cli mainly contains three logical components: sources, dependent libraries, and extensions. These three components also correspond to three configuration files: source.json, lib.json, and ext.json.

A complete process for building standalone static PHP is:

  1. Use the source download module Downloader to download specified or all source codes. These resources include PHP source code, dependent library source code, and extension source code.
  2. Use the resource decompression module SourceExtractor to decompress the downloaded resources to the compilation directory.
  3. Use the dependency tool to calculate the dependent extensions and dependent libraries of the currently added extension, and then compile each library that needs to be compiled in the order of dependencies.
  4. After building each dependent library using Builder under the corresponding operating system, install it to the buildroot directory.
  5. If external extensions are included (the source code does not contain extensions within PHP), copy the external extensions to the source/php-src/ext/ directory.
  6. Use Builder to build the PHP source code and build target to the buildroot directory.

The project is mainly divided into several folders:

The operating principle is to start a ConsoleApplication of symfony/console, and then parse the commands entered by the user in the terminal.

Basic command line structure

bin/spc is an entry file, including the Unix common #!/usr/bin/env php, which is used to allow the system to automatically execute with the PHP interpreter installed on the system. After the project executes new ConsoleApplication(), the framework will automatically register them as commands.

The project does not directly use the Command registration method and command execution method recommended by Symfony. Here are small changes:

  1. Each command uses the #[AsCommand()] Attribute to register the name and description.
  2. Abstract execute() so that all commands are based on BaseCommand (which is based on Symfony\\Component\\Console\\Command\\Command), and the execution code of each command itself is written in the handle() method .
  3. Added variable $no_motd to BaseCommand, which is used to display the Figlet greeting when the command is executed.
  4. BaseCommand saves InputInterface and OutputInterface as member variables. You can use $this->input and $this->output within the command class.

Basic source code structure

The source code of the project is located in the src/SPC directory, supports automatic loading of the PSR-4 standard, and contains the following subdirectories and classes:

If you have read the source code, you may find that there is also a src/globals/ directory, which is used to store some global variables, global methods, and non-PSR-4 standard PHP source code that is relied upon during the build process, such as extension sanity check code etc.

',15),s=[n];function i(r,a,l,u,h,m){return t(),o("div",null,s)}const f=e(d,[["render",i]]);export{b as __pageData,f as default}; diff --git a/assets/en_guide_manual-build.md.99d1a681.js b/assets/en_guide_manual-build.md.fc78940a.js similarity index 94% rename from assets/en_guide_manual-build.md.99d1a681.js rename to assets/en_guide_manual-build.md.fc78940a.js index 4cdeef07..b0b2a8ab 100644 --- a/assets/en_guide_manual-build.md.99d1a681.js +++ b/assets/en_guide_manual-build.md.fc78940a.js @@ -33,7 +33,7 @@ import{_ as s,c as a,o as e,U as o}from"./chunks/framework.a73c7ff7.js";const m= bin/spc doctor # Quickly check and fix when it can be automatically repaired (use package management to install dependent packages, only support the above-mentioned operating systems and distributions) -bin/spc doctor --auto-fix

Command - build

Use the build command to start building the static php binary. Before executing the bin/spc build command, be sure to use the download command to download resources. It is recommended to use doctor to check the environment.

Basic build

You need to go to Extension List or Command Generator to select the extension you want to add, and then use the command bin/spc build to compile. You need to specify a compilation target, choose from the following parameters:

bash
# Compile PHP with bcmath,curl,openssl,ftp,posix,pcntl extensions, the compilation target is cli
+bin/spc doctor --auto-fix

Command - build

Use the build command to start building the static php binary. Before executing the bin/spc build command, be sure to use the download command to download sources. It is recommended to use doctor to check the environment.

Basic build

You need to go to Extension List or Command Generator to select the extension you want to add, and then use the command bin/spc build to compile. You need to specify a compilation target, choose from the following parameters:

bash
# Compile PHP with bcmath,curl,openssl,ftp,posix,pcntl extensions, the compilation target is cli
 bin/spc build bcmath,curl,openssl,ftp,posix,pcntl --build-cli
 
 # Compile PHP with phar,curl,posix,pcntl,tokenizer extensions, compile target is micro
@@ -54,7 +54,7 @@ import{_ as s,c as a,o as e,U as o}from"./chunks/framework.a73c7ff7.js";const m=
 # Use ini file specification (-N is shorthand for --with-ini-file)
 bin/spc micro:combine a.php -N /path/to/your/custom.ini

WARNING

Note, please do not directly use the PHP source code or the php.ini file in the system-installed PHP, it is best to manually write an ini configuration file that you need, for example:

ini
; custom.ini
 curl.cainfo=/path/to/your/cafile.pem
-memory_limit=1G

The ini injection of this command is achieved by appending a special structure after micro.sfx, which is different from the function of inserting hard-coded INI during compilation.

Command - extract

Use the command bin/spc extract to unpack and copy the source code required for compilation, including php-src and the source code of various dependent libraries (you need to specify the name of the library to be unpacked).

For example, after we have downloaded resources, we want to distribute and execute the build process, manually unpack and copy the package to a specified location, and we can use commands.

bash
# Unzip the downloaded compressed package of php-src and libxml2, and store the decompressed source code in the source directory
+memory_limit=1G

The ini injection of this command is achieved by appending a special structure after micro.sfx, which is different from the function of inserting hard-coded INI during compilation.

Command - extract

Use the command bin/spc extract to unpack and copy the source code required for compilation, including php-src and the source code of various dependent libraries (you need to specify the name of the library to be unpacked).

For example, after we have downloaded sources, we want to distribute and execute the build process, manually unpack and copy the package to a specified location, and we can use commands.

bash
# Unzip the downloaded compressed package of php-src and libxml2, and store the decompressed source code in the source directory
 bin/spc extract php-src,libxml2

Dev Command - dev

Debug commands refer to a collection of commands that can assist in outputting some information when you use static-php-cli to build PHP or modify and enhance the static-php-cli project itself.

bash
# output all extensions information
 bin/spc dev:extensions
 
diff --git a/assets/en_guide_manual-build.md.99d1a681.lean.js b/assets/en_guide_manual-build.md.fc78940a.lean.js
similarity index 100%
rename from assets/en_guide_manual-build.md.99d1a681.lean.js
rename to assets/en_guide_manual-build.md.fc78940a.lean.js
diff --git a/en/contributing/index.html b/en/contributing/index.html
index e355f3ba..0c6c845d 100644
--- a/en/contributing/index.html
+++ b/en/contributing/index.html
@@ -16,7 +16,7 @@
   
   
     
Skip to content
On this page

Contributing

Thank you for being here, this project welcomes your contributions!

Contribution Guide

If you have code or documentation to contribute, here's what you need to know first.

  1. What type of code are you contributing? (new extensions, bug fixes, security issues, project framework optimizations, documentation)
  2. If you contribute new files or new snippets, is your code checked by php-cs-fixer and phpstan?
  3. Have you fully read the Developer Guide before contributing code?

If you can answer the above questions and have made changes to the code, you can initiate a Pull Request in the project GitHub repository in time. After the code review is completed, the code can be modified according to the suggestion, or directly merged into the main branch.

Contribution Type

The main purpose of this project is to compile statically linked PHP binaries, and the command line processing function is written based on symfony/console. Before development, if you are not familiar with it, Check out the symfony/console documentation first.

Security Update

Because this project is basically a PHP project running locally, generally speaking, there will be no remote attacks. But if you find such a problem, please **DO NOT submit a PR or Issue in the GitHub repository, You need to contact the project maintainer (crazywhalecc) via mail.

Fix Bugs

Fixing bugs generally does not involve modification of the project structure and framework, so if you can locate the wrong code and fix it directly, please submit a PR directly.

New Extensions

For adding a new extension, you need to understand some basic structure of the project and how to add a new extension according to the existing logic. It will be covered in detail in the next section on this page. In general, you will need:

  1. Evaluate whether the extension can be compiled inline into PHP.
  2. Evaluate whether the extension's dependent libraries (if any) can be compiled statically.
  3. Write library compile commands on different platforms.
  4. Verify that the extension and its dependencies are compatible with existing extensions and dependencies.
  5. Verify that the extension works normally in cli, micro, fpm SAPIs.
  6. Write documentation and add your extension.

Project Framework Optimization

If you are already familiar with the working principle of symfony/console, and at the same time want to make some modifications or optimizations to the framework of the project, please understand the following things first:

  1. Adding extensions does not belong to project framework optimization, but if you find that you have to optimize the framework when adding new extensions, you need to modify the framework itself before adding extensions.
  2. For some large-scale logical modifications (such as those involving LibraryBase, Extension objects, etc.), it is recommended to submit an Issue or Draft PR for discussion first.
  3. In the early stage of the project, it was a pure private development project, and there were some Chinese comments in the code. After internationalizing your project you can submit a PR to translate these comments into English.
  4. Please do not submit more useless code fragments in the code, such as a large number of unused variables, methods, classes, and code that has been rewritten many times.
- diff --git a/en/develop/doctor-module.html b/en/develop/doctor-module.html index ca126548..e2ec5772 100644 --- a/en/develop/doctor-module.html +++ b/en/develop/doctor-module.html @@ -44,7 +44,7 @@ } return true; }

#[AsFixItem()] first parameter is the name of the fix item, and this method must return True or False. When False is returned, the automatic repair failed and manual handling is required.

In the code here, shell()->exec() is the method of executing commands of the project, which is used to replace exec() and system(), and also provides debugging, obtaining execution status, entering directories, etc. characteristic.

- diff --git a/en/develop/index.html b/en/develop/index.html index 3c2e1e15..d9eae722 100644 --- a/en/develop/index.html +++ b/en/develop/index.html @@ -16,7 +16,7 @@
Skip to content
On this page

Start Developing

Developing this project requires the installation and deployment of a PHP environment, as well as some extensions and Composer commonly used in PHP projects.

The development environment and running environment of the project are almost exactly the same. You can refer to the Manual Build section to install system PHP or use the pre-built static PHP of this project as the environment. I will not go into details here.

Regardless of its purpose, this project itself is actually a php-cli program. You can edit and develop it as a normal PHP project. At the same time, you need to understand the Shell languages of different systems.

The current purpose of this project is to compile statically compiled independent PHP, but the main part also includes compiling static versions of many dependent libraries, so you can reuse this set of compilation logic to build independent binary versions of other programs, such as Nginx, etc.

Start development

Continuing down to see the project structure documentation, you can learn how static-php-cli works.

- diff --git a/en/develop/source-module.html b/en/develop/source-module.html index 41d29d35..e5ffa52b 100644 --- a/en/develop/source-module.html +++ b/en/develop/source-module.html @@ -10,12 +10,12 @@ - + -
Skip to content
On this page

Source module

The download source module of static-php-cli is a major module. It includes dependent libraries, external extensions, PHP source code download methods and file decompression methods. The download configuration file mainly involves the source.json file, which records the download method of all downloadable resources.

The main commands involved in the download function are bin/spc download and bin/spc extract. The download command is a downloader that downloads resources according to the configuration file, and the extract command is an extractor that extract sources from downloaded files.

Generally speaking, downloading resources may be slow because these resources come from various official websites, GitHub, and other different locations. At the same time, they also occupy a large space, so you can download the resources once and reuse them.

The configuration file of the downloader is source.json, which contains the download methods of all resources. You can add the resource download methods you need, or modify the existing resource download methods.

The download configuration structure of each resource is as follows. The following is the resource download configuration corresponding to the libevent extension:

json
{
+    
Skip to content
On this page

Source module

The download source module of static-php-cli is a major module. It includes dependent libraries, external extensions, PHP source code download methods and file decompression methods. The download configuration file mainly involves the source.json file, which records the download method of all downloadable sources.

The main commands involved in the download function are bin/spc download and bin/spc extract. The download command is a downloader that downloads sources according to the configuration file, and the extract command is an extractor that extract sources from downloaded files.

Generally speaking, downloading sources may be slow because these sources come from various official websites, GitHub, and other different locations. At the same time, they also occupy a large space, so you can download the sources once and reuse them.

The configuration file of the downloader is source.json, which contains the download methods of all sources. You can add the source download methods you need, or modify the existing source download methods.

The download configuration structure of each source is as follows. The following is the source download configuration corresponding to the libevent extension:

json
{
   "libevent": {
     "type": "ghrel",
     "repo": "libevent/libevent",
@@ -25,8 +25,8 @@
       "path": "LICENSE"
     }
   }
-}

The most important field here is type. Currently, the types it supports are:

  • url: Directly use URL to download, for example: https://download.libsodium.org/libsodium/releases/libsodium-1.0.18.tar.gz.
  • ghrel: Use the GitHub Release API to download, download the artifacts uploaded from the latest version released by maintainers.
  • ghtar: Use the GitHub Release API to download. Different from ghrel, ghtar is downloaded from the source code (tar.gz) in the latest Release of the project.
  • ghtagtar: Use GitHub Release API to download. Compared with ghtar, ghtagtar can find the latest one from the tags list and download the source code in tar.gz format (because some projects only use tag release version).
  • bitbuckettag: Download using BitBucket API, basically the same as ghtagtar, except this one applies to BitBucket.
  • git: Clone the project directly from a Git address to download resources, applicable to any public Git repository.
  • filelist: Use a crawler to crawl the Web download site that provides file index, and get the latest version of the file name and download it.
  • custom: If none of the above download methods are satisfactory, you can write custom, create a new class under src/SPC/store/source/, inherit CustomSourceBase, and write the download script yourself.

(TODO)

- diff --git a/en/develop/structure.html b/en/develop/structure.html index 185753d8..8265475b 100644 --- a/en/develop/structure.html +++ b/en/develop/structure.html @@ -10,13 +10,13 @@ - + -
Skip to content
On this page

Introduction to project structure

static-php-cli mainly contains three logical components: sources, dependent libraries, and extensions. These three components also correspond to three configuration files: source.json, lib.json, and ext.json.

A complete process for building standalone static PHP is:

  1. Use the source download module Downloader to download specified or all source codes. These resources include PHP source code, dependent library source code, and extension source code.
  2. Use the resource decompression module SourceExtractor to decompress the downloaded resources to the compilation directory.
  3. Use the dependency tool to calculate the dependent extensions and dependent libraries of the currently added extension, and then compile each library that needs to be compiled in the order of dependencies.
  4. After building each dependent library using Builder under the corresponding operating system, install it to the buildroot directory.
  5. If external extensions are included (the source code does not contain extensions within PHP), copy the external extensions to the source/php-src/ext/ directory.
  6. Use Builder to build the PHP source code and build target to the buildroot directory.

The project is mainly divided into several folders:

  • bin/: used to store program entry files, including bin/spc, bin/spc-alpine-docker, bin/setup-runtime.
  • config/: Contains all the extensions and dependent libraries supported by the project, as well as the download link and download methods of these sources. It is divided into three files: lib.json, ext.json, source.json .
  • src/: The core code of the project, including the entire framework and commands for compiling various extensions and libraries.
  • vendor/: The directory that Composer depends on, you do not need to make any modifications to it.

The operating principle is to start a ConsoleApplication of symfony/console, and then parse the commands entered by the user in the terminal.

Basic command line structure

bin/spc is an entry file, including the Unix common #!/usr/bin/env php, which is used to allow the system to automatically execute with the PHP interpreter installed on the system. After the project executes new ConsoleApplication(), the framework will automatically register them as commands.

The project does not directly use the Command registration method and command execution method recommended by Symfony. Here are small changes:

  1. Each command uses the #[AsCommand()] Attribute to register the name and description.
  2. Abstract execute() so that all commands are based on BaseCommand (which is based on Symfony\Component\Console\Command\Command), and the execution code of each command itself is written in the handle() method .
  3. Added variable $no_motd to BaseCommand, which is used to display the Figlet greeting when the command is executed.
  4. BaseCommand saves InputInterface and OutputInterface as member variables. You can use $this->input and $this->output within the command class.

Basic source code structure

The source code of the project is located in the src/SPC directory, supports automatic loading of the PSR-4 standard, and contains the following subdirectories and classes:

  • src/SPC/builder/: The core compilation command code used to build libraries, PHP and related extensions under different operating systems, and also includes some compilation system tool methods.
  • src/SPC/command/: All commands of the project are here.
  • src/SPC/doctor/: Doctor module, which is a relatively independent module used to check the system environment. It can be entered using the command bin/spc doctor.
  • src/SPC/exception/: exception class.
  • src/SPC/store/: Classes related to storage, files and resources are all here.
  • src/SPC/util/: Some reusable tool methods are here.
  • src/SPC/ConsoleApplication.php: command line program entry file.

If you have read the source code, you may find that there is also a src/globals/ directory, which is used to store some global variables, global methods, and non-PSR-4 standard PHP source code that is relied upon during the build process, such as extension sanity check code etc.

- diff --git a/en/faq/index.html b/en/faq/index.html index 0bb07dce..d075f7c2 100644 --- a/en/faq/index.html +++ b/en/faq/index.html @@ -17,7 +17,7 @@
Skip to content
On this page

FAQ

Here will be some questions that you may encounter easily. There are currently many, but I need to take time to organize them.

Can statically compiled PHP install extensions?

Because the principle of installing extensions in PHP under the traditional architecture is to install new extensions using .so type dynamic link libraries, and statically linked PHP compiled using this project cannot directly install new extensions using dynamic link libraries.

For the macOS platform, almost all binary files under macOS cannot be linked purely statically, and almost all binary files will link macOS system libraries: /usr/lib/libresolv.9.dylib and /usr/lib/libSystem.B.dylib. So under macOS system, statically compiled php binary files can be used under certain compilation conditions, and dynamic link extensions can be used at the same time:

  1. Using the --no-strip parameter will not strip information such as debugging symbols from the binary file for use with external Zend extensions such as Xdebug.
  2. If you want to compile some Zend extensions, use Homebrew, MacPorts, source code compilation, and install a normal version of PHP on your operating system.
  3. Use the phpize && ./configure && make command to compile the extensions you want to use.
  4. Copy the extension file xxxx.so to the outside, use the statically compiled PHP binary, for example to use the Xdebug extension: cd buildroot/bin/ && ./php -d "zend_extension=/path/to/xdebug.so".
bash
# build statically linked php-cli but not stripped
 bin/spc build ffi --build-cli --no-strip

For the Linux platform, the current compilation result is a purely statically linked binary file, and new extensions cannot be installed using a dynamic link library.

Can it support Oracle, MSSQL database extension?

Some extensions that rely on closed source libraries, such as oci8, sqlsrv, sourceguardian, etc., they do not provide purely statically compiled dependent library files (.a), only dynamic dependent library files (.so). These extensions cannot be compiled into static-php-cli from source, so this project may never support them. However, in theory, you can access and use such extensions under macOS according to the above questions.

If you have a need for such extensions, or most people have needs for these closed-source extensions, see the discussion on standalone-php-cli. Welcome to leave a message.

Will it support Windows?

The project does not currently support Windows, because Windows is a new underlying architecture, not Unix. This project started on Linux, and the refactored version supports macOS.

Windows support mainly has the following issues:

  1. The compilation process of Windows is different from that of *nix, and the tool chain used is also different. Compiling each dependent library may encounter completely different problems.
  2. PHP officially provides an independent installation package of the Windows version. Although it is not statically linked, you can easily package the entire PHP and dependent libraries in one folder, so Windows support is not a core requirement.
  3. Windows supports WSL(2) environment. For developers, we recommend you to use WSL to develop PHP-related programs.
  4. The demand for the Windows version will also be advanced according to the needs of all people who use this project. If there are many people who need it, I will support it as soon as possible.

Can I protect my source code with micro?

You can't. micro.sfx is essentially combining php and php code into one file, there is no process of compiling or encrypting the PHP code.

First of all, php-src is the official interpreter of PHP code, and there is no PHP compiler compatible with mainstream branches on the market. I saw on the Internet that there is a project called BPC (Binary PHP Compiler?) that can compile PHP into binary, but there are many restrictions.

The direction of encrypting and protecting the code is not the same as compiling. After compiling, the code can also be obtained through reverse engineering and other methods. The real protection is still carried out by means of packing and encrypting the code.

Therefore, this project (static-php-cli) and related projects (lwmbs, swoole-cli) all provide a convenient compilation tool for php-src source code. The phpmicro referenced by this project and related projects is only a package of PHP's sapi interface, not a compilation tool for PHP code. The compiler for PHP code is a completely different project, so the extra cases are not taken into account. If you are interested in encryption, you can consider using existing encryption technologies, such as Swoole Compiler, Source Guardian, etc.

- diff --git a/en/guide/action-build.html b/en/guide/action-build.html index 2199ff6c..a5a0b0f3 100644 --- a/en/guide/action-build.html +++ b/en/guide/action-build.html @@ -16,7 +16,7 @@
Skip to content
On this page

GitHub Action Build

Action Build refers to compiling directly using GitHub Action.

If you don't want to compile it yourself, you can download the artifact from the existing Action in this project, or you can download it from a self-hosted server:Enter.

Self-hosted binaries are also built from Actions: repo.

Build Guide

Using GitHub Action makes it easy to build a statically compiled PHP and phpmicro, while also defining the extensions to compile.

  1. Fork project.
  2. Go to the Actions of the project and select CI.
  3. Select Run workflow, fill in the PHP version you want to compile, the target type, and the list of extensions. (extensions comma separated, e.g. bcmath,curl,mbstring)
  4. After waiting for about a period of time, enter the corresponding task and get Artifacts.

If you enable debug, all logs will be output at build time, including compiled logs, for troubleshooting.

Extensions

You can go to extensions check here to see if all the extensions you need currently support. and then go to command generator select the extension you need to compile, copy the extensions string to extensions option.

- diff --git a/en/guide/cli-generator.html b/en/guide/cli-generator.html index 2630a561..9bd30dd1 100644 --- a/en/guide/cli-generator.html +++ b/en/guide/cli-generator.html @@ -17,7 +17,7 @@
Skip to content
On this page

CLI Build Command Generator

Select Extensions

Select common extensions
Unselect all

Build Target

Build Options

Build Environment
Enable debug message
Enable ZTS

Hardcoded INI options

Result

bin/spc build --build-cli ""
- diff --git a/en/guide/extension-notes.html b/en/guide/extension-notes.html index 408815f0..a8b61872 100644 --- a/en/guide/extension-notes.html +++ b/en/guide/extension-notes.html @@ -16,7 +16,7 @@
Skip to content
On this page

Extension Notes

Because it is a static compilation, extensions will not compile 100% perfectly, and different extensions have different requirements for PHP and the environment, which will be listed one by one here.

phpmicro

  1. Only PHP >= 8.0 is supported.

swoole

  1. swoole >= 5.0 Only PHP >= 8.0 is supported.
  2. swoole Compilation is only supported in Alpine Linux in Linux environments. #51
  3. swoole Currently, curl hooks are not supported (which may be fixed in the future).

swow

  1. Only PHP version >= 8.0 is supported.

intl

  1. intl Due to its dependency: ICU extensions (ICU uses some C++libraries), compilation is only supported in Alpine Linux in Linux environments, similar to Swoole.

gd

  1. gd Extension relies on more additional Graphics library. By default, using bin/spc build gd directly will not support some Graphics library, such as libjpeg, libavif, etc. Currently, it supports four libraries: freetype,libjpeg,libavif,libwebp. Therefore, the following command can be used to introduce them into the gd library:
bash
bin/spc build gd --with-libs=freetype,libjpeg,libavif,libwebp --build-cli

mcrypt

  1. Currently not supported, and this extension will not be supported in the future. #32

oci8

  1. oci8 is an extension of the Oracle database, because the library on which the extension provided by Oracle does not provide a statically compiled version (.a) or source code, and this extension cannot be compiled into php by static linking, so it cannot be supported.

sqlsrv

  1. sqlsrv is an extension of the SQL Server database. Because the library on which the extension provided by SQL Server does not provide a statically compiled version (a) or source code, the extension cannot be compiled into php by static linking, so it cannot be supported.

xdebug

  1. Xdebug is a Zend extension. The functions of Xdebug depend on PHP's Zend engine and underlying code. If you want to statically compile it into PHP, you may need a huge amount of patch code, which is not possible.
  2. The macOS platform can compile an xdebug extension under PHP compiled on the same platform, extract the xdebug.so file, and then use the --no-strip parameter in static-php-cli to retain the debug symbol table and add the ffi extension. The compiled ./php binary can be configured and run by specifying the INI, eg ./php -d 'zend_extension=/path/to/xdebug.so' your-code.php.

glfw

  1. glfw depends on OpenGL, and linux environment also needs X11, which cannot be linked statically.
  2. macOS platform, we can compile and link system builtin OpenGL and related libraries dynamically.

snappy

  1. The snappy library is written in C++, so you can only compile purely statically linked PHP binaries under Alpine Linux.
- diff --git a/en/guide/extensions.html b/en/guide/extensions.html index 052fc009..96624a9a 100644 --- a/en/guide/extensions.html +++ b/en/guide/extensions.html @@ -16,7 +16,7 @@
Skip to content
On this page

Extensions

  • yes: supported and tested
  • untested: supported, but not tested
  • empty: not supported yet, or WIP
  • no with issue link: not supported yet due to issue
  • partial with issue link: supported but not perfect due to issue
LinuxmacOSWindows
apcuyes, untestedyes, untested
bcmathyesyes
bz2yesyes
calendaryesyes
ctypeyesyes
curlyesyes
dbayesyes
domyesyes
enchant
eventyesyes
exifyesyes
ffiyes
filteryesyes
fileinfoyesyes
ftpyesyes
gdyesyes
gettext
glfwnoyes
gmpyesyes
iconvyesyes
imagickyesyes
inotifyyesyes
intlpartialyes
mbstringyesyes
mbregexyesyes
mcryptnono
memcacheyes, untestedyes, untested
memcachednoyes, untested
mongodbyesyes
mysqliyesyes
mysqlndyesyes
oci8nono
opcacheyesyes
opensslyesyes
password-argon2
pcntlyesyes
pdoyesyes
pdo_mysqlyesyes
pdo_sqliteyesyes
pdo_pgsqlyes, untestedyes, untested
pgsqlyes, untestedyes, untested
pharyesyes
posixyesyes
protobufyesyes
rdkafka
readlineyes, untestedyes, untested
redisyesyes
sessionyesyes
shmopyesyes
simplexmlyesyes
snappyyes, untestedyes, untested
soapyesyes
socketsyesyes
sodiumyesyes
sqlite3yesyes
sqlsrvnono
ssh2yes, untestedyes, untested
swowyesyes
swoolepartialyes
tokenizeryesyes
xdebug
xlswriteryesyes
xmlyesyes
xmlreaderyes, untestedyes, untested
xmlwriteryes, untestedyes, untested
xslyes, untestedyes, untested
zipyes, untestedyes, untested
zlibyesyes
zstdyesyes

TIP

If there is no extension you need here, you can submit an Issue.

- diff --git a/en/guide/index.html b/en/guide/index.html index 16334020..8a2388f8 100644 --- a/en/guide/index.html +++ b/en/guide/index.html @@ -16,7 +16,7 @@
Skip to content
On this page

Guide

Static php cli is a tool used to build statically compiled PHP binaries, currently supporting Linux and macOS systems.

In the guide section, you will learn how to use static php cli to build standalone PHP programs.

TIP

Part of the English document is written by me, and part is translated by Google, and there may be inaccurate descriptions, strange or offensive expressions. If you are a native English speaker, some corrections to the documentation are welcome. (Docs repo)

Compilation Environment

The following is the architecture support situation, where CI represents support for GitHub Action build, Local represents support for native build, and empty represents temporarily not supported.

x86_64aarch64
macOSCI, LocalLocal
LinuxCI, LocalCI, Local
Windows
FreeBSD

Among them, Linux is currently only tested on Ubuntu, Debian, and Alpine distributions, and other distributions have not been tested, which cannot guarantee successful compilation. For untested distributions, local compilation can be done using methods such as Docker to avoid environmental issues.

There are two architectures for macOS: x86_64 and Arm, but binaries compiled on one architecture cannot be directly used on the other architecture. Rosetta 2 cannot guarantee that programs compiled with Arm architecture can fully run on x86_64 environment.

Supported PHP Version

Currently, static php cli supports PHP versions 8.0 to 8.2, and theoretically supports PHP 7.4 and earlier versions. Simply select the earlier version when downloading. However, due to some extensions and special components that have stopped supporting earlier versions of PHP, static-php-cli will not explicitly support earlier versions.

- diff --git a/en/guide/manual-build.html b/en/guide/manual-build.html index fd74f117..d23f13a3 100644 --- a/en/guide/manual-build.html +++ b/en/guide/manual-build.html @@ -10,7 +10,7 @@ - + @@ -50,7 +50,7 @@ bin/spc doctor # Quickly check and fix when it can be automatically repaired (use package management to install dependent packages, only support the above-mentioned operating systems and distributions) -bin/spc doctor --auto-fix

Command - build

Use the build command to start building the static php binary. Before executing the bin/spc build command, be sure to use the download command to download resources. It is recommended to use doctor to check the environment.

Basic build

You need to go to Extension List or Command Generator to select the extension you want to add, and then use the command bin/spc build to compile. You need to specify a compilation target, choose from the following parameters:

  • --build-cli: Build a cli sapi (command line interface, which can execute PHP code on the command line)
  • --build-fpm: Build a fpm sapi (php-fpm, used in conjunction with other traditional fpm architecture software such as nginx)
  • --build-micro: Build a micro sapi (used to build a standalone executable binary containing PHP code)
  • --build-embed: Build an embed sapi (used to embed into other C language programs)
  • --build-all: build all above sapi
bash
# Compile PHP with bcmath,curl,openssl,ftp,posix,pcntl extensions, the compilation target is cli
+bin/spc doctor --auto-fix

Command - build

Use the build command to start building the static php binary. Before executing the bin/spc build command, be sure to use the download command to download sources. It is recommended to use doctor to check the environment.

Basic build

You need to go to Extension List or Command Generator to select the extension you want to add, and then use the command bin/spc build to compile. You need to specify a compilation target, choose from the following parameters:

  • --build-cli: Build a cli sapi (command line interface, which can execute PHP code on the command line)
  • --build-fpm: Build a fpm sapi (php-fpm, used in conjunction with other traditional fpm architecture software such as nginx)
  • --build-micro: Build a micro sapi (used to build a standalone executable binary containing PHP code)
  • --build-embed: Build an embed sapi (used to embed into other C language programs)
  • --build-all: build all above sapi
bash
# Compile PHP with bcmath,curl,openssl,ftp,posix,pcntl extensions, the compilation target is cli
 bin/spc build bcmath,curl,openssl,ftp,posix,pcntl --build-cli
 
 # Compile PHP with phar,curl,posix,pcntl,tokenizer extensions, compile target is micro
@@ -71,7 +71,7 @@
 # Use ini file specification (-N is shorthand for --with-ini-file)
 bin/spc micro:combine a.php -N /path/to/your/custom.ini

WARNING

Note, please do not directly use the PHP source code or the php.ini file in the system-installed PHP, it is best to manually write an ini configuration file that you need, for example:

ini
; custom.ini
 curl.cainfo=/path/to/your/cafile.pem
-memory_limit=1G

The ini injection of this command is achieved by appending a special structure after micro.sfx, which is different from the function of inserting hard-coded INI during compilation.

Command - extract

Use the command bin/spc extract to unpack and copy the source code required for compilation, including php-src and the source code of various dependent libraries (you need to specify the name of the library to be unpacked).

For example, after we have downloaded resources, we want to distribute and execute the build process, manually unpack and copy the package to a specified location, and we can use commands.

bash
# Unzip the downloaded compressed package of php-src and libxml2, and store the decompressed source code in the source directory
+memory_limit=1G

The ini injection of this command is achieved by appending a special structure after micro.sfx, which is different from the function of inserting hard-coded INI during compilation.

Command - extract

Use the command bin/spc extract to unpack and copy the source code required for compilation, including php-src and the source code of various dependent libraries (you need to specify the name of the library to be unpacked).

For example, after we have downloaded sources, we want to distribute and execute the build process, manually unpack and copy the package to a specified location, and we can use commands.

bash
# Unzip the downloaded compressed package of php-src and libxml2, and store the decompressed source code in the source directory
 bin/spc extract php-src,libxml2

Dev Command - dev

Debug commands refer to a collection of commands that can assist in outputting some information when you use static-php-cli to build PHP or modify and enhance the static-php-cli project itself.

  • dev:extensions: output all currently supported extension names, or output the specified extension information
  • dev:php-version: output the currently compiled PHP version (by reading php_version.h)
  • dev:sort-config: Sort the list of configuration files in the config/ directory in alphabetical order
bash
# output all extensions information
 bin/spc dev:extensions
 
@@ -85,7 +85,7 @@
 
 # Sort the configuration files in the config/ directory in alphabetical order (e.g. ext.json)
 bin/spc dev:sort-config ext
- diff --git a/en/guide/troubleshooting.html b/en/guide/troubleshooting.html index f44b9f92..2ec7406b 100644 --- a/en/guide/troubleshooting.html +++ b/en/guide/troubleshooting.html @@ -16,7 +16,7 @@
Skip to content
On this page

Troubleshooting

Various failures may be encountered in the process of using static-php-cli, here will describe how to check the errors by yourself and report Issue.

- diff --git a/en/index.html b/en/index.html index 44e75900..b7a19fc1 100644 --- a/en/index.html +++ b/en/index.html @@ -16,7 +16,7 @@
Skip to content

static-php-cli

Build single static PHP binary, with PHP project together, with popular extensions included.

Static CLI Binary

You can easily compile a standalone php binary for general use. Including CLI, FPM sapi.

Micro Self-Extracted Executable

You can compile a self-extracted executable and build with your php source code.

Dependency Management

static-php-cli comes with dependency management and supports installation of different types of PHP extensions.

- diff --git a/hashmap.json b/hashmap.json index 963e3875..b8221a8c 100644 --- a/hashmap.json +++ b/hashmap.json @@ -1 +1 @@ -{"en_guide_action-build.md":"3c51197b","en_guide_cli-generator.md":"cae66d07","en_guide_extension-notes.md":"316ce293","en_guide_index.md":"3bcc28bd","zh_guide_cli-generator.md":"ac36ab33","zh_guide_extension-notes.md":"d2542bb2","zh_guide_extensions.md":"c27c0294","zh_guide_index.md":"6f5ad307","zh_guide_manual-build.md":"4065505d","zh_index.md":"1bc9d3bc","en_guide_extensions.md":"794e3b8d","en_index.md":"b2ea28ce","index.md":"ea5c3419","en_guide_manual-build.md":"99d1a681","en_develop_doctor-module.md":"3dea0df2","en_develop_structure.md":"39368a84","en_contributing_index.md":"3aef0360","zh_develop_doctor-module.md":"a40ca335","zh_develop_index.md":"5bd3843b","en_develop_index.md":"fd2957b1","zh_contributing_index.md":"fc3f3bd5","zh_develop_source-module.md":"e87243ca","zh_develop_structure.md":"26636f6d","zh_faq_index.md":"c2c13635","zh_guide_action-build.md":"5146cf9a","en_guide_troubleshooting.md":"413d7af5","en_develop_source-module.md":"9402ba24","en_faq_index.md":"17039d83","zh_guide_troubleshooting.md":"80d0ca6c"} +{"zh_guide_index.md":"6f5ad307","en_contributing_index.md":"3aef0360","en_develop_source-module.md":"faae4360","en_develop_structure.md":"1a717dd0","en_faq_index.md":"17039d83","en_guide_action-build.md":"3c51197b","en_guide_cli-generator.md":"cae66d07","en_guide_extension-notes.md":"316ce293","en_guide_extensions.md":"794e3b8d","en_guide_troubleshooting.md":"413d7af5","en_index.md":"b2ea28ce","index.md":"ea5c3419","zh_contributing_index.md":"fc3f3bd5","zh_develop_doctor-module.md":"a40ca335","zh_develop_structure.md":"26636f6d","zh_faq_index.md":"c2c13635","zh_guide_action-build.md":"5146cf9a","zh_guide_extension-notes.md":"d2542bb2","zh_guide_extensions.md":"c27c0294","en_guide_index.md":"3bcc28bd","en_guide_manual-build.md":"fc78940a","zh_develop_index.md":"5bd3843b","zh_guide_manual-build.md":"4065505d","zh_index.md":"1bc9d3bc","en_develop_index.md":"fd2957b1","zh_guide_troubleshooting.md":"80d0ca6c","en_develop_doctor-module.md":"3dea0df2","zh_guide_cli-generator.md":"ac36ab33","zh_develop_source-module.md":"e87243ca"} diff --git a/index.html b/index.html index 371b26e4..13a8c76e 100644 --- a/index.html +++ b/index.html @@ -16,7 +16,7 @@
Skip to content

Please select your language

- diff --git a/zh/contributing/index.html b/zh/contributing/index.html index 429b3b20..3c6b384b 100644 --- a/zh/contributing/index.html +++ b/zh/contributing/index.html @@ -16,7 +16,7 @@
Skip to content
On this page

贡献指南

感谢你能够看到这里,本项目非常欢迎你的贡献!

贡献方法

如果你有代码或文档想要贡献,需要先了解以下内容。

  1. 你要贡献什么类型的代码?(新扩展、修复 Bug、安全问题、项目框架优化、文档)
  2. 如果你贡献了新文件或新片段,你的代码是否经过 php-cs-fixerphpstan 的检查?
  3. 在贡献代码前是否充分阅读了 开发指南

如果你可以回答以上问题,并已经对代码做出了修改,可以及时在项目 GitHub 仓库发起 Pull Request。待代码审查完毕后,可根据建议修改代码,或直接合并到主分支。

贡献类型

本项目主要用途是编译静态链接的 PHP 二进制,基于 symfony/console 编写了命令行处理功能。在开发之前,如果你对它不够熟悉, 可以先查看 symfony/console 文档

安全问题

因为本项目基本上是属于本地运行的 PHP 项目,一般来说不会存在远程攻击行为。但如果你发现了此类问题,请不要在 GitHub 仓库提交 PR 或 Issue, 你需要通过 邮件 的方式联系项目维护者(crazywhalecc)。

修复 Bug

修复 Bug 一般不涉及项目结构和框架的修改,所以如果你可以定位到错误代码并直接修复它,请直接提交 PR。

新扩展

对于添加一个新扩展来说,你需要先了解一些本项目的基本结构,以及如何根据现有的逻辑添加新扩展。在本页的下一章节将会详细介绍。 总的来说,你需要:

  1. 评估扩展是否可以内联编译到 PHP 中。
  2. 评估扩展的依赖库(如果有)是否可以静态编译。
  3. 写出扩展的依赖库在不同平台编译命令。
  4. 验证扩展及其依赖库能否与现有扩展和依赖库兼容。
  5. 验证扩展在 climicrofpm 几种 SAPI 中均正常工作。
  6. 编写文档,加入你的扩展。

项目框架优化

如果你已经熟悉 symfony/console 的工作原理,并同时要对项目的框架进行一些修改或优化,请先了解以下事情:

  1. 加入扩展不属于项目框架优化,但如果你在加入新的扩展时发现不得不优化框架,则需先对框架本身进行修改,然后再加入扩展。
  2. 对于一些大规模逻辑修改(例如涉及 LibraryBase、Extension 对象等的修改)时,建议先提交 Issue 或 Draft PR 进行讨论方案。
  3. 项目早期为纯中文开发项目,代码中存在一部分中文的注释。国际化项目后你可以提交 PR 将这些注释翻译为英语。
  4. 请不要在代码中提交包含较多无用的代码片段,例如大量未被使用的变量、方法、类、重复写了很多次的代码。
- diff --git a/zh/develop/doctor-module.html b/zh/develop/doctor-module.html index be725ef9..2c921b72 100644 --- a/zh/develop/doctor-module.html +++ b/zh/develop/doctor-module.html @@ -44,7 +44,7 @@ } return true; }

#[AsFixItem()] 属性传入的参数即修复项的名称,该方法必须返回 True 或 False。当返回 False 时,表明自动修复失败,需要手动处理。

此处的代码中 shell()->exec() 是项目的执行命令的方法,用于替代 exec()system(),同时提供了 debug、获取执行状态、进入目录等特性。

- diff --git a/zh/develop/index.html b/zh/develop/index.html index 1c4d52f4..a761ce13 100644 --- a/zh/develop/index.html +++ b/zh/develop/index.html @@ -16,7 +16,7 @@
Skip to content
On this page

开发简介

开发本项目需要安装部署 PHP 环境,以及一些 PHP 项目常用的扩展和 Composer。

项目的开发环境和运行环境几乎完全一致,你可以参照 指南-本地构建 部分安装系统 PHP 或使用本项目预构建的静态 PHP 作为环境,这里不再赘述。

抛开用途,本项目本身其实就是一个 php-cli 程序,你可以将它当作一个正常的 PHP 项目进行编辑和开发,同时你需要了解不同系统的 Shell 命令行。

本项目目前的目的就是为了编译静态编译的独立 PHP,但主体部分也包含编译很多依赖库的静态版本,所以你可以复用这套编译逻辑,用于构建其他程序的独立二进制版本,例如 Nginx 等。

开始开发

继续向下查看项目结构的文档,你可以从中了解 static-php-cli 是如何运作的。

- diff --git a/zh/develop/source-module.html b/zh/develop/source-module.html index 6ad3676c..e921415b 100644 --- a/zh/develop/source-module.html +++ b/zh/develop/source-module.html @@ -26,7 +26,7 @@ } } }

这里最主要的字段是 type,目前它支持的类型有:

(TODO)

- diff --git a/zh/develop/structure.html b/zh/develop/structure.html index 7cf0bc59..280ab981 100644 --- a/zh/develop/structure.html +++ b/zh/develop/structure.html @@ -16,7 +16,7 @@
Skip to content
On this page

项目结构简介

static-php-cli 主要包含三种逻辑组件:资源、依赖库、扩展。这三种组件也对应了三个配置文件:source.jsonlib.jsonext.json

一个完整的构建静态 PHP 流程是:

  1. 使用资源下载模块 Downloader 下载指定或所有资源,这些资源包含 PHP 源码、依赖库源码、扩展源码。
  2. 使用资源解压模块 SourceExtractor 解压下载的资源到编译目录。
  3. 使用依赖工具计算出当前加入的扩展的依赖扩展、依赖库,然后对每个需要编译的依赖库进行编译,按照依赖顺序。
  4. 使用对应操作系统下的 Builder 构建每个依赖库后,将其安装到 buildroot 目录。
  5. 如果包含外部扩展(源码没有包含在 PHP 内的扩展),将外部扩展拷贝到 source/php-src/ext/ 目录。
  6. 使用 Builder 构建 PHP 源码,将其安装到 buildroot 目录。

项目主要分为几个文件夹:

  • bin/: 用于存放程序入口文件,包含 bin/spcbin/spc-alpine-dockerbin/setup-runtime
  • config/: 包含了所有项目支持的扩展、依赖库以及这些资源下载的地址、下载方式等,分为三个文件:lib.jsonext.jsonsource.json
  • src/SPC/: 项目的核心代码,包含了整个框架以及编译各种扩展和库的命令。
  • src/globals/: 项目的全局方法和常量、运行时需要的测试文件(例如:扩展的可用性检查代码)。
  • vendor/: Composer 依赖的目录,你无需对它做出任何修改。

其中运行原理就是启动一个 symfony/consoleConsoleApplication,然后解析用户在终端输入的命令。

基本命令行结构

bin/spc 是一个 PHP 代码入口文件,包含了 Unix 通用的 #!/usr/bin/env php 用来让系统自动以系统安装好的 PHP 解释器执行。 在项目执行了 new ConsoleApplication() 后,框架会自动使用反射的方式,解析 src/SPC/command 目录下的所有类,并将其注册成为命令。

项目并没有直接使用 Symfony 推荐的 Command 注册方式和命令执行方式,这里做出了一点小变动:

  1. 每个命令都使用 #[AsCommand()] Attribute 来注册名称和简介。
  2. execute() 抽象化,让所有命令基于 BaseCommand(它基于 Symfony\Component\Console\Command\Command),每个命令本身的执行代码写到了 handle() 方法中。
  3. BaseCommand 添加了变量 $no_motd,用于是否在该命令执行时显示 Figlet 欢迎词。
  4. BaseCommandInputInterfaceOutputInterface 保存为成员变量,你可以在命令的类内使用 $this->input$this->output

基本源码结构

项目的源码位于 src/SPC 目录,支持 PSR-4 标准的自动加载,包含以下子目录和类:

  • src/SPC/builder/: 用于不同操作系统下构建依赖库、PHP 及相关扩展的核心编译命令代码,还包含了一些编译的系统工具方法。
  • src/SPC/command/: 项目的所有命令都在这里。
  • src/SPC/doctor/: Doctor 模块,它是一个较为独立的用于检查系统环境的模块,可使用命令 bin/spc doctor 进入。
  • src/SPC/exception/: 异常类。
  • src/SPC/store/: 有关存储、文件和资源的类都在这里。
  • src/SPC/util/: 一些可以复用的工具方法都在这里。
  • src/SPC/ConsoleApplication.php: 命令行程序入口文件。

如果你阅读过源码,你可能会发现还有一个 src/globals/ 目录,它是用于存放一些全局变量、全局方法、构建过程中依赖的非 PSR-4 标准的 PHP 源码,例如测试扩展代码等。

- diff --git a/zh/faq/index.html b/zh/faq/index.html index b25bd644..33984223 100644 --- a/zh/faq/index.html +++ b/zh/faq/index.html @@ -17,7 +17,7 @@
Skip to content
On this page

常见问题

这里将会编写一些你容易遇到的问题。目前有很多,但是我需要花时间来整理一下。

静态编译的 PHP 可以安装扩展吗

因为传统架构下的 PHP 安装扩展的原理是使用 .so 类型的动态链接的库方式安装新扩展,而使用本项目编译的静态链接的 PHP 无法直接使用动态链接库安装新扩展。

对于 macOS 平台来说,macOS 下的几乎所有二进制文件都无法真正纯静态链接,几乎所有二进制文件都会链接 macOS 的系统库:/usr/lib/libresolv.9.dylib/usr/lib/libSystem.B.dylib。 所以在 macOS 系统下,在特定的编译条件下可以使用静态编译的 php 二进制文件,同时使用动态链接的扩展:

  1. 使用 --no-strip 参数,将不会对二进制文件去除调试符号等信息,以供使用 Xdebug 等外部 Zend 扩展。
  2. 如果要编译某些 Zend 扩展,使用 Homebrew、MacPorts、源码编译的形式,在所在的操作系统安装一个普通版本的 PHP。
  3. 使用 phpize && ./configure && make 命令编译想要使用的扩展。
  4. 将扩展文件 xxxx.so 拷贝到外部,使用静态编译的 PHP 二进制,例如使用 Xdebug 扩展:cd buildroot/bin/ && ./php -d "zend_extension=/path/to/xdebug.so"
bash
# 构建静态 php-cli
 bin/spc build ffi --build-cli --no-strip

对于 Linux 平台来说,目前的编译结果为纯静态链接的二进制文件,无法使用动态链接库安装新扩展。

可以支持 Oracle、MSSQL 数据库扩展吗

部分依赖库闭源的扩展,如 oci8sqlsrvsourceguardian 等,它们没有提供纯静态编译的依赖库文件(.a),仅提供了动态依赖库文件(.so), 这些扩展无法使用源码的形式编译到 static-php-cli 中,所以本项目可能永远也不会支持这些扩展。不过,理论上你可以根据上面的问题在 macOS 下接入和使用这类扩展。

如果你对此类扩展有需求,或者大部分人都对这些闭源扩展使用有需求, 可以看看有关 standalone-php-cli 的讨论。欢迎留言。

会支持 Windows 吗

该项目目前不支持 Windows,因为 Windows 是一套全新的底层架构,不是 Unix 架构。本项目起始于 Linux,重构的版本支持了 macOS。

Windows 的支持主要有以下几个问题:

  1. Windows 的编译流程与 *nix 不同,使用的工具链也不同,编译各个依赖库可能会遇到完全不同的问题。
  2. PHP 官方提供了 Windows 版本的独立安装包,虽然不是静态链接,但你可以方便地将整个 PHP 及依赖库打包在一个文件夹下,所以 Windows 的支持不是一个最核心的需求。
  3. Windows 支持 WSL(2) 环境,对于开发人员,我们更推荐你使用 WSL 开发 PHP 相关的程序。
  4. Windows 版本的需求也会根据所有使用本项目的人的需求推进,如果有很多人需要,我会尽快支持。

使用 micro 可以保护我的源码吗

不可以。micro.sfx 本质上是将 php 和 php 代码结合为一个文件,没有 PHP 代码编译或加密的过程。 首先 php-src 是 PHP 代码的官方解释器,而且现在市面上还没有一个能兼容主流分支的 PHP 编译器。 之前我在网上看到有一个项目是 BPC(Binary PHP Compiler?)可以把 PHP 编译为二进制,但是限制也是很多很多。

加密保护代码的方向和编译也不是一回事,编译过后也可以通过逆向工程等方式拿到代码,真正保护还是通过加壳、加密代码等手段进行。

所以本项目(static-php-cli)、相关项目(lwmbs、swoole-cli)都是提供一个对 php-src 源码的便捷编译工具, 本项目和相关项目引用的 phpmicro 也仅仅是 PHP 的 sapi 接口封装,而不是 PHP 代码的编译工具。 PHP 代码的编译器是完全不同的项目,因此不会考虑额外的情况。如果你对加密感兴趣,可以考虑使用现有的加密技术,如 Swoole Compiler、Source Guardian 等。

- diff --git a/zh/guide/action-build.html b/zh/guide/action-build.html index 8d2d3eb5..ed0ba42a 100644 --- a/zh/guide/action-build.html +++ b/zh/guide/action-build.html @@ -16,7 +16,7 @@
Skip to content
On this page

Action 构建

Action 构建指的是直接使用 GitHub Action 进行编译。

如果你不想自行编译,可以从本项目现有的 Action 下载 Artifact,也可以从自托管的服务器下载:进入

自托管的二进制也是由 Action 构建而来,项目仓库地址

构建方法

使用 GitHub Action 可以方便地构建一个静态编译的 PHP 和 phpmicro,同时可以自行定义要编译的扩展。

  1. Fork 本项目。
  2. 进入项目的 Actions,选择 CI 开头的 Workflow(根据你需要的操作系统选择)。
  3. 选择 Run workflow,填入你要编译的 PHP 版本、目标类型、扩展列表。(扩展列表使用英文逗号分割,例如 bcmath,curl,mbstring
  4. 等待大约一段时间后,进入对应的任务中,获取 Artifacts

如果你选择了 debug,则会在构建时输出所有日志,包括编译的日志,以供排查错误。

扩展选择

你可以到 扩展列表 中查看目前你需要的扩展是否均支持, 然后到 在线命令生成 中选择你需要编译的扩展,复制扩展字符串到 Action 的 extensions 中,编译即可。

- diff --git a/zh/guide/cli-generator.html b/zh/guide/cli-generator.html index 34e3ae3c..11a85115 100644 --- a/zh/guide/cli-generator.html +++ b/zh/guide/cli-generator.html @@ -17,7 +17,7 @@
Skip to content
On this page

CLI 编译命令生成器

选择扩展

选择常用扩展
全部取消选择

选择编译目标

编译选项

编译环境
是否开启调试输出
是否编译线程安全版

硬编码 INI 选项

结果展示

bin/spc build --build-cli ""
- diff --git a/zh/guide/extension-notes.html b/zh/guide/extension-notes.html index 2d441669..89987655 100644 --- a/zh/guide/extension-notes.html +++ b/zh/guide/extension-notes.html @@ -16,7 +16,7 @@
Skip to content
On this page

扩展注意事项

因为是静态编译,扩展不会 100% 完美编译,而且不同扩展对 PHP、环境都有不同的要求,这里将一一列举。

phpmicro

  1. phpmicro SAPI 仅支持 PHP >= 8.0 版本。

swoole

  1. swoole >= 5.0 版本仅支持 PHP >= 8.0 版本。
  2. swoole 在 Linux 环境中仅支持在 Alpine Linux 下编译。#51
  3. swoole 目前不支持 curl 的 hook(后续有可能会修复)。

swow

  1. swow 仅支持 PHP >= 8.0 版本。

intl

  1. intl 扩展由于依赖 icu 扩展(icu 使用了部分 C++ 的库),在 Linux 环境中仅支持在 Alpine Linux 下编译,同 Swoole。

gd

  1. gd 扩展依赖了较多的额外图形库,默认情况下,直接使用 bin/spc build gd 不会引入和支持部分图形库,例如 libjpeglibavif 等, 需要使用 --with-libs 参数补全。目前支持 freetype,libjpeg,libavif,libwebp 四个库的支持,所以这里可以使用以下命令来让 gd 库引入它们:
bash
bin/spc build gd --with-libs=freetype,libjpeg,libavif,libwebp --build-cli

mcrypt

  1. 目前未支持,未来也不计划支持此扩展。#32

oci8

  1. oci8 是 Oracle 数据库的扩展,因为 Oracle 提供的扩展所依赖的库未提供静态编译版本(.a)或源代码,无法使用静态链接的方式将此扩展编译到 php 内,故无法支持。

sqlsrv

  1. sqlsrv 是 SQL Server 数据库的扩展,因为 SQL Server 提供的扩展所依赖的库未提供静态编译版本(.a)或源代码,无法使用静态链接的方式将此扩展编译到 php 内,故无法支持。

xdebug

  1. Xdebug 是一个 Zend 扩展,Xdebug 的功能依赖于 PHP 的 Zend 引擎和底层代码,如果要将其静态编译到 PHP 中,可能需要巨量的 patch 代码,这是不划算的。
  2. macOS 平台可以通过在相同平台编译的 PHP 下编译一个 xdebug 扩展,并提取其中的 xdebug.so 文件,再在 static-php-cli 中使用 --no-strip 参数保留调试符号表,同时加入 ffi 扩展。 编译的 ./php 二进制可以通过指定 INI 配置并运行,例如./php -d 'zend_extension=xdebug.so' your-code.php

glfw

  1. glfw 扩展依赖 OpenGL,在 Linux 平台还依赖 X11 等环境,这些库都无法被轻易地动态链接。
  2. 在 macOS 系统下,我们可以动态链接系统的 OpenGL 和一些相关的库。

snappy

  1. snappy 的库采用 C++ 编写,所以你只能在 Alpine Linux 下编译纯静态链接的 PHP 二进制。
- diff --git a/zh/guide/extensions.html b/zh/guide/extensions.html index da25eb08..4d0a2bef 100644 --- a/zh/guide/extensions.html +++ b/zh/guide/extensions.html @@ -16,7 +16,7 @@
Skip to content
On this page

Extensions

  • yes: supported and tested
  • untested: supported, but not tested
  • empty: not supported yet, or WIP
  • no with issue link: not supported yet due to issue
  • partial with issue link: supported but not perfect due to issue
LinuxmacOSWindows
apcuyes, untestedyes, untested
bcmathyesyes
bz2yesyes
calendaryesyes
ctypeyesyes
curlyesyes
dbayesyes
domyesyes
enchant
eventyesyes
exifyesyes
ffiyes
filteryesyes
fileinfoyesyes
ftpyesyes
gdyesyes
gettext
glfwnoyes
gmpyesyes
iconvyesyes
imagickyesyes
inotifyyesyes
intlpartialyes
mbstringyesyes
mbregexyesyes
mcryptnono
memcacheyes, untestedyes, untested
memcachednoyes, untested
mongodbyesyes
mysqliyesyes
mysqlndyesyes
oci8nono
opcacheyesyes
opensslyesyes
password-argon2
pcntlyesyes
pdoyesyes
pdo_mysqlyesyes
pdo_sqliteyesyes
pdo_pgsqlyes, untestedyes, untested
pgsqlyes, untestedyes, untested
pharyesyes
posixyesyes
protobufyesyes
rdkafka
readlineyes, untestedyes, untested
redisyesyes
sessionyesyes
shmopyesyes
simplexmlyesyes
snappyyes, untestedyes, untested
soapyesyes
socketsyesyes
sodiumyesyes
sqlite3yesyes
sqlsrvnono
ssh2yes, untestedyes, untested
swowyesyes
swoolepartialyes
tokenizeryesyes
xdebug
xlswriteryesyes
xmlyesyes
xmlreaderyes, untestedyes, untested
xmlwriteryes, untestedyes, untested
xslyes, untestedyes, untested
zipyes, untestedyes, untested
zlibyesyes
zstdyesyes

TIP

If there is no extension you need here, you can submit an Issue.

- diff --git a/zh/guide/index.html b/zh/guide/index.html index 21c266d6..806bf0ed 100644 --- a/zh/guide/index.html +++ b/zh/guide/index.html @@ -16,7 +16,7 @@
Skip to content
On this page

指南

static-php-cli 是一个用于构建静态编译的 PHP 二进制的工具,目前支持 Linux 和 macOS 系统。

在指南章节中,你将了解到如何使用 static-php-cli 构建独立的 php 程序。

编译环境

下面是架构支持情况,CI 代表支持 GitHub Action 构建,Local 代表支持本地构建,空 代表暂不支持。

x86_64aarch64
macOSCI, LocalLocal
LinuxCI, LocalCI, Local
Windows

其中,Linux 目前仅在 Ubuntu、Debian、Alpine 发行版测试通过,其他发行版未进行测试,不能保证编译成功。 对于未经过测试的发行版,可以使用 Docker 等方式本地编译,避免环境导致的问题。

macOS 下支持 x86_64 和 Arm 两种架构,但在其中一个架构上编译的二进制无法直接在另一个架构上使用。 Rosetta 2 不能保证 Arm 架构编译的程序可以完全运行在 x86_64 环境下。

PHP 支持版本

目前,static-php-cli 对 PHP 8.0 ~ 8.2 版本是支持的,对于 PHP 7.4 及更早版本理论上支持,只需下载时选择早期版本即可。 但由于部分扩展和特殊组件已对早期版本的 PHP 停止了支持,所以 static-php-cli 不会明确支持早期版本。

- diff --git a/zh/guide/manual-build.html b/zh/guide/manual-build.html index 535ad553..2206849c 100644 --- a/zh/guide/manual-build.html +++ b/zh/guide/manual-build.html @@ -83,7 +83,7 @@ # 排序配置文件 ext.json(也可以排序 lib、source) bin/spc dev:sort-config ext - diff --git a/zh/guide/troubleshooting.html b/zh/guide/troubleshooting.html index 04616bdb..e486393e 100644 --- a/zh/guide/troubleshooting.html +++ b/zh/guide/troubleshooting.html @@ -16,7 +16,7 @@
Skip to content
On this page

故障排除

使用 static-php-cli 过程中可能会碰到各种各样的故障,这里将讲述如何自行查看错误并反馈 Issue。

- diff --git a/zh/index.html b/zh/index.html index 91516bfe..b46ace81 100644 --- a/zh/index.html +++ b/zh/index.html @@ -16,7 +16,7 @@
Skip to content

static-php-cli

构建单个静态 PHP 二进制文件,将 PHP 项目放在一起,并包含流行的扩展。

静态二进制

您可以轻松地编译一个独立的 PHP 二进制文件以供嵌入程序使用。包括 cli、fpm、micro。

phpmicro 自执行二进制

您可以使用 micro SAPI 编译一个自解压的可执行文件,并将 PHP 代码与二进制文件打包为一个文件。

依赖管理

static-php-cli 附带依赖项管理,支持安装不同类型的 PHP 扩展和不同的依赖库。

-