@@ -47,12 +47,12 @@ function diff($key, $data_from, $data_to) {
47
47
$ pkgs = array ();
48
48
49
49
foreach ($ data_from ->$ key as $ pkg ) {
50
- $ pkgs [$ pkg ->name ] = array (version ($ pkg ), 'REMOVED ' , '' );
50
+ $ pkgs [$ pkg ->name ] = array (version ($ pkg ), 'REMOVED ' , '' , array ( ' direct ' => property_exists ( $ pkg , ' direct ' )) );
51
51
}
52
52
53
53
foreach ($ data_to ->$ key as $ pkg ) {
54
54
if (! array_key_exists ($ pkg ->name , $ pkgs )) {
55
- $ pkgs [$ pkg ->name ] = array ('NEW ' , version ($ pkg ), '' );
55
+ $ pkgs [$ pkg ->name ] = array ('NEW ' , version ($ pkg ), '' , array ( ' direct ' => property_exists ( $ pkg , ' direct ' )) );
56
56
continue ;
57
57
}
58
58
@@ -61,6 +61,9 @@ function diff($key, $data_from, $data_to) {
61
61
} else {
62
62
$ pkgs [$ pkg ->name ][1 ] = version ($ pkg );
63
63
$ pkgs [$ pkg ->name ][2 ] = makeCompareUrl ($ pkg , $ pkgs );
64
+ if ($ pkgs [$ pkg ->name ][3 ]['direct ' ] === false ) { // Don't overwrite direct if it was already set to true
65
+ $ pkgs [$ pkg ->name ][3 ]['direct ' ] = property_exists ($ pkg , 'direct ' );
66
+ }
64
67
}
65
68
}
66
69
@@ -101,7 +104,7 @@ function tableize($header, $data, $opts = array()) {
101
104
102
105
$ widths = array (maxLength (array_merge (array ($ header ), array_keys ($ data ))));
103
106
104
- $ count = count ( reset ( $ data ));
107
+ $ count = 3 ; // it will always be 3. The fourth item is a properties array
105
108
for ($ i = 0 ; $ i < $ count ; $ i ++) {
106
109
$ widths [] = max (strlen ($ titles [$ i + 1 ]), maxLength (array_map (function ($ k ) use ($ data , $ i ) { return $ data [$ k ][$ i ]; }, array_keys ($ data ))));
107
110
}
@@ -113,8 +116,18 @@ function tableize($header, $data, $opts = array()) {
113
116
$ lines [] = tabelizeLine ($ titles , $ widths );
114
117
$ lines [] = separatorLine ($ widths , $ opts ['joint ' ]);
115
118
119
+ $ lines [] = fillLine (array ("Direct " ), '~ ' , $ widths );
120
+
121
+ foreach ($ data as $ key => $ v ) {
122
+ if (! $ v [3 ]['direct ' ]) continue ;
123
+ $ lines [] = tabelizeLine (array_merge (array ($ key ), array_slice ($ v , 0 , $ count )), $ widths );
124
+ }
125
+
126
+ $ lines [] = fillLine (array ("Indirect " ), '~ ' , $ widths );
127
+
116
128
foreach ($ data as $ key => $ v ) {
117
- $ lines [] = tabelizeLine (array_merge (array ($ key ), $ v ), $ widths );
129
+ if ($ v [3 ]['direct ' ]) continue ;
130
+ $ lines [] = tabelizeLine (array_merge (array ($ key ), array_slice ($ v , 0 , $ count )), $ widths );
118
131
}
119
132
120
133
if ($ opts ['capped ' ]) {
@@ -132,6 +145,19 @@ function maxLength(array $array) {
132
145
return max (array_map ('strlen ' , $ array ));
133
146
}
134
147
148
+ function fillLine ($ data , $ fill_char , $ widths ) {
149
+ $ count = count ($ data );
150
+ for ($ i = 0 ; $ i < count ($ widths ); $ i ++) {
151
+ if ($ i < $ count ) {
152
+ $ data [$ i ] = $ fill_char . " " . $ data [$ i ] . " " . str_repeat ($ fill_char , $ widths [$ i ] - (strlen ($ data [$ i ]) + 3 ));
153
+ } else {
154
+ $ data [$ i ] = str_repeat ($ fill_char , $ widths [$ i ]);
155
+ }
156
+ }
157
+
158
+ return tabelizeLine ($ data , $ widths );
159
+ }
160
+
135
161
function tabelizeLine ($ data , $ widths ) {
136
162
$ fields = array ();
137
163
$ count = max (array (count ($ data ), count ($ widths )));
@@ -209,11 +235,20 @@ function loadFile($fileish, $base_path, $default_fileish) {
209
235
}
210
236
211
237
// Is it a file in the local filesystem?
212
- if (file_exists ($ fileish )) {
213
- return array (mustDecodeJson (file_get_contents ($ fileish ), $ fileish ), false );
238
+ if (! file_exists ($ fileish )) {
239
+ return array (false , "Candidate ' $ fileish' does not look loadable from the fs or php stream wrappers " );
240
+ }
241
+
242
+ $ data = mustDecodeJson (file_get_contents ($ fileish ), $ fileish );
243
+
244
+ // Try to load composer.json and mark deps.
245
+ if (substr ($ fileish , -4 ) == "lock " ) {
246
+ $ composer_json_fileish = substr ($ fileish , 0 , -4 ) . 'json ' ;
247
+ $ composer_json = mustDecodeJson (file_get_contents ($ composer_json_fileish ), $ composer_json_fileish );
248
+ markDirectDependencies ($ data , $ composer_json );
214
249
}
215
250
216
- return array (false , " Candidate ' $ fileish ' does not look loadable from the fs or php stream wrappers " );
251
+ return array ($ data , false );
217
252
}
218
253
219
254
function isUrl ($ string ) {
@@ -231,6 +266,26 @@ function mustDecodeJson($json, $context) {
231
266
return $ data ;
232
267
}
233
268
269
+ function markDirectDependencies ($ lock , $ json ) {
270
+ foreach (array ('' , '-dev ' ) as $ ext ) {
271
+ foreach ($ json ->{'require ' .$ ext } as $ pkg => $ _ ) {
272
+ $ packages = 'packages ' .$ ext ;
273
+
274
+ $ index = false ;
275
+ for ($ i = 0 ; $ i < count ($ lock ->{$ packages }); $ i ++) {
276
+ if ($ lock ->{$ packages }[$ i ]->name == $ pkg ) {
277
+ $ index = $ i ;
278
+ break ;
279
+ }
280
+ }
281
+
282
+ if ($ index !== false ) {
283
+ $ lock ->{$ packages }[$ i ]->direct = true ;
284
+ }
285
+ }
286
+ }
287
+ }
288
+
234
289
function makeCompareUrl ($ pkg , $ diff ) {
235
290
$ func = 'formatCompare ' . ucfirst (getSourceRepoType ((string ) @$ pkg ->source ->url ));
236
291
return call_user_func ($ func , @$ pkg ->source ->url , $ diff [$ pkg ->name ][0 ], $ diff [$ pkg ->name ][1 ]);
0 commit comments