-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDirectoryPrinter.php
109 lines (86 loc) · 2.9 KB
/
DirectoryPrinter.php
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
<?php
require_once 'RealRecursiveDirectoryIterator.php';
require_once 'AdvancedDirectoryIterator.php';
class DirectoryPrinter
{
protected $directoryPattern;
public function __construct($directoryPattern = null)
{
$this->setDirectoryPattern($directoryPattern);
}
public function printMusicDirectory()
{
$this->printDirectory('musicFiles');
}
public function printDocumentDirectory()
{
$this->printDirectory('documentFiles');
}
public function printOtherDirectory()
{
$this->printDirectory('otherFiles');
}
public function printAllDirectory()
{
// Get files and combine them, then set a different css class depending on type
$files = $this->getFileArrays();
$combinedFiles = array_merge($files['musicFiles'], $files['documentFiles'], $files['otherFiles']);
sort($combinedFiles, SORT_LOCALE_STRING);
$directoryPattern = trim(str_replace('-R', '', $this->directoryPattern));
/** @var $file DirectoryIterator */
foreach ($combinedFiles as $file) {
$prettyName = str_replace($directoryPattern, '', $file->getPathname());
print '<a class="' . $file->category . '" href="' . $file->getPathname() . '">' . $prettyName . '</a><br />';
}
}
protected function printDirectory($type)
{
$files = $this->getFileArrays();
sort($files[$type], SORT_LOCALE_STRING);
$directoryPattern = trim(str_replace('-R', '', $this->directoryPattern));
/** @var $file DirectoryIterator */
foreach ($files[$type] as $file) {
$prettyName = str_replace($directoryPattern, '', $file->getPathname());
print '<a class="' . $type . '" href="' . $file->getPathname() . '">' . $prettyName . '</a><br />';
}
}
protected function getFileArrays()
{
$allFiles = $musicFiles = $documentFiles = $otherFiles = array();
/** @var $file DirectoryIterator */
foreach (new AdvancedDirectoryIterator($this->directoryPattern) as $file) {
$fileInfo = $file->getFileInfo(); // have to do it this way to be php 5.2 compatible
$extension = pathinfo($fileInfo->getFilename(), PATHINFO_EXTENSION);
if (in_array($extension, array('mp3', 'ogg', 'flac', 'aac', 'wav', 'aiff'))) { // need a more complete list of music formats
$file->category = 'musicFiles';
$musicFiles[] = $file;
}
else if (in_array($extension, array('pdf', 'doc', 'mobi', 'epub', 'docx', 'txt', 'conf', 'ini'))) { // need a more complete list of document formats
$file->category = 'documentFiles';
$documentFiles[] = $file;
}
else {
$file->category = 'otherFiles';
$otherFiles[] = $file;
}
$allFiles[] = $file;
}
$return = array(
'allFiles' => $allFiles,
'musicFiles' => $musicFiles,
'documentFiles' => $documentFiles,
'otherFiles' => $otherFiles,
);
return $return;
}
public function getDirectoryPattern()
{
return $this->directoryPattern;
}
public function setDirectoryPattern($directoryPattern)
{
if ($directoryPattern) {
$this->directoryPattern = $directoryPattern;
}
}
}