Skip to content

Commit 5ce83ac

Browse files
committed
Cleaned up prefix trimmer code
1 parent a868125 commit 5ce83ac

4 files changed

Lines changed: 127 additions & 49 deletions

File tree

src/OpCacheGUI/Format/Prefix.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
/**
3+
* Trims the common prefix from the full_path index of cached scripts
4+
*
5+
* PHP version 5.5
6+
*
7+
* @category OpCacheGUI
8+
* @package Format
9+
* @author Pieter Hordijk <info@pieterhordijk.com>
10+
* @copyright Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa>
11+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
12+
* @version 1.0.0
13+
*/
14+
namespace OpCacheGUI\Format;
15+
16+
/**
17+
* Trims the common prefix from the full_path index of cached scripts
18+
*
19+
* @category OpCacheGUI
20+
* @package Format
21+
* @author Pieter Hordijk <info@pieterhordijk.com>
22+
*/
23+
class Prefix implements Trimmer
24+
{
25+
/**
26+
* Trims the common prefix from an array of strings
27+
*
28+
* @param array $scripts The list of scripts
29+
*
30+
* @return array The trimmed strings
31+
*/
32+
public function trim(array $scripts)
33+
{
34+
$length = $this->getPrefixLength($scripts);
35+
36+
foreach ($scripts as $index => $script) {
37+
$scripts[$index]['full_path'] = mb_substr($script['full_path'], $length);
38+
}
39+
40+
return $scripts;
41+
}
42+
43+
/**
44+
* Gets the length of the common prefix to trim
45+
*
46+
* @param array $scripts The list of scripts
47+
*
48+
* @return int The length of the common prefix
49+
*/
50+
private function getPrefixLength(array $scripts)
51+
{
52+
$prefix = $scripts[0]['full_path'];
53+
54+
foreach ($scripts as $script) {
55+
if ($prefix === '') {
56+
return 0;
57+
}
58+
59+
if (strpos($script['full_path'], $prefix) === 0) {
60+
continue;
61+
}
62+
63+
$prefix = $this->findLongestPrefix($prefix, $script['full_path']);
64+
}
65+
66+
return mb_strlen($prefix);
67+
}
68+
69+
/**
70+
* Get the longest common prefix between two strings
71+
*
72+
* @param string $prefix The current common prefix
73+
* @param string $path The path to match against the common prefix
74+
*
75+
* @return string The common prefix
76+
*/
77+
private function findLongestPrefix($prefix, $path)
78+
{
79+
$prefixChars = str_split($prefix);
80+
$pathChars = str_split($path);
81+
82+
foreach ($prefixChars as $index => $char) {
83+
if ($char !== $pathChars[$index]) {
84+
return mb_substr($prefix, 0, $index);
85+
}
86+
}
87+
88+
return $prefix;
89+
}
90+
}

src/OpCacheGUI/Format/Trimmer.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Interface for string trimmers
4+
*
5+
* PHP version 5.5
6+
*
7+
* @category OpCacheGUI
8+
* @package Format
9+
* @author Pieter Hordijk <info@pieterhordijk.com>
10+
* @copyright Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa>
11+
* @license http://www.opensource.org/licenses/mit-license.html MIT License
12+
* @version 1.0.0
13+
*/
14+
namespace OpCacheGUI\Format;
15+
16+
/**
17+
* Interface for string trimmers
18+
*
19+
* @category OpCacheGUI
20+
* @package Format
21+
* @author Pieter Hordijk <info@pieterhordijk.com>
22+
*/
23+
interface Trimmer
24+
{
25+
/**
26+
* Trims the common prefix from an array of strings
27+
*
28+
* @param array $scripts The list of scripts
29+
*
30+
* @return array The trimmed strings
31+
*/
32+
public function trim(array $scripts);
33+
}

src/OpCacheGUI/OpCache/Status.php

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use OpCacheGUI\Format\Byte;
1717
use OpCacheGUI\I18n\Translator;
18+
use OpCacheGUI\Format\Trimmer;
1819

1920
/**
2021
* Container for the current status of OpCache
@@ -273,55 +274,9 @@ public function getCachedScripts()
273274
return $scripts;
274275
}
275276

276-
public function getCachedScriptsForOverview()
277+
public function getCachedScriptsForOverview(Trimmer $trimmer)
277278
{
278-
$scripts = $this->getCachedScripts();
279-
$prefix = $this->getCommonPrefix($scripts);
280-
281-
foreach ($scripts as $index => $script) {
282-
$scripts[$index]['full_path'] = mb_substr($script['full_path'], $prefix);
283-
}
284-
285-
return $scripts;
286-
}
287-
288-
private function getCommonPrefix(array $scripts)
289-
{
290-
$prefix = null;
291-
292-
foreach ($scripts as $script) {
293-
if ($prefix === null) {
294-
$prefix = $script['full_path'];
295-
296-
continue;
297-
}
298-
299-
if ($prefix === '') {
300-
return 0;
301-
}
302-
303-
if (strpos($script['full_path'], $prefix) === 0) {
304-
continue;
305-
}
306-
307-
$prefix = $this->findLongestPrefix($prefix, $script['full_path']);
308-
}
309-
310-
return mb_strlen($prefix);
311-
}
312-
313-
private function findLongestPrefix($prefix, $path)
314-
{
315-
$prefixChars = str_split($prefix);
316-
$pathChars = str_split($path);
317-
318-
foreach ($prefixChars as $index => $char) {
319-
if ($char !== $pathChars[$index]) {
320-
return mb_substr($prefix, 0, $index);
321-
}
322-
}
323-
324-
return $prefix;
279+
return $trimmer->trim($this->getCachedScripts());
325280
}
326281

327282
/**

template/cached.phtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ foreach($status->getCachedScripts() as $data) {
7777
</tr>
7878
</thead>
7979
<tbody>
80-
<?php foreach($status->getCachedScriptsForOverview() as $script) { ?>
80+
<?php foreach($status->getCachedScriptsForOverview(new \OpCacheGUI\Format\Prefix) as $script) { ?>
8181
<tr>
8282
<td><?= htmlspecialchars($script['full_path']); ?></td>
8383
<td><?= $script['hits']; ?></td>

0 commit comments

Comments
 (0)