parent
13832e4cc7
commit
1a75dad626
@ -0,0 +1,116 @@ |
|||||||
|
<?php |
||||||
|
|
||||||
|
/** |
||||||
|
* SPDX-License-Identifier: EUPL-1.2 |
||||||
|
* Authors: see /README.md |
||||||
|
*/ |
||||||
|
|
||||||
|
namespace SeaCMS; |
||||||
|
|
||||||
|
use Exception; |
||||||
|
use Pico; |
||||||
|
use Throwable; |
||||||
|
|
||||||
|
set_error_handler(function ( |
||||||
|
int $errno, |
||||||
|
string $errstr, |
||||||
|
string $errfile = '', |
||||||
|
int $errline = -1 |
||||||
|
) { |
||||||
|
if (!isset($GLOBALS['errorMessages'])){ |
||||||
|
$GLOBALS['errorMessages']= []; |
||||||
|
} |
||||||
|
$GLOBALS['errorMessages'][] = <<<HTML |
||||||
|
<div> |
||||||
|
Error : $errstr<br> |
||||||
|
in file '$errfile' (line '$errline').<br/> |
||||||
|
ErrCode : $errno |
||||||
|
</div> |
||||||
|
|
||||||
|
HTML; |
||||||
|
return true; |
||||||
|
}); |
||||||
|
|
||||||
|
class App |
||||||
|
{ |
||||||
|
/** |
||||||
|
* Pico instance. |
||||||
|
* @var Pico |
||||||
|
*/ |
||||||
|
protected $pico; |
||||||
|
|
||||||
|
public function __construct(string $contentFolderFromRoot) |
||||||
|
{ |
||||||
|
// sanitize content folder |
||||||
|
$cwd = getcwd(); |
||||||
|
if (empty($contentFolderFromRoot)){ |
||||||
|
$contentFolderFromRoot = 'content'; |
||||||
|
} else { |
||||||
|
$contentFolderFromRoot = str_replace('\\','/',$contentFolderFromRoot); |
||||||
|
} |
||||||
|
if (!is_dir($cwd)){ |
||||||
|
throw new Exception("getcwd returned a path that is not a directory !"); |
||||||
|
} |
||||||
|
if (!is_dir("$cwd/$contentFolderFromRoot")){ |
||||||
|
$contentFolderFromRoot = 'vendor/picocms/pico/content-sample'; |
||||||
|
} |
||||||
|
if (substr($contentFolderFromRoot,-1) !== '/'){ |
||||||
|
$contentFolderFromRoot .= '/'; |
||||||
|
} |
||||||
|
// instance Pico |
||||||
|
$this->pico = new Pico( |
||||||
|
$cwd, // root dir |
||||||
|
$contentFolderFromRoot, // config dir |
||||||
|
'vendor/picocms/plugins/', // plugins dir |
||||||
|
'vendor/picocms/themes/' // themes dir |
||||||
|
); |
||||||
|
} |
||||||
|
|
||||||
|
public function runPico(): string |
||||||
|
{ |
||||||
|
return $this->pico->run(); |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* instanciate Pico and run it then echo output |
||||||
|
* @param string $contentFolderFromRoot where is the root folder |
||||||
|
*/ |
||||||
|
public static function run(string $contentFolderFromRoot) |
||||||
|
{ |
||||||
|
try { |
||||||
|
$app = new App($contentFolderFromRoot); |
||||||
|
$output = $app->runPico(); |
||||||
|
self::appendErrorMessagesIfNeeded($output); |
||||||
|
} catch (Throwable $th) { |
||||||
|
$output = <<<HTML |
||||||
|
<div style="color:red;"> |
||||||
|
Exception : {$th->__toString()} |
||||||
|
</div> |
||||||
|
HTML; |
||||||
|
} finally { |
||||||
|
echo $output; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
/** |
||||||
|
* append error messages from $GLOBALS['errorMessages'] |
||||||
|
* only if $_GET['debug'] === 'yes' |
||||||
|
* @param string &$output |
||||||
|
*/ |
||||||
|
protected static function appendErrorMessagesIfNeeded(string &$output) |
||||||
|
{ |
||||||
|
if (!empty($_GET['debug']) && $_GET['debug'] === 'yes' && !empty($GLOBALS['errorMessages'])){ |
||||||
|
$formattedMessages = implode("\n", $GLOBALS['errorMessages']); |
||||||
|
$formattedMessages = <<<HTML |
||||||
|
<div style="background-color:navajowhite;"> |
||||||
|
$formattedMessages |
||||||
|
</div> |
||||||
|
HTML; |
||||||
|
if (preg_match('/<\/body>/i', $output, $match)) { |
||||||
|
$output = str_replace($match[0], $formattedMessages.$match[0], $output); |
||||||
|
} else { |
||||||
|
$output = $output.$formattedMessages; |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
Loading…
Reference in new issue