Completed
Pull Request — master (#4)
by
unknown
02:38
created

Resource   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 124
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 79.31%

Importance

Changes 5
Bugs 1 Features 1
Metric Value
c 5
b 1
f 1
dl 0
loc 124
ccs 23
cts 29
cp 0.7931
rs 10
wmc 11
lcom 1
cbo 1

4 Methods

Rating   Name   Duplication   Size   Complexity  
B scan() 0 34 6
A getWebRelativePath() 0 4 1
A getRelativePath() 0 21 3
A getProjectRelativePath() 0 4 1
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 15.05.16 at 10:52
5
 */
6
namespace samsonphp\resource;
7
8
use samsonphp\resource\exception\ResourceNotFound;
9
10
/**
11
 * Static resource entity class.
12
 *
13
 * @package samsonphp\resource
14
 */
15
class Resource
16
{
17
    /** Collection of excluding scanning folder patterns */
18
    const EXCLUDING_FOLDERS = [
19
        '*/cache/*',
20
        '*/tests/*',
21
        '*/vendor/*/vendor/*'
22
    ];
23
24
    /** @var string Full path to project web root directory */
25
    public static $webRoot;
26
27
    /** @var string Full path to project root directory */
28
    public static $projectRoot;
29
30
    /**
31
     * Recursively scan collection of paths to find assets with passed
32
     * extensions. Method is based on linux find command so this method
33
     * can face difficulties on other OS.
34
     *
35
     * TODO: Add windows support
36
     * TODO: Check if CMD commands can be executed
37
     *
38
     * @param array $paths      Paths for files scanning
39
     * @param array $extensions File extension filter
40
     * @param array $excludeFolders Path patterns for excluding
41
     *
42
     * @return array Found files
43
     */
44 1
    public static function scan(array $paths, array $extensions, array $excludeFolders = self::EXCLUDING_FOLDERS)
45
    {
46
        // Generate LINUX command to gather resources as this is 20 times faster
47 1
        $files = [];
48
49
        // Generate exclusion conditions
50
        $exclude = implode(' ', array_map(function ($value) {
51 1
            return '-not -path ' . $value.' ';
52 1
        }, $excludeFolders));
53
54
        // Generate other types
55
        $filters = implode('-o ', array_map(function ($value) use ($exclude) {
56 1
            return '-name "*.' . $value . '" '.$exclude;
57 1
        }, $extensions));
58
59
        // Scan path excluding folder patterns
60 1
        exec('find ' . implode(' ', $paths) . ' -type f '.$filters, $files);
61
62
        // Sort files alphabeticall
63 1
        usort ($files, function($a, $b) {
0 ignored issues
show
Coding Style introduced by
Space before opening parenthesis of function call prohibited
Loading history...
Coding Style introduced by
Expected 1 space after FUNCTION keyword; 0 found
Loading history...
64 1
            if (strpos($a, 'vendor/') !== false && strpos($b, 'vendor/') === false) {
65
                return -1;
66 1
            } elseif (strpos($b, 'vendor/') !== false && strpos($a, 'vendor/') === false) {
67
                return 1;
68 1
            } elseif ($a === $b) {
69
                return 0;
70
            } else {
71 1
                return strcmp($a, $b);
72
            }
73 1
        });
74
75
        // TODO: Why some paths have double slashes? Investigate speed of realpath, maybe // changing if quicker
76 1
        return array_map('realpath', $files);
77
    }
78
79
    /**
80
     * Build relative path to static resource relatively to web root path.
81
     *
82
     * @param string $relativePath Relative path to static resource
83
     * @param string $parentPath   Path to parent entity
84
     *
85
     * @return string Validated relative path to static resource relatively to web root path
86
     * @throws ResourceNotFound
87
     */
88
    public static function getWebRelativePath($relativePath, $parentPath = '')
89
    {
90
        return static::getRelativePath($relativePath, $parentPath, static::$webRoot);
91
    }
92
93
    /**
94
     * Build correct relative path to static resource using relative path and parent path.
95
     *
96
     * @param string $relativePath Relative path to static resource
97
     * @param string $parentPath   Path to parent entity
98
     * @param string $rootPath     Root path for relative path building
99
     *
100
     * @return string Validated relative path to static resource
101
     * @throws ResourceNotFound
102
     */
103 1
    public static function getRelativePath($relativePath, $parentPath = '', $rootPath = '')
104
    {
105
        // If parent path if not passed - use project root path
106 1
        $parentPath = $parentPath === '' ? static::$projectRoot : $parentPath;
107
108
        // Build full path to resource from given relative path
109 1
        $fullPath = rtrim($parentPath, DIRECTORY_SEPARATOR)
110
            .DIRECTORY_SEPARATOR
111 1
            .ltrim($relativePath, DIRECTORY_SEPARATOR);
112
113
        // Make real path with out possible "../"
114 1
        $realPath = realpath($fullPath);
115
116
        // Output link to static resource handler with relative path to project root
117 1
        if ($realPath) {
118
            // Build relative path to static resource
119 1
            return str_replace($rootPath, '', $realPath);
120
        }
121
122
        throw new ResourceNotFound($fullPath);
123
    }
124
125
    /**
126
     * Build relative path to static resource relatively to project root path.
127
     *
128
     * @param string $relativePath Relative path to static resource
129
     * @param string $parentPath   Path to parent entity
130
     *
131
     * @return string Validated relative path to static resource relatively to project root path
132
     * @throws ResourceNotFound
133
     */
134 1
    public static function getProjectRelativePath($relativePath, $parentPath = '')
135
    {
136 1
        return static::getRelativePath($relativePath, $parentPath, static::$projectRoot);
137
    }
138
}
139