-
Notifications
You must be signed in to change notification settings - Fork 11
/
php_file_manager.go
88 lines (73 loc) · 2.45 KB
/
php_file_manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package phpdist
import (
"bytes"
"fmt"
"html/template"
"io"
"os"
"path/filepath"
"github.com/paketo-buildpacks/packit/v2/fs"
)
// PHPIniConfig represents the data that will be inserted in a templated
// php.ini file.
type PhpIniConfig struct {
IncludePath string
// include_path = "{{.PhpHome}}/lib/php:{{.AppRoot}}/{{.LibDirectory}}"
ExtensionDir string
// extension_dir = "{{.PhpHome}}/lib/php/extensions/no-debug-non-zts-{{.PhpAPI}}"
Extensions []string
ZendExtensions []string
}
// PHPFileManager finds, copies, and creates files necessary for a proper PHP
// installation.
type PHPFileManager struct{}
func NewPHPFileManager() PHPFileManager {
return PHPFileManager{}
}
// FindExtensions checks a path relative to the layer passed as input
// where it expects extensions to be pre-installed. It fails if a directory
// in the expected location does not exist.
func (f PHPFileManager) FindExtensions(layerRoot string) (string, error) {
folders, err := filepath.Glob(filepath.Join(layerRoot, "lib/php/extensions/no-debug-non-zts-*"))
if err != nil {
return "", err
}
if len(folders) != 1 {
return "", fmt.Errorf("Expected 1 PHP extensions dir matching '%s', but found %d", filepath.Join(layerRoot, "lib/php/extensions/no-debug-non-zts-*"), len(folders))
}
return folders[0], nil
}
// WriteConfig generates a default PHP configuration and stores the resulting *.ini
// files in the layer provided as input.
func (f PHPFileManager) WriteConfig(layerRoot, cnbPath string, data PhpIniConfig) (string, string, error) {
err := os.MkdirAll(filepath.Join(layerRoot, "etc"), os.ModePerm)
if err != nil {
return "", "", err
}
defaultConfig := filepath.Join(layerRoot, "etc", "php.ini")
err = fs.Copy(filepath.Join(cnbPath, "config", "default.ini"), defaultConfig)
if err != nil {
return "", "", err
}
tmpl, err := template.New("buildpack.ini").ParseFiles(filepath.Join(cnbPath, "config", "buildpack.ini"))
if err != nil {
return "", "", fmt.Errorf("failed to parse php.ini template: %w", err)
}
var b bytes.Buffer
err = tmpl.Execute(&b, data)
if err != nil {
// not tested
return "", "", err
}
buildpackConfig, err := os.OpenFile(filepath.Join(layerRoot, "etc", "buildpack.ini"), os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.ModePerm)
if err != nil {
return "", "", err
}
defer buildpackConfig.Close()
_, err = io.Copy(buildpackConfig, &b)
if err != nil {
// not tested
return "", "", err
}
return defaultConfig, buildpackConfig.Name(), nil
}