jontynewman/oku-aggregate

为 JontyNewman\Oku 将多个仓库聚合到一个仓库的功能。

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

This package is not auto-updated.

Last update: 2024-09-27 09:01:29 UTC


README

将多个仓库聚合到一个仓库的功能,用于 JontyNewman\Oku

安装

composer require 'jontynewman/oku-aggregate ^1.1'

示例

以下示例假设已安装 rst2html5,以下包也已安装。

编辑器允许用户输入 reStructuredText 或上传文件以创建静态网页。

<?php

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.
$handler->run();

以下是编辑器的示例模板(即 editor.php)。

<?php

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>
<html>
  <head>
    <title>JontyNewman\Oku\Aggregate</title>
  </head>
  <body>
    <form action="" method="post" enctype="multipart/form-data">
      <p>
        <label><?= $io; ?>reStructuredText</label>
      </p>
      <p>
        <label>
          Source
          <textarea name="<?= Html::escape(IoRepository::TEXT); ?>"><?php ($render)(); ?></textarea>
        </label>
      </p>
      <p>
        <label>
          Document
          <input type="file" name="<?= Html::escape(IoRepository::FILE); ?>">
        </label>
      </p>
      <p>
        <label><?= $upload; ?>Upload</label>
      </p>
      <p>
        <label>
          File
          <input type="file" name="<?= Html::escape(UploadRepository::NAME); ?>">
        </label>
      </p>
      <p>
        <?= $context->token(); ?>
        <?= $context->put(); ?>
        <input type="submit" value="Save">
      </p>
    </form>
    <form action="" method="post">
      <p>
        <?= $context->token(); ?>
        <?= $context->delete(); ?>
        <input type="submit" value="Delete">
      </p>
    </form>
    <?= $context->inset(); ?>
  </body>
</html>