Pico::run PHP Method

run() public method

Loads plugins, evaluates the config file, does URL routing, parses meta headers, processes Markdown, does Twig processing and returns the rendered contents.
public run ( ) : string
return string rendered Pico contents
    public function run()
    {
        // lock Pico
        $this->locked = true;
        // load plugins
        $this->loadPlugins();
        $this->triggerEvent('onPluginsLoaded', array(&$this->plugins));
        // load config
        $this->loadConfig();
        $this->triggerEvent('onConfigLoaded', array(&$this->config));
        // check content dir
        if (!is_dir($this->getConfig('content_dir'))) {
            throw new RuntimeException('Invalid content directory "' . $this->getConfig('content_dir') . '"');
        }
        // evaluate request url
        $this->evaluateRequestUrl();
        $this->triggerEvent('onRequestUrl', array(&$this->requestUrl));
        // discover requested file
        $this->discoverRequestFile();
        $this->triggerEvent('onRequestFile', array(&$this->requestFile));
        // load raw file content
        $this->triggerEvent('onContentLoading', array(&$this->requestFile));
        $notFoundFile = '404' . $this->getConfig('content_ext');
        if (file_exists($this->requestFile) && basename($this->requestFile) !== $notFoundFile) {
            $this->rawContent = $this->loadFileContent($this->requestFile);
        } else {
            $this->triggerEvent('on404ContentLoading', array(&$this->requestFile));
            header($_SERVER['SERVER_PROTOCOL'] . ' 404 Not Found');
            $this->rawContent = $this->load404Content($this->requestFile);
            $this->triggerEvent('on404ContentLoaded', array(&$this->rawContent));
        }
        $this->triggerEvent('onContentLoaded', array(&$this->rawContent));
        // parse file meta
        $headers = $this->getMetaHeaders();
        $this->triggerEvent('onMetaParsing', array(&$this->rawContent, &$headers));
        $this->meta = $this->parseFileMeta($this->rawContent, $headers);
        $this->triggerEvent('onMetaParsed', array(&$this->meta));
        // register parsedown
        $this->triggerEvent('onParsedownRegistration');
        $this->registerParsedown();
        // parse file content
        $this->triggerEvent('onContentParsing', array(&$this->rawContent));
        $this->content = $this->prepareFileContent($this->rawContent, $this->meta);
        $this->triggerEvent('onContentPrepared', array(&$this->content));
        $this->content = $this->parseFileContent($this->content);
        $this->triggerEvent('onContentParsed', array(&$this->content));
        // read pages
        $this->triggerEvent('onPagesLoading');
        $this->readPages();
        $this->sortPages();
        $this->discoverCurrentPage();
        $this->triggerEvent('onPagesLoaded', array(&$this->pages, &$this->currentPage, &$this->previousPage, &$this->nextPage));
        // register twig
        $this->triggerEvent('onTwigRegistration');
        $this->registerTwig();
        // render template
        $this->twigVariables = $this->getTwigVariables();
        if (isset($this->meta['template']) && $this->meta['template']) {
            $templateName = $this->meta['template'];
        } else {
            $templateName = 'index';
        }
        if (file_exists($this->getThemesDir() . $this->getConfig('theme') . '/' . $templateName . '.twig')) {
            $templateName .= '.twig';
        } else {
            $templateName .= '.html';
        }
        $this->triggerEvent('onPageRendering', array(&$this->twig, &$this->twigVariables, &$templateName));
        $output = $this->twig->render($templateName, $this->twigVariables);
        $this->triggerEvent('onPageRendered', array(&$output));
        return $output;
    }

Usage Example

コード例 #1
0
ファイル: index.php プロジェクト: biggtfish/Pico
<?php

// @codingStandardsIgnoreFile
// load dependencies
if (is_file(__DIR__ . '/vendor/autoload.php')) {
    // composer root package
    require_once __DIR__ . '/vendor/autoload.php';
} elseif (is_file(__DIR__ . '/../../../vendor/autoload.php')) {
    // composer dependency package
    require_once __DIR__ . '/../../../vendor/autoload.php';
} else {
    die("Cannot find `vendor/autoload.php`. Run `composer install`.");
}
// instance Pico
$pico = new Pico(__DIR__, 'config/', 'plugins/', 'themes/');
// override configuration?
//$pico->setConfig(array());
// run application
echo $pico->run();