Skip to content

Commit

Permalink
Upd.
Browse files Browse the repository at this point in the history
  • Loading branch information
dim-s committed May 10, 2018
1 parent 47b081e commit 6e12216
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 28 deletions.
10 changes: 10 additions & 0 deletions exts/jphp-httpserver-ext/api-docs/classes/php/http/HttpServer.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Class HttpServer
- `->`[`addHandler()`](#method-addhandler)
- `->`[`clearHandlers()`](#method-clearhandlers) - _Remove all handlers._
- `->`[`setRequestLogHandler()`](#method-setrequestloghandler)
- `->`[`setErrorHandler()`](#method-seterrorhandler)
- `->`[`isRunning()`](#method-isrunning)
- `->`[`isFailed()`](#method-isfailed)
- `->`[`isStopped()`](#method-isstopped)
Expand Down Expand Up @@ -222,6 +223,15 @@ setRequestLogHandler([ callable|null $handler): void

---

<a name="method-seterrorhandler"></a>

### setErrorHandler()
```php
setErrorHandler([ callable|null $handler): void
```

---

<a name="method-isrunning"></a>

### isRunning()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ Class HttpServer
- `->`[`addHandler()`](#method-addhandler)
- `->`[`clearHandlers()`](#method-clearhandlers) - _Remove all handlers._
- `->`[`setRequestLogHandler()`](#method-setrequestloghandler)
- `->`[`setErrorHandler()`](#method-seterrorhandler)
- `->`[`isRunning()`](#method-isrunning)
- `->`[`isFailed()`](#method-isfailed)
- `->`[`isStopped()`](#method-isstopped)
Expand Down Expand Up @@ -222,6 +223,15 @@ setRequestLogHandler([ callable|null $handler): void

---

<a name="method-seterrorhandler"></a>

### setErrorHandler()
```php
setErrorHandler([ callable|null $handler): void
```

---

<a name="method-isrunning"></a>

### isRunning()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.develnext.jphp.ext.httpserver.classes;

import javax.servlet.RequestDispatcher;
import org.develnext.jphp.ext.httpserver.HttpServerExtension;
import org.eclipse.jetty.server.*;
import org.eclipse.jetty.server.handler.*;
Expand Down Expand Up @@ -442,6 +443,23 @@ public void setRequestLogHandler(Environment env, @Nullable Invoker invoker) {
}
}

@Signature
public void setErrorHandler(Environment env, @Nullable Invoker invoker) {
if (invoker == null) {
server.setErrorHandler(null);
} else {
ErrorHandler errorHandler = new ErrorHandler();

server.setErrorHandler(new ErrorHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
Throwable th = (Throwable)request.getAttribute(RequestDispatcher.ERROR_EXCEPTION);
invoker.callAny(th, new PHttpServerRequest(env, Request.getBaseRequest(request)), new PHttpServerResponse(env, response));
}
});
}
}

public static class InvokeHandler extends AbstractHandler {
private final Invoker invoker;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
<?php
namespace php\http;

use Throwable;

/**
* Class HttpServer
* @package php\http
Expand Down Expand Up @@ -150,6 +152,13 @@ public function setRequestLogHandler(?callable $handler)
{
}

/**
* @param callable|null $handler (Throwable $err, $request, $response)
*/
public function setErrorHandler(?callable $handler)
{
}

/**
* @return bool
*/
Expand Down
94 changes: 69 additions & 25 deletions packager/buildSrc/AppPlugin.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<?php

use compress\GzipOutputStream;
use compress\TarArchive;
use compress\TarArchiveEntry;
use compress\ZipArchive;
use compress\ZipArchiveEntry;
use packager\{
Expand Down Expand Up @@ -67,21 +70,21 @@ function build(Event $event)
$exec->addVendorClassPath($vendor);
$exec->addPackageClassPath($event->package());

$buildFileName = "{$event->package()->getName()}-{$event->package()->getVersion('last')}.jar";
$buildFileName = "{$event->package()->getName()}-{$event->package()->getVersion('last')}";

if ($build['fileName']) {
$buildFileName = $build['fileName'];
if ($build['file-name']) {
$buildFileName = $build['file-name'];
}

$buildDir = $event->package()->getConfigBuildPath();

Tasks::createDir("$buildDir");
Tasks::createFile("$buildDir/$buildFileName");
Tasks::createFile("$buildDir/$buildFileName.jar");

$zip = new ZipArchive("$buildDir/$buildFileName");
$zip = new ZipArchive("$buildDir/$buildFileName.jar");
$zip->open();

Tasks::createDir("$buildDir/app");
Tasks::createDir("$buildDir/.app");

$metaInfServices = [];

Expand All @@ -91,7 +94,7 @@ function build(Event $event)

fs::scan($classPath, function ($filename) use ($zip, $classPath, &$metaInfServices, $buildDir) {
$name = fs::relativize($filename, $classPath);
$file = "$buildDir/app/$name";
$file = "$buildDir/.app/$name";

Console::log("--> add file: {$classPath}/{$name}");

Expand Down Expand Up @@ -120,7 +123,7 @@ function build(Event $event)
if (str::startsWith($name, "META-INF/services/")) {
$metaInfServices[$name] = flow((array) $metaInfServices[$name], str::lines($stream->readFully()))->toArray();
} else {
$file = "$buildDir/app/{$name}";
$file = "$buildDir/.app/{$name}";

//Console::log("--> add jar file: $name");

Expand All @@ -131,18 +134,18 @@ function build(Event $event)
}
}

Tasks::cleanDir("$buildDir/app/JPHP-INF/sdk");
Tasks::deleteFile("$buildDir/app/JPHP-INF/sdk");
Tasks::cleanDir("$buildDir/.app/JPHP-INF/sdk");
Tasks::deleteFile("$buildDir/.app/JPHP-INF/sdk");

fs::delete("$buildDir/app/META-INF/manifest.mf");
fs::delete("$buildDir/app/META-INF/Manifest.mf");
fs::delete("$buildDir/app/META-INF/MANIFEST.MF");
fs::delete("$buildDir/.app/META-INF/manifest.mf");
fs::delete("$buildDir/.app/META-INF/Manifest.mf");
fs::delete("$buildDir/.app/META-INF/MANIFEST.MF");

if (!$launcher['disable-launcher']) {
Console::log("-> create jphp app launcher");

Tasks::createDir("$buildDir/app/JPHP-INF/");
Tasks::createDir("$buildDir/app/META-INF/");
Tasks::createDir("$buildDir/.app/JPHP-INF/");
Tasks::createDir("$buildDir/.app/META-INF/");

$includes = (array) $vendor->fetchPaths()['includes'];

Expand All @@ -152,14 +155,14 @@ function build(Event $event)
$launcher['bootstrap'] = arr::pop($includes);
}

fs::formatAs("$buildDir/app/JPHP-INF/launcher.conf", [
fs::formatAs("$buildDir/.app/JPHP-INF/launcher.conf", [
'bootstrap.files' => flow($includes)->map(function ($one) { return "res://$one"; })->toString('|'),
'bootstrap.file' => $launcher['bootstrap'] ? "res://{$launcher['bootstrap']}" : 'res://JPHP-INF/.bootstrap.php'
], 'ini');

Tasks::createFile("$buildDir/app/META-INF/MANIFEST.MF");
Tasks::createFile("$buildDir/.app/META-INF/MANIFEST.MF");

Stream::putContents("$buildDir/app/META-INF/MANIFEST.MF", str::join([
Stream::putContents("$buildDir/.app/META-INF/MANIFEST.MF", str::join([
"Manifest-Version: 1.0",
"Created-By: jppm (JPHP Packager " . $event->packager()->getVersion() . ")",
"Main-Class: " . ($launcher['mainClass'] ?? 'php.runtime.launcher.Launcher'),
Expand All @@ -171,24 +174,65 @@ function build(Event $event)
}

foreach ($metaInfServices as $name => $lines) {
fs::ensureParent("$buildDir/app/$name");
Stream::putContents("$buildDir/app/$name", str::join($lines, "\n"));
fs::ensureParent("$buildDir/.app/$name");
Stream::putContents("$buildDir/.app/$name", str::join($lines, "\n"));
}

fs::scan("$buildDir/app", function (File $file) use ($zip, $buildDir) {
fs::scan("$buildDir/.app", function (File $file) use ($zip, $buildDir) {
if ($file->isFile()) {
$zip->addFile($file, fs::relativize($file, "$buildDir/app"));
$zip->addFile($file, fs::relativize($file, "$buildDir/.app"));
}
});

$zip->close();

$time = Time::millis() - $time;
Tasks::deleteFile("$buildDir/.app");

if (!$launcher['disable-launcher']) {
Console::log("\n Use 'java -jar \"$buildDir/$buildFileName\"' to run the result app.");
foreach ($event->package()->getAny('app.assets', []) as $asset) {
Tasks::deleteFile("$buildDir/" . fs::name($asset));

if (fs::isDir($asset)) {
Tasks::copy($asset, "$buildDir/" . fs::name($asset));
} else {
Tasks::copy($asset, "$buildDir/");
}
}

switch (str::lower($event->package()->getAny('app.build.result'))) {
case 'tar':
Console::log("-> pack build dir to tar.gz archive ...");

$archFile = File::createTemp($buildFileName, ".tar.gz");
$archFile->deleteOnExit();

$tar = new TarArchive(new GzipOutputStream($archFile));
$tar->open();

fs::scan("$buildDir/", function ($file) use ($buildDir, $tar) {
Console::log("-> pack '{0}'", fs::relativize($file, $buildDir . "/"));
if (fs::isFile($file)) {
$tar->addFile($file, fs::relativize($file, $buildDir . "/"));
} else {
$tar->addEmptyEntry(new TarArchiveEntry(fs::relativize($file, $buildDir . "/")));
}
});

$tar->close();

Tasks::cleanDir("$buildDir/");
Tasks::copy($archFile, "$buildDir/");
fs::rename("$buildDir/" . fs::name($archFile), "{$buildFileName}.tar.gz");

break;

default:
if (!$launcher['disable-launcher']) {
Console::log("\n Use 'java -jar \"$buildDir/$buildFileName.jar\"' to run the result app.");
}
}

$time = Time::millis() - $time;

Console::log("\n-----");
Console::log("Building time: {0} sec.", round($time / 1000, 2));
Console::log("Building is SUCCESSFUL. :)");
Expand Down
5 changes: 3 additions & 2 deletions packager/package.php.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: jppm
version: 0.2.7
version: 0.2.8

plugins: [GitHub, Hub, Doc]

Expand All @@ -14,7 +14,8 @@ github:
> JPHP Package Manager v%version%
**What's new**
+ Improve the `tasks` section in package yml.
+ Improve App Plugin.
+ Improve internal api.
**Downloads**
+ For Windows: [JPPM Windows Installer](%github.address%/releases/download/jppm-%version%/jppm-setup-%version%.exe)
Expand Down
39 changes: 38 additions & 1 deletion packager/src-php/Tasks.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,43 @@ static function run(string $name, array $args = [], ...$flags)
$app->invokeTask($name, $args, ...arr::combine($flags, $flags));
}

/**
* Copy file or directory.
*
* @param string $path
* @param string $intoDir
* @param bool $ignoreErrs
*/
static function copy(string $path, string $intoDir, bool $ignoreErrs = false)
{
if (fs::isFile($path)) {
if (fs::copy($path, "$intoDir/" . fs::name($path), 1024 * 128) < 0) {
Console::error("Failed to copy file '{0}' into dir '{1}'", $path, $intoDir);
if (!$ignoreErrs) {
exit(-1);
}
}
} else if (fs::isDir($path)) {
fs::scan($path, function ($file) use ($path, $intoDir, $ignoreErrs) {
$name = fs::relativize($file, $path);

if (fs::isDir($file)) {
fs::makeDir("$intoDir/$name");
return;
}

fs::ensureParent("$intoDir/$name");

if (fs::copy($file, "$intoDir/$name", 1024 * 128) < 0) {
Console::error("Failed to copy file '{0}' into dir '{1}'", $path, $intoDir);
if (!$ignoreErrs) {
exit(-1);
}
}
});
}
}

/**
* @param string $path
* @param string $content
Expand All @@ -35,7 +72,7 @@ static function createFile(string $path, string $content = '', bool $ignoreErrs
Stream::putContents($path, $content);
return true;
} catch (IOException $e) {
Console::error("Failed to create file '{0}', cause = {0}", $path, $e->getMessage());
Console::error("Failed to create file '{0}', cause = {1}", $path, $e->getMessage());
if (!$ignoreErrs) {
exit(-1);
}
Expand Down

0 comments on commit 6e12216

Please sign in to comment.