File: Config.php

package info (click to toggle)
php-doc 20250827~git.abe740d%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 71,968 kB
  • sloc: xml: 985,760; php: 25,504; javascript: 671; sh: 177; makefile: 37
file content (180 lines) | stat: -rw-r--r-- 5,892 bytes parent folder | download
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?php
namespace phpdotnet\phd;

class Config
{
    public const VERSION = '@phd_version@';
    public readonly string $copyright;

    /** @var array<string, string> */
    public array $outputFormat = [];
    public bool $noIndex = false;
    public bool $forceIndex = false;
    public bool $noToc = false;
    public string $xmlRoot = '.';
    public string $xmlFile = './.manual.xml';
    public string $historyFile = './fileModHistory.php';
    public string $langDir = './';
    public string $language = 'en';
    public string $fallbackLanguage = 'en';
    public int $verbose = VERBOSE_DEFAULT;
    public string $dateFormat = 'H:i:s';
    /** @var array<string> */
    public array $renderIds = [];
    /** @var array<string> */
    public array $skipIds = [];
    private bool $colorOutput = true;
    public string $outputDir = './output/';
    public string $outputFilename = '';
    /** @var resource */
    public $phpErrorOutput = \STDERR;
    public string|false $phpErrorColor = '01;31'; // Red
    /** @var resource */
    public $userErrorOutput = \STDERR;
    public string|false $userErrorColor = '01;33'; // Yellow
    /** @var resource */
    public $phdInfoOutput = \STDOUT;
    public string|false $phdInfoColor = '01;32'; // Green
    /** @var resource */
    public $phdWarningOutput = \STDOUT;
    public string|false $phdWarningColor = '01;35'; // Magenta
    public string $highlighter = 'phpdotnet\\phd\\Highlighter';
    /** @var array<string> */
    public array $package =['Generic'];
    /** @var array<string> $css */
    public array $css = [];
    public bool $processXincludes = false;
    public ?string $ext = null;
    /** @var array<string> */
    public array $packageDirs = [__INSTALLDIR__];
    public bool $saveConfig = false;
    public bool $quit = false;
    public ?IndexRepository $indexCache = null;
    public bool $memoryIndex = false;

    public string $phpwebVersionFilename = '';
    public string $phpwebAcronymFilename = '';
    public string $phpwebSourcesFilename = '';
    public string $phpwebHistoryFilename = '';
    
    public function __construct() {
        $this->copyright = 'Copyright(c) 2007-'  . \date('Y', getenv('SOURCE_DATE_EPOCH') ?: time()) . ' The PHP Documentation Group';
        
        if('WIN' === \strtoupper(\substr(\PHP_OS, 0, 3))) {
        	$this->colorOutput = false;
        }
    }
    
    /**
     * Sets one or more configuration options from an array
     * 
     * @param array<string, mixed> $configOptions
     */
    public function init(array $configOptions): void {
        foreach ($configOptions as $option => $value) {
            if (! \property_exists($this, $option)) {
                throw new \Exception("Invalid option supplied: $option");
            }
            
            $this->$option = $value;
        }
        
        \error_reporting($GLOBALS['olderrrep'] | $this->verbose);
    }

    /**
     * Returns all configuration options and their values
     * 
     * @return array<string, mixed>
     */
    public function getAllFiltered(): array {
        return \get_object_vars($this);
    }

    /**
     * Returns the list of supported formats from the package directories set
     * 
     * @return array<string>
     */
    public function getSupportedPackages(): array {
        $packageList = [];
        foreach($this->packageDirs as $dir) {
            foreach (\glob($dir . "/phpdotnet/phd/Package/*", \GLOB_ONLYDIR) as $item) {
                $baseitem = \basename($item);
                if ($baseitem[0] !== '.') {
                    $packageList[] = $baseitem;
                }
            }
        }
        return $packageList;
    }

    /**
     * Returns whether terminal output supports colors 
     */
    public function getColorOutput(): bool {
        return $this->colorOutput;
    }

    /**
     * Enables/disables color output on the terminal
     */
    public function setColorOutput(bool $colorOutput): void {
        // Disable colored output if the terminal doesn't support colors
        if ($colorOutput && function_exists('posix_isatty')) {
            if (!posix_isatty($this->phdInfoOutput)) {
                $this->phdInfoColor = false;
            }
            if (!posix_isatty($this->phdWarningOutput)) {
                $this->phdWarningColor = false;
            }
            if (!posix_isatty($this->phpErrorOutput)) {
                $this->phpErrorColor = false;
            }
            if (!posix_isatty($this->userErrorOutput)) {
                $this->userErrorColor = false;
            }
        }
        $this->colorOutput = $colorOutput;
    }

    /**
     * Checks if indexing is needed.
     *
     * This is determined the following way:
     * 0. If no index file exists, indexing is required.
     * 1. If the config option --no-index is supplied, nothing is indexed
     * 2. If the config option --force-index is supplied, indexing is required
     * 3. If no option is given, the file modification time of the index and
     *    the manual docbook file are compared. If the index is older than
     *    the docbook file, indexing will be done.
     *
     * @return boolean True if indexing is required.
     */
    public function requiresIndexing(): bool {
        if (! $this->indexCache) {
            $indexfile = $this->outputDir . 'index.sqlite';
            if (!\file_exists($indexfile)) {
                return true;
            }
        }

        if ($this->noIndex) {
            return false;
        }

        if ($this->forceIndex) {
            return true;
        }

        if ($this->indexCache->getIndexingTimeCount() === 0) {
            return true;
        }

        $xmlLastModification = \filemtime($this->xmlFile);
        if ($this->indexCache->getIndexingTime() > $xmlLastModification) {
            return false;
        }
        return true;
    }
}