Skip to content

Instantly share code, notes, and snippets.

@VottusCode
Created March 22, 2023 12:48
Show Gist options
  • Save VottusCode/f8ecec36b8d37883a5a25aa5a016c24f to your computer and use it in GitHub Desktop.
Save VottusCode/f8ecec36b8d37883a5a25aa5a016c24f to your computer and use it in GitHub Desktop.
wip random bullshit
<?php declare(strict_types=1);
ini_set('auto_detect_line_endings', true);
// Utility code
interface Processor
{
/**
* @param resource|string $source
*/
public function __construct(mixed $source);
/**
* @return resource
*/
public function getSource(): mixed;
public function read(): array;
public function write(array $data): void;
}
abstract class AbstractProcessor implements Processor
{
/** @var resource */
private mixed $source;
public function __construct(mixed $source) {
if (is_resource($source)) {
$this->source = $source;
} elseif (is_string($source)) {
if (is_file($source)) {
$this->source = fopen($source, 'a+');
} else {
$stream = fopen('php://temp', 'r+');
fwrite($stream, $source);
rewind($stream);
$this->source = stream_get_contents($stream);
}
} else {
throw new Exception("Unknown input passed to " . static::class);
}
}
public function getSource(): mixed {
return $this->source;
}
}
class CsvProcessor extends AbstractProcessor
{
public function read(): array {
$rows = [];
// Each row (line)
while (($data = fgetcsv($this->getSource(), 1000, ",")) !== false) {
// Each column
$rows[] = $data;
}
fclose($this->getSource());
return $rows;
}
public function write(array $data): void {
fputcsv($this->getSource(), $data);
fclose($this->getSource());
}
}
// Redirect definitions code
class RouteDefinition
{
public function __construct(
public readonly string $from,
public readonly string $to
)
{
}
}
interface TransformerInterface
{
/**
* @return RouteDefinition[]
*/
public static function from(mixed $transform): array;
public static function fromTo(mixed $source, TransformerInterface $transformerInterface): mixed;
/**
* @param RouteDefinition[]
*/
public static function to(array $definitions): mixed;
}
class NginxTransformer implements TransformerInterface
{
public static function from(mixed $source): array {
throw new Exception("Not implemented");
}
public static function fromTo(mixed $source, TransformerInterface $transformer): mixed
{
throw new Exception("Not implemented");
}
public static function to(array $definitions): string {
$str = "";
/** @var RouteDefinition $def */
foreach ($definitions as $def) {
$str .= "location = $def->from {\n";
$str .= " return 301 $def->to\n";
$str .= "}\n\n";
}
return $str;
}
}
class CsvTransformer implements TransformerInterface
{
public static function from(mixed $source): array {
$rows = (new CsvProcessor($source))->read();
/** @var RouteDefinition[] */
$result = [];
foreach ($rows as $row) {
$result[] = new RouteDefinition($row[0], $row[1]);
}
return $result;
}
public static function fromTo(mixed $source, TransformerInterface|string $transformer): mixed
{
return [$transformer, 'to'](self::from($source));
}
public static function to(array $definitions): mixed {
throw new Exception("WIP");
}
}
// very todo
if (php_sapi_name() === "cli") {
$input = getopt('f:')['f'];
} else {
switch ($_SERVER["REQUEST_METHOD"]) {
case "GET":
$action = 'http' . ($_SERVER['HTTPS'] ? 's' : '') . '://' . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
$method = "POST";
echo "<form action='$action' method='$method'>";
return;
case "POST":
$input = $_POST["data"];
return;
default:
throw new Exception("Unsupported method");
}
}
if (php_sapi_name() !== "cli") {
header("Content-Type: text/plain");
}
echo CsvTransformer::fromTo($input, NginxTransformer::class);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment