mirror of
https://github.com/crazywhalecc/static-php-cli.git
synced 2026-03-19 05:14:52 +08:00
11 lines
16 KiB
JavaScript
11 lines
16 KiB
JavaScript
|
|
import{_ as o,c as t,o as c,ah as d}from"./chunks/framework.Bhsyh9kO.js";const m=JSON.parse('{"title":"Introduction to project structure","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/structure.md","filePath":"en/develop/structure.md"}'),a={name:"en/develop/structure.md"};function r(i,e,s,n,p,l){return c(),t("div",null,[...e[0]||(e[0]=[d(`<h1 id="introduction-to-project-structure" tabindex="-1">Introduction to project structure <a class="header-anchor" href="#introduction-to-project-structure" aria-label="Permalink to “Introduction to project structure”"></a></h1><p>static-php-cli mainly contains three logical components: sources, dependent libraries, and extensions. These components contains 4 configuration files: <code>source.json</code>, <code>pkg.json</code>, <code>lib.json</code>, and <code>ext.json</code>.</p><p>A complete process for building standalone static PHP is:</p><ol><li>Use the source download module <code>Downloader</code> to download specified or all source codes. These sources include PHP source code, dependent library source code, and extension source code.</li><li>Use the source decompression module <code>SourceExtractor</code> to decompress the downloaded sources to the compilation directory.</li><li>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.</li><li>After building each dependent library using <code>Builder</code> under the corresponding operating system, install it to the <code>buildroot</code> directory.</li><li>If external extensions are included (the source code does not contain extensions within PHP), copy the external extensions to the <code>source/php-src/ext/</code> directory.</li><li>Use <code>Builder</code> to build the PHP source code and build target to the <code>buildroot</code> directory.</li></ol><p>The project is mainly divided into several folders:</p><ul><li><code>bin/</code>: used to store program entry files, including <code>bin/spc</code>, <code>bin/spc-alpine-docker</code>, <code>bin/setup-runtime</code>.</li><li><code>config/</code>: 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 files: <code>lib.json</code>, <code>ext.json</code>, <code>source.json</code>, <code>pkg.json</code>, <code>pre-built.json</code> .</li><li><code>src/</code>: The core code of the project, including the entire framework and commands for compiling various extensions and libraries.</li><li><code>vendor/</code>: The directory that Composer depends on, you do not need to make any modifications to it.</li></ul><p>The operating principle is to start a <code>ConsoleApplication</code> of <code>symfony/console</code>, and then parse the commands entered by the user in the terminal.</p><h2 id="basic-command-line-structure" tabindex="-1">Basic command line structure <a class="header-anchor" href="#basic-command-line-structure" aria-label="Permalink to “Basic command line structure”"></a></h2><p><code>bin/spc</code> is an entry file, including the Unix common <code>#!/usr/bin/env php</code>, which is used to allow the system to automatically execute with the PHP interpreter installed on the system. After the project executes <code>new ConsoleApplication()</code>, the framework will automatically register them as commands.</p><p>The project does not directly use the Command registration method and command execution method recommended by Symfony. Here are small changes:</p><ol><li>Each command uses the <code>#[AsCommand()]</code> Attribute to register the name and description.</li><li>Abstract <code>execute()</code> so that all commands are based on <code>BaseCommand</code> (which is based on <code>Symfony\\Component\\Console\\Command\\Command</code>), and the execution code of each command itself is written in the <code>handle()</code> method .</li><li>Added variable <code>$no_motd</code> to <cod
|
|||
|
|
<span class="line"><span>src/App/MyCommand.app</span></span>
|
|||
|
|
<span class="line"><span>vendor/*</span></span>
|
|||
|
|
<span class="line"><span>bin/entry.php</span></span></code></pre></div><p>We assume that the above constants are obtained from <code>src/App/MyCommand.php</code>:</p><table tabindex="0"><thead><tr><th>Constant</th><th>Value</th></tr></thead><tbody><tr><td><code>WORKING_DIR</code></td><td><code>/home/example/static-php-cli</code></td></tr><tr><td><code>SOURCE_ROOT_DIR</code></td><td><code>/home/example/static-php-cli</code></td></tr><tr><td><code>FRAMEWORK_ROOT_DIR</code></td><td><code>/home/example/static-php-cli</code></td></tr><tr><td><code>__DIR__</code></td><td><code>/home/example/static-php-cli/src/App</code></td></tr><tr><td><code>__FILE__</code></td><td><code>/home/example/static-php-cli/src/App/MyCommand.php</code></td></tr></tbody></table><p>In this case, the values of <code>WORKING_DIR</code>, <code>SOURCE_ROOT_DIR</code>, and <code>FRAMEWORK_ROOT_DIR</code> are exactly the same: <code>/home/example/static-php-cli</code>.</p><p>The source code of the framework and the source code of the application are both in the current path.</p><h3 id="vendor-library-mode-vendor" tabindex="-1">Vendor library mode (vendor) <a class="header-anchor" href="#vendor-library-mode-vendor" aria-label="Permalink to “Vendor library mode (vendor)”"></a></h3><p>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, and the storage location is in the <code>vendor/author/XXX</code> directory.</p><p>Suppose your project is <code>crazywhalecc/static-php-cli</code>, and you or others install this project in another project using <code>composer require</code>.</p><p>We assume that static-php-cli contains all files except the <code>vendor</code> directory with the same <code>Git mode</code>, and get the constant value from <code>src/App/MyCommand</code>, Directory constant should be:</p><table tabindex="0"><thead><tr><th>Constant</th><th>Value</th></tr></thead><tbody><tr><td><code>WORKING_DIR</code></td><td><code>/home/example/another-app</code></td></tr><tr><td><code>SOURCE_ROOT_DIR</code></td><td><code>/home/example/another-app</code></td></tr><tr><td><code>FRAMEWORK_ROOT_DIR</code></td><td><code>/home/example/another-app/vendor/crazywhalecc/static-php-cli</code></td></tr><tr><td><code>__DIR__</code></td><td><code>/home/example/another-app/vendor/crazywhalecc/static-php-cli/src/App</code></td></tr><tr><td><code>__FILE__</code></td><td><code>/home/example/another-app/vendor/crazywhalecc/static-php-cli/src/App/MyCommand.php</code></td></tr></tbody></table><p>Here <code>SOURCE_ROOT_DIR</code> refers to the root directory of the project using <code>static-php-cli</code>.</p><h3 id="git-project-phar-mode-source-phar" tabindex="-1">Git project Phar mode (source-phar) <a class="header-anchor" href="#git-project-phar-mode-source-phar" aria-label="Permalink to “Git project Phar mode (source-phar)”"></a></h3><p>Git project Phar mode refers to the mode of packaging the project directory of the Git project mode into a <code>phar</code> file. We assume that <code>/home/example/static-php-cli</code> will be packaged into a Phar file, and the directory has the following files:</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span>composer.json</span></span>
|
|||
|
|
<span class="line"><span>src/App/MyCommand.app</span></span>
|
|||
|
|
<span class="line"><span>vendor/*</span></span>
|
|||
|
|
<span class="line"><span>bin/entry.php</span></span></code></pre></div><p>When packaged into <code>app.phar</code> and stored in the <code>/home/example/static-php-cli</code> directory, <code>app.phar</code> is executed at this time. Assuming that the <code>src/App/MyCommand</code> code is executed, the constant is obtained in the file:</p><table tabindex="0"><thead><tr><th>Constant</th><th>Value</th></tr></thead><tbody><tr><td><code>WORKING_DIR</code></td><td><code>/home/example/static-php-cli</code></td></tr><tr><td><code>SOURCE_ROOT_DIR</code></td><td><code>phar:///home/example/static-php-cli/app.phar/</code></td></tr><tr><td><code>FRAMEWORK_ROOT_DIR</code></td><td><code>phar:///home/example/static-php-cli/app.phar/</code></td></tr><tr><td><code>__DIR__</code></td><td><code>phar:///home/example/static-php-cli/app.phar/src/App</code></td></tr><tr><td><code>__FILE__</code></td><td><code>phar:///home/example/static-php-cli/app.phar/src/App/MyCommand.php</code></td></tr></tbody></table><p>Because the <code>phar://</code> protocol is required to read files in the phar itself, the project root directory and the framework directory will be different from <code>WORKING_DIR</code>.</p><h3 id="vendor-library-phar-mode-vendor-phar" tabindex="-1">Vendor Library Phar Mode (vendor-phar) <a class="header-anchor" href="#vendor-library-phar-mode-vendor-phar" aria-label="Permalink to “Vendor Library Phar Mode (vendor-phar)”"></a></h3><p>Vendor Library Phar Mode means that your project is installed as a framework in other projects and stored in the <code>vendor</code> directory.</p><p>We assume that your project directory structure is as follows:</p><div class="language-"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark" style="--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e;" tabindex="0" dir="ltr"><code><span class="line"><span>composer.json # Composer configuration file of the current project</span></span>
|
|||
|
|
<span class="line"><span>box.json # Configuration file for packaging Phar</span></span>
|
|||
|
|
<span class="line"><span>another-app.php # Entry file of another project</span></span>
|
|||
|
|
<span class="line"><span>vendor/crazywhalecc/static-php-cli/* # Your project is used as a dependent library</span></span></code></pre></div><p>When packaging these files under the directory <code>/home/example/another-app/</code> into <code>app.phar</code>, the value of the following constant for your project should be:</p><table tabindex="0"><thead><tr><th>Constant</th><th>Value</th></tr></thead><tbody><tr><td><code>WORKING_DIR</code></td><td><code>/home/example/another-app</code></td></tr><tr><td><code>SOURCE_ROOT_DIR</code></td><td><code>phar:///home/example/another-app/app.phar/</code></td></tr><tr><td><code>FRAMEWORK_ROOT_DIR</code></td><td><code>phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli</code></td></tr><tr><td><code>__DIR__</code></td><td><code>phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli/src/App</code></td></tr><tr><td><code>__FILE__</code></td><td><code>phar:///home/example/another-app/app.phar/vendor/crazywhalecc/static-php-cli/src/App/MyCommand.php</code></td></tr></tbody></table>`,50)])])}const u=o(a,[["render",r]]);export{m as __pageData,u as default};
|