Commit 07843cdf authored by Marc Egger's avatar Marc Egger
Browse files

ColumnScript.php: use Path Class

parent b8037e1b
Pipeline #3881 failed with stages
in 1 minute and 55 seconds
......@@ -17,4 +17,18 @@ class Thrower
ERROR_MESSAGE_TO_USER => $errorToUser,
ERROR_MESSAGE_TO_DEVELOPER => $errorToDeveloper]), ERROR_IO_READ_FILE);
}
/**
* Throws UserReportException with given messages to user and developer.
*
* @param string $errorToUser
* @param string $errorToDeveloper
* @param int $errorCode
* @throws \UserReportException
*/
public static function userReportException(string $errorToUser, string $errorToDeveloper = '', int $errorCode = E_ERROR) {
throw new \UserReportException(json_encode([
ERROR_MESSAGE_TO_USER => $errorToUser,
ERROR_MESSAGE_TO_DEVELOPER => $errorToDeveloper]), ERROR_IO_READ_FILE);
}
}
\ No newline at end of file
......@@ -4,8 +4,7 @@
namespace IMATHUZH\Qfq\Core\Helper;
/*
* Naming convention of path variables:
*
* Naming convention of path constants/functions/variables:
* 1) name a path by its origin and its destination separated by 'to'. E.g. APP_TO_SYSTEM_LOG, $appToProject.
* 2) if the destination is a file, append "File". E.g. APP_TO_SYSTEM_QFQ_LOG_FILE.
* 3) if a path has to be variable, create a setter and getter. E.g. self::setCwdToApp(), self::cwdToApp(), private static $cwdToApp.
......@@ -14,40 +13,6 @@ namespace IMATHUZH\Qfq\Core\Helper;
* 6) avoid defining redundant paths in constants. E.g. create cwdToApi() by combining cwdToExt() and extToApi() instead of defining CWD_TO_API.
*/
/*
Unittests:
Path::apiRelToCwd(API_SAVE_PHP) === API_DIR . '/save.php'
Path::apiRelToCwd(API_LOAD_PHP) === API_DIR . '/load.php'
Path::apiRelToCwd(API_FILE_PHP) . '?' . $actionUpload === API_DIR . '/file.php?' . $actionUpload
Path::apiRelToCwd(API_FILE_PHP) . '?' . $actionDelete === API_DIR . '/file.php?' . $actionDelete
Path::apiRelToCwd(API_DIRTY_PHP) === API_DIR . "/" . API_DIRTY_PHP
Path::apiRelToCwd(API_DRAG_AND_DROP_PHP) === API_DIR . '/' . API_DRAG_AND_DROP_PHP
Path::apiRelToCwd(API_SAVE_PHP) === API_DIR . "/" . "save.php"
Path::apiRelToCwd(API_DOWNLOAD_PHP) === API_DIR . '/' . API_DOWNLOAD_PHP
Path::apiRelToCwd(API_DELETE_PHP) === API_DIR . '/' . API_DELETE_PHP
Path::apiRelToExt() === API_DIR_EXT
Path::apiRelToExt(API_DOWNLOAD_PHP) === API_DIR_EXT . '/' . API_DOWNLOAD_PHP
Path::extRelToCwd(GFX_INFO_REL_TO_EXT) === GFX_INFO
Path::extRelToCwd(PATH_ICONS_REL_TO_EXT) === PATH_ICONS
Path::appRelToCwd(SYSTEM_FILEADMIN_PROTECTED_LOG_REL_TO_APP) === SYSTEM_FILEADMIN_PROTECTED_LOG
Path::appRelToCwd(SYSTEM_QFQ_LOG_FILE_REL_TO_APP) === SYSTEM_QFQ_LOG_FILE
Path::appRelToCwd(SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT_REL_TO_APP) === SYSTEM_THUMBNAIL_DIR_SECURE_DEFAULT
Path::appRelToCwd(SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT_REL_TO_APP) === SYSTEM_THUMBNAIL_DIR_PUBLIC_DEFAULT
Path::appRelToCwd(THUMBNAIL_UNKNOWN_TYPE_REL_TO_APP) === THUMBNAIL_UNKNOWN_TYPE
Path::appRelToCwd(DIR_HIGHLIGHT_JSON_REL_TO_APP) === DIR_HIGHLIGHT_JSON
$qfqProjectDirRelToCwd = Path::appRelToCwd(Store::getInstance()->getVar(SYSTEM_QFQ_PROJECT_DIR_SECURE_REL_TO_APP, STORE_SYSTEM));
Path::join($qfqProjectDirRelToCwd, Path::REPORT_REL_TO_PROJECT_DIR) === "../../../../../fileadmin/protected/qfqProject/report"
*/
use IMATHUZH\Qfq\Core\Exception\Thrower;
use IMATHUZH\Qfq\Core\Store\Config;
......
......@@ -2,7 +2,9 @@
namespace IMATHUZH\Qfq\Core\Report;
use IMATHUZH\Qfq\Core\Exception\Thrower;
use IMATHUZH\Qfq\Core\Helper\KeyValueStringParser;
use IMATHUZH\Qfq\Core\Helper\Path;
use IMATHUZH\Qfq\Core\Store\Store;
......@@ -19,6 +21,14 @@ use IMATHUZH\Qfq\Core\Store\Store;
* The second argument passed is an instance of ScriptFunctions which acts as an interface to QFQ functionality.
*/
class ColumnScript {
/**
* @param string $columnValue
* @return false|string
* @throws \CodeException
* @throws \UserFormException
* @throws \UserReportException
*/
public function render(string $columnValue) {
// Parse arguments
......@@ -41,9 +51,9 @@ class ColumnScript {
}
// include script
$scriptPath = getcwd() . '/' . $param[TOKEN_SCRIPT];
$scriptPath = Path::cwdToApp($param[TOKEN_SCRIPT]);
if (!is_readable($scriptPath)) {
throw new \UserReportException("Can't read script file.", ERROR_IO_READ_FILE);
Thrower::userReportException("Can't read script file.", "Can't read file: $scriptPath");
}
try {
ob_start();
......@@ -55,14 +65,12 @@ class ColumnScript {
throw new \Exception('Include failed.');
}
} catch (\Exception | \Error $e) {
throw new \UserReportException(json_encode([
ERROR_MESSAGE_TO_USER => 'Error during reading script file.',
ERROR_MESSAGE_TO_DEVELOPER => "Error meassage:\n" . $e->getMessage()]));
Thrower::userReportException('Error during reading script file.', "Error message:\n" . $e->getMessage());
}
// execute function, write output to buffer
if (!function_exists($param[TOKEN_FUNCTION_CALL])) {
throw new \UserReportException("Function doesn't exist.", ERROR_IO_READ_FILE);
Thrower::userReportException("Function doesn't exist.", "In file '$scriptPath' there is no function " . $param[TOKEN_FUNCTION_CALL]);
}
ob_start();
try {
......@@ -70,9 +78,8 @@ class ColumnScript {
$output = ob_get_clean();
} catch (\Exception | \Error $e) {
ob_end_clean();
throw new \UserReportException(json_encode([
ERROR_MESSAGE_TO_USER => 'Function execution failed.',
ERROR_MESSAGE_TO_DEVELOPER => "Error message:\n" . $e->getMessage() . "\n\nFunction: " . $param[TOKEN_FUNCTION_CALL] . "\n\nParameters:\n" . print_r($param,true)]));
Thrower::userReportException('Function execution failed.',
"Error message:\n" . $e->getMessage() . "\n\nFunction: " . $param[TOKEN_FUNCTION_CALL] . "\n\nParameters:\n" . print_r($param,true));
}
// return buffer output and fill store
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment