This package is not installable via Composer 1.x, please make sure you upgrade to Composer 2+. Read more about our Composer 1.x deprecation policy.

Functionality for aggregating multiple repositories into a single repository for JontyNewman\Oku.

v1.1 2018-10-31 21:35 UTC

This package is not auto-updated.

Last update: 2021-09-16 18:20:49 UTC


Functionality for aggregating multiple repositories into a single repository for JontyNewman\Oku.


composer require 'jontynewman/oku-aggregate ^1.1'


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

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


use GuzzleHttp\Psr7\Response;
use JontyNewman\HtmlFilter;
use JontyNewman\Oku\Aggregate;
use JontyNewman\Oku\ContextInterface;
use JontyNewman\Oku\IO\Cache;
use JontyNewman\Oku\IO\Repository as IoRepository;
use JontyNewman\Oku\Process;
use JontyNewman\Oku\RequestHandler;
use JontyNewman\Oku\ResponseBuilderInterface;
use JontyNewman\Oku\Upload\Repository as UploadRepository;

require 'vendor/autoload.php';

$dir = '/path/to/aggregate/directories';

// 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');

// Construct an empty repository of repositories.
$aggregate = new Aggregate();

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

// Associate reStructuredText with the aggregate value 'io'.
$aggregate->set('io', $repository);

// Persist uploaded files and associate them with the aggregate value 'upload'.
$aggregate->set('upload', new UploadRepository("{$dir}/uploads/"));

// Allow users to either edit reStructuredText or upload files.
$editor = function (
    ResponseBuilderInterface $builder,
    ContextInterface $context
) use ($aggregate, $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 ($path, $aggregate, $render, $context) {

        $io = $aggregate->input($path, 'io');
        $upload = $aggregate->input($path, 'upload');
        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($aggregate, $default, $editor);

// Run the application.

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


use JontyNewman\Oku\Aggregate;
use JontyNewman\Oku\IO\Repository as IoRepository;
use JontyNewman\Oku\Upload\Repository as UploadRepository;
use JontyNewman\Oku\Helpers\Html;

/* @var $path string */
/* @var $aggregate \JontyNewman\Oku\Aggregate */
/* @var $io \JontyNewman\Oku\Aggregate\Input */
/* @var $upload \JontyNewman\Oku\Aggregate\Input */
/* @var $render callable */
/* @var $context \JontyNewman\Oku\ContextInterface */

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