|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: egorov |
|
5
|
|
|
* Date: 13.08.2015 |
|
6
|
|
|
* Time: 10:43 |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsonphp\compressor\resource; |
|
9
|
|
|
|
|
10
|
|
|
use \samsonphp\compressor\CompressibleInterface; |
|
11
|
|
|
use \samsonphp\compressor\Compressor; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Module compression resource management |
|
15
|
|
|
* @package samsonphp\compressor |
|
16
|
|
|
*/ |
|
17
|
|
|
class Generic implements CompressibleInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** @var Compressor Parent compressing object */ |
|
20
|
|
|
protected $parent; |
|
21
|
|
|
|
|
22
|
|
|
/** @var string[] Collection of ignored file extensions */ |
|
23
|
|
|
protected $ignoredExtensions = array('php', 'txt', 'js', 'css', 'md', 'map', 'dbs', 'vphp', 'less' , 'gz', 'lock', 'json', 'sql', 'xml', 'yml'); |
|
24
|
|
|
|
|
25
|
|
|
/** @var string[] Collection of ignored files */ |
|
26
|
|
|
protected $ignoredFiles = array('.project', '.buildpath', '.gitignore', '.travis.yml', 'phpunit.xml', 'thumbs.db'); |
|
27
|
|
|
|
|
28
|
|
|
/** @var string[] Collection of ignored folders */ |
|
29
|
|
|
protected $ignoredFolders = array('vendor', 'var', 'docs'); |
|
30
|
|
|
|
|
31
|
|
|
/** @var string[] Collection of module paths for creating beautiful structure */ |
|
32
|
|
|
protected $moduleFolders = array(); |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param Compressor $compressor Pointer to parent compressing object |
|
36
|
|
|
* @param string[] $ignoredExtensions Collection of ignored file extensions |
|
37
|
|
|
* @param string[] $ignoredFiles Collection of ignored files |
|
38
|
|
|
* @param string[] $ignoredFolders Collection of ignored folders |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(Compressor & $compressor, $ignoredExtensions = array(), $ignoredFiles = array(), $ignoredFolders = array()) |
|
41
|
|
|
{ |
|
42
|
|
|
$this->parent = & $compressor; |
|
43
|
|
|
|
|
44
|
|
|
// TODO: Maybe we need to fully override defaults, or merge? |
|
45
|
|
|
|
|
46
|
|
|
// Merge passed parameters |
|
47
|
|
|
$this->ignoredExtensions = array_merge($this->ignoredExtensions, $ignoredExtensions); |
|
48
|
|
|
$this->ignoredFiles = array_merge($this->ignoredFiles, $ignoredFiles); |
|
49
|
|
|
$this->ignoredFolders = array_merge($this->ignoredFolders, $ignoredFolders); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Create folder, method use recursive approach for creating if |
|
54
|
|
|
* "folder/folder/.." is passed. |
|
55
|
|
|
* @param string $path Folder path |
|
56
|
|
|
* @param string $group Folder group(www-data) |
|
57
|
|
|
* @param int $mode Folder mode(0775) |
|
58
|
|
|
* @return int 1 - success, 0 - folder exists, -1 - errors |
|
59
|
|
|
*/ |
|
60
|
|
|
public function createFolderStructure($path, $group = 'www-data', $mode = 0775) |
|
61
|
|
|
{ |
|
62
|
|
|
// If folder does not exists |
|
63
|
|
|
if (!file_exists($path)) { |
|
64
|
|
|
// Create folder with correct mode |
|
65
|
|
|
if (mkdir($path, $mode, true)) { |
|
66
|
|
|
// Change folder group |
|
67
|
|
|
@chgrp($path, $group); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
return true; |
|
70
|
|
|
} else { |
|
71
|
|
|
return -1; |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// Folder already exists |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Update file resource |
|
81
|
|
|
* @param string $fromFilePath Source file path |
|
82
|
|
|
* @param string $toFilePath Destination file path |
|
83
|
|
|
*/ |
|
84
|
|
|
protected function update($fromFilePath, $toFilePath) |
|
85
|
|
|
{ |
|
86
|
|
|
copy($fromFilePath, $toFilePath); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Compress file resource |
|
91
|
|
|
* @param string $fromFilePath Source file path |
|
92
|
|
|
* @param string $toFilePath Destination file path |
|
93
|
|
|
* @return mixed Compression result |
|
94
|
|
|
*/ |
|
95
|
|
|
public function compress($fromFilePath, $toFilePath) |
|
96
|
|
|
{ |
|
97
|
|
|
$this->parent->log(' - Compressing from file[##] to [##]', $fromFilePath, $toFilePath); |
|
98
|
|
|
|
|
99
|
|
|
// Compress only valid resources |
|
100
|
|
|
if ($this->isValid($fromFilePath)) { |
|
101
|
|
|
// Create folder structure in destination location if necessary |
|
102
|
|
|
$destinationFolderPath = dirname($toFilePath); |
|
103
|
|
|
if ($this->createFolderStructure($destinationFolderPath)) { |
|
104
|
|
|
$this->parent->log(' +- Created folder structure for [##] in [##]', dirname($fromFilePath), $destinationFolderPath); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
// If destination file does not exists or source file has been modified |
|
108
|
|
|
if (!file_exists($toFilePath) || (filemtime($fromFilePath) <> filemtime($toFilePath))) { |
|
109
|
|
|
$this->parent->log(' +- Updated from file[##] to [##]', $fromFilePath, $toFilePath); |
|
110
|
|
|
$this->update($fromFilePath, $toFilePath); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
// Sync destination file with source file |
|
114
|
|
|
if (is_writable($toFilePath)) { |
|
115
|
|
|
// Change destination file permission |
|
116
|
|
|
chmod($toFilePath, 0775); |
|
117
|
|
|
// Modify destination modification to match source |
|
118
|
|
|
touch($toFilePath, filemtime($fromFilePath)); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Define if this file is a valid resource |
|
125
|
|
|
* @param string $filePath File path |
|
126
|
|
|
* @return bool True if file is valid |
|
127
|
|
|
*/ |
|
128
|
|
|
protected function isValid($filePath) |
|
129
|
|
|
{ |
|
130
|
|
|
$this->parent->log(' - Validating file[##]', $filePath); |
|
131
|
|
|
|
|
132
|
|
|
// Does this file exists |
|
133
|
|
|
if (file_exists($filePath)) { |
|
134
|
|
|
// Check if this extension is valid |
|
135
|
|
|
$extension = pathinfo($filePath, PATHINFO_EXTENSION); |
|
136
|
|
|
if (!in_array($extension, $this->ignoredExtensions)) { |
|
137
|
|
|
// Check if this file is not ignored |
|
138
|
|
|
$fileName = pathinfo($filePath, PATHINFO_FILENAME); |
|
139
|
|
|
if (!in_array($fileName, $this->ignoredFiles)) { |
|
140
|
|
|
$this->parent->log(' +- File[##] is valid', $filePath); |
|
141
|
|
|
// File is valid |
|
142
|
|
|
return true; |
|
143
|
|
|
} else { |
|
144
|
|
|
$this->parent->log(' +- File[##] is ignored', $filePath); |
|
145
|
|
|
} |
|
146
|
|
|
} else { |
|
147
|
|
|
$this->parent->log(' +- File[##] extension[##] is ignored', $filePath, $extension); |
|
148
|
|
|
} |
|
149
|
|
|
} else { |
|
150
|
|
|
$this->parent->log(' +- File[##] does not exists', $filePath); |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
// Not valid |
|
154
|
|
|
return false; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
If you suppress an error, we recommend checking for the error condition explicitly: