WithRequestDocumentRoot sets the root directory of the PHP application. if resolveSymlink is true, oath declared as root directory will be resolved to its absolute value after the evaluation of any symbolic links. Due to the nature of PHP opcache, root directory path is cached: when using a symlinke
(documentRoot string, resolveSymlink bool)
| 31 | // symlink is changed without PHP being restarted; enabling this |
| 32 | // directive will set $_SERVER['DOCUMENT_ROOT'] to the real directory path. |
| 33 | func WithRequestDocumentRoot(documentRoot string, resolveSymlink bool) RequestOption { |
| 34 | return func(o *frankenPHPContext) (err error) { |
| 35 | v, ok := documentRootCache.Load(documentRoot) |
| 36 | if !ok { |
| 37 | // make sure file root is absolute |
| 38 | v, err = fastabs.FastAbs(documentRoot) |
| 39 | if err != nil { |
| 40 | return err |
| 41 | } |
| 42 | |
| 43 | // prevent the cache to grow forever, this is a totally arbitrary value |
| 44 | if documentRootCacheLen.Load() < 1024 { |
| 45 | documentRootCache.LoadOrStore(documentRoot, v) |
| 46 | documentRootCacheLen.Add(1) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | if resolveSymlink { |
| 51 | if v, err = filepath.EvalSymlinks(v.(string)); err != nil { |
| 52 | return err |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | o.documentRoot = v.(string) |
| 57 | |
| 58 | return nil |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | // WithRequestResolvedDocumentRoot is similar to WithRequestDocumentRoot |
| 63 | // but doesn't do any checks or resolving on the path to improve performance. |