|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | const TEMPLATE = <<< 'EOT' |
4 | | -<h2>Components</h2> |
| 4 | +<h2>%group%</h2> |
| 5 | +
|
5 | 6 | <div class="components"> |
6 | 7 | %components% |
7 | 8 | </div> |
|
34 | 35 | $json = file_get_contents($listFile); |
35 | 36 | $packages = json_decode($json, true); |
36 | 37 |
|
37 | | -$componentMarkup = []; |
38 | | -foreach ($packages as $package) { |
39 | | - // @codingStandardsIgnoreStart |
40 | | - $componentMarkup []= str_replace( |
41 | | - [ '%href%', '%name%', '%package%', '%description%'], |
42 | | - [$package['url'], $package['name'], $package['package'], $package['description']], |
43 | | - PACKAGE_TEMPLATE |
44 | | - ); |
45 | | -} |
46 | | -$componentMarkup = implode("\n\n", $componentMarkup); |
| 38 | +// Group packages by type |
| 39 | +$packagesByType = [ |
| 40 | + 'learn' => [ |
| 41 | + 'title' => 'Learn ZF', |
| 42 | + 'packages' => [], |
| 43 | + ], |
| 44 | + 'mvc' => [ |
| 45 | + 'title' => 'MVC Framework', |
| 46 | + 'packages' => [], |
| 47 | + ], |
| 48 | + 'middleware' => [ |
| 49 | + 'title' => 'Expressive and PSR-15 Middleware', |
| 50 | + 'packages' => [], |
| 51 | + ], |
| 52 | + 'projects' => [ |
| 53 | + 'title' => 'Tooling and Composer Plugins', |
| 54 | + 'packages' => [], |
| 55 | + ], |
| 56 | + 'components' => [ |
| 57 | + 'title' => 'Components', |
| 58 | + 'packages' => [], |
| 59 | + ], |
| 60 | +]; |
| 61 | + |
| 62 | +$types = array_keys($packagesByType); |
| 63 | + |
| 64 | +// Sort packages into various groups, generating markup for each as we do. |
| 65 | +$packagesByType = array_reduce( |
| 66 | + $packages, |
| 67 | + function ($grouped, $package) use ($types) { |
| 68 | + if (! isset($package['group']) || ! in_array($package['group'], $types, true)) { |
| 69 | + $package['group'] = 'components'; |
| 70 | + } |
| 71 | + |
| 72 | + $grouped[$package['group']]['packages'][] = str_replace( |
| 73 | + [ '%href%', '%name%', '%package%', '%description%'], |
| 74 | + [$package['url'], $package['name'], $package['package'], $package['description']], |
| 75 | + PACKAGE_TEMPLATE |
| 76 | + ); |
| 77 | + |
| 78 | + return $grouped; |
| 79 | + }, |
| 80 | + $packagesByType |
| 81 | +); |
| 82 | + |
| 83 | +// Generate per-group markup |
| 84 | +$markup = array_reduce( |
| 85 | + $packagesByType, |
| 86 | + function ($markup, $group) { |
| 87 | + $markup []= str_replace( |
| 88 | + [ '%group%', '%components%'], |
| 89 | + [$group['title'], implode("\n\n", $group['packages'])], |
| 90 | + TEMPLATE |
| 91 | + ); |
| 92 | + return $markup; |
| 93 | + }, |
| 94 | + [] |
| 95 | +); |
47 | 96 |
|
48 | | -$markup = str_replace('%components%', $componentMarkup, TEMPLATE); |
| 97 | +$markup = implode("\n\n", $markup); |
49 | 98 |
|
50 | 99 | copy($distPath, $indexPath); |
51 | 100 | $fh = fopen($indexPath, 'a'); |
|
0 commit comments