en_develop_structure.md.BA5PDIse.js 16 KB

12345678910
  1. import{_ as e,c as o,o as t,a1 as c}from"./chunks/framework.gjrnbxUT.js";const y=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"},a=c(`<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 &quot;Introduction to project structure&quot;">​</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 &quot;Basic command line structure&quot;">​</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 <code>BaseCommand</code>, which is used to display the Figlet greeting when the command is executed.</li><li><code>BaseCommand</code> saves <code>InputInterface</code> and <code>OutputInterface</code> as member variables. You can use <code>$this-&gt;input</code> and <code>$this-&gt;output</code> within the command class.</li></ol><h2 id="basic-source-code-structure" tabindex="-1">Basic source code structure <a class="header-anchor" href="#basic-source-code-structure" aria-label="Permalink to &quot;Basic source code structure&quot;">​</a></h2><p>The source code of the project is located in the <code>src/SPC</code> directory, supports automatic loading of the PSR-4 standard, and contains the following subdirectories and classes:</p><ul><li><code>src/SPC/builder/</code>: 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.</li><li><code>src/SPC/command/</code>: All commands of the project are here.</li><li><code>src/SPC/doctor/</code>: Doctor module, which is a relatively independent module used to check the system environment. It can be entered using the command <code>bin/spc doctor</code>.</li><li><code>src/SPC/exception/</code>: exception class.</li><li><code>src/SPC/store/</code>: Classes related to storage, files and sources are all here.</li><li><code>src/SPC/util/</code>: Some reusable tool methods are here.</li><li><code>src/SPC/ConsoleApplication.php</code>: command line program entry file.</li></ul><p>If you have read the source code, you may find that there is also a <code>src/globals/</code> 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.</p><h2 id="phar-application-directory-issue" tabindex="-1">Phar application directory issue <a class="header-anchor" href="#phar-application-directory-issue" aria-label="Permalink to &quot;Phar application directory issue&quot;">​</a></h2><p>Like other php-cli projects, spc itself has additional considerations for paths. Because spc can run in multiple modes such as <code>php-cli directly</code>, <code>micro SAPI</code>, <code>php-cli with Phar</code>, <code>vendor with Phar</code>, etc., there are ambiguities in various root directories. A complete explanation is given here. This problem is generally common in the base class path selection problem of accessing files in PHP projects, especially when used with <code>micro.sfx</code>.</p><p>Note that this may only be useful for you when developing Phar projects or PHP frameworks.</p><blockquote><p>Next, we will treat <code>static-php-cli</code> (that is, spc) as a normal <code>php</code> command line program. You can understand spc as any of your own php-cli applications for reference.</p></blockquote><p>There are three basic constant theoretical values below. We recommend that you introduce these three constants when writing PHP projects:</p><ul><li><p><code>WORKING_DIR</code>: the working directory when executing PHP scripts</p></li><li><p><code>SOURCE_ROOT_DIR</code> or <code>ROOT_DIR</code>: the root directory of the project folder, generally the directory where <code>composer.json</code> is located</p></li><li><p><code>FRAMEWORK_ROOT_DIR</code>: the root directory of the framework used, which may be used by self-developed frameworks. Generally, the framework directory is read-only</p></li></ul><p>You can define these constants in your framework entry or cli applications to facilitate the use of paths in your project.</p><p>The following are PHP built-in constant values, which have been defined inside the PHP interpreter:</p><ul><li><p><code>__DIR__</code>: the directory where the file of the currently executed script is located</p></li><li><p><code>__FILE__</code>: the file path of the currently executed script</p></li></ul><h3 id="git-project-mode-source" tabindex="-1">Git project mode (source) <a class="header-anchor" href="#git-project-mode-source" aria-label="Permalink to &quot;Git project mode (source)&quot;">​</a></h3><p>Git project mode refers to a framework or program itself stored in plain text in the current folder, and running through <code>php path/to/entry.php</code>.</p><p>Assume that your project is stored in the <code>/home/example/static-php-cli/</code> directory, or your project is the framework itself, which contains project files such as <code>composer.json</code>:</p><div class="language- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>composer.json</span></span>
  2. <span class="line"><span>src/App/MyCommand.app</span></span>
  3. <span class="line"><span>vendor/*</span></span>
  4. <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 &quot;Vendor library mode (vendor)&quot;">​</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 &quot;Git project Phar mode (source-phar)&quot;">​</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- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>composer.json</span></span>
  5. <span class="line"><span>src/App/MyCommand.app</span></span>
  6. <span class="line"><span>vendor/*</span></span>
  7. <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 &quot;Vendor Library Phar Mode (vendor-phar)&quot;">​</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- vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang"></span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span>composer.json # Composer configuration file of the current project</span></span>
  8. <span class="line"><span>box.json # Configuration file for packaging Phar</span></span>
  9. <span class="line"><span>another-app.php # Entry file of another project</span></span>
  10. <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),r=[a];function i(s,n,p,l,h,m){return t(),o("div",null,r)}const b=e(d,[["render",i]]);export{y as __pageData,b as default};