Commit cb7c3933 authored by Marc Egger's avatar Marc Egger
Browse files

Refactor Path.php: Remove function setMainPaths() and replace it with lazy loading.

parent 79238d2a
Pipeline #5240 passed with stages
in 3 minutes and 33 seconds
......@@ -38,7 +38,6 @@ $status = HTTP_400_BAD_REQUEST;
try {
try {
Path::setMainPaths();
$qfq = new QuickFormQuery(['bodytext' => '']);
$data = $qfq->dataReport();
......
......@@ -72,8 +72,6 @@ $flagSuccess = false;
try {
try {
Path::setMainPaths();
$qfq = new QuickFormQuery(['bodytext' => '']);
$answer = $qfq->delete();
......
......@@ -18,7 +18,6 @@ use IMATHUZH\Qfq\Core\Helper\Path;
*
*/
try {
Path::setMainPaths();
$dirty = new Dirty();
$answer = $dirty->process();
......
......@@ -22,7 +22,6 @@ $output = '';
try {
try {
Path::setMainPaths();
$download = new Download();
// If all is fine: process() will output file via print() !!
......
......@@ -51,7 +51,6 @@ $answer[API_MESSAGE] = '';
try {
try {
Path::setMainPaths();
$qfq = new QuickFormQuery(['bodytext' => '']);
$data = $qfq->dragAndDrop();
......
......@@ -38,7 +38,6 @@ $answer[API_MESSAGE] = '';
try {
try {
Path::setMainPaths();
$fileUpload = new File();
$fileUpload->process();
......
......@@ -52,7 +52,6 @@ $answer[API_MESSAGE] = '';
try {
try {
Path::setMainPaths();
$qfq = new QuickFormQuery(['bodytext' => '']);
$data = $qfq->updateForm();
......
......@@ -20,7 +20,6 @@ use IMATHUZH\Qfq\Core\Store\Config;
* Main
*/
try {
Path::setMainPaths();
$html2pdf = new Html2Pdf(Config::getConfigArray());
$html2pdf->outputHtml2Pdf();
......
......@@ -22,7 +22,6 @@ $data = array();
try {
try {
Path::setMainPaths();
$form = OnString::splitPathInfoToIdForm($_SERVER['PATH_INFO'] ?? '', $restId, $restForm);
// get latest `ìd`
......
......@@ -52,7 +52,6 @@ $answer[API_MESSAGE] = '';
try {
try {
Path::setMainPaths();
$qfq = new QuickFormQuery(['bodytext' => ""]);
$data = $qfq->saveForm();
......
......@@ -36,7 +36,6 @@ $status = HTTP_400_BAD_REQUEST;
try {
try {
Path::setMainPaths();
$qfq = new QuickFormQuery(['bodytext' => '']);
$qfq->setting();
......
......@@ -19,7 +19,6 @@ use IMATHUZH\Qfq\Core\Helper\Path;
*
*/
try {
Path::setMainPaths();
$typeAhead = new TypeAhead();
$answer = $typeAhead->process();
......
......@@ -35,7 +35,6 @@ class QfqController extends \TYPO3\CMS\Extbase\Mvc\Controller\ActionController {
$flagOk = false;
try {
Path::setMainPaths();
$contentObject = $this->configurationManager->getContentObject();
// By T3 default 'E_NOTICE' is unset. E.g. 'Undefined Index' will throw an exception.
......
......@@ -3,7 +3,7 @@
namespace IMATHUZH\Qfq\Core\Helper;
/*
/**
* Glossar:
* - App: directory in which the index.php file is located. All urls should be relative to this.
* - Ext: directory in which the QFQ extension is loacted. e.g. the folder Classes is in there.
......@@ -27,7 +27,7 @@ use IMATHUZH\Qfq\Core\Store\Config;
class Path {
// App
private static $absoluteApp = null; // This is manually set in self::setMainPaths()
private static $absoluteApp = null;
private static $urlApp = null;
// Extension
......@@ -91,32 +91,18 @@ class Path {
// Send Email
const EXT_TO_SEND_EMAIL_FILE = 'Classes/External/sendEmail';
/**
* Manually set the paths which are not constant nor can be inferred by other paths.
* This function must be called at the beginning of every entry point, to tell the Path class, where things are.
*
* @param string|null $absoluteApp
* @throws \CodeException
* @throws \UserFormException
*/
public static function setMainPaths(string $absoluteApp = null) {
if (is_null($absoluteApp)) {
$absoluteApp = self::findAbsoluteApp();
}
self::setAbsoluteApp($absoluteApp);
// Only executed on first call:
self::findAppToProject();
self::findAbsoluteLog();
}
/**
* @param array $pathPartsToAppend
* @return string
* @throws \UserFormException
*/
public static function absoluteApp(...$pathPartsToAppend): string {
self::enforcePathIsSet(self::$absoluteApp);
if (is_null(self::$absoluteApp)) {
self::findAbsoluteApp();
}
if (is_null(self::$absoluteApp)) {
Thrower::userFormException('Path not set.', 'Absolute app path is null. This should not happen at this point.');
}
return self::join(self::$absoluteApp, $pathPartsToAppend);
}
......@@ -138,7 +124,9 @@ class Path {
if (is_null(self::$absoluteLog)) {
self::findAbsoluteLog();
}
self::enforcePathIsSet(self::$absoluteLog);
if (is_null(self::$absoluteLog)) {
Thrower::userFormException('Path not set.', 'Absolute log path is null. This should not happen at this point.');
}
return self::join(self::$absoluteLog, $pathPartsToAppend);
}
......@@ -178,10 +166,16 @@ class Path {
/**
* @param array $pathPartsToAppend
* @return string
* @throws \CodeException
* @throws \UserFormException
*/
public static function appToProject(...$pathPartsToAppend): string {
self::enforcePathIsSet(self::$appToProject);
if (is_null(self::$appToProject)) {
self::findAppToProject();
}
if (is_null(self::$appToProject)) {
Thrower::userFormException('Path not set.', 'Project path is null. This should not happen at this point.');
}
return self::join(self::$appToProject, $pathPartsToAppend);
}
......@@ -215,9 +209,15 @@ class Path {
/**
* @param mixed ...$pathPartsToAppend
* @return string
* @throws \CodeException
* @throws \UserFormException
* @throws \UserReportException
*/
public static function urlApp(...$pathPartsToAppend): string {
if (is_null(self::$urlApp)) {
self::setUrlApp(Config::get(SYSTEM_BASE_URL));
}
// ensure base url is configured
if (is_null(self::$urlApp) || self::$urlApp === '') {
Thrower::userFormException('Base url not configured.', 'Go to QFQ extension configuration in the Typo3 backend and fill in a value for config.baseUrl');
......@@ -410,12 +410,10 @@ class Path {
return $protocol . $path;
}
/////////////////////////////////////////////////// Private //////////////////////////////////////////////////////
/**
* @param string $newPath
*/
private static function setAbsoluteApp(string $newPath) {
public static function setAbsoluteApp(string $newPath) {
self::$absoluteApp = $newPath;
}
......@@ -442,10 +440,6 @@ class Path {
* @throws \UserFormException
*/
private static function findAbsoluteLog() {
if (!is_null(self::$absoluteLog)) {
// only execute once
return;
}
// search log dir qfqProject/log
$absoluteLog = self::absoluteApp(self::appToProject(self::PROJECT_TO_LOG_DEFAULT));
......@@ -470,10 +464,6 @@ class Path {
* @throws \UserFormException
*/
private static function findAppToProject() {
if (!is_null(self::$appToProject)) {
// only execute once
return;
}
// does qfq.project.path.php exist? => read path
$absoluteProjectPathFile = self::absoluteApp(PROJECT_PATH_PHP_FILE);
......@@ -506,15 +496,15 @@ class Path {
*
* @throws \UserFormException
*/
private static function findAbsoluteApp() {
public static function findAbsoluteApp() {
// look for typo3conf directory
$absoluteApp = self::realpath(self::join(__DIR__, '../../../../../../'));
if (!file_exists(self::join($absoluteApp, self::APP_TO_TYPO3_CONF))) {
Thrower::userFormException('App path seems to be wrong: Directory "typo3conf" not found in app path.'
, "Current app path: $absoluteApp");
Thrower::userFormException('App path seems to be wrong: Directory "typo3conf" not found in app path.',
" Current app path: $absoluteApp .",
" In unit tests this can be manually set using Path::setAbsoluteApp() before the path is accessed.");
}
return $absoluteApp;
self::setAbsoluteApp($absoluteApp);
}
/**
......@@ -538,18 +528,6 @@ EOF;
HelperFile::file_put_contents(self::absoluteApp(PROJECT_PATH_PHP_FILE), $fileContent);
}
/**
* Throw an exception if the given path is not set (i.e. === null).
*
* @param $path
* @throws \UserFormException
*/
private static function enforcePathIsSet($path) {
if (is_null($path)) {
Thrower::userFormException('Path accessed before set.', 'Make sure Path::setMainPaths(...) is called before this code is executed..');
}
}
/**
* Throw exception if path does not start with '/'
*
......
......@@ -113,8 +113,6 @@ class Config {
self::$config = $config;
Path::setUrlApp(self::get(SYSTEM_BASE_URL));
// Set log paths
Path::overrideLogPathsFromConfig();
}
......
......@@ -35,7 +35,6 @@ try {
}
}
chdir($baseDir);
Path::setMainPaths();
}
$autoCron = new AutoCron($verbose);
......
......@@ -79,17 +79,20 @@ class DeleteTest extends AbstractDatabaseTest {
if (!is_dir($tempAppDir)) {
mkdir('/tmp/deleteTest');
}
Path::setMainPaths('/tmp/deleteTest');
Path::setAbsoluteApp('/tmp/deleteTest');
// The above replaces the following line with a Path:: function. Probably won't work.
// $this->store->setVar(SYSTEM_SITE_PATH_ABSOLUTE, '/tmp', STORE_SYSTEM, true);
$this->executeSQLFile(__DIR__ . '/Database/fixtures/Generic.sql', true);
}
/**
* @throws \UserFormException
*/
protected function tearDown()
{
parent::tearDown();
Path::setMainPaths();
Path::findAbsoluteApp();
}
}
......@@ -18,8 +18,6 @@ function delTree($dir) {
Path::setProjectToForm(Path::PROJECT_TO_FORM_PHPUNIT);
Path::setProjectToReport(Path::PROJECT_TO_REPORT_PHPUNIT);
Path::setMainPaths();
// remove old form_phpunit directory and all its content
$absoluteForm = Path::absoluteProject(Path::PROJECT_TO_FORM_PHPUNIT);
......
......@@ -389,7 +389,6 @@ try {
$origErrorReporting = error_reporting();
error_reporting($origErrorReporting | E_NOTICE);
Path::setMainPaths('TODO'); TODO: fix absolute path to app
$t3data["bodytext"] = "file=Home/selenium/QFQ__basicform.qfqr";
$qfq = new QuickFormQuery($t3data);
$html = $qfq->process();
......
Markdown is supported
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