-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
6,865 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
Oops, something went wrong.