Functionality for generating output from set input per path.

v1.0 2018-10-09 22:25 UTC

This package is not auto-updated.

Last update: 2024-07-07 08:59:17 UTC


Functionality for generating output from set input per path.

For example, a given path could take set reStructuredText as input and produce cached HTML as output.


composer require 'jontynewman/oku-io ^1.0'

Example Usage

The following example assumes that rst2html5 is available, and that the following packages are installed.

The editor will allow users to either input or upload reStructuredText in order to create static web pages.


use GuzzleHttp\Psr7\Response;
use JontyNewman\HtmlFilter;
use JontyNewman\Oku\ContextInterface;
use JontyNewman\Oku\IO\Cache;
use JontyNewman\Oku\IO\Repository;
use JontyNewman\Oku\Process;
use JontyNewman\Oku\RequestHandler;
use JontyNewman\Oku\ResponseBuilderInterface;

require 'vendor/autoload.php';

$dir = '/path/to/io/directory';

// Convert reStructuredText input files to HTML output files.
$process = new Process('rst2html5');

// Cache HTML output in the specified directory.
$cache = new Cache($process, "{$dir}/html/", 'html');

// Persist reStructuredText input in the specified directory (for future edits).
$repository = new Repository($cache, "{$dir}/rst/", 'rst');

// Allow users to edit and upload reStructuredText.
$editor = function (
	ResponseBuilderInterface $builder,
	ContextInterface $context
) use ($repository): void {

	$path = $context->request()->getUri()->getPath();
	$in = $repository->directory()->offsetGet($path);
	$render = function () use ($in) {

		if (file_exists($in)) {
			HtmlFilter::passthru($in, 'rb');

	$builder->content(function () use ($render, $context) {
		require 'editor.php';

// Use a simple 404 page as the default response.
$default = new Response(404, ['Content-Type' => 'text/plain'], 'Not Found');

// Set up the request handler.
$handler = new RequestHandler($repository, $default, $editor);

// Run the application.

An example template of the editor (i.e. editor.php) follows.


use JontyNewman\Oku\IO\Repository;
use JontyNewman\Oku\Helpers\Html;

/* @var $render callable */
/* @var $context \JontyNewman\Oku\ContextInterface */

<!DOCTYPE html>
    <form action="" method="post" enctype="multipart/form-data">
          <input type="file" name="<?= Html::escape(Repository::FILE); ?>">
          <textarea name="<?= Html::escape(Repository::TEXT); ?>"><?php ($render)(); ?></textarea>
        <?= $context->token(); ?>
        <?= $context->put(); ?>
        <input type="submit" value="Save">
    <form action="" method="post">
        <?= $context->token(); ?>
        <?= $context->delete(); ?>
        <input type="submit" value="Delete">
    <?= $context->inset(); ?>