Commit 73479f27 authored by Markus Willman's avatar Markus Willman

finish "extensions"

parent d416dae5
......@@ -32,6 +32,7 @@ if (!defined('DCBASE_VENDOR_PATH')) define('DCBASE_VENDOR_PATH', DCBASE_ROOT_PAT
if (!defined('DCBASE_CACHE_PATH')) define('DCBASE_CACHE_PATH', DCBASE_ROOT_PATH . 'cache/');
if (!defined('DCBASE_CONFIG_PATH')) define('DCBASE_CONFIG_PATH', DCBASE_ROOT_PATH . 'config/');
if (!defined('DCBASE_TEMPLATE_PATH')) define('DCBASE_TEMPLATE_PATH', DCBASE_ROOT_PATH . 'template/');
if (!defined('DCBASE_EXTENSION_PATH')) define('DCBASE_EXTENSION_PATH', DCBASE_ROOT_PATH . 'extension/');
if (!defined('DCBASE_CONTENT_ROOT')) define('DCBASE_CONTENT_ROOT', DCBASE_ROOT_PATH . 'content/');
// Autoloader
......@@ -39,7 +40,7 @@ require(DCBASE_VENDOR_PATH . 'DCBase/ClassLoader.' . PHP_EXT);
ClassLoader::create(array(
'Parsedown' => DCBASE_VENDOR_PATH . 'Parsedown/Parsedown.' . PHP_EXT,
'ParsedownExtra' => DCBASE_VENDOR_PATH . 'Parsedown/ParsedownExtra.' . PHP_EXT
), true)->addDefaultPaths();
), true)->addDefaultPaths()->addPaths(DCBASE_EXTENSION_PATH, 'DCBase\Extension');
// Load constants file
ConfigLoader::load('Constants');
......@@ -58,4 +58,15 @@ return array(
'api_token' => '(?:\{){0,1}[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\\-[0-9a-fA-F]{12}(?:\}){0,1}',
)
),
// --- EXTENSIONS BELOW THIS LINE ---
'/extension/hello(/<name>)' => array(
// required: set the handler for this dynamic path, must be a subclass of \DCBase\Handler\AbstractHandler
'handler' => \DCBase\Extension\HelloWorld\HelloHandler::class,
// required: default arguments, sets expected variable type
'defaults' => array('name' => 'World'),
// optional: override the regex pattern used to match the named argument, the patterns are automatically enclosed in a capture group
'regex' => array('name' => '\w++')
),
);
<?php
/**
*
* @package dcbase
* @copyright (c) 2019 Direct Connect Network Foundation / www.dcbase.org
* @license https://www.dcbase.org/DCBase/LICENSE GNU General Public License v2
*
*/
namespace DCBase\Extension\HelloWorld;
/**
* @ignore
*/
if (!defined('IN_DCBASE')) exit;
use SplFileInfo;
use DCBase\Handler\AbstractHandler;
use DCBase\WebRequest;
use DCBase\Response\ResponseHelper;
use DCBase\WebResponse;
/**
* The obligatory hello world sample
*
* Basic config block for `config/HandlerMap.php`:
*
* ```php
* '/extension/hello(/<name>)' => array(
* // required: set the handler for this dynamic path, must be a subclass of \DCBase\Handler\AbstractHandler
* 'handler' => \DCBase\Extension\HelloWorld\HelloHandler::class,
* // required: default arguments, sets expected variable type
* 'defaults' => array('name' => 'World'),
* // optional: override the regex pattern used to match the named argument, the patterns are automatically enclosed in a capture group
* 'regex' => array('name' => '\w++')
* ),
* ```
*/
class HelloHandler extends AbstractHandler
{
/**
* Handle an incoming request for a resource outside of FrontController
*
* @param WebRequest $request The incoming request expected to be handled
* @param string $relative_path The relative file path as part of the query string
* @param SplFileInfo|null $file File object bound to a document root, referenced path may not exist and cannot be a symlink
* @return mixed Valid WebResponse instance to be served, any other values (e.g. false) will continue processing of the request in FrontController
*/
public function handleResource(WebRequest $request, string $relative_path, SplFileInfo $file = null)
{
// EXAMPLE ONLY: since this does nothing intelligent we don't want to handle paths that would actully be served by FrontController
if ($file->isFile())
return false;
return WebResponse::text($request, 'Hello '. ucfirst($request->getPathSegment('name')) .'!');
}
}
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