Skip to content

Commit

Permalink
Added Doctrine sql-formatter, routes view and configuration to print …
Browse files Browse the repository at this point in the history
…SQL of reports (#131)
  • Loading branch information
kchapple committed Sep 17, 2020
1 parent 57631aa commit 9ff7fd0
Show file tree
Hide file tree
Showing 30 changed files with 25,816 additions and 5 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "careset/zermelo",
"name": "careset/zermelo",
"description": "Zermelo, a PHP reporting engine for Laravel",
"type": "library",
"license": "Apache-2.0",
Expand All @@ -13,7 +13,8 @@
"vega"
],
"require": {
"php": ">=7.2.0"
"php": ">=7.2.0",
"doctrine/sql-formatter": "1.1.*"
},
"autoload": {
"psr-4" : {
Expand Down
14 changes: 13 additions & 1 deletion src/CareSet/Zermelo/Console/ZermeloInstallCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@

class ZermeloInstallCommand extends AbstractZermeloInstallCommand
{
/**
* The views that need to be exported.
*
* @var array
*/
public static $views = [
'zermelo/sql.blade.php',
'zermelo/layouts/sql.blade.php',
];

protected static $view_path = __DIR__.'/../../../../views';

/*
* Automatically copy the zermelo.js library in assets/js
*/
Expand All @@ -20,7 +32,7 @@ class ZermeloInstallCommand extends AbstractZermeloInstallCommand

/**
* @var string
*
*
* Console command signature
*/
protected $signature = 'zermelo:install_api
Expand Down
88 changes: 88 additions & 0 deletions src/CareSet/Zermelo/Http/Controllers/SQLPrintController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

namespace CareSet\Zermelo\Http\Controllers;

use CareSet\Zermelo\Http\Controllers\AbstractWebController;
use CareSet\Zermelo\Http\Requests\CardsReportRequest;
use CareSet\Zermelo\Interfaces\ZermeloReportInterface;
use CareSet\Zermelo\Models\ZermeloReport;
use Doctrine\SqlFormatter\SqlFormatter;

class SQLPrintController extends AbstractWebController
{
/**
* @return \Illuminate\Config\Repository|mixed
*
* Get the view template
*/
public function getViewTemplate()
{
return config("zermelo.SQL_PRINT_VIEW_TEMPLATE");
}

/**
* @return string
*
* Specify the path to this report's API
* This report uses the tabular api prefix
*/
public function getReportApiPrefix()
{
return tabular_api_prefix();
}

/**
* @param $report
*
* Build presenter and push our required varialbes for this web view
*/
public function onBeforeShown(ZermeloReportInterface $report)
{
// before we show the report SQL, make sure SQL printing is enabled,
// If not, throw an error
if (!$report->isSQLPrintEnabled()) {
abort(403, 'SQL Printing Is Not Enabled.');
}

$bootstrap_css_location = asset(config('zermelo.BOOTSTRAP_CSS_LOCATION','/css/bootstrap.min.css'));
$report->pushViewVariable('bootstrap_css_location', $bootstrap_css_location);
$report->pushViewVariable('report_uri', $this->getReportUri($report));
$report->pushViewVariable('summary_uri', $this->getSummaryUri($report));

// Use the SQL Formatter to push a formatted sql string to view
$report_sql = $report->GetSQL();
if (is_array($report_sql)) {

// If the report SQL is an array, we have some more work to do to show formatting
$formatted_sql = "";
$cnt = 0;
foreach ($report_sql as $sql_part) {
$formatted_sql .= "<div><h2>[{$cnt}]</h2><br>" . (new SqlFormatter())->format($sql_part) . "</div>";
$cnt++;
}
} else {
$formatted_sql = (new SqlFormatter())->format($report_sql);
}
$report->pushViewVariable('formatted_sql', $formatted_sql);
}

/**
* @param $report
* @return string
*
* Protected method, specific to this controller, to build the report URI (though as of now it's the same as tabular)
*/
protected function getReportUri($report)
{
$parameterString = implode("/", $report->getMergedParameters() );
$report_api_uri = "/{$this->getApiPrefix()}/{$this->getReportApiPrefix()}/{$report->uriKey()}/{$parameterString}";
return $report_api_uri;
}

protected function getSummaryUri($report)
{
$parameterString = implode("/", $report->getMergedParameters() );
$summary_api_uri = "/{$this->getApiPrefix()}/{$this->getReportApiPrefix()}/{$report->uriKey()}/Summary/{$parameterString}";
return $summary_api_uri;
}
}
2 changes: 2 additions & 0 deletions src/CareSet/Zermelo/Interfaces/ZermeloReportInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ interface ZermeloReportInterface
public function pushViewVariable($name, $value);

public function setToken($token);

public function isSQLPrintEnabled();
}
39 changes: 39 additions & 0 deletions src/CareSet/Zermelo/Models/ZermeloReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,20 @@ abstract class ZermeloReport implements ZermeloReportInterface
*/
protected $_token = null;

/**
* @var bool
*
* Specify whether the cache is enabled, or not. NOTE: Reports are always
* run from the "cache" but disabling this will regenerate the cache on each load,
* and not retain the result of the report query in the cache table
*/
private $_isCacheEnabled = false; //cache is always off by default.

/**
* @var null
*
* When cache is enabled, how long to retain the results of the report query in the cache table
*/
private $_howLongToCacheInSeconds = null;

private $_socketService = null;
Expand All @@ -81,6 +93,15 @@ abstract class ZermeloReport implements ZermeloReportInterface
*/
protected $HOW_LONG_TO_CACHE_IN_SECONDS = 600;

/**
* @var bool
*
* This enables the sql print feature for this report, which enables the route
* for printing the SQL that generates a report. There is also a global
* configuration zermelo.SQL_PRINT_ENABLE in the zeremelo config file which also
* needs to be enabled FIRST if you want to enable on the report level.
*/
protected $SQL_PRINT_ENABLED = false;

/**
* $INDICIES
Expand Down Expand Up @@ -240,6 +261,24 @@ public static function uriKey()
// return Str::plural(Str::snake(class_basename(get_called_class()), '-'));
}

/**
* @return bool
*
* Return true if the print function is enabled both in the config file AND
* in the child report, return false OW
*
* There is only a getter because this will be set by an attribute by the same
* name in the child class, and we also check the configuration in zermelo config
*/
public function isSQLPrintEnabled(): bool
{
if (config('zermelo.SQL_PRINT_ENABLED', false)) {
return $this->SQL_PRINT_ENABLED;
}

return false;
}

/**
* Should we enable the cache on this table?
* This will improve the performance of very large and complex queries by only running the SQL once and then storing
Expand Down
38 changes: 36 additions & 2 deletions src/CareSet/Zermelo/ZermeloServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public function register()
}//end register function..


public $is_socket_ok = false; //start assuming it is not.
public $is_socket_ok = false; //start assuming it is not.
public $is_socket_checked = false;
/**
* @param Router $router
Expand Down Expand Up @@ -97,7 +97,9 @@ public function boot( Router $router )
// during composer package discovery, or installation
if (php_sapi_name() !== 'cli') {
$this->registerApiRoutes();
$this->registerWebRoutes();
$this->registerReports();
$this->loadViewsFrom( resource_path( 'views/zermelo' ), 'Zermelo');
}
}

Expand Down Expand Up @@ -153,12 +155,26 @@ protected function registerApiRoutes()
Route::group( ['prefix' => $tree_api_prefix ], function() {
$this->loadRoutesFrom(__DIR__.'/routes/api.tree.php');
});
});
}

/**
* Get the WEB route configurations
*/
protected function registerWebRoutes()
{
Route::group($this->webRouteConfiguration(), function() {

// Load the pretty-print SQL routes from web.sql.php using the configured prefix
$sql_print_prefix = config( 'zermelo.SQL_PRINT_PREFIX' );
Route::group([ 'prefix' => $sql_print_prefix ], function () {
$this->loadRoutesFrom(__DIR__.'/routes/web.sql.php');
});
});
}

/**
* Get the Nova route group configuration array.
* Get the API route group configuration array.
*
* @return array
*/
Expand All @@ -174,4 +190,22 @@ protected function routeConfiguration()
'middleware' => $middleware,
];
}

/**
* Get the web route group configuration array.
*
* @return array
*/
protected function webRouteConfiguration()
{
$middleware = config('zermelo.WEB_MIDDLEWARE',[ 'web' ]);

return [
'namespace' => 'CareSet\Zermelo\Http\Controllers',
// 'domain' => config('zermelo.domain', null),
'as' => 'zermelo.web.',
'prefix' => '',
'middleware' => $middleware,
];
}
}
Loading

0 comments on commit 9ff7fd0

Please sign in to comment.