@@ -105,74 +105,61 @@ public function __construct(FileManagerInterface $fileManager)
105
105
}
106
106
107
107
/**
108
- * Create static assets.
109
- *
110
- * @param string[] $paths Collection of paths for gathering assets
111
- *
112
- * @return string[] Cached assets full paths collection
108
+ * Recursively process asset
109
+ * @param array $dependencies Collection of assets for compilation
113
110
*/
114
- public function manage ( array $ paths )
111
+ protected function processAsset ( $ dependencies )
115
112
{
116
- $ assets = $ this ->fileManager ->scan ($ paths , self ::TYPES , self ::$ excludeFolders );
113
+ foreach ($ dependencies as $ source => $ nothing ) {
114
+ // Read asset content
115
+ $ content = $ this ->fileManager ->read ($ source );
117
116
118
- // Iterate all assets for analyzing
119
- $ cache = [];
120
- foreach ($ assets as $ asset ) {
121
- $ cache [$ asset ] = $ this ->analyzeAsset ($ asset );
122
- }
123
- $ cache = array_filter ($ cache );
117
+ $ extension = pathinfo ($ source , PATHINFO_EXTENSION );
124
118
125
- // Iterate invalid assets
126
- foreach ($ cache as $ file => $ content ) {
127
- $ extension = pathinfo ($ file , PATHINFO_EXTENSION );
119
+ // Resource dependant resources
120
+ $ innerDependencies = [];
128
121
129
122
// Compile content
130
123
$ compiled = $ content ;
131
- Event::fire (self ::E_COMPILE , [$ file , &$ extension , &$ compiled ]);
124
+ Event::fire (self ::E_COMPILE , [$ source , &$ extension , &$ compiled, & $ innerDependencies ]);
132
125
133
- $ asset = $ this ->getAssetCachedPath ($ file );
134
- $ this ->fileManager ->write ($ asset , $ compiled );
135
- $ this ->fileManager ->touch ($ asset , $ this ->fileManager ->lastModified ($ file ));
126
+ // Write compiled asset
127
+ $ target = $ this ->getAssetProcessedPath ($ source );
128
+ $ this ->fileManager ->write ($ target , $ compiled );
129
+ $ this ->fileManager ->touch ($ target , $ this ->fileManager ->lastModified ($ source ));
136
130
137
- $ this ->assets [$ extension ][] = $ asset ;
131
+ // Go deeper in recursion
132
+ $ this ->processAsset ($ innerDependencies );
138
133
}
139
-
140
- return $ this ->assets ;
141
134
}
142
135
143
136
/**
144
- * Analyze asset .
137
+ * Create static assets .
145
138
*
146
- * @param string $asset Full path to asset
139
+ * @param string[] $paths Collection of paths for gathering assets
147
140
*
148
- * @return string Analyzed asset content
141
+ * @return string[] Cached assets full paths collection
149
142
*/
150
- protected function analyzeAsset ( $ asset )
143
+ public function manage ( array $ paths )
151
144
{
152
- // Generate cached resource path with possible new extension after compiling
153
- $ cachedAsset = $ this ->getAssetCachedPath ($ asset );
154
-
155
- $ extension = pathinfo ($ asset , PATHINFO_EXTENSION );
145
+ // Get assets list
146
+ $ assets = $ this ->fileManager ->scan ($ paths , self ::TYPES , self ::$ excludeFolders );
156
147
157
- // If cached assets was modified or new
158
- if (!$ this ->isValid ($ asset , $ cachedAsset )) {
159
- // Read asset content
160
- $ content = $ this ->fileManager ->read ($ asset );
161
-
162
- // Fire event for analyzing resource
163
- Event::fire (self ::E_ANALYZE , [
164
- $ asset ,
165
- $ extension ,
166
- &$ content
167
- ]);
168
-
169
- return $ content ;
170
- } else {
171
- // Add this resource to resource collection grouped by resource type
172
- $ this ->assets [$ this ->convertType ($ asset )][] = $ cachedAsset ;
148
+ // Iterate all assets for analyzing
149
+ foreach ($ assets as $ asset ) {
150
+ // Build path to processed asset
151
+ $ cachedAsset = $ this ->getAssetProcessedPath ($ asset );
152
+
153
+ // If cached assets was modified or new
154
+ if (!$ this ->isValid ($ asset , $ cachedAsset )) {
155
+ // Recursively process asset and possible dependencies
156
+ $ this ->processAsset ([$ asset => []]);
157
+ // Store processed asset
158
+ $ this ->assets [pathinfo ($ cachedAsset , PATHINFO_EXTENSION )][] = $ cachedAsset ;
159
+ }
173
160
}
174
161
175
- return '' ;
162
+ return $ this -> assets ;
176
163
}
177
164
178
165
/**
@@ -182,7 +169,7 @@ protected function analyzeAsset($asset)
182
169
*
183
170
* @return string Full path to cached asset
184
171
*/
185
- protected function getAssetCachedPath ($ asset )
172
+ protected function getAssetProcessedPath ($ asset )
186
173
{
187
174
// Build asset project root relative path
188
175
$ relativePath = str_replace (self ::$ projectRoot , '' , $ asset );
0 commit comments