1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: egorov |
5
|
|
|
* Date: 11.01.2015 |
6
|
|
|
* Time: 9:18 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace samsonos\compressor; |
10
|
|
|
|
11
|
|
|
use samsonphp\compressor\Code; |
12
|
|
|
use samsonphp\compressor\Compressor; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class for automatic SamsonPHP module optimization|compression |
16
|
|
|
* |
17
|
|
|
* @package samsonos\compressor |
18
|
|
|
* @author Vitaly Iegorov <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class Module |
21
|
|
|
{ |
22
|
|
|
/** @var \samson\core\Core Core pointer */ |
23
|
|
|
protected $core; |
24
|
|
|
|
25
|
|
|
/** @var \samson\core\CompressableExternalModule Module pointer */ |
26
|
|
|
protected $module; |
27
|
|
|
|
28
|
|
|
/** @var Object Logger object */ |
29
|
|
|
protected $logger; |
30
|
|
|
|
31
|
|
|
/** @var array Collection of module PHP code */ |
32
|
|
|
protected $code = array(); |
33
|
|
|
|
34
|
|
|
/** @var array Ignored resource extensions */ |
35
|
|
|
public $ignoredExtensions = array( |
36
|
|
|
'php', 'js', 'css', 'md', 'map', 'dbs', 'vphp', 'less' , 'gz', 'lock', 'json', 'sql', 'xml', 'yml' |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param \samson\core\Core $core Core pointer |
41
|
|
|
* @param \samson\core\CompressableExternalModule $module Module pointer |
42
|
|
|
* @param object $logger Logger object |
43
|
|
|
*/ |
44
|
|
|
public function __construct($core, $module, $logger = null) |
45
|
|
|
{ |
46
|
|
|
$this->core = & $core; |
47
|
|
|
$this->module = & $module; |
48
|
|
|
$this->logger = & $logger; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Perform module compression |
53
|
|
|
* @return bool True if module was successfully compressed |
54
|
|
|
*/ |
55
|
|
|
public function compress() |
56
|
|
|
{ |
57
|
|
|
// Cache module ID |
58
|
|
|
$id = $this->module->id(); |
59
|
|
|
|
60
|
|
|
$this->logger->log(' - Compressing module[##]', $id); |
61
|
|
|
|
62
|
|
|
// Call special method enabling module personal resource pre-management on compressing |
63
|
|
|
if ($this->module->beforeCompress($this, $this->code) !== false) { |
64
|
|
|
//$this->compressResources(); |
65
|
|
|
//$this->compressView(); |
66
|
|
|
//$this->compressTemplate(); |
67
|
|
|
|
68
|
|
|
if (is_a($this->module->resourceMap, 'samson\core\ResourceMap')) { |
69
|
|
|
$sources = array_merge( |
70
|
|
|
$this->module->resourceMap->php, |
|
|
|
|
71
|
|
|
$this->module->resourceMap->controllers, |
|
|
|
|
72
|
|
|
$this->module->resourceMap->models, |
|
|
|
|
73
|
|
|
$this->module->resourceMap->globals |
|
|
|
|
74
|
|
|
); |
75
|
|
|
|
76
|
|
|
// Add module class files to array of sources |
77
|
|
|
foreach ($this->module->resourceMap->modules as $module) { |
|
|
|
|
78
|
|
|
$sources = array_merge(array($module[1]), $sources); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
// Create code collector |
82
|
|
|
$code = new Code($sources, $this->logger); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
// Change module path, now all modules would be located at wwwroot folder |
86
|
|
|
//$this->module->path($id.'/'); |
87
|
|
|
|
88
|
|
|
// Call special method enabling module personal resource post-management on compressing |
89
|
|
|
//$this->module->afterCompress($this, $this->code); |
90
|
|
|
|
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
$this->logger->log(' - Module[##] compression stopped', $id); |
95
|
|
|
|
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function compressResources() |
100
|
|
|
{ |
101
|
|
|
// Build output module path |
102
|
|
|
$destination = $id == 'local' ? '' : $id.'/'; |
|
|
|
|
103
|
|
|
|
104
|
|
|
// Build resource source path |
105
|
|
|
$source = $id == 'local' ? $this->module->path().__SAMSON_PUBLIC_PATH : $this->module->path(); |
106
|
|
|
|
107
|
|
|
$this->log(' -> Copying resources from [##] to [##]', $module_path, $module_output_path); |
|
|
|
|
108
|
|
|
|
109
|
|
|
// Iterate module resources |
110
|
|
|
foreach ($this->module->resourceMap->resources as $extension => $resources) { |
|
|
|
|
111
|
|
|
// Iterate only allowed resource types |
112
|
|
|
if (!in_array( $extension , $this->ignored_extensions)) { |
|
|
|
|
113
|
|
|
foreach ( $resources as $resource ) { |
|
|
|
|
114
|
|
|
// Get only filename |
115
|
|
|
$filename = basename( $resource ); |
116
|
|
|
|
117
|
|
|
// Copy only allowed resources |
118
|
|
|
if (!in_array( $filename, $this->ignored_resources)) { |
|
|
|
|
119
|
|
|
// Build relative module resource path |
120
|
|
|
$relative_path = str_replace($module_path, '', $resource); |
121
|
|
|
|
122
|
|
|
// Build correct destination folder |
123
|
|
|
$dst = $this->output.$module_output_path.$relative_path; |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Copy/update file if necessary |
126
|
|
|
$this->copy_resource( $resource, $dst ); |
|
|
|
|
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Copy all module resources |
133
|
|
|
$this->copy_path_resources($this->module->resourceMap->resources, $source, $destination); |
|
|
|
|
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
public function compressView() |
137
|
|
|
{ |
138
|
|
|
// Iterate all views |
139
|
|
|
foreach ($this->module->resourceMap->views as $view) { |
|
|
|
|
140
|
|
|
|
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Recursively gather PHP code from file and gather |
146
|
|
|
* it into array, grouped by namespace |
147
|
|
|
*/ |
148
|
|
|
public function compressCode($file, array $code = array(), $namespace = Compressor::NS_GLOBAL) |
|
|
|
|
149
|
|
|
{ |
150
|
|
|
|
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
public function compressTemplate() |
154
|
|
|
{ |
155
|
|
|
|
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** @deprecated Use compressCode() instead */ |
159
|
|
|
public function compress_php($file, $module, array $code = array(), $namespace = Compressor::NS_GLOBAL) |
|
|
|
|
160
|
|
|
{ |
161
|
|
|
$this->logger->log(' - Compressing PHP file[##]', $file); |
162
|
|
|
return $this->compressCode($file, $code, $namespace); |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
If you access a property on an interface, you most likely code against a concrete implementation of the interface.
Available Fixes
Adding an additional type check:
Changing the type hint: