Scanner::search()   B
last analyzed

Complexity

Conditions 7
Paths 2

Size

Total Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 26
rs 8.5706
c 0
b 0
f 0
cc 7
nc 2
nop 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2011 - 2014 Oleksandr Torosh (http://wezoom.net)
4
 * @author Oleksandr Torosh <[email protected]>
5
 */
6
7
namespace Cms;
8
9
class Scanner
10
{
11
12
    /**
13
     * @return array $phrases
14
     */
15
    public function search()
16
    {
17
        $phrases = [];
18
        $files_pattern = "/.*\\.(volt|php|phtml)$/";
19
        $files_plugins = $this->rsearch(APPLICATION_PATH . '/plugins', $files_pattern);
20
        $files_modules = $this->rsearch(APPLICATION_PATH . '/modules', $files_pattern);
21
        $files_views = $this->rsearch(APPLICATION_PATH . '/views', $files_pattern);
22
        $files = array_merge($files_plugins, $files_views, $files_modules);
23
        if (!empty($files)) {
24
            foreach ($files as $file) {
25
                if (file_exists($file)) {
26
                    $contents = file_get_contents($file);
27
                    $pattern = "/translate\('(.+?)'(?:.*)\)/";
28
                    $matchesCount = preg_match_all($pattern, $contents, $matches);
29
                    if ($matchesCount) {
30
                        foreach ($matches[1] as $match) {
31
                            if (!in_array($match, $phrases)) {
32
                                $phrases[] = $match;
33
                            }
34
                        }
35
                    }
36
                }
37
            }
38
        }
39
        return $phrases;
40
    }
41
42
    /**
43
     * @param string $folder
44
     * @param string $pattern
45
     * @return array $fileList
46
     */
47
    private function rsearch($folder, $pattern)
48
    {
49
        $dir = new \RecursiveDirectoryIterator($folder);
50
        $ite = new \RecursiveIteratorIterator($dir);
51
        $files = new \RegexIterator($ite, $pattern, \RegexIterator::GET_MATCH);
52
        $fileList = [];
53
        foreach ($files as $file) {
54
            $fileList = array_merge($fileList, $file);
55
        }
56
        return $fileList;
57
    }
58
59
} 
60