1
0
mirror of https://github.com/processwire/processwire.git synced 2025-08-08 15:57:01 +02:00

Update PW's error handler to recognize failed functions API calls when functions API isn't enabled, and provide instruction on how to enable it

This commit is contained in:
Ryan Cramer
2019-01-11 09:13:40 -05:00
parent 1a94578b1e
commit 2735ee397e
2 changed files with 64 additions and 1 deletions

View File

@@ -504,3 +504,43 @@ function wirePaths($key = '') {
return wire('config')->paths($key);
}
/**
* Return array of functions available from the functions API
*
* Returned array is shortVersion => longVersion
*
* @return array
*
*/
function _wireFunctionsAPI() {
$names = array(
'cache' => 'wireCache',
'config' => 'wireConfig',
'database' => 'wireDatabase',
'datetime' => 'wireDatetime',
'fields' => 'wireFields',
'files' => 'wireFiles',
'input' => 'wireInput',
'inputGet' => 'wireInputGet',
'inputPost' => 'wireInputPost',
'inputCookie' => 'wireInputCookie',
'languages' => 'wireLanguages',
'modules' => 'wireModules',
'page' => 'wirePage',
'pages' => 'wirePages',
'paths' => 'wirePaths',
'permissions' => 'wirePermissions',
'profiler' => 'wireProfiler',
'region' => 'wireRegion',
'roles' => 'wireRoles',
'sanitizer' => 'wireSanitizer',
'setting' => '', // no implemented
'session' => 'wireSession',
'templates' => 'wireTemplates',
'urls' => 'wireUrls',
'user' => 'wireUser',
'users' => 'wireUsers',
);
return $names;
}

View File

@@ -185,6 +185,28 @@ class WireShutdown extends Wire {
return $url;
}
/**
* Add helpful info or replace error message with something better, when possible
*
* @param string $message
* @return string
*
*/
protected function amendErrorMessage($message) {
if(!$this->config->useFunctionsAPI && strpos($message, "undefined function ")) {
$names = _wireFunctionsAPI();
$names = implode('|', array_keys($names));
if(preg_match('/undefined function (ProcessWire.|\b)(' . $names . ')\(/', $message, $matches)) {
$name = $matches[2];
$message = // replace error message with the following
"You have attempted to access function $name(); that is only available if the ProcessWire Functions API is enabled. " .
"Enable it by setting \$config->useFunctionsAPI = true; in your /site/config.php file, and then this " .
"error message should no longer appear.";
}
}
return $message;
}
/**
* Render an error message and reason why
*
@@ -202,7 +224,7 @@ class WireShutdown extends Wire {
echo "\n\n$message\n\n$why\n\n";
return;
}
// output HTML error
$html = $this->config->fatalErrorHTML ? $this->config->fatalErrorHTML : self::defaultFatalErrorHTML;
$html = str_replace(array(
@@ -368,6 +390,7 @@ class WireShutdown extends Wire {
if($why) {
$why = $this->labels['shown-because'] . " $why $who";
$message = $this->amendErrorMessage($message);
$this->sendErrorMessage($message, $why, $useHTML);
} else {
$this->sendError500($who, $useHTML);