Browse Source

init psr-4 composer package

main
ghost 10 months ago
parent
commit
ae72a00361
  1. 41
      README.md
  2. 11
      composer.json
  3. 18
      composer.lock
  4. 2
      src/Request.php
  5. 2
      src/Response.php
  6. 11
      src/Server.php
  7. 2
      test/certs/.gitignore
  8. 44
      test/server.php

41
README.md

@ -1,40 +1,39 @@
# Titan II # Titan II
Gemini Protocol library for PHP. Gemini Protocol library for PHP 8
- [gemini://gemini.circumlunar.space/](gemini://gemini.circumlunar.space/) - [gemini://gemini.circumlunar.space/](gemini://gemini.circumlunar.space/)
- [https://gemini.circumlunar.space/](https://gemini.circumlunar.space/) - [https://gemini.circumlunar.space/](https://gemini.circumlunar.space/)
## Basic Implentation ## Install
1. `composer require yggverse/titan-ii`
2. `openssl req -x509 -newkey rsa:4096 -keyout key.rsa -out cert.pem -days 3650 -nodes -subj "/CN=127.0.0.1"`
3. `php server.php`
4. `gemini://127.0.0.1`
## Usage
``` ```
<?php <?php
use TitanII\Request; $server = new \YGGverse\TitanII\Server();
use TitanII\Response;
use TitanII\Server;
$server = new Server();
$server->setCert('cert.pem'); $server->setCert('cert.pem');
$server->setKey('key.rsa'); $server->setKey('key.rsa');
$server->setHandler(function (Request $request): Response { $server->setHandler(
$response = new Response(); function (\YGGverse\TitanII\Request $request): \YGGverse\TitanII\Response
{
$response = new \YGGverse\TitanII\Response();
$response->setCode(20); $response->setCode(20);
$response->setMeta("text/plain"); $response->setMeta('text/plain');
$response->setContent("Hello World!"); $response->setContent('Hello World!');
return $response; return $response;
}); }
);
$server->start(); $server->start();
``` ```
## Instructions
1. run `cd test/certs; openssl req -x509 -newkey rsa:4096 -keyout key.rsa -out cert.pem -days 3650 -nodes -subj "/CN=127.0.0.1"`
2. run `composer install`
3. run `cd ..; php server.php`
4. Open `gemini://127.0.0.1`

11
composer.json

@ -1,9 +1,10 @@
{ {
"name": "tdmckenney0/titan-ii", "name": "yggverse/titan-ii",
"version": "0.1",
"description": "Gemini Protocol Library", "description": "Gemini Protocol Library",
"keywords": [ "gemini", "gemini-protocol", "ipv6" ],
"homepage": "https://github.com/YGGverse/titan-II",
"type": "library", "type": "library",
"license": "GPLv3", "license": "GPL-3.0-only",
"authors": [ "authors": [
{ {
"name": "Tanner Mckenney", "name": "Tanner Mckenney",
@ -12,6 +13,8 @@
], ],
"require": {}, "require": {},
"autoload": { "autoload": {
"classmap": ["src/"] "psr-4": {
"YGGverse\\TitanII\\": "src/"
}
} }
} }

18
composer.lock generated

@ -1,18 +0,0 @@
{
"_readme": [
"This file locks the dependencies of your project to a known state",
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "1d8aa09a1cb642af953e3d80b990a54a",
"packages": [],
"packages-dev": [],
"aliases": [],
"minimum-stability": "stable",
"stability-flags": [],
"prefer-stable": false,
"prefer-lowest": false,
"platform": [],
"platform-dev": [],
"plugin-api-version": "2.0.0"
}

2
src/Request.php

@ -1,6 +1,6 @@
<?php <?php
namespace TitanII; namespace YGGverse\TitanII;
/** /**
* Gemini Request * Gemini Request

2
src/Response.php

@ -1,6 +1,6 @@
<?php <?php
namespace TitanII; namespace YGGverse\TitanII;
/** /**
* Gemini Response * Gemini Response

11
src/Server.php

@ -1,9 +1,6 @@
<?php <?php
namespace TitanII; namespace YGGverse\TitanII;
use TitanII\Request;
use TitanII\Response;
/** /**
* Gemini Server * Gemini Server
@ -57,9 +54,9 @@ class Server {
* *
* Parameter 1 Callable is expected to handle the following: * Parameter 1 Callable is expected to handle the following:
* *
* @param Request * @param \YGGverse\TitanII\Request
* *
* @return Response * @return \YGGverse\TitanII\Response
*/ */
public function setHandler(callable $handler): self public function setHandler(callable $handler): self
{ {
@ -153,7 +150,7 @@ class Server {
stream_set_blocking($incoming, false); stream_set_blocking($incoming, false);
$request = new Request($body); $request = new \YGGverse\TitanII\Request($body);
$response = call_user_func($this->handler, $request); $response = call_user_func($this->handler, $request);

2
test/certs/.gitignore vendored

@ -1,2 +0,0 @@
*
!.gitignore

44
test/server.php

@ -1,44 +0,0 @@
<?php
namespace TitanII\Test;
require_once(__DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php');
use TitanII\Server;
use TitanII\Request;
use TitanII\Response;
// Make a new server
$server = new Server();
// Set the certs.
$server->setCert(__DIR__ . DIRECTORY_SEPARATOR . 'certs' . DIRECTORY_SEPARATOR . 'cert.pem');
$server->setKey(__DIR__ . DIRECTORY_SEPARATOR . 'certs' . DIRECTORY_SEPARATOR . 'key.rsa');
// Response Body (Gemini Text!)
$body = <<<GEMINI
# Titan II Lifts off!
The tower is clear!
GEMINI;
/**
* Set a request handler.
*
* This function must take a `TitanII\Request` object, and return a `TitanII\Response` object.
*/
$server->setHandler(function (Request $request) use (&$body): Response {
$response = new Response();
$response->setMeta('text/gemini');
$response->setContent($body);
echo $request;
return $response;
});
/**
* Boot the server!
*/
$server->start();
Loading…
Cancel
Save