Passed
Push — develop ( 76f1bd...bca4a5 )
by Bjarn
01:40 queued 11s
created

src/extensions/phpExtension.ts   A

Complexity

Total Complexity 11
Complexity/F 1.83

Size

Lines of Code 103
Function Count 6

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 73
dl 0
loc 103
rs 10
c 0
b 0
f 0
wmc 11
mnd 5
bc 5
fnc 6
bpm 0.8333
cpm 1.8333
noi 0

6 Functions

Rating   Name   Duplication   Size   Complexity  
A PhpExtension.disable 0 10 1
A PhpExtension.isInstalled 0 3 1
A PhpExtension.uninstall 0 2 1
A PhpExtension.isEnabled 0 5 1
A PhpExtension.enable 0 14 2
B PhpExtension.install 0 24 5
1
import execa from 'execa'
2
import * as fs from 'fs'
3
import {NORMAL_EXTENSION_TYPE} from './extensions'
4
import Pecl from './pecl'
5
6
abstract class PhpExtension {
7
    abstract extension: string
8
    abstract alias: string
9
10
    // Extension settings
11
    default: boolean = true
12
    extensionType: string = NORMAL_EXTENSION_TYPE
13
14
    /**
15
     * Check if the extension is enabled.
16
     */
17
    isEnabled = async (): Promise<boolean> => {
18
        const {stdout} = await execa('php', ['-m', '|', 'grep', this.extension])
19
        const extensions = stdout.split('\n')
20
21
        return extensions.includes(this.extension)
22
    }
23
24
    /**
25
     * Check if the extension is installed.
26
     */
27
    isInstalled = async (): Promise<boolean> => {
28
        const {stdout} = await execa('pecl', ['list', '|', 'grep', this.extension])
29
        return stdout.includes(this.extension)
30
    }
31
32
    /**
33
     * Install the extension.
34
     */
35
    install = async (): Promise<void> => {
36
        if (await this.isInstalled()) {
37
            console.log(`Extension ${this.extension} is already installed.`)
38
            return
39
        }
40
41
        const {stdout} = await execa('pecl', ['install', this.extension])
42
43
        const installRegex = new RegExp(`Installing '(.*${this.alias}.so)'`, 'g').test(stdout)
44
        if (!installRegex)
45
            throw new Error(`Unable to find installation path for ${this.extension}. Result:\n\n`)
46
47
        if (stdout.includes('Error:'))
48
            throw new Error(`Found installation path, but installation still failed: \n\n${stdout}`)
49
50
        const phpIniPath = await Pecl.getPhpIni()
51
        let phpIni = await fs.readFileSync(phpIniPath, 'utf-8')
52
53
        // TODO: Fix duplicate extension entires in php.ini
54
        const extensionRegex = new RegExp(`(zend_extension|extension)="(.*${this.alias}.so)"`, 'g').test(phpIni)
55
        if (!extensionRegex)
56
            throw new Error(`Unable to find definition in ${phpIniPath} for ${this.extension}`)
57
58
        console.log(`Extension ${this.extension} has been installed.`)
59
    }
60
61
    /**
62
     * Uninstall the extension.
63
     */
64
    uninstall = async (): Promise<void> => {
65
        await execa('pecl', ['uninstall', this.extension])
66
    }
67
68
    /**
69
     * Enable the extension.
70
     */
71
    enable = async (): Promise<void> => {
72
        if (await this.isEnabled()) {
73
            console.log(`Extension ${this.extension} is already enabled.`)
74
        }
75
76
        const phpIniPath = await Pecl.getPhpIni()
77
        let phpIni = await fs.readFileSync(phpIniPath, 'utf-8')
78
        const regex = new RegExp(`(zend_extension|extension)="(.*${this.alias}.so)"\/n`, 'g')
79
        phpIni = phpIni.replace(regex, '')
80
        phpIni = `${this.extensionType}="${this.alias}.so"\n${phpIni}`
81
82
        await fs.writeFileSync(phpIniPath, phpIni)
83
84
        console.log(`Extension ${this.extension} has been enabled`)
85
    }
86
87
    /**
88
     * Disable the extension.
89
     */
90
    disable = async (): Promise<void> => {
91
        const phpIniPath = await Pecl.getPhpIni()
92
        let phpIni = await fs.readFileSync(phpIniPath, 'utf-8')
93
94
        const regex = new RegExp(`;?(zend_extension|extension)=".*${this.alias}.so"\n`, 'g')
95
        phpIni = phpIni.replace(regex, '')
96
97
        await fs.writeFileSync(phpIniPath, phpIni)
98
99
        console.log(`Extension ${this.extension} has been disabled`)
100
    }
101
}
102
103
export default PhpExtension