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 @@ - -
-Filename | -Source | -Date (Y//M/D) |
- Channel | -Version | -MD5 Checksum | -
---|---|---|---|---|---|
" . $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
Channel | +Version | +Filename | +Size | +Source | +Date (Y//M/D) |
+ |
---|---|---|---|---|---|---|
{{ 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') }} | +