1234567891011121314151617181920212223242526272829 |
- import{_ as s,c as i,o as a,a1 as e}from"./chunks/framework.gjrnbxUT.js";const g=JSON.parse('{"title":"Doctor module","description":"","frontmatter":{},"headers":[],"relativePath":"en/develop/doctor-module.md","filePath":"en/develop/doctor-module.md"}'),t={name:"en/develop/doctor-module.md"},h=e(`<h1 id="doctor-module" tabindex="-1">Doctor module <a class="header-anchor" href="#doctor-module" aria-label="Permalink to "Doctor module""></a></h1><p>The Doctor module is a relatively independent module used to check the system environment, which can be entered with the command <code>bin/spc doctor</code>, and the entry command class is in <code>DoctorCommand.php</code>.</p><p>The Doctor module is a checklist with a series of check items and automatic repair items. These items are stored in the <code>src/SPC/doctor/item/</code> directory, And two Attributes are used as check item tags and auto-fix item tags: <code>#[AsCheckItem]</code> and <code>#[AsFixItem]</code>.</p><p>Take the existing check item <code>if necessary tools are installed</code>, which is used to check whether the packages necessary for compilation are installed in the macOS system. The following is its source code:</p><div class="language-php vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">php</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">use</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> SPC\\doctor\\AsCheckItem</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">use</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> SPC\\doctor\\AsFixItem</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">use</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> SPC\\doctor\\CheckResult</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
- <span class="line"></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">#[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">AsCheckItem</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'if necessary tools are installed'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">limit_os</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Darwin'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">level</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">997</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)]</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> function</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> checkCliTools</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">()</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">:</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> ?</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">CheckResult</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $missing </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [];</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">self::</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">REQUIRED_COMMANDS</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> as</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $cmd) {</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">$this</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-></span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">findCommand</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">($cmd) </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">===</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> null</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $missing[] </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">=</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $cmd;</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">!</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">empty</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">($missing)) {</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> CheckResult</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">::</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">fail</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'missing system commands: '</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> .</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> implode</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">', '</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, $missing), </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'build-tools'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, [$missing]);</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> CheckResult</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">::</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">ok</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><p>The first parameter of the attribute is the name of the check item, and the following <code>limit_os</code> parameter restricts the check item to be triggered only under the specified system, and <code>level</code> is the priority of executing the check item, the larger the number, the higher the priority higher.</p><p>The <code>$this->findCommand()</code> method used in it is the method of <code>SPC\\builder\\traits\\UnixSystemUtilTrait</code>, the purpose is to find the location of the system command, and return NULL if it cannot be found.</p><p>Each check item method should return a <code>SPC\\doctor\\CheckResult</code>:</p><ul><li>When returning <code>CheckResult::fail()</code>, the first parameter is used to output the error prompt of the terminal, and the second parameter is the name of the repair item when this check item can be automatically repaired.</li><li>When <code>CheckResult::ok()</code> is returned, the check passed. You can also pass a parameter to return the check result, for example: <code>CheckResult::ok('OS supported')</code>.</li><li>When returning <code>CheckResult::fail()</code>, if the third parameter is included, the array of the third parameter will be used as the parameter of <code>AsFixItem</code>.</li></ul><p>The following is the method for automatically repairing items corresponding to this check item:</p><div class="language-php vp-adaptive-theme"><button title="Copy Code" class="copy"></button><span class="lang">php</span><pre class="shiki shiki-themes github-light github-dark vp-code" tabindex="0"><code><span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">#[</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">AsFixItem</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'build-tools'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)]</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">public</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> function</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> fixBuildTools</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">array</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $missing)</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">:</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> bool</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">{</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> foreach</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> ($missing </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">as</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> $cmd) {</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
- <span class="line"><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> shell</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">)</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">-></span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">exec</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'brew install '</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> .</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> escapeshellarg</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">($cmd));</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> } </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">RuntimeException</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">) {</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> false</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> }</span></span>
- <span class="line"><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> return</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> true</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">;</span></span>
- <span class="line"><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></div><p><code>#[AsFixItem()]</code> 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.</p><p>In the code here, <code>shell()->exec()</code> is the method of executing commands of the project, which is used to replace <code>exec()</code> and <code>system()</code>, and also provides debugging, obtaining execution status, entering directories, etc. characteristic.</p>`,13),n=[h];function l(k,p,r,d,o,c){return a(),i("div",null,n)}const y=s(t,[["render",l]]);export{g as __pageData,y as default};
|