Skip to content

Commit

Permalink
CMS 5 composer.json check API
Browse files Browse the repository at this point in the history
  • Loading branch information
ssmarco committed Jan 29, 2024
1 parent 66295b9 commit b558de1
Show file tree
Hide file tree
Showing 6 changed files with 6,865 additions and 0 deletions.
1 change: 1 addition & 0 deletions app/_config/routes.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ SilverStripe\Control\Director:
rules:
# Homepage
'': 'App\Control\AppController'
'cms5': 'App\Control\CMS5Controller'
# Catch all rule for all links
'/$*': 'App\Control\AppController'

Expand Down
116 changes: 116 additions & 0 deletions app/src/Control/CMS5Controller.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
<?php

namespace App\Control;

use Exception;
use League\Csv\Reader;
use SilverStripe\Control\Director;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTPResponse_Exception;
use SilverStripe\Core\Path;
use SilverStripe\ORM\ArrayList;
use SilverStripe\ORM\FieldType\DBHTMLText;
use SilverStripe\View\ArrayData;

class CMS5Controller extends AppController
{

private static string $sourceFolder = 'csv';

/**
* @throws HTTPResponse_Exception
*/
public function index(HTTPRequest $request): HTTPResponse
{
echo '<pre>';

try {
$modules = $this->getModules();
$body = json_decode($request->getBody(), true);
$packages = new ArrayList();

foreach ($body['require'] ?? [] as $module => $version) {
if (!strpos($module, '/')) {
continue;
}

$cms5 = $modules->find('Module', $module);
$cms5Version = $cms5['Version'] ?? '';
$cms5Notes = $cms5Version ? 'Passed ' . $cms5['Notes'] ?? '' : 'Failed';

$packages->push([
'Module' => $module,
'Version' => $version,
'CMS5' => $cms5Version,
'Notes' => $cms5Notes,
]);
}

$total = $packages->count();
$failed = $packages->filter(['Notes' => 'Failed'])->count();

$percentage = round((($total - $failed) / $total) * 100, 2);
$tBody = '';

foreach ($packages as $package) {
$tBody .= sprintf(
'<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
$package->Module,
$package->Version,
$package->CMS5,
$package->Notes,
);
}

$table = <<<TABLE
<table>
<thead>
<tr>
<th>Module</th>
<th>Version</th>
<th>CMS 5</th>
<th>Notes</th>
</tr>
</thead>
<tbody>
{$tBody}
</tbody>
<tfoot>
<tr>
<th colspan="4">Failed: {$failed}/{$total} modules Score: {$percentage}%</th>
</tr>
</tfoot>
</table>
TABLE;

return $this->getResponse()
->setBody(
$this->getViewer('index')->process(
$this->customise(new ArrayData(['Content' => DBHTMLText::create()->setValue($table)]))
)
);
} catch (Exception $exception) {
return $this->httpError(404, 'Not found. ' . $exception->getMessage());
}
}

/**
* @return ArrayList
* @throws \League\Csv\Exception
* @throws \League\Csv\UnavailableStream
*/
protected function getModules(): ArrayList
{
$source = Path::join(Director::baseFolder(), $this->config()->get('sourceFolder'), 'cms5_modules - Sheet1.csv');
$reader = Reader::createFromPath($source, 'r');
$reader->setHeaderOffset(0);
$modules = new ArrayList();

foreach ($reader->getIterator() as $row) {
$modules->push($row);
}

return $modules;
}
}
Loading

0 comments on commit b558de1

Please sign in to comment.