Browse Source

Moved the server startup to the "index.php" file

master
Cédric Belin 6 months ago
parent
commit
85fbd217be
5 changed files with 27 additions and 30 deletions
  1. +1
    -1
      share/fast_transformer.http
  2. +4
    -3
      src/fast_transformer.ts
  3. +6
    -0
      src/php/index.php
  4. +14
    -24
      src/php/server.php
  5. +2
    -2
      src/transformer.ts

+ 1
- 1
share/fast_transformer.http View File

@@ -3,4 +3,4 @@
###

// Minifies the PHP code of the specified document.
GET {{baseUrl}}/server.php?file=server.php
GET {{baseUrl}}/?file=server.php

+ 4
- 3
src/fast_transformer.ts View File

@@ -1,7 +1,7 @@
import {ChildProcess, spawn} from 'child_process';
import {AddressInfo, createServer} from 'net';
import fetch from 'node-fetch';
import {dirname, normalize, resolve} from 'path';
import {dirname, normalize, join, resolve} from 'path';
import {fileURLToPath} from 'url';
import {Transformer} from './transformer.js';

@@ -55,7 +55,8 @@ export class FastTransformer implements Transformer {

this.#port = await this._getPort();
return new Promise((fulfill, reject) => {
const args = ['-S', `${FastTransformer.address}:${this.#port}`, '-t', dirname(fileURLToPath(import.meta.url))];
const libDir = dirname(fileURLToPath(import.meta.url));
const args = ['-S', `${FastTransformer.address}:${this.#port}`, '-t', join(libDir, 'php')];
this.#process = spawn(normalize(this.#executable), args);
this.#process.on('error', err => reject(err));
setTimeout(() => fulfill(this.#port), 1000);
@@ -71,7 +72,7 @@ export class FastTransformer implements Transformer {
const file = encodeURIComponent(resolve(script));
const port = await this.listen();

const res = await fetch(`http://${FastTransformer.address}:${port}/server.php?file=${file}`);
const res = await fetch(`http://${FastTransformer.address}:${port}/?file=${file}`);
if (!res.ok) throw new Error('An error occurred while transforming the script.');
return res.text();
}


+ 6
- 0
src/php/index.php View File

@@ -0,0 +1,6 @@
<?php declare(strict_types=1);
use Gulp\PhpMinify\{Server};

// Start the application.
require_once __DIR__.'/Server.php';
(new Server)->run($_GET);

src/server.php → src/php/server.php View File

@@ -4,30 +4,19 @@ namespace Gulp\PhpMinify;
/** An HTTP server that strip comments and whitespace from the files specified in client requests. */
class Server {

/**
* Handles the PHP execution errors such as warnings and notices.
* @param int $severity The level of the error raised.
* @param string $message The error message.
* @param string $file The filename that the error was raised in.
* @param int $line The line number the error was raised at.
* @return bool Whether the error was handled.
* @throws \ErrorException When an error occurred.
*/
function handleError(int $severity, string $message, string $file = __FILE__, int $line = __LINE__): bool {
assert(mb_strlen($file) > 0);
assert($line > 0);
if (error_reporting() & $severity) throw new \ErrorException($message, 0, $severity, $file, $line);
return false;
}

/**
* Runs the application.
* @param array<string, string> $args The request parameters.
*/
function run(array $args = []): void {
set_error_handler([$this, 'handleError']);
try { $this->sendResponse($this->processRequest($args)); }
catch (\Throwable $e) { $this->sendResponse($e->getMessage(), $e->getCode()); }
try {
$this->sendResponse($this->processRequest($args));
}

catch (\Throwable $e) {
$code = $e->getCode();
$this->sendResponse($e->getMessage(), $code >= 400 && $code < 600 ? $code : 500);
}
}

/**
@@ -37,9 +26,13 @@ class Server {
*/
function sendResponse(string $body, int $status = 200): void {
assert($status >= 100 && $status < 600);

http_response_code($status);
header('Content-Length: '.strlen($body));
header('Content-Type: text/plain; charset='.mb_internal_encoding());
if (!headers_sent()) {
header('Content-Length: '.strlen($body));
header('Content-Type: text/plain; charset='.mb_internal_encoding());
}

echo $body;
}

@@ -60,6 +53,3 @@ class Server {
return $output;
}
}

// Start the application.
(new Server)->run($_GET);

+ 2
- 2
src/transformer.ts View File

@@ -5,12 +5,12 @@ export interface Transformer {
* Closes this transformer and releases any resources associated with it.
* @return Completes when the transformer is finally disposed.
*/
close(): Promise<void>;
close: () => Promise<void>;

/**
* Processes a PHP script.
* @param script The path to the PHP script.
* @return The transformed script.
*/
transform(script: string): Promise<string>;
transform: (script: string) => Promise<string>;
}

Loading…
Cancel
Save