From 8181e0b31de3399dc27c0a83ec60964e59dc2e20 Mon Sep 17 00:00:00 2001
From: Greg
Date: Mon, 18 Apr 2022 18:10:08 -0400
Subject: [PATCH] Replace Flight templating engine with Twig
Also cleanup the default tables template.
---
README.md | 4 ++-
composer.json | 3 +-
lineageota.json | 2 +-
src/CmOta.php | 32 ++++++++++++++----
views/ota-list-simple.php | 13 -------
views/ota-list-simple.twig | 11 ++++++
views/ota-list-tables.css | 10 +++---
views/ota-list-tables.php | 69 --------------------------------------
views/ota-list-tables.twig | 62 ++++++++++++++++++++++++++++++++++
9 files changed, 110 insertions(+), 96 deletions(-)
delete mode 100644 views/ota-list-simple.php
create mode 100644 views/ota-list-simple.twig
delete mode 100644 views/ota-list-tables.php
create mode 100644 views/ota-list-tables.twig
diff --git a/README.md b/README.md
index 3e64d73..02da59c 100644
--- a/README.md
+++ b/README.md
@@ -196,7 +196,9 @@ There are several configuration settings for temples as follows:
Included Templates:
* ota-list-simple: a simple header and list of files names, no additional details or links provided.
-* ota-list-table: a page containing a seires of tables, one per device, that list in date order all builds for that device. Includes jump lists to find devices, links to local/github pages, dates, versions, md4sums, etc.
+* ota-list-table: a page containing a seires of tables, one per device, that list in date order all builds for that device. Includes jump lists to find devices, links to local/github pages, dates, versions, md5sums, etc.
+
+Twig is used as the templating language, see their [documentation](https://twig.symfony.com/doc/3.x/) for more details.
## REST Server Unit Testing
diff --git a/composer.json b/composer.json
index 164ded9..53de131 100644
--- a/composer.json
+++ b/composer.json
@@ -41,7 +41,8 @@
"require": {
"mikecao/flight": "2.*",
"julianxhokaxhiu/dotnotation": "dev-master",
- "ext-zip": "*"
+ "ext-zip": "*",
+ "twig/twig": "3.*"
},
"autoload": {
"psr-4": {
diff --git a/lineageota.json b/lineageota.json
index 9c66a28..316b611 100644
--- a/lineageota.json
+++ b/lineageota.json
@@ -8,7 +8,7 @@
"EnableGithubCache": true,
"LocalCacheTimeout": 86400,
"GithubCacheTimeout": 86400,
- "OTAListTemplate": "ota-list-table",
+ "OTAListTemplate": "ota-list-tables",
"BrandName": "",
"LocalHomeURL": "",
"GithubHomeURL": "",
diff --git a/src/CmOta.php b/src/CmOta.php
index 5f849b7..66656df 100644
--- a/src/CmOta.php
+++ b/src/CmOta.php
@@ -102,6 +102,14 @@ public function loadConfigJSON( $file ) {
* @return class Return always itself, so it can be chained within calls
*/
public function run() {
+ $loader = new \Twig\Loader\FilesystemLoader( Flight::cfg()->get('realBasePath') . '/views' );
+
+ $twigConfig = array();
+
+ Flight::register( 'twig', '\Twig\Environment', array( $loader, array() ), function ($twig) {
+ // Nothing to do here
+ });
+
Flight::start();
return $this;
@@ -110,19 +118,27 @@ public function run() {
/* Utility / Internal */
// Used to compare timestamps in the build ksort call inside of initRouting for the "/" route
- function compareByTimeStamp( $a, $b ) {
+ private function compareByTimeStamp( $a, $b ) {
return $a['timestamp'] - $b['timestamp'];
}
+ // Format a file size string nicely
+ private function formatFileSize( $bytes, $dec = 2 ) {
+ $size = array( ' B', ' KB', ' MB', ' GB', ' TB', ' PB', ' EB', ' ZB', ' YB' );
+ $factor = floor( ( strlen( $bytes ) - 1 ) / 3 );
+
+ return sprintf( "%.{$dec}f", $bytes / pow( 1024, $factor ) ) . @$size[$factor];
+ }
+
// Setup Flight's routing information
private function initRouting() {
// Just list the builds folder for now
Flight::route('/', function() {
- // Get the template name we're going to use and tack on .php
- $templateName = Flight::cfg()->get('OTAListTemplate') . '.php';
+ // Get the template name we're going to use and tack on .twig
+ $templateName = Flight::cfg()->get('OTAListTemplate') . '.twig';
// Make sure the template exists, otherwise fall back to our default
- if( ! file_exists( 'views/' . $templateName ) ) { $templateName = 'ota-list-tables.php'; }
+ if( ! file_exists( 'views/' . $templateName ) ) { $templateName = 'ota-list-tables.twig'; }
// Time to setup some variables for use later.
$builds = Flight::builds()->get();
@@ -132,6 +148,7 @@ private function initRouting() {
$deviceNames = Flight::cfg()->get('DeviceNames');
$vendorNames = Flight::cfg()->get('DeviceVendors');
$parsedFilenames = array();
+ $formatedFileSizes = array();
$githubURL = '';
if( ! is_array( $deviceNames ) ) { $deviceNames = array(); }
@@ -151,6 +168,8 @@ private function initRouting() {
$githubURL = 'https://github.com/' . $pathParts[1];
}
+ $formatedFileSizes[$build['filename']] = $this->formatFileSize( $build['size'], 0 );
+
// Add the build to a list based on model names
$buildsToSort[$filenameParts['model']][] = $build;
}
@@ -174,14 +193,15 @@ private function initRouting() {
if( $branding['GithubURL'] == '' ) { $branding['GithubURL'] = $githubURL; }
if( $branding['LocalURL'] == '' ) { $branding['LocalURL'] = Flight::cfg()->get( 'basePath' ) . '/builds'; }
- // Render the template
- Flight::render( $templateName,
+ // Render the template with Twig
+ Flight::twig()->display( $templateName,
array( 'builds' => $builds,
'sortedBuilds' => $buildsToSort,
'parsedFilenames' => $parsedFilenames,
'deviceNames' => $deviceNames,
'vendorNames' => $vendorNames,
'branding' => $branding,
+ 'formatedFileSizes' => $formatedFileSizes,
)
);
});
diff --git a/views/ota-list-simple.php b/views/ota-list-simple.php
deleted file mode 100644
index 5ff5f09..0000000
--- a/views/ota-list-simple.php
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-LineageOTA Builds for
-
-
-Currently available builds for
-" . $build['filename'] . '
' . PHP_EOL;
- }
-?>
-
-
\ No newline at end of file
diff --git a/views/ota-list-simple.twig b/views/ota-list-simple.twig
new file mode 100644
index 0000000..d9f6bff
--- /dev/null
+++ b/views/ota-list-simple.twig
@@ -0,0 +1,11 @@
+
+
+LineageOTA Builds for {{ branding['name'] }}
+
+
+Currently available builds for {{ branding['name'] }}
+{% for build in builds %}
+ {{ build['filename'] }}
+{% endfor %}
+
+
\ No newline at end of file
diff --git a/views/ota-list-tables.css b/views/ota-list-tables.css
index 69ed80e..18ed0b4 100644
--- a/views/ota-list-tables.css
+++ b/views/ota-list-tables.css
@@ -32,12 +32,12 @@ tr:nth-child(even) {
}
td {
- padding: 5px;
+ padding: 20px;
border: 0px;
}
table th:nth-child(1) {
- text-align: left;
+ text-align: center;
}
table th:nth-child(2) {
@@ -45,7 +45,7 @@ table th:nth-child(2) {
}
table th:nth-child(3) {
- text-align: center;
+ text-align: left;
}
table th:nth-child(4) {
@@ -56,11 +56,11 @@ table th:nth-child(5) {
text-align: center;
}
-table td:nth-child(2) {
+table td:nth-child(1) {
text-align: center;
}
-table td:nth-child(3) {
+table td:nth-child(2) {
text-align: center;
}
diff --git a/views/ota-list-tables.php b/views/ota-list-tables.php
deleted file mode 100644
index d52db79..0000000
--- a/views/ota-list-tables.php
+++ /dev/null
@@ -1,69 +0,0 @@
-
-
-LineageOTA Builds for
-
-
-
-Currently available builds for
-
- $build ) {
- if( array_key_exists( $model, $deviceNames ) ) {
- echo '- ' . $model . ' (' . $vendorNames[$model] . ' ' . $deviceNames[$model] . ')
' . PHP_EOL;
- } else {
- echo '- ' . $model . '
' . PHP_EOL;
- }
- }
-?>
-
-
-
-
- $builds ) {
- if( array_key_exists( $model, $deviceNames ) ) {
- echo '' . $model . '
' . PHP_EOL;
- echo '(' . $vendorNames[$model] . ' ' . $deviceNames[$model] . ')
' . PHP_EOL;
- } else {
- echo '' . $model . '
' . PHP_EOL;
- }
-?>
-
-
-
- Filename |
- Source |
- Date (Y//M/D) |
- Channel |
- Version |
- MD5 Checksum |
-
-
-
-
-local';
- if( strstr( $build['url'], 'github.com' ) ) { $source = 'Github'; }
-
- echo "\t\t" . PHP_EOL;
- echo "\t\t\t" . $build['filename'] . ' | ' . PHP_EOL;
- echo "\t\t\t" . $source . ' | ' . PHP_EOL;
- echo "\t\t\t" . date( 'Y/m/d', $build['timestamp'] ) . ' | ' . PHP_EOL;
- echo "\t\t\t" . $build['channel'] . ' | ' . PHP_EOL;
- echo "\t\t\t" . $build['version'] . ' | ' . PHP_EOL;
- echo "\t\t\t" . $build['md5sum'] . ' | ' . PHP_EOL;
- echo "\t\t
" . PHP_EOL;
- }
-?>
-
-
-
-
-
-
-
-
-
\ No newline at end of file
diff --git a/views/ota-list-tables.twig b/views/ota-list-tables.twig
new file mode 100644
index 0000000..b8a4a98
--- /dev/null
+++ b/views/ota-list-tables.twig
@@ -0,0 +1,62 @@
+
+
+LineageOTA Builds for {{ branding['name'] }}
+
+
+
+Currently available builds for {{ branding['name'] }}
+
+{% for model,build in sortedBuilds %}
+ {% if deviceNames[model] is defined %}
+ - {{ model }} ({{ vendorNames[model] }} {{ deviceNames[model] }})
+ {% else %}
+ - {{ model }}
+ {% endif %}
+{% endfor %}
+
+
+
+
+ {% for model,builds in sortedBuilds %}
+{{ model }}
+ {% if deviceNames[model] is defined %}
+( {{ vendorNames[model] }} {{ deviceNames[model] }})
+ {% endif %}
+
+
+
+ Channel |
+ Version |
+ Filename |
+ Size |
+ Source |
+ Date (Y//M/D) |
+
+
+
+
+ {% for build in builds %}
+
+ {{ build['channel'] }} |
+ {{ build['version'] }} |
+ {{ build['filename'] }} [md5sum: {{ build['md5sum'] }}] |
+ {% set filename = build['filename'] %}{{ formatedFileSizes[filename] }} |
+
+ {% if build['url'] matches '/github\.com/' %}
+ local |
+ {% else %}
+ Github |
+ {% endif %}
+
+ {{ build['timestamp'] | date('Y/m/d') }} |
+
+ {% endfor %}
+
+
+
+
+
+{% endfor %}
+
+
+
\ No newline at end of file