Localization_Source_Folder::processFolder()   B
last analyzed

Complexity

Conditions 8
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 8
eloc 11
nc 5
nop 2
dl 0
loc 19
rs 8.4444
c 1
b 1
f 0
1
<?php
2
/**
3
 * File containing the class {@see \AppLocalize\Localization_Source_Folder}.
4
 *
5
 * @package Localization
6
 * @subpackage Parser
7
 * @see \AppLocalize\Localization_Source_Folder
8
 */
9
10
declare(strict_types=1);
11
12
namespace AppLocalize;
13
14
use DirectoryIterator;
15
16
/**
17
 * Localization source that reads text to translate from files
18
 * stored in a folder.
19
 *
20
 * @package Localization
21
 * @subpackage Parser
22
 * @author Sebastian Mordziol <[email protected]>
23
 */
24
class Localization_Source_Folder extends Localization_Source
25
{
26
   /**
27
    * The folder under which all translatable files are kept.
28
    * @var string
29
    */
30
    protected $sourcesFolder;
31
    
32
   /**
33
    * @var string
34
    */
35
    protected $id;
36
37
   /**
38
    * @param string $alias An alias for this source, to recognize it by.
39
    * @param string $label The human-readable label, used in the editor.
40
    * @param string $group A human-readable group label to group several sources by. Used in the editor.
41
    * @param string $storageFolder The folder in which to store the localization files.
42
    * @param string $sourcesFolder The folder in which to analyze files to find translatable strings.
43
    */
44
    public function __construct(string $alias, string $label, string $group, string $storageFolder, string $sourcesFolder)
45
    {
46
        parent::__construct($alias, $label, $group, $storageFolder);
47
        
48
        $this->sourcesFolder = $sourcesFolder;
49
        $this->id = md5($sourcesFolder);
50
    }
51
    
52
    public function getID() : string
53
    {
54
        return $this->id;
55
    }
56
    
57
    public function getSourcesFolder() : string
58
    {
59
        return $this->sourcesFolder;
60
    }
61
62
    /**
63
     * @var array<string,string[]>
64
     */
65
    protected $excludes = array(
66
        'folders' => array(),
67
        'files' => array()
68
    );
69
    
70
    public function excludeFolder(string $folder) : Localization_Source_Folder
71
    {
72
        if(!in_array($folder, $this->excludes['folders'])) {
73
            $this->excludes['folders'][] = $folder;
74
        }
75
        
76
        return $this;
77
    }
78
    
79
    public function excludeFolders(array $folders) : Localization_Source_Folder
80
    {
81
        foreach($folders as $folder) {
82
            $this->excludeFolder($folder);
83
        }
84
        
85
        return $this;
86
    }
87
    
88
    public function excludeFiles(array $files) : Localization_Source_Folder
89
    {
90
        $this->excludes['files'] = array_merge($this->excludes['files'], $files);
91
        return $this;
92
    }
93
    
94
    protected function _scan(Localization_Source_Scanner $scanner) : void
95
    {
96
        $this->processFolder($this->getSourcesFolder(), $scanner);
97
    }
98
99
    /**
100
     * Processes the target folder, and recurses into sub folders.
101
     * @param string $folder
102
     * @param Localization_Source_Scanner $scanner
103
     * @throws Localization_Exception
104
     */
105
    protected function processFolder(string $folder, Localization_Source_Scanner $scanner) : void
106
    {
107
        $parser = $scanner->getParser();
108
        $d = new DirectoryIterator($folder);
109
        foreach ($d as $item) 
110
        {
111
            if ($item->isDot()) {
112
                continue;
113
            }
114
            
115
            $filename = $item->getFilename();
116
            
117
            if ($item->isDir() && !in_array($filename, $this->excludes['folders'])) {
118
                $this->processFolder($item->getPathname(), $scanner);
119
                continue;
120
            }
121
            
122
            if ($item->isFile() && $parser->isFileSupported($filename) && !$this->isExcluded($filename)) {
123
                $scanner->parseFile($item->getPathname());
124
            }
125
        }
126
    }
127
    
128
    protected function isExcluded(string $filename) : bool
129
    {
130
        foreach ($this->excludes['files'] as $search) {
131
            if (stristr($filename, $search)) {
132
                return true;
133
            }
134
        }
135
        
136
        return false;
137
    }
138
}
139