|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\SourceOptimizer |
|
5
|
|
|
* @copyright Copyright (c) 2014-2017 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\SourceOptimizer; |
|
9
|
|
|
|
|
10
|
|
|
class Optimizer |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var array<\s9e\SourceOptimizer\Pass> |
|
14
|
|
|
*/ |
|
15
|
|
|
protected $passes = []; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Constructor |
|
19
|
|
|
*/ |
|
20
|
2 |
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
2 |
|
$this->enable('ConcatenateConstantStrings'); |
|
23
|
2 |
|
$this->enable('EnforceFQN'); |
|
24
|
2 |
|
$this->enable('RemoveBraces'); |
|
25
|
2 |
|
$this->enable('RemoveComments'); |
|
26
|
2 |
|
$this->enable('RemoveWhitespace'); |
|
27
|
2 |
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Disable an optimization pass |
|
31
|
|
|
* |
|
32
|
|
|
* @param string $passName Name of the optimization pass |
|
33
|
|
|
* @return void |
|
34
|
|
|
*/ |
|
35
|
|
|
public function disable($passName) |
|
36
|
|
|
{ |
|
37
|
|
|
unset($this->passes[$passName]); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Disable all optimization passes |
|
42
|
|
|
* |
|
43
|
|
|
* @return void |
|
44
|
|
|
*/ |
|
45
|
|
|
public function disableAll() |
|
46
|
|
|
{ |
|
47
|
|
|
$this->passes = []; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Enable an optimization pass |
|
52
|
|
|
* |
|
53
|
|
|
* @param string $passName Name of the optimization pass |
|
54
|
|
|
* @param array $options Options to be set on the pass instance |
|
55
|
|
|
* @return void |
|
56
|
|
|
*/ |
|
57
|
2 |
|
public function enable($passName, array $options = []) |
|
58
|
|
|
{ |
|
59
|
2 |
|
$className = __NAMESPACE__ . '\\Passes\\' . $passName; |
|
60
|
2 |
|
if (!class_exists($className)) |
|
61
|
|
|
{ |
|
62
|
|
|
trigger_error("Pass '" . $passName . "' does not exist"); |
|
63
|
|
|
|
|
64
|
|
|
return; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
2 |
|
$this->passes[$passName] = new $className; |
|
68
|
2 |
|
foreach ($options as $optionName => $optionValue) |
|
69
|
|
|
{ |
|
70
|
|
|
$this->passes[$passName]->$optionName = $optionValue; |
|
71
|
|
|
} |
|
72
|
2 |
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Optimize given source |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $php Original source code |
|
78
|
|
|
* @return string Optimize source code |
|
79
|
|
|
*/ |
|
80
|
2 |
|
public function optimize($php) |
|
81
|
|
|
{ |
|
82
|
2 |
|
$stream = new TokenStream($php); |
|
83
|
2 |
|
foreach ($this->passes as $pass) |
|
84
|
|
|
{ |
|
85
|
2 |
|
$stream->reset(); |
|
86
|
2 |
|
$pass->optimize($stream); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
2 |
|
return $stream->serialize(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Optimize all .php files in given directory and its subdirectories |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $path Path to the dir |
|
96
|
|
|
* @return void |
|
97
|
|
|
*/ |
|
98
|
|
|
public function optimizeDir($path) |
|
99
|
|
|
{ |
|
100
|
|
|
array_map([$this, 'optimizeFile'], glob($path . '/*.php')); |
|
101
|
|
|
array_map([$this, 'optimizeDir'], glob($path . '/*', GLOB_ONLYDIR)); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Optimize a PHP file |
|
106
|
|
|
* |
|
107
|
|
|
* @param string $filepath Path to the file |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function optimizeFile($filepath) |
|
111
|
|
|
{ |
|
112
|
|
|
$old = file_get_contents($filepath); |
|
113
|
|
|
$new = $this->optimize($old); |
|
114
|
|
|
if ($new !== $old) |
|
115
|
|
|
{ |
|
116
|
|
|
file_put_contents($filepath, $new); |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
} |