diff --git a/App.php b/App.php new file mode 100644 index 0000000..813abe3 --- /dev/null +++ b/App.php @@ -0,0 +1,116 @@ + + Error : $errstr
+ in file '$errfile' (line '$errline').
+ ErrCode : $errno + + + 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 = << + Exception : {$th->__toString()} + + 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 = << + $formattedMessages + + HTML; + if (preg_match('/<\/body>/i', $output, $match)) { + $output = str_replace($match[0], $formattedMessages.$match[0], $output); + } else { + $output = $output.$formattedMessages; + } + } + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 79c30b0..e43a632 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,9 @@ "suggest": { "picocms/pico-theme": "Default pico theme : revision ^3.0" }, + "autoload": { + "classmap": ["App.php"] + }, "extra": { "pico-plugin-dir": "vendor/picocms/plugins/", "pico-theme-dir": "vendor/picocms/themes/"