Completed
Push — master ( 5aec63...25b23b )
by Peter
03:34 queued 01:01
created

Filesystem   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 147
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 6
Bugs 0 Features 2
Metric Value
wmc 29
lcom 1
cbo 1
dl 0
loc 147
c 6
b 0
f 2
ccs 0
cts 81
cp 0
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A getUserName() 0 4 2
A getUserHomeDir() 0 3 1
C doGetUserHomeDir() 0 34 8
C scandir() 0 57 17
A getRealPath() 0 5 1
1
<?php
2
/**
3
 * AnimeDb package
4
 *
5
 * @package   AnimeDb
6
 * @author    Peter Gribanov <[email protected]>
7
 * @copyright Copyright (c) 2011, Peter Gribanov
8
 * @license   http://opensource.org/licenses/GPL-3.0 GPL v3
9
 */
10
11
namespace AnimeDb\Bundle\AppBundle\Util;
12
13
use Patchwork\Utf8;
14
15
/**
16
 * Filesystem
17
 *
18
 * @package AnimeDb\Bundle\AppBundle\Util
19
 * @author  Peter Gribanov <[email protected]>
20
 */
21
class Filesystem
22
{
23
    /**
24
     * @var int
25
     */
26
    const FILE = 1;
27
28
    /**
29
     * @var int
30
     */
31
    const DIRECTORY = 2;
32
33
    /**
34
     * Gets the name of the owner of the current PHP script
35
     *
36
     * @return string
37
     */
38
    public static function getUserName()
39
    {
40
        return get_current_user() ?: getenv('USERNAME');
41
    }
42
43
    /**
44
     * Get user home directory
45
     *
46
     * @return string
47
     */
48
    public static function getUserHomeDir() {
49
        return self::getRealPath(self::doGetUserHomeDir());
50
    }
51
52
    /**
53
     * @return string
54
     */
55
    private static function doGetUserHomeDir()
56
    {
57
        // have home env var
58
        if ($home = getenv('HOME')) {
59
            return $home;
60
        }
61
62
        // *nix os
63
        if (!defined('PHP_WINDOWS_VERSION_BUILD')) {
64
            return '/home/'.self::getUserName();
65
        }
66
67
        // have drive and path env vars
68
        if (getenv('HOMEDRIVE') && getenv('HOMEPATH')) {
69
            return iconv('cp1251', 'utf-8', getenv('HOMEDRIVE').getenv('HOMEPATH'));
70
        }
71
72
        // Windows
73
        if ($username = self::getUserName()) {
74
            $username = iconv('cp1251', 'utf-8', $username);
75
            // is Vista or older
76
            if (is_dir($win7path = 'C:\Users\\'.$username.'\\')) {
77
                return $win7path;
78
            }
79
            return 'C:\Documents and Settings\\'.$username.'\\';
80
        }
81
82
        // is Vista or older
83
        if (is_dir('C:\Users\\')) {
84
            return 'C:\Users\\';
85
        }
86
87
        return 'C:\Documents and Settings\\';
88
    }
89
90
    /**
91
     * List files and directories inside the specified path
92
     *
93
     * @param string $path
94
     * @param int $filter
95
     * @param int $order
96
     *
97
     * @return array
98
     */
99
    public static function scandir($path, $filter = 0, $order = SCANDIR_SORT_ASCENDING)
100
    {
101
        if (!$filter || (
102
            ($filter & self::FILE) != self::FILE &&
103
            ($filter & self::DIRECTORY) != self::DIRECTORY
104
        )) {
105
            $filter = self::FILE|self::DIRECTORY;
106
        }
107
        // add slash if need
108
        $path = self::getRealPath($path);
109
        // wrap path for current fs
110
        $wrap = Utf8::wrapPath($path);
111
112
        // scan directory
113
        $folders = [];
114
        foreach (new \DirectoryIterator($wrap) as $file) {
115
            /* @var $file \SplFileInfo */
116
            try {
117
                if (
118
                    $file->getFilename()[0] != '.' &&
119
                    substr($file->getFilename(), -1) != '~' &&
120
                    $file->getFilename() != 'pagefile.sys' && // failed read C:\pagefile.sys
121
                    $file->isReadable() &&
122
                    (
123
                        (($filter & self::FILE) == self::FILE && $file->isFile()) ||
124
                        (($filter & self::DIRECTORY) == self::DIRECTORY && $file->isDir())
125
                    )
126
                ) {
127
                    $folders[$file->getFilename()] = [
128
                        'name' => $file->getFilename(),
129
                        'path' => $path . $file->getFilename() . DIRECTORY_SEPARATOR
130
                    ];
131
                }
132
            } catch (\Exception $e) {
133
                // ignore all errors
134
            }
135
        }
136
137
        // order files
138
        if ($order == SCANDIR_SORT_ASCENDING) {
139
            ksort($folders);
140
        } elseif ($order == SCANDIR_SORT_DESCENDING) {
141
            ksort($folders);
142
            $folders = array_reverse($folders);
143
        }
144
145
        // add link on parent folder
146
        if (substr_count($path, DIRECTORY_SEPARATOR) > 1) {
147
            $pos = strrpos(substr($path, 0, -1), DIRECTORY_SEPARATOR) + 1;
148
            array_unshift($folders, [
149
                'name' => '..',
150
                'path' => substr($path, 0, $pos)
151
            ]);
152
        }
153
154
        return array_values($folders);
155
    }
156
157
    /**
158
     * @param string $path
159
     *
160
     * @return string
161
     */
162
    public static function getRealPath($path)
163
    {
164
        $path = str_replace(['\\', '/'], DIRECTORY_SEPARATOR, $path);
165
        return rtrim($path, DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
166
    }
167
}
168