Total Complexity | 16 |
Total Lines | 115 |
Duplicated Lines | 0 % |
Coverage | 98.04% |
Changes | 4 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
43 | class Ini extends Abstracts\Engine\Ini implements Interfaces\Engine\Ini |
||
44 | { |
||
45 | public function __construct(Interfaces\Engine $php) |
||
46 | { |
||
47 | 1 | parent::__construct($php); |
|
48 | |||
49 | 1 | $this->setupPickleSectionPositions(); |
|
50 | 1 | } |
|
51 | |||
52 | public function updatePickleSection(array $dlls_add, array $dlls_del = []) |
||
53 | { |
||
54 | 1 | $before = ''; |
|
|
|||
55 | 1 | $after = ''; |
|
56 | |||
57 | 1 | $pickleSection = $this->rebuildPickleParts($this->getPickleSection(), $dlls_add, $dlls_del); |
|
58 | |||
59 | 1 | $before = substr($this->raw, 0, $this->pickleHeaderStartPos); |
|
60 | |||
61 | /* If the footer end pos is < 0, there was no footer in php.ini. In this case the footer start pos |
||
62 | means the end of the last extension directive after the header start, where the footer should be */ |
||
63 | 1 | if ($this->pickleFooterEndPos > 0) { |
|
64 | 1 | $after = substr($this->raw, $this->pickleFooterEndPos); |
|
65 | } else { |
||
66 | 1 | $after = substr($this->raw, $this->pickleFooterStartPos); |
|
67 | } |
||
68 | |||
69 | 1 | $before = rtrim($before); |
|
70 | 1 | $after = ltrim($after); |
|
71 | |||
72 | 1 | $this->raw = $before . "\n\n" . self::PICKLE_HEADER . "\n" . trim($pickleSection) . "\n" . self::PICKLE_FOOTER . "\n\n" . $after; |
|
73 | 1 | if (!@file_put_contents($this->path, $this->raw)) { |
|
74 | throw new Exception('Cannot update php.ini'); |
||
75 | } |
||
76 | 1 | } |
|
77 | |||
78 | /** |
||
79 | * @param string $pickleSection |
||
80 | * |
||
81 | * @return string |
||
82 | */ |
||
83 | protected function rebuildPickleParts($pickleSection, array $dlls_add, array $dlls_del = []) |
||
84 | { |
||
85 | 1 | $lines = explode("\n", $pickleSection); |
|
86 | 1 | $new = []; |
|
87 | |||
88 | /* First add the lines for exts that are requested to be added. */ |
||
89 | 1 | foreach ($dlls_add as $dll) { |
|
90 | 1 | $new[] = $this->buildDllIniLine($dll); |
|
91 | } |
||
92 | |||
93 | /* Then, go over the existing lines, restore those that are not |
||
94 | requested to be deleted and not already added. */ |
||
95 | 1 | foreach ($lines as $l) { |
|
96 | 1 | $l = trim($l); |
|
97 | 1 | if (strpos($l, 'extension') !== 0) { |
|
98 | 1 | continue; |
|
99 | } |
||
100 | 1 | [, $dllname] = explode('=', $l); |
|
101 | |||
102 | 1 | if (in_array(trim($dllname), $dlls_add)) { |
|
103 | /* don't create a duplicated item */ |
||
104 | 1 | continue; |
|
105 | } |
||
106 | 1 | if (in_array(trim($dllname), $dlls_del)) { |
|
107 | /* don't restore as it should be deleted */ |
||
108 | 1 | continue; |
|
109 | } |
||
110 | 1 | $new[] = $l; |
|
111 | } |
||
112 | |||
113 | 1 | sort($new); |
|
114 | |||
115 | 1 | return implode("\n", $new); |
|
116 | } |
||
117 | |||
118 | protected function setupPickleSectionPositions() |
||
119 | { |
||
120 | 1 | $posHeader = strpos($this->raw, self::PICKLE_HEADER); |
|
121 | 1 | if ($posHeader === false) { |
|
122 | /* no pickle section here yet */ |
||
123 | 1 | $this->pickleHeaderStartPos = strlen($this->raw); |
|
124 | |||
125 | 1 | return; |
|
126 | } |
||
127 | |||
128 | 1 | $this->pickleHeaderStartPos = $posHeader; |
|
129 | 1 | $this->pickleHeaderEndPos = $this->pickleHeaderStartPos + strlen(self::PICKLE_HEADER); |
|
130 | |||
131 | 1 | $posFooter = strpos($this->raw, self::PICKLE_FOOTER); |
|
132 | 1 | if ($posFooter === false) { |
|
133 | /* This is bad, no end of section marker, will have to lookup. The strategy is |
||
134 | - look for the last extension directve after the header |
||
135 | - extension directives are expected to come one after another one per line |
||
136 | - comments are not expected inbetveen |
||
137 | - mark the next pos after the last extension directive as the footer pos |
||
138 | */ |
||
139 | 1 | $pos = $this->pickleHeaderEndPos; |
|
140 | do { |
||
141 | 1 | $pos = strpos($this->raw, 'extension', $pos); |
|
142 | 1 | if ($pos !== false) { |
|
143 | 1 | $this->pickleFooterStartPos = $pos; |
|
144 | 1 | $pos++; |
|
145 | } |
||
146 | 1 | } while ($pos !== false); |
|
147 | |||
148 | 1 | $this->pickleFooterStartPos = strpos($this->raw, "\n", $this->pickleFooterStartPos); |
|
149 | } else { |
||
150 | 1 | $this->pickleFooterStartPos = $posFooter; |
|
151 | 1 | $this->pickleFooterEndPos = $this->pickleFooterStartPos + strlen(self::PICKLE_FOOTER); |
|
152 | } |
||
153 | 1 | } |
|
154 | |||
155 | protected function buildDllIniLine($dll) |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /* vim: set tabstop=4 shiftwidth=4 expandtab: fdm=marker */ |
||
162 |